<s> """<STR_LIT>""" <EOL> import os <EOL> import win32api <EOL> import win32con <EOL> import win32event <EOL> import win32file <EOL> import win32pipe <EOL> import win32process <EOL> import win32security <EOL> import pywintypes <EOL> PIPE_ATTRS_INHERITABLE = win32security . SECURITY_ATTRIBUTES ( ) <EOL> PIPE_ATTRS_INHERITABLE . bInheritHandle = <NUM_LIT:1> <EOL> from zope . interface import implements <EOL> from twisted . internet . interfaces import IProcessTransport , IConsumer , IProducer <EOL> from twisted . python . win32 import quoteArguments <EOL> from twisted . internet import error <EOL> from twisted . python import failure <EOL> from twisted . internet import _pollingfile <EOL> from twisted . internet . _baseprocess import BaseProcess <EOL> def debug ( msg ) : <EOL> import sys <EOL> print msg <EOL> sys . stdout . flush ( ) <EOL> class _Reaper ( _pollingfile . _PollableResource ) : <EOL> def __init__ ( self , proc ) : <EOL> self . proc = proc <EOL> def checkWork ( self ) : <EOL> if win32event . WaitForSingleObject ( self . proc . hProcess , <NUM_LIT:0> ) != win32event . WAIT_OBJECT_0 : <EOL> return <NUM_LIT:0> <EOL> exitCode = win32process . GetExitCodeProcess ( self . proc . hProcess ) <EOL> self . deactivate ( ) <EOL> self . proc . processEnded ( exitCode ) <EOL> return <NUM_LIT:0> <EOL> def _findShebang ( filename ) : <EOL> """<STR_LIT>""" <EOL> f = file ( filename , '<STR_LIT>' ) <EOL> if f . read ( <NUM_LIT:2> ) == '<STR_LIT>' : <EOL> exe = f . readline ( <NUM_LIT> ) . strip ( '<STR_LIT:\n>' ) <EOL> return exe <EOL> def _invalidWin32App ( pywinerr ) : <EOL> """<STR_LIT>""" <EOL> return pywinerr . args [ <NUM_LIT:0> ] == <NUM_LIT> <EOL> class Process ( _pollingfile . _PollingTimer , BaseProcess ) : <EOL> """<STR_LIT>""" <EOL> implements ( IProcessTransport , IConsumer , IProducer ) <EOL> closedNotifies = <NUM_LIT:0> <EOL> def __init__ ( self , reactor , protocol , command , args , environment , path ) : <EOL> _pollingfile . _PollingTimer . __init__ ( self , reactor ) <EOL> BaseProcess . __init__ ( self , protocol ) <EOL> sAttrs = win32security . SECURITY_ATTRIBUTES ( ) <EOL> sAttrs . bInheritHandle = <NUM_LIT:1> <EOL> self . hStdoutR , hStdoutW = win32pipe . CreatePipe ( sAttrs , <NUM_LIT:0> ) <EOL> self . hStderrR , hStderrW = win32pipe . CreatePipe ( sAttrs , <NUM_LIT:0> ) <EOL> hStdinR , self . hStdinW = win32pipe . CreatePipe ( sAttrs , <NUM_LIT:0> ) <EOL> win32pipe . SetNamedPipeHandleState ( self . hStdinW , <EOL> win32pipe . PIPE_NOWAIT , <EOL> None , <EOL> None ) <EOL> StartupInfo = win32process . STARTUPINFO ( ) <EOL> StartupInfo . hStdOutput = hStdoutW <EOL> StartupInfo . hStdError = hStderrW <EOL> StartupInfo . hStdInput = hStdinR <EOL> StartupInfo . dwFlags = win32process . STARTF_USESTDHANDLES <EOL> currentPid = win32api . GetCurrentProcess ( ) <EOL> tmp = win32api . DuplicateHandle ( currentPid , self . hStdoutR , currentPid , <NUM_LIT:0> , <NUM_LIT:0> , <EOL> win32con . DUPLICATE_SAME_ACCESS ) <EOL> win32file . CloseHandle ( self . hStdoutR ) <EOL> self . hStdoutR = tmp <EOL> tmp = win32api . DuplicateHandle ( currentPid , self . hStderrR , currentPid , <NUM_LIT:0> , <NUM_LIT:0> , <EOL> win32con . DUPLICATE_SAME_ACCESS ) <EOL> win32file . CloseHandle ( self . hStderrR ) <EOL> self . hStderrR = tmp <EOL> tmp = win32api . DuplicateHandle ( currentPid , self . hStdinW , currentPid , <NUM_LIT:0> , <NUM_LIT:0> , <EOL> win32con . DUPLICATE_SAME_ACCESS ) <EOL> win32file . CloseHandle ( self . hStdinW ) <EOL> self . hStdinW = tmp <EOL> env = os . environ . copy ( ) <EOL> env . update ( environment or { } ) <EOL> cmdline = quoteArguments ( args ) <EOL> def doCreate ( ) : <EOL> self . hProcess , self . hThread , self . pid , dwTid = win32process . CreateProcess ( <EOL> command , cmdline , None , None , <NUM_LIT:1> , <NUM_LIT:0> , env , path , StartupInfo ) <EOL> try : <EOL> doCreate ( ) <EOL> except pywintypes . error , pwte : <EOL> if not _invalidWin32App ( pwte ) : <EOL> raise OSError ( pwte ) <EOL> else : <EOL> sheb = _findShebang ( command ) <EOL> if sheb is None : <EOL> raise OSError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % command ) <EOL> else : <EOL> args = list ( args ) <EOL> args . insert ( <NUM_LIT:0> , command ) <EOL> cmdline = quoteArguments ( args ) <EOL> origcmd = command <EOL> command = sheb <EOL> try : <EOL> doCreate ( ) <EOL> except pywintypes . error , pwte2 : <EOL> if _invalidWin32App ( pwte2 ) : <EOL> raise OSError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % ( <EOL> origcmd , sheb ) ) <EOL> raise OSError ( pwte2 ) <EOL> win32file . CloseHandle ( hStderrW ) <EOL> win32file . CloseHandle ( hStdoutW ) <EOL> win32file . CloseHandle ( hStdinR ) <EOL> self . stdout = _pollingfile . _PollableReadPipe ( <EOL> self . hStdoutR , <EOL> lambda data : self . proto . childDataReceived ( <NUM_LIT:1> , data ) , <EOL> self . outConnectionLost ) <EOL> self . stderr = _pollingfile . _PollableReadPipe ( <EOL> self . hStderrR , <EOL> lambda data : self . proto . childDataReceived ( <NUM_LIT:2> , data ) , <EOL> self . errConnectionLost ) <EOL> self . stdin = _pollingfile . _PollableWritePipe ( <EOL> self . hStdinW , self . inConnectionLost ) <EOL> for pipewatcher in self . stdout , self . stderr , self . stdin : <EOL> self . _addPollableResource ( pipewatcher ) <EOL> self . proto . makeConnection ( self ) <EOL> self . _addPollableResource ( _Reaper ( self ) ) <EOL> def signalProcess ( self , signalID ) : <EOL> if self . pid is None : <EOL> raise error . ProcessExitedAlready ( ) <EOL> if signalID in ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) : <EOL> win32process . TerminateProcess ( self . hProcess , <NUM_LIT:1> ) <EOL> def _getReason ( self , status ) : <EOL> if status == <NUM_LIT:0> : <EOL> return error . ProcessDone ( status ) <EOL> return error . ProcessTerminated ( status ) <EOL> def write ( self , data ) : <EOL> """<STR_LIT>""" <EOL> self . stdin . write ( data ) <EOL> def writeSequence ( self , seq ) : <EOL> """<STR_LIT>""" <EOL> self . stdin . writeSequence ( seq ) <EOL> def closeChildFD ( self , fd ) : <EOL> if fd == <NUM_LIT:0> : <EOL> self . closeStdin ( ) <EOL> elif fd == <NUM_LIT:1> : <EOL> self . closeStdout ( ) <EOL> elif fd == <NUM_LIT:2> : <EOL> self . closeStderr ( ) <EOL> else : <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> def closeStdin ( self ) : <EOL> """<STR_LIT>""" <EOL> self . stdin . close ( ) <EOL> def closeStderr ( self ) : <EOL> self . stderr . close ( ) <EOL> def closeStdout ( self ) : <EOL> self . stdout . close ( ) <EOL> def loseConnection ( self ) : <EOL> """<STR_LIT>""" <EOL> self . closeStdin ( ) <EOL> self . closeStdout ( ) <EOL> self . closeStderr ( ) <EOL> def outConnectionLost ( self ) : <EOL> self . proto . childConnectionLost ( <NUM_LIT:1> ) <EOL> self . connectionLostNotify ( ) <EOL> def errConnectionLost ( self ) : <EOL> self . proto . childConnectionLost ( <NUM_LIT:2> ) <EOL> self . connectionLostNotify ( ) <EOL> def inConnectionLost ( self ) : <EOL> self . proto . childConnectionLost ( <NUM_LIT:0> ) <EOL> self . connectionLostNotify ( ) <EOL> def connectionLostNotify ( self ) : <EOL> """<STR_LIT>""" <EOL> self . closedNotifies += <NUM_LIT:1> <EOL> self . maybeCallProcessEnded ( ) <EOL> def maybeCallProcessEnded ( self ) : <EOL> if self . closedNotifies == <NUM_LIT:3> and self . lostProcess : <EOL> win32file . CloseHandle ( self . hProcess ) <EOL> win32file . CloseHandle ( self . hThread ) <EOL> self . hProcess = None <EOL> self . hThread = None <EOL> BaseProcess . maybeCallProcessEnded ( self ) <EOL> def registerProducer ( self , producer , streaming ) : <EOL> self . stdin . registerProducer ( producer , streaming ) <EOL> def unregisterProducer ( self ) : <EOL> self . stdin . unregisterProducer ( ) <EOL> def pauseProducing ( self ) : <EOL> self . _pause ( ) <EOL> def resumeProducing ( self ) : <EOL> self . _unpause ( ) <EOL> def stopProducing ( self ) : <EOL> self . loseConnection ( ) <EOL> def __repr__ ( self ) : <EOL> """<STR_LIT>""" <EOL> return "<STR_LIT>" % ( self . __class__ . __name__ , self . pid ) </s>
