<s> '''<STR_LIT>''' <EOL> from __future__ import absolute_import <EOL> import os <EOL> import sys <EOL> import time <EOL> import errno <EOL> import select <EOL> import logging <EOL> import tempfile <EOL> import subprocess <EOL> mswindows = ( sys . platform == "<STR_LIT:win32>" ) <EOL> if mswindows : <EOL> from win32file import ReadFile , WriteFile <EOL> from win32pipe import PeekNamedPipe <EOL> import msvcrt <EOL> else : <EOL> import fcntl <EOL> log = logging . getLogger ( __name__ ) <EOL> class NonBlockingPopen ( subprocess . Popen ) : <EOL> _stdout_logger_name_ = '<STR_LIT>' <EOL> _stderr_logger_name_ = '<STR_LIT>' <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> self . stream_stds = kwargs . pop ( '<STR_LIT>' , False ) <EOL> self . max_size_in_mem = kwargs . pop ( '<STR_LIT>' , <NUM_LIT> ) <EOL> self . _stdout_logger_name_ = kwargs . pop ( <EOL> '<STR_LIT>' , self . _stdout_logger_name_ <EOL> ) <EOL> self . _stderr_logger_name_ = kwargs . pop ( <EOL> '<STR_LIT>' , self . _stderr_logger_name_ <EOL> ) <EOL> logging_command = kwargs . pop ( '<STR_LIT>' , None ) <EOL> stderr = kwargs . get ( '<STR_LIT>' , None ) <EOL> super ( NonBlockingPopen , self ) . __init__ ( * args , ** kwargs ) <EOL> self . stdout_buff = tempfile . SpooledTemporaryFile ( self . max_size_in_mem ) <EOL> self . _stdout_logger = logging . getLogger ( <EOL> self . _stdout_logger_name_ . format ( pid = self . pid ) <EOL> ) <EOL> if stderr is subprocess . STDOUT : <EOL> self . stderr_buff = self . stdout_buff <EOL> self . _stderr_logger = self . _stdout_logger <EOL> else : <EOL> self . stderr_buff = tempfile . SpooledTemporaryFile ( <EOL> self . max_size_in_mem <EOL> ) <EOL> self . _stderr_logger = logging . getLogger ( <EOL> self . _stderr_logger_name_ . format ( pid = self . pid ) <EOL> ) <EOL> self . _stderr_logger = logging . getLogger ( <EOL> self . _stderr_logger_name_ . format ( pid = self . pid ) <EOL> ) <EOL> if logging_command is None : <EOL> log . info ( <EOL> '<STR_LIT>' . format ( <EOL> self . pid , <EOL> * args <EOL> ) <EOL> ) <EOL> else : <EOL> log . info ( <EOL> '<STR_LIT>' . format ( <EOL> self . pid , <EOL> logging_command <EOL> ) <EOL> ) <EOL> def recv ( self , maxsize = None ) : <EOL> return self . _recv ( '<STR_LIT>' , maxsize ) <EOL> def recv_err ( self , maxsize = None ) : <EOL> return self . _recv ( '<STR_LIT>' , maxsize ) <EOL> def send_recv ( self , input = '<STR_LIT>' , maxsize = None ) : <EOL> return self . send ( input ) , self . recv ( maxsize ) , self . recv_err ( maxsize ) <EOL> def get_conn_maxsize ( self , which , maxsize ) : <EOL> if maxsize is None : <EOL> maxsize = <NUM_LIT> <EOL> elif maxsize < <NUM_LIT:1> : <EOL> maxsize = <NUM_LIT:1> <EOL> return getattr ( self , which ) , maxsize <EOL> def _close ( self , which ) : <EOL> getattr ( self , which ) . close ( ) <EOL> setattr ( self , which , None ) <EOL> if mswindows : <EOL> def send ( self , input ) : <EOL> if not self . stdin : <EOL> return None <EOL> try : <EOL> x = msvcrt . get_osfhandle ( self . stdin . fileno ( ) ) <EOL> ( errCode , written ) = WriteFile ( x , input ) <EOL> except ValueError : <EOL> return self . _close ( '<STR_LIT>' ) <EOL> except ( subprocess . pywintypes . error , Exception ) as why : <EOL> if why . args [ <NUM_LIT:0> ] in ( <NUM_LIT> , errno . ESHUTDOWN ) : <EOL> return self . _close ( '<STR_LIT>' ) <EOL> raise <EOL> return written <EOL> def _recv ( self , which , maxsize ) : <EOL> conn , maxsize = self . get_conn_maxsize ( which , maxsize ) <EOL> if conn is None : <EOL> return None <EOL> try : <EOL> x = msvcrt . get_osfhandle ( conn . fileno ( ) ) <EOL> ( read , nAvail , nMessage ) = PeekNamedPipe ( x , <NUM_LIT:0> ) <EOL> if maxsize < nAvail : <EOL> nAvail = maxsize <EOL> if nAvail > <NUM_LIT:0> : <EOL> ( errCode , read ) = ReadFile ( x , nAvail , None ) <EOL> except ValueError : <EOL> return self . _close ( which ) <EOL> except ( subprocess . pywintypes . error , Exception ) as why : <EOL> if why . args [ <NUM_LIT:0> ] in ( <NUM_LIT> , errno . ESHUTDOWN ) : <EOL> return self . _close ( which ) <EOL> raise <EOL> getattr ( self , '<STR_LIT>' . format ( which ) ) . write ( read ) <EOL> getattr ( self , '<STR_LIT>' . format ( which ) ) . debug ( read . rstrip ( ) ) <EOL> if self . stream_stds : <EOL> getattr ( sys , which ) . write ( read ) <EOL> if self . universal_newlines : <EOL> read = self . _translate_newlines ( read ) <EOL> return read <EOL> else : <EOL> def send ( self , input ) : <EOL> if not self . stdin : <EOL> return None <EOL> if not select . select ( [ ] , [ self . stdin ] , [ ] , <NUM_LIT:0> ) [ <NUM_LIT:1> ] : <EOL> return <NUM_LIT:0> <EOL> try : <EOL> written = os . write ( self . stdin . fileno ( ) , input ) <EOL> except OSError as why : <EOL> if why . args [ <NUM_LIT:0> ] == errno . EPIPE : <EOL> return self . _close ( '<STR_LIT>' ) <EOL> raise <EOL> return written <EOL> def _recv ( self , which , maxsize ) : <EOL> conn , maxsize = self . get_conn_maxsize ( which , maxsize ) <EOL> if conn is None : <EOL> return None <EOL> flags = fcntl . fcntl ( conn , fcntl . F_GETFL ) <EOL> if not conn . closed : <EOL> fcntl . fcntl ( conn , fcntl . F_SETFL , flags | os . O_NONBLOCK ) <EOL> try : <EOL> if not select . select ( [ conn ] , [ ] , [ ] , <NUM_LIT:0> ) [ <NUM_LIT:0> ] : <EOL> return '<STR_LIT>' <EOL> buff = conn . read ( maxsize ) <EOL> if not buff : <EOL> return self . _close ( which ) <EOL> if self . universal_newlines : <EOL> buff = self . _translate_newlines ( buff ) <EOL> getattr ( self , '<STR_LIT>' . format ( which ) ) . write ( buff ) <EOL> getattr ( self , '<STR_LIT>' . format ( which ) ) . debug ( buff . rstrip ( ) ) <EOL> if self . stream_stds : <EOL> getattr ( sys , which ) . write ( buff ) <EOL> return buff <EOL> finally : <EOL> if not conn . closed : <EOL> fcntl . fcntl ( conn , fcntl . F_SETFL , flags ) <EOL> def poll_and_read_until_finish ( self , interval = <NUM_LIT> ) : <EOL> silent_iterations = <NUM_LIT:0> <EOL> while self . poll ( ) is None : <EOL> if self . stdout is not None : <EOL> silent_iterations = <NUM_LIT:0> <EOL> self . recv ( ) <EOL> if self . stderr is not None : <EOL> silent_iterations = <NUM_LIT:0> <EOL> self . recv_err ( ) <EOL> silent_iterations += <NUM_LIT:1> <EOL> if silent_iterations > <NUM_LIT:100> : <EOL> silent_iterations = <NUM_LIT:0> <EOL> ( stdoutdata , stderrdata ) = self . communicate ( ) <EOL> if stdoutdata : <EOL> log . debug ( stdoutdata ) <EOL> if stderrdata : <EOL> log . error ( stderrdata ) <EOL> time . sleep ( interval ) <EOL> def communicate ( self , input = None ) : <EOL> super ( NonBlockingPopen , self ) . communicate ( input ) <EOL> self . stdout_buff . flush ( ) <EOL> self . stdout_buff . seek ( <NUM_LIT:0> ) <EOL> self . stderr_buff . flush ( ) <EOL> self . stderr_buff . seek ( <NUM_LIT:0> ) <EOL> return self . stdout_buff . read ( ) , self . stderr_buff . read ( ) </s>
