<s> import logging <EOL> import sys <EOL> import subprocess <EOL> from threading import Thread <EOL> import time <EOL> import fcntl <EOL> import os <EOL> from django . conf import settings <EOL> from socketio . namespace import BaseNamespace <EOL> from socketio . mixins import RoomsMixin , BroadcastMixin <EOL> from socketio . sdjango import namespace <EOL> from fabric_bolt . projects . models import Deployment <EOL> @ namespace ( '<STR_LIT>' ) <EOL> class ChatNamespace ( BaseNamespace , RoomsMixin , BroadcastMixin ) : <EOL> def initialize ( self ) : <EOL> self . logger = logging . getLogger ( "<STR_LIT>" ) <EOL> self . log ( "<STR_LIT>" ) <EOL> def log ( self , message ) : <EOL> self . logger . info ( "<STR_LIT>" . format ( self . socket . sessid , message ) ) <EOL> def on_join ( self , deployment_id ) : <EOL> self . deployment = Deployment . objects . get ( pk = deployment_id ) <EOL> if self . deployment . status != self . deployment . PENDING : <EOL> return True <EOL> update_thread = Thread ( target = self . output_stream_generator , args = ( self , ) ) <EOL> update_thread . daemon = True <EOL> update_thread . start ( ) <EOL> return True <EOL> def on_input ( self , text ) : <EOL> self . process . stdin . write ( text + '<STR_LIT:\n>' ) <EOL> return True <EOL> def recv_disconnect ( self ) : <EOL> self . log ( '<STR_LIT>' ) <EOL> self . disconnect ( silent = True ) <EOL> return True <EOL> def output_stream_generator ( self , * args , ** kwargs ) : <EOL> from fabric_bolt . task_runners import backend <EOL> self . process = subprocess . Popen ( <EOL> backend . build_command ( self . deployment . stage . project , self . deployment , self . request . session , False ) , <EOL> stdout = subprocess . PIPE , <EOL> stderr = subprocess . STDOUT , <EOL> stdin = subprocess . PIPE , <EOL> shell = True , <EOL> executable = getattr ( settings , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> close_fds = True , <EOL> ) <EOL> fd = self . process . stdout . fileno ( ) <EOL> fl = fcntl . fcntl ( fd , fcntl . F_GETFL ) <EOL> fcntl . fcntl ( fd , fcntl . F_SETFL , fl | os . O_NONBLOCK ) <EOL> all_output = '<STR_LIT>' <EOL> while True : <EOL> try : <EOL> nextline = self . process . stdout . read ( ) <EOL> except IOError as e : <EOL> print e <EOL> nextline = '<STR_LIT>' <EOL> if nextline == '<STR_LIT>' and self . process . poll ( ) != None : <EOL> break <EOL> all_output += nextline <EOL> if nextline : <EOL> self . broadcast_event ( '<STR_LIT>' , { '<STR_LIT:status>' : '<STR_LIT>' , '<STR_LIT>' : str ( nextline ) } ) <EOL> time . sleep ( <NUM_LIT> ) <EOL> sys . stdout . flush ( ) <EOL> self . deployment . status = self . deployment . SUCCESS if self . process . returncode == <NUM_LIT:0> else self . deployment . FAILED <EOL> self . deployment . output = all_output <EOL> self . deployment . save ( ) <EOL> self . broadcast_event ( '<STR_LIT>' , { '<STR_LIT:status>' : self . deployment . status } ) <EOL> self . disconnect ( ) </s>
