<s> r"""<STR_LIT>""" <EOL> import subprocess <EOL> import sys <EOL> import os <EOL> import time <EOL> import types <EOL> try : <EOL> from subprocess import CalledProcessError <EOL> except ImportError : <EOL> class CalledProcessError ( Exception ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , returncode , cmd ) : <EOL> self . returncode = returncode <EOL> self . cmd = cmd <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT>" % ( self . cmd , self . returncode ) <EOL> mswindows = ( sys . platform == "<STR_LIT:win32>" ) <EOL> if mswindows : <EOL> import winprocess <EOL> else : <EOL> import signal <EOL> def call ( * args , ** kwargs ) : <EOL> waitargs = { } <EOL> if "<STR_LIT>" in kwargs : <EOL> waitargs [ "<STR_LIT>" ] = kwargs . pop ( "<STR_LIT>" ) <EOL> return Popen ( * args , ** kwargs ) . wait ( ** waitargs ) <EOL> def check_call ( * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> retcode = call ( * args , ** kwargs ) <EOL> if retcode : <EOL> cmd = kwargs . get ( "<STR_LIT:args>" ) <EOL> if cmd is None : <EOL> cmd = args [ <NUM_LIT:0> ] <EOL> raise CalledProcessError ( retcode , cmd ) <EOL> if not mswindows : <EOL> def DoNothing ( * args ) : <EOL> pass <EOL> class Popen ( subprocess . Popen ) : <EOL> if not mswindows : <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> if len ( args ) >= <NUM_LIT:7> : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> real_preexec_fn = kwargs . pop ( "<STR_LIT>" , None ) <EOL> def setpgid_preexec_fn ( ) : <EOL> os . setpgid ( <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> if real_preexec_fn : <EOL> apply ( real_preexec_fn ) <EOL> kwargs [ '<STR_LIT>' ] = setpgid_preexec_fn <EOL> subprocess . Popen . __init__ ( self , * args , ** kwargs ) <EOL> if mswindows : <EOL> def _execute_child ( self , args , executable , preexec_fn , close_fds , <EOL> cwd , env , universal_newlines , startupinfo , <EOL> creationflags , shell , <EOL> p2cread , p2cwrite , <EOL> c2pread , c2pwrite , <EOL> errread , errwrite ) : <EOL> if not isinstance ( args , types . StringTypes ) : <EOL> args = subprocess . list2cmdline ( args ) <EOL> if startupinfo is None : <EOL> startupinfo = winprocess . STARTUPINFO ( ) <EOL> if None not in ( p2cread , c2pwrite , errwrite ) : <EOL> startupinfo . dwFlags |= winprocess . STARTF_USESTDHANDLES <EOL> startupinfo . hStdInput = int ( p2cread ) <EOL> startupinfo . hStdOutput = int ( c2pwrite ) <EOL> startupinfo . hStdError = int ( errwrite ) <EOL> if shell : <EOL> startupinfo . dwFlags |= winprocess . STARTF_USESHOWWINDOW <EOL> startupinfo . wShowWindow = winprocess . SW_HIDE <EOL> comspec = os . environ . get ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> args = comspec + "<STR_LIT>" + args <EOL> self . _job = winprocess . CreateJobObject ( ) <EOL> creationflags |= winprocess . CREATE_SUSPENDED <EOL> creationflags |= winprocess . CREATE_UNICODE_ENVIRONMENT <EOL> creationflags |= winprocess . CREATE_BREAKAWAY_FROM_JOB <EOL> hp , ht , pid , tid = winprocess . CreateProcess ( <EOL> executable , args , <EOL> None , None , <EOL> <NUM_LIT:1> , <EOL> creationflags , <EOL> winprocess . EnvironmentBlock ( env ) , <EOL> cwd , startupinfo ) <EOL> self . _child_created = True <EOL> self . _handle = hp <EOL> self . _thread = ht <EOL> self . pid = pid <EOL> winprocess . AssignProcessToJobObject ( self . _job , hp ) <EOL> winprocess . ResumeThread ( ht ) <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> def kill ( self , group = True ) : <EOL> """<STR_LIT>""" <EOL> if mswindows : <EOL> if group : <EOL> winprocess . TerminateJobObject ( self . _job , <NUM_LIT> ) <EOL> else : <EOL> winprocess . TerminateProcess ( self . _handle , <NUM_LIT> ) <EOL> self . returncode = <NUM_LIT> <EOL> else : <EOL> if group : <EOL> os . killpg ( self . pid , signal . SIGKILL ) <EOL> else : <EOL> os . kill ( self . pid , signal . SIGKILL ) <EOL> self . returncode = - <NUM_LIT:9> <EOL> def wait ( self , timeout = - <NUM_LIT:1> , group = True ) : <EOL> """<STR_LIT>""" <EOL> if self . returncode is not None : <EOL> return self . returncode <EOL> if mswindows : <EOL> if timeout != - <NUM_LIT:1> : <EOL> timeout = timeout * <NUM_LIT:1000> <EOL> rc = winprocess . WaitForSingleObject ( self . _handle , timeout ) <EOL> if rc == winprocess . WAIT_TIMEOUT : <EOL> self . kill ( group ) <EOL> else : <EOL> self . returncode = winprocess . GetExitCodeProcess ( self . _handle ) <EOL> else : <EOL> if timeout == - <NUM_LIT:1> : <EOL> subprocess . Popen . wait ( self ) <EOL> return self . returncode <EOL> starttime = time . time ( ) <EOL> oldsignal = signal . signal ( signal . SIGCHLD , DoNothing ) <EOL> while time . time ( ) < starttime + timeout - <NUM_LIT> : <EOL> pid , sts = os . waitpid ( self . pid , os . WNOHANG ) <EOL> if pid != <NUM_LIT:0> : <EOL> self . _handle_exitstatus ( sts ) <EOL> signal . signal ( signal . SIGCHLD , oldsignal ) <EOL> return self . returncode <EOL> newtimeout = timeout - time . time ( ) + starttime <EOL> time . sleep ( newtimeout ) <EOL> self . kill ( group ) <EOL> signal . signal ( signal . SIGCHLD , oldsignal ) <EOL> subprocess . Popen . wait ( self ) <EOL> return self . returncode </s>
