<s> r"""<STR_LIT>""" <EOL> import sys <EOL> mswindows = ( sys . platform == "<STR_LIT:win32>" ) <EOL> import os <EOL> import types <EOL> import traceback <EOL> import gc <EOL> import signal <EOL> import errno <EOL> try : <EOL> set <EOL> except NameError : <EOL> from sets import Set as set <EOL> class CalledProcessError ( Exception ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , returncode , cmd , output = None ) : <EOL> self . returncode = returncode <EOL> self . cmd = cmd <EOL> self . output = output <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT>" % ( self . cmd , self . returncode ) <EOL> if mswindows : <EOL> import threading <EOL> import msvcrt <EOL> import _subprocess <EOL> class STARTUPINFO : <EOL> dwFlags = <NUM_LIT:0> <EOL> hStdInput = None <EOL> hStdOutput = None <EOL> hStdError = None <EOL> wShowWindow = <NUM_LIT:0> <EOL> class pywintypes : <EOL> error = IOError <EOL> else : <EOL> import select <EOL> _has_poll = hasattr ( select , '<STR_LIT>' ) <EOL> import fcntl <EOL> import pickle <EOL> _PIPE_BUF = getattr ( select , '<STR_LIT>' , <NUM_LIT> ) <EOL> __all__ = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" ] <EOL> if mswindows : <EOL> from _subprocess import CREATE_NEW_CONSOLE , CREATE_NEW_PROCESS_GROUP , STD_INPUT_HANDLE , STD_OUTPUT_HANDLE , STD_ERROR_HANDLE , SW_HIDE , STARTF_USESTDHANDLES , STARTF_USESHOWWINDOW <EOL> __all__ . extend ( [ "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" ] ) <EOL> try : <EOL> MAXFD = os . sysconf ( "<STR_LIT>" ) <EOL> except : <EOL> MAXFD = <NUM_LIT> <EOL> _active = [ ] <EOL> def _cleanup ( ) : <EOL> for inst in _active [ : ] : <EOL> res = inst . _internal_poll ( _deadstate = sys . maxint ) <EOL> if res is not None : <EOL> try : <EOL> _active . remove ( inst ) <EOL> except ValueError : <EOL> pass <EOL> PIPE = - <NUM_LIT:1> <EOL> STDOUT = - <NUM_LIT:2> <EOL> def _eintr_retry_call ( func , * args ) : <EOL> while True : <EOL> try : <EOL> return func ( * args ) <EOL> except ( OSError , IOError ) as e : <EOL> if e . errno == errno . EINTR : <EOL> continue <EOL> raise <EOL> def call ( * popenargs , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> return Popen ( * popenargs , ** kwargs ) . wait ( ) <EOL> def check_call ( * popenargs , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> retcode = call ( * popenargs , ** kwargs ) <EOL> if retcode : <EOL> cmd = kwargs . get ( "<STR_LIT:args>" ) <EOL> if cmd is None : <EOL> cmd = popenargs [ <NUM_LIT:0> ] <EOL> raise CalledProcessError ( retcode , cmd ) <EOL> return <NUM_LIT:0> <EOL> def check_output ( * popenargs , ** kwargs ) : <EOL> r"""<STR_LIT>""" <EOL> if '<STR_LIT>' in kwargs : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> process = Popen ( stdout = PIPE , * popenargs , ** kwargs ) <EOL> output , unused_err = process . communicate ( ) <EOL> retcode = process . poll ( ) <EOL> if retcode : <EOL> cmd = kwargs . get ( "<STR_LIT:args>" ) <EOL> if cmd is None : <EOL> cmd = popenargs [ <NUM_LIT:0> ] <EOL> raise CalledProcessError ( retcode , cmd , output = output ) <EOL> return output <EOL> def list2cmdline ( seq ) : <EOL> """<STR_LIT>""" <EOL> result = [ ] <EOL> needquote = False <EOL> for arg in seq : <EOL> bs_buf = [ ] <EOL> if result : <EOL> result . append ( '<STR_LIT:U+0020>' ) <EOL> needquote = ( "<STR_LIT:U+0020>" in arg ) or ( "<STR_LIT:\t>" in arg ) or not arg <EOL> if needquote : <EOL> result . append ( '<STR_LIT:">' ) <EOL> for c in arg : <EOL> if c == '<STR_LIT:\\>' : <EOL> bs_buf . append ( c ) <EOL> elif c == '<STR_LIT:">' : <EOL> result . append ( '<STR_LIT:\\>' * len ( bs_buf ) * <NUM_LIT:2> ) <EOL> bs_buf = [ ] <EOL> result . append ( '<STR_LIT>' ) <EOL> else : <EOL> if bs_buf : <EOL> result . extend ( bs_buf ) <EOL> bs_buf = [ ] <EOL> result . append ( c ) <EOL> if bs_buf : <EOL> result . extend ( bs_buf ) <EOL> if needquote : <EOL> result . extend ( bs_buf ) <EOL> result . append ( '<STR_LIT:">' ) <EOL> return '<STR_LIT>' . join ( result ) <EOL> class Popen ( object ) : <EOL> def __init__ ( self , args , bufsize = <NUM_LIT:0> , executable = None , <EOL> stdin = None , stdout = None , stderr = None , <EOL> preexec_fn = None , close_fds = False , shell = False , <EOL> cwd = None , env = None , universal_newlines = False , <EOL> startupinfo = None , creationflags = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> _cleanup ( ) <EOL> self . _child_created = False <EOL> if not isinstance ( bufsize , ( int , long ) ) : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> if mswindows : <EOL> if preexec_fn is not None : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> if close_fds and ( stdin is not None or stdout is not None or <EOL> stderr is not None ) : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> else : <EOL> if startupinfo is not None : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> if creationflags != <NUM_LIT:0> : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> self . stdin = None <EOL> self . stdout = None <EOL> self . stderr = None <EOL> self . pid = None <EOL> self . returncode = None <EOL> self . universal_newlines = universal_newlines <EOL> ( p2cread , p2cwrite , <EOL> c2pread , c2pwrite , <EOL> errread , errwrite ) = self . _get_handles ( stdin , stdout , stderr ) <EOL> self . _execute_child ( args , executable , preexec_fn , close_fds , <EOL> cwd , env , universal_newlines , <EOL> startupinfo , creationflags , shell , <EOL> p2cread , p2cwrite , <EOL> c2pread , c2pwrite , <EOL> errread , errwrite ) <EOL> if mswindows : <EOL> if p2cwrite is not None : <EOL> p2cwrite = msvcrt . open_osfhandle ( p2cwrite . Detach ( ) , <NUM_LIT:0> ) <EOL> if c2pread is not None : <EOL> c2pread = msvcrt . open_osfhandle ( c2pread . Detach ( ) , <NUM_LIT:0> ) <EOL> if errread is not None : <EOL> errread = msvcrt . open_osfhandle ( errread . Detach ( ) , <NUM_LIT:0> ) <EOL> if p2cwrite is not None : <EOL> self . stdin = os . fdopen ( p2cwrite , '<STR_LIT:wb>' , bufsize ) <EOL> if c2pread is not None : <EOL> if universal_newlines : <EOL> self . stdout = os . fdopen ( c2pread , '<STR_LIT>' , bufsize ) <EOL> else : <EOL> self . stdout = os . fdopen ( c2pread , '<STR_LIT:rb>' , bufsize ) <EOL> if errread is not None : <EOL> if universal_newlines : <EOL> self . stderr = os . fdopen ( errread , '<STR_LIT>' , bufsize ) <EOL> else : <EOL> self . stderr = os . fdopen ( errread , '<STR_LIT:rb>' , bufsize ) <EOL> def _translate_newlines ( self , data ) : <EOL> data = data . replace ( "<STR_LIT:\r\n>" , "<STR_LIT:\n>" ) <EOL> data = data . replace ( "<STR_LIT:\r>" , "<STR_LIT:\n>" ) <EOL> return data <EOL> def __del__ ( self , _maxint = sys . maxint , _active = _active ) : <EOL> if not getattr ( self , '<STR_LIT>' , False ) : <EOL> return <EOL> self . _internal_poll ( _deadstate = _maxint ) <EOL> if self . returncode is None and _active is not None : <EOL> _active . append ( self ) <EOL> def communicate ( self , input = None ) : <EOL> """<STR_LIT>""" <EOL> if [ self . stdin , self . stdout , self . stderr ] . count ( None ) >= <NUM_LIT:2> : <EOL> stdout = None <EOL> stderr = None <EOL> if self . stdin : <EOL> if input : <EOL> try : <EOL> self . stdin . write ( input ) <EOL> except IOError , e : <EOL> if e . errno != errno . EPIPE and e . errno != errno . EINVAL : <EOL> raise <EOL> self . stdin . close ( ) <EOL> elif self . stdout : <EOL> stdout = _eintr_retry_call ( self . stdout . read ) <EOL> self . stdout . close ( ) <EOL> elif self . stderr : <EOL> stderr = _eintr_retry_call ( self . stderr . read ) <EOL> self . stderr . close ( ) <EOL> self . wait ( ) <EOL> return ( stdout , stderr ) <EOL> return self . _communicate ( input ) <EOL> def poll ( self ) : <EOL> return self . _internal_poll ( ) <EOL> if mswindows : <EOL> def _get_handles ( self , stdin , stdout , stderr ) : <EOL> """<STR_LIT>""" <EOL> if stdin is None and stdout is None and stderr is None : <EOL> return ( None , None , None , None , None , None ) <EOL> p2cread , p2cwrite = None , None <EOL> c2pread , c2pwrite = None , None <EOL> errread , errwrite = None , None <EOL> if stdin is None : <EOL> p2cread = _subprocess . GetStdHandle ( _subprocess . STD_INPUT_HANDLE ) <EOL> if p2cread is None : <EOL> p2cread , _ = _subprocess . CreatePipe ( None , <NUM_LIT:0> ) <EOL> elif stdin == PIPE : <EOL> p2cread , p2cwrite = _subprocess . CreatePipe ( None , <NUM_LIT:0> ) <EOL> elif isinstance ( stdin , int ) : <EOL> p2cread = msvcrt . get_osfhandle ( stdin ) <EOL> else : <EOL> p2cread = msvcrt . get_osfhandle ( stdin . fileno ( ) ) <EOL> p2cread = self . _make_inheritable ( p2cread ) <EOL> if stdout is None : <EOL> c2pwrite = _subprocess . GetStdHandle ( _subprocess . STD_OUTPUT_HANDLE ) <EOL> if c2pwrite is None : <EOL> _ , c2pwrite = _subprocess . CreatePipe ( None , <NUM_LIT:0> ) <EOL> elif stdout == PIPE : <EOL> c2pread , c2pwrite = _subprocess . CreatePipe ( None , <NUM_LIT:0> ) <EOL> elif isinstance ( stdout , int ) : <EOL> c2pwrite = msvcrt . get_osfhandle ( stdout ) <EOL> else : <EOL> c2pwrite = msvcrt . get_osfhandle ( stdout . fileno ( ) ) <EOL> c2pwrite = self . _make_inheritable ( c2pwrite ) <EOL> if stderr is None : <EOL> errwrite = _subprocess . GetStdHandle ( _subprocess . STD_ERROR_HANDLE ) <EOL> if errwrite is None : <EOL> _ , errwrite = _subprocess . CreatePipe ( None , <NUM_LIT:0> ) <EOL> elif stderr == PIPE : <EOL> errread , errwrite = _subprocess . CreatePipe ( None , <NUM_LIT:0> ) <EOL> elif stderr == STDOUT : <EOL> errwrite = c2pwrite <EOL> elif isinstance ( stderr , int ) : <EOL> errwrite = msvcrt . get_osfhandle ( stderr ) <EOL> else : <EOL> errwrite = msvcrt . get_osfhandle ( stderr . fileno ( ) ) <EOL> errwrite = self . _make_inheritable ( errwrite ) <EOL> return ( p2cread , p2cwrite , <EOL> c2pread , c2pwrite , <EOL> errread , errwrite ) <EOL> def _make_inheritable ( self , handle ) : <EOL> """<STR_LIT>""" <EOL> return _subprocess . DuplicateHandle ( _subprocess . GetCurrentProcess ( ) , <EOL> handle , _subprocess . GetCurrentProcess ( ) , <NUM_LIT:0> , <NUM_LIT:1> , <EOL> _subprocess . DUPLICATE_SAME_ACCESS ) <EOL> def _find_w9xpopen ( self ) : <EOL> """<STR_LIT>""" <EOL> w9xpopen = os . path . join ( <EOL> os . path . dirname ( _subprocess . GetModuleFileName ( <NUM_LIT:0> ) ) , <EOL> "<STR_LIT>" ) <EOL> if not os . path . exists ( w9xpopen ) : <EOL> w9xpopen = os . path . join ( os . path . dirname ( sys . exec_prefix ) , <EOL> "<STR_LIT>" ) <EOL> if not os . path . exists ( w9xpopen ) : <EOL> raise RuntimeError ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> return w9xpopen <EOL> def _execute_child ( self , args , executable , preexec_fn , close_fds , <EOL> cwd , env , universal_newlines , <EOL> startupinfo , creationflags , shell , <EOL> p2cread , p2cwrite , <EOL> c2pread , c2pwrite , <EOL> errread , errwrite ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( args , types . StringTypes ) : <EOL> args = list2cmdline ( args ) <EOL> if startupinfo is None : <EOL> startupinfo = STARTUPINFO ( ) <EOL> if None not in ( p2cread , c2pwrite , errwrite ) : <EOL> startupinfo . dwFlags |= _subprocess . STARTF_USESTDHANDLES <EOL> startupinfo . hStdInput = p2cread <EOL> startupinfo . hStdOutput = c2pwrite <EOL> startupinfo . hStdError = errwrite <EOL> if shell : <EOL> startupinfo . dwFlags |= _subprocess . STARTF_USESHOWWINDOW <EOL> startupinfo . wShowWindow = _subprocess . SW_HIDE <EOL> comspec = os . environ . get ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> args = '<STR_LIT>' . format ( comspec , args ) <EOL> if ( _subprocess . GetVersion ( ) >= <NUM_LIT> or <EOL> os . path . basename ( comspec ) . lower ( ) == "<STR_LIT>" ) : <EOL> w9xpopen = self . _find_w9xpopen ( ) <EOL> args = '<STR_LIT>' % ( w9xpopen , args ) <EOL> creationflags |= _subprocess . CREATE_NEW_CONSOLE <EOL> try : <EOL> try : <EOL> hp , ht , pid , tid = _subprocess . CreateProcess ( executable , args , <EOL> None , None , <EOL> int ( not close_fds ) , <EOL> creationflags , <EOL> env , <EOL> cwd , <EOL> startupinfo ) <EOL> except pywintypes . error as e : <EOL> raise WindowsError ( * e . args ) <EOL> finally : <EOL> if p2cread is not None : <EOL> p2cread . Close ( ) <EOL> if c2pwrite is not None : <EOL> c2pwrite . Close ( ) <EOL> if errwrite is not None : <EOL> errwrite . Close ( ) <EOL> self . _child_created = True <EOL> self . _handle = hp <EOL> self . pid = pid <EOL> ht . Close ( ) <EOL> def _internal_poll ( self , _deadstate = None , <EOL> _WaitForSingleObject = _subprocess . WaitForSingleObject , <EOL> _WAIT_OBJECT_0 = _subprocess . WAIT_OBJECT_0 , <EOL> _GetExitCodeProcess = _subprocess . GetExitCodeProcess ) : <EOL> """<STR_LIT>""" <EOL> if self . returncode is None : <EOL> if _WaitForSingleObject ( self . _handle , <NUM_LIT:0> ) == _WAIT_OBJECT_0 : <EOL> self . returncode = _GetExitCodeProcess ( self . _handle ) <EOL> return self . returncode <EOL> def wait ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . returncode is None : <EOL> _subprocess . WaitForSingleObject ( self . _handle , <EOL> _subprocess . INFINITE ) <EOL> self . returncode = _subprocess . GetExitCodeProcess ( self . _handle ) <EOL> return self . returncode <EOL> def _readerthread ( self , fh , buffer ) : <EOL> buffer . append ( fh . read ( ) ) <EOL> def _communicate ( self , input ) : <EOL> stdout = None <EOL> stderr = None <EOL> if self . stdout : <EOL> stdout = [ ] <EOL> stdout_thread = threading . Thread ( target = self . _readerthread , <EOL> args = ( self . stdout , stdout ) ) <EOL> stdout_thread . setDaemon ( True ) <EOL> stdout_thread . start ( ) <EOL> if self . stderr : <EOL> stderr = [ ] <EOL> stderr_thread = threading . Thread ( target = self . _readerthread , <EOL> args = ( self . stderr , stderr ) ) <EOL> stderr_thread . setDaemon ( True ) <EOL> stderr_thread . start ( ) <EOL> if self . stdin : <EOL> if input is not None : <EOL> try : <EOL> self . stdin . write ( input ) <EOL> except IOError as e : <EOL> if e . errno != errno . EPIPE : <EOL> raise <EOL> self . stdin . close ( ) <EOL> if self . stdout : <EOL> stdout_thread . join ( ) <EOL> if self . stderr : <EOL> stderr_thread . join ( ) <EOL> if stdout is not None : <EOL> stdout = stdout [ <NUM_LIT:0> ] <EOL> if stderr is not None : <EOL> stderr = stderr [ <NUM_LIT:0> ] <EOL> if self . universal_newlines and hasattr ( file , '<STR_LIT>' ) : <EOL> if stdout : <EOL> stdout = self . _translate_newlines ( stdout ) <EOL> if stderr : <EOL> stderr = self . _translate_newlines ( stderr ) <EOL> self . wait ( ) <EOL> return ( stdout , stderr ) <EOL> def send_signal ( self , sig ) : <EOL> """<STR_LIT>""" <EOL> if sig == signal . SIGTERM : <EOL> self . terminate ( ) <EOL> elif sig == signal . CTRL_C_EVENT : <EOL> os . kill ( self . pid , signal . CTRL_C_EVENT ) <EOL> elif sig == signal . CTRL_BREAK_EVENT : <EOL> os . kill ( self . pid , signal . CTRL_BREAK_EVENT ) <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" . format ( sig ) ) <EOL> def terminate ( self ) : <EOL> """<STR_LIT>""" <EOL> _subprocess . TerminateProcess ( self . _handle , <NUM_LIT:1> ) <EOL> kill = terminate <EOL> else : <EOL> def _get_handles ( self , stdin , stdout , stderr ) : <EOL> """<STR_LIT>""" <EOL> p2cread , p2cwrite = None , None <EOL> c2pread , c2pwrite = None , None <EOL> errread , errwrite = None , None <EOL> if stdin is None : <EOL> pass <EOL> elif stdin == PIPE : <EOL> p2cread , p2cwrite = self . pipe_cloexec ( ) <EOL> elif isinstance ( stdin , int ) : <EOL> p2cread = stdin <EOL> else : <EOL> p2cread = stdin . fileno ( ) <EOL> if stdout is None : <EOL> pass <EOL> elif stdout == PIPE : <EOL> c2pread , c2pwrite = self . pipe_cloexec ( ) <EOL> elif isinstance ( stdout , int ) : <EOL> c2pwrite = stdout <EOL> else : <EOL> c2pwrite = stdout . fileno ( ) <EOL> if stderr is None : <EOL> pass <EOL> elif stderr == PIPE : <EOL> errread , errwrite = self . pipe_cloexec ( ) <EOL> elif stderr == STDOUT : <EOL> errwrite = c2pwrite <EOL> elif isinstance ( stderr , int ) : <EOL> errwrite = stderr <EOL> else : <EOL> errwrite = stderr . fileno ( ) <EOL> return ( p2cread , p2cwrite , <EOL> c2pread , c2pwrite , <EOL> errread , errwrite ) <EOL> def _set_cloexec_flag ( self , fd , cloexec = True ) : <EOL> try : <EOL> cloexec_flag = fcntl . FD_CLOEXEC <EOL> except AttributeError : <EOL> cloexec_flag = <NUM_LIT:1> <EOL> old = fcntl . fcntl ( fd , fcntl . F_GETFD ) <EOL> if cloexec : <EOL> fcntl . fcntl ( fd , fcntl . F_SETFD , old | cloexec_flag ) <EOL> else : <EOL> fcntl . fcntl ( fd , fcntl . F_SETFD , old & ~ cloexec_flag ) <EOL> def pipe_cloexec ( self ) : <EOL> """<STR_LIT>""" <EOL> r , w = os . pipe ( ) <EOL> self . _set_cloexec_flag ( r ) <EOL> self . _set_cloexec_flag ( w ) <EOL> return r , w <EOL> def _close_fds ( self , but ) : <EOL> if hasattr ( os , '<STR_LIT>' ) : <EOL> os . closerange ( <NUM_LIT:3> , but ) <EOL> os . closerange ( but + <NUM_LIT:1> , MAXFD ) <EOL> else : <EOL> for i in xrange ( <NUM_LIT:3> , MAXFD ) : <EOL> if i == but : <EOL> continue <EOL> try : <EOL> os . close ( i ) <EOL> except : <EOL> pass <EOL> def _execute_child ( self , args , executable , preexec_fn , close_fds , <EOL> cwd , env , universal_newlines , <EOL> startupinfo , creationflags , shell , <EOL> p2cread , p2cwrite , <EOL> c2pread , c2pwrite , <EOL> errread , errwrite ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( args , types . StringTypes ) : <EOL> args = [ args ] <EOL> else : <EOL> args = list ( args ) <EOL> if shell : <EOL> args = [ "<STR_LIT>" , "<STR_LIT:-c>" ] + args <EOL> if executable : <EOL> args [ <NUM_LIT:0> ] = executable <EOL> if executable is None : <EOL> executable = args [ <NUM_LIT:0> ] <EOL> errpipe_read , errpipe_write = self . pipe_cloexec ( ) <EOL> try : <EOL> try : <EOL> gc_was_enabled = gc . isenabled ( ) <EOL> gc . disable ( ) <EOL> try : <EOL> self . pid = os . fork ( ) <EOL> except : <EOL> if gc_was_enabled : <EOL> gc . enable ( ) <EOL> raise <EOL> self . _child_created = True <EOL> if self . pid == <NUM_LIT:0> : <EOL> try : <EOL> if p2cwrite is not None : <EOL> os . close ( p2cwrite ) <EOL> if c2pread is not None : <EOL> os . close ( c2pread ) <EOL> if errread is not None : <EOL> os . close ( errread ) <EOL> os . close ( errpipe_read ) <EOL> if c2pwrite == <NUM_LIT:0> : <EOL> c2pwrite = os . dup ( c2pwrite ) <EOL> if errwrite == <NUM_LIT:0> or errwrite == <NUM_LIT:1> : <EOL> errwrite = os . dup ( errwrite ) <EOL> def _dup2 ( a , b ) : <EOL> if a == b : <EOL> self . _set_cloexec_flag ( a , False ) <EOL> elif a is not None : <EOL> os . dup2 ( a , b ) <EOL> _dup2 ( p2cread , <NUM_LIT:0> ) <EOL> _dup2 ( c2pwrite , <NUM_LIT:1> ) <EOL> _dup2 ( errwrite , <NUM_LIT:2> ) <EOL> closed = set ( [ None ] ) <EOL> for fd in [ p2cread , c2pwrite , errwrite ] : <EOL> if fd not in closed and fd > <NUM_LIT:2> : <EOL> os . close ( fd ) <EOL> closed . add ( fd ) <EOL> if close_fds : <EOL> self . _close_fds ( but = errpipe_write ) <EOL> if cwd is not None : <EOL> os . chdir ( cwd ) <EOL> if preexec_fn : <EOL> preexec_fn ( ) <EOL> if env is None : <EOL> os . execvp ( executable , args ) <EOL> else : <EOL> os . execvpe ( executable , args , env ) <EOL> except : <EOL> exc_type , exc_value , tb = sys . exc_info ( ) <EOL> exc_lines = traceback . format_exception ( exc_type , <EOL> exc_value , <EOL> tb ) <EOL> exc_value . child_traceback = '<STR_LIT>' . join ( exc_lines ) <EOL> os . write ( errpipe_write , pickle . dumps ( exc_value ) ) <EOL> os . _exit ( <NUM_LIT:255> ) <EOL> if gc_was_enabled : <EOL> gc . enable ( ) <EOL> finally : <EOL> os . close ( errpipe_write ) <EOL> if p2cread is not None and p2cwrite is not None : <EOL> os . close ( p2cread ) <EOL> if c2pwrite is not None and c2pread is not None : <EOL> os . close ( c2pwrite ) <EOL> if errwrite is not None and errread is not None : <EOL> os . close ( errwrite ) <EOL> data = _eintr_retry_call ( os . read , errpipe_read , <NUM_LIT> ) <EOL> finally : <EOL> os . close ( errpipe_read ) <EOL> if data != "<STR_LIT>" : <EOL> try : <EOL> _eintr_retry_call ( os . waitpid , self . pid , <NUM_LIT:0> ) <EOL> except OSError as e : <EOL> if e . errno != errno . ECHILD : <EOL> raise <EOL> child_exception = pickle . loads ( data ) <EOL> for fd in ( p2cwrite , c2pread , errread ) : <EOL> if fd is not None : <EOL> os . close ( fd ) <EOL> raise child_exception <EOL> def _handle_exitstatus ( self , sts , _WIFSIGNALED = os . WIFSIGNALED , <EOL> _WTERMSIG = os . WTERMSIG , _WIFEXITED = os . WIFEXITED , <EOL> _WEXITSTATUS = os . WEXITSTATUS ) : <EOL> if _WIFSIGNALED ( sts ) : <EOL> self . returncode = - _WTERMSIG ( sts ) <EOL> elif _WIFEXITED ( sts ) : <EOL> self . returncode = _WEXITSTATUS ( sts ) <EOL> else : <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> def _internal_poll ( self , _deadstate = None , _waitpid = os . waitpid , <EOL> _WNOHANG = os . WNOHANG , _os_error = os . error ) : <EOL> """<STR_LIT>""" <EOL> if self . returncode is None : <EOL> try : <EOL> pid , sts = _waitpid ( self . pid , _WNOHANG ) <EOL> if pid == self . pid : <EOL> self . _handle_exitstatus ( sts ) <EOL> except _os_error : <EOL> if _deadstate is not None : <EOL> self . returncode = _deadstate <EOL> return self . returncode <EOL> def wait ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . returncode is None : <EOL> try : <EOL> pid , sts = _eintr_retry_call ( os . waitpid , self . pid , <NUM_LIT:0> ) <EOL> except OSError as e : <EOL> if e . errno != errno . ECHILD : <EOL> raise <EOL> sts = <NUM_LIT:0> <EOL> self . _handle_exitstatus ( sts ) <EOL> return self . returncode <EOL> def _communicate ( self , input ) : <EOL> if self . stdin : <EOL> self . stdin . flush ( ) <EOL> if not input : <EOL> self . stdin . close ( ) <EOL> if _has_poll : <EOL> stdout , stderr = self . _communicate_with_poll ( input ) <EOL> else : <EOL> stdout , stderr = self . _communicate_with_select ( input ) <EOL> if stdout is not None : <EOL> stdout = '<STR_LIT>' . join ( stdout ) <EOL> if stderr is not None : <EOL> stderr = '<STR_LIT>' . join ( stderr ) <EOL> if self . universal_newlines and hasattr ( file , '<STR_LIT>' ) : <EOL> if stdout : <EOL> stdout = self . _translate_newlines ( stdout ) <EOL> if stderr : <EOL> stderr = self . _translate_newlines ( stderr ) <EOL> self . wait ( ) <EOL> return ( stdout , stderr ) <EOL> def _communicate_with_poll ( self , input ) : <EOL> stdout = None <EOL> stderr = None <EOL> fd2file = { } <EOL> fd2output = { } <EOL> poller = select . poll ( ) <EOL> def register_and_append ( file_obj , eventmask ) : <EOL> poller . register ( file_obj . fileno ( ) , eventmask ) <EOL> fd2file [ file_obj . fileno ( ) ] = file_obj <EOL> def close_unregister_and_remove ( fd ) : <EOL> poller . unregister ( fd ) <EOL> fd2file [ fd ] . close ( ) <EOL> fd2file . pop ( fd ) <EOL> if self . stdin and input : <EOL> register_and_append ( self . stdin , select . POLLOUT ) <EOL> select_POLLIN_POLLPRI = select . POLLIN | select . POLLPRI <EOL> if self . stdout : <EOL> register_and_append ( self . stdout , select_POLLIN_POLLPRI ) <EOL> fd2output [ self . stdout . fileno ( ) ] = stdout = [ ] <EOL> if self . stderr : <EOL> register_and_append ( self . stderr , select_POLLIN_POLLPRI ) <EOL> fd2output [ self . stderr . fileno ( ) ] = stderr = [ ] <EOL> input_offset = <NUM_LIT:0> <EOL> while fd2file : <EOL> try : <EOL> ready = poller . poll ( ) <EOL> except select . error as e : <EOL> if e . args [ <NUM_LIT:0> ] == errno . EINTR : <EOL> continue <EOL> raise <EOL> for fd , mode in ready : <EOL> if mode & select . POLLOUT : <EOL> chunk = input [ input_offset : input_offset + _PIPE_BUF ] <EOL> try : <EOL> input_offset += os . write ( fd , chunk ) <EOL> except OSError as e : <EOL> if e . errno == errno . EPIPE : <EOL> close_unregister_and_remove ( fd ) <EOL> else : <EOL> raise <EOL> else : <EOL> if input_offset >= len ( input ) : <EOL> close_unregister_and_remove ( fd ) <EOL> elif mode & select_POLLIN_POLLPRI : <EOL> data = os . read ( fd , <NUM_LIT> ) <EOL> if not data : <EOL> close_unregister_and_remove ( fd ) <EOL> fd2output [ fd ] . append ( data ) <EOL> else : <EOL> close_unregister_and_remove ( fd ) <EOL> return ( stdout , stderr ) <EOL> def _communicate_with_select ( self , input ) : <EOL> read_set = [ ] <EOL> write_set = [ ] <EOL> stdout = None <EOL> stderr = None <EOL> if self . stdin and input : <EOL> write_set . append ( self . stdin ) <EOL> if self . stdout : <EOL> read_set . append ( self . stdout ) <EOL> stdout = [ ] <EOL> if self . stderr : <EOL> read_set . append ( self . stderr ) <EOL> stderr = [ ] <EOL> input_offset = <NUM_LIT:0> <EOL> while read_set or write_set : <EOL> try : <EOL> rlist , wlist , xlist = select . select ( read_set , write_set , [ ] ) <EOL> except select . error as e : <EOL> if e . args [ <NUM_LIT:0> ] == errno . EINTR : <EOL> continue <EOL> raise <EOL> if self . stdin in wlist : <EOL> chunk = input [ input_offset : input_offset + _PIPE_BUF ] <EOL> try : <EOL> bytes_written = os . write ( self . stdin . fileno ( ) , chunk ) <EOL> except OSError as e : <EOL> if e . errno == errno . EPIPE : <EOL> self . stdin . close ( ) <EOL> write_set . remove ( self . stdin ) <EOL> else : <EOL> raise <EOL> else : <EOL> input_offset += bytes_written <EOL> if input_offset >= len ( input ) : <EOL> self . stdin . close ( ) <EOL> write_set . remove ( self . stdin ) <EOL> if self . stdout in rlist : <EOL> data = os . read ( self . stdout . fileno ( ) , <NUM_LIT> ) <EOL> if data == "<STR_LIT>" : <EOL> self . stdout . close ( ) <EOL> read_set . remove ( self . stdout ) <EOL> stdout . append ( data ) <EOL> if self . stderr in rlist : <EOL> data = os . read ( self . stderr . fileno ( ) , <NUM_LIT> ) <EOL> if data == "<STR_LIT>" : <EOL> self . stderr . close ( ) <EOL> read_set . remove ( self . stderr ) <EOL> stderr . append ( data ) <EOL> return ( stdout , stderr ) <EOL> def send_signal ( self , sig ) : <EOL> """<STR_LIT>""" <EOL> os . kill ( self . pid , sig ) <EOL> def terminate ( self ) : <EOL> """<STR_LIT>""" <EOL> self . send_signal ( signal . SIGTERM ) <EOL> def kill ( self ) : <EOL> """<STR_LIT>""" <EOL> self . send_signal ( signal . SIGKILL ) <EOL> def _demo_posix ( ) : <EOL> plist = Popen ( [ "<STR_LIT>" ] , stdout = PIPE ) . communicate ( ) [ <NUM_LIT:0> ] <EOL> print "<STR_LIT>" <EOL> print plist <EOL> if os . getuid ( ) == <NUM_LIT:0> : <EOL> p = Popen ( [ "<STR_LIT:id>" ] , preexec_fn = lambda : os . setuid ( <NUM_LIT:100> ) ) <EOL> p . wait ( ) <EOL> print "<STR_LIT>" <EOL> p1 = Popen ( [ "<STR_LIT>" ] , stdout = PIPE ) <EOL> p2 = Popen ( [ "<STR_LIT>" , "<STR_LIT>" ] , stdin = p1 . stdout , stdout = PIPE ) <EOL> print repr ( p2 . communicate ( ) [ <NUM_LIT:0> ] ) <EOL> print <EOL> print "<STR_LIT>" <EOL> try : <EOL> print Popen ( [ "<STR_LIT>" ] ) . communicate ( ) <EOL> except OSError , e : <EOL> if e . errno == errno . ENOENT : <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT>" <EOL> print e . child_traceback <EOL> else : <EOL> print "<STR_LIT>" , e . errno <EOL> else : <EOL> print >> sys . stderr , "<STR_LIT>" <EOL> def _demo_windows ( ) : <EOL> print "<STR_LIT>" <EOL> p1 = Popen ( "<STR_LIT>" , stdout = PIPE , shell = True ) <EOL> p2 = Popen ( '<STR_LIT>' , stdin = p1 . stdout , stdout = PIPE ) <EOL> print repr ( p2 . communicate ( ) [ <NUM_LIT:0> ] ) <EOL> print "<STR_LIT>" <EOL> p = Popen ( "<STR_LIT>" ) <EOL> p . wait ( ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> if mswindows : <EOL> _demo_windows ( ) <EOL> else : <EOL> _demo_posix ( ) </s>
