Fa}@sdZddlZejdkZddlZddlZddlZddlZddlZddl Z ddl Z ddlm Z Gddde ZGdddeZGd d d eZerddlZddlZddlZGd d d ZnddlZddlZddlZyddlZWnek r[ddlZYnXeed dZeedrejZn ejZddddddddddddd dgZ er=ddlm!Z!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z(e j)ddddd d!d"d#gGd$d%d%e*Z+gZ,d&d'Z-d;Z.d<Z/d=Z0d+d,Z1d-dd.dZ2d/dZ3d-dd0dZ4Gd1dde5Z6d2dd-dd3d4d5dZ7d6d7Z8d8dZ9d9dZ:e5Z;Gd:dde5Z<dS)>aSubprocesses with accessible I/O streams This module allows you to spawn processes, connect to their input/output/error pipes, and obtain their return codes. For a complete description of this module see the Python documentation. Main API ======== run(...): Runs a command, waits for it to complete, then returns a CompletedProcess instance. Popen(...): A class for flexibly executing a command in a new process Constants --------- DEVNULL: Special value that indicates that os.devnull should be used PIPE: Special value that indicates a pipe should be created STDOUT: Special value that indicates that stderr should go to stdout Older API ========= call(...): Runs a command, waits for it to complete, then returns the return code. check_call(...): Same as call() but raises CalledProcessError() if return code is not 0 check_output(...): Same as check_call() but returns the contents of stdout instead of a return code getoutput(...): Runs a command in the shell, waits for it to complete, then returns the output getstatusoutput(...): Runs a command in the shell, waits for it to complete, then returns a (status, output) tuple NZwin32) monotonicc@seZdZdS)SubprocessErrorN)__name__ __module__ __qualname__rr /usr/lib/python3.5/subprocess.pyr9s rc@s[eZdZdZddddZddZeddZejd dZdS) CalledProcessErrorzRaised when run() is called with check=True and the process returns a non-zero exit status. Attributes: cmd, returncode, stdout, stderr, output NcCs(||_||_||_||_dS)N) returncodecmdoutputstderr)selfr r r r rrr__init__Cs   zCalledProcessError.__init__cCsd|j|jfS)Nz-Command '%s' returned non-zero exit status %d)r r )rrrr__str__IszCalledProcessError.__str__cCs|jS)z+Alias for output attribute, to match stderr)r )rrrrstdoutLszCalledProcessError.stdoutcCs ||_dS)N)r )rvaluerrrrQs) rrr__doc__rrpropertyrsetterrrrrr <s  r c@s[eZdZdZddddZddZeddZejd dZdS) TimeoutExpiredzThis exception is raised when the timeout expires while waiting for a child process. Attributes: cmd, output, stdout, stderr, timeout NcCs(||_||_||_||_dS)N)r timeoutr r )rr rr r rrrr_s   zTimeoutExpired.__init__cCsd|j|jfS)Nz'Command '%s' timed out after %s seconds)r r)rrrrreszTimeoutExpired.__str__cCs|jS)N)r )rrrrriszTimeoutExpired.stdoutcCs ||_dS)N)r )rrrrrrms) rrrrrrrrrrrrrrXs  rc@s.eZdZdZdZdZdZdZdS) STARTUPINFOrN)rrrdwFlags hStdInput hStdOutput hStdError wShowWindowrrrrrxs rZPIPE_BUFi PollSelectorPopenPIPESTDOUTcall check_callgetstatusoutput getoutput check_outputrunDEVNULLCompletedProcess)CREATE_NEW_CONSOLECREATE_NEW_PROCESS_GROUPSTD_INPUT_HANDLESTD_OUTPUT_HANDLESTD_ERROR_HANDLESW_HIDESTARTF_USESTDHANDLESSTARTF_USESHOWWINDOWr*r+r,r-r.r/r0r1c@sLeZdZdZejddZddZddZeZ eZ dS) HandleFcCs |jsd|_||dS)NT)closed)r CloseHandlerrrCloses  z Handle.ClosecCs,|jsd|_t|StddS)NTzalready closed)r3int ValueError)rrrrDetachs   z Handle.DetachcCsd|jjt|fS)Nz%s(%d)) __class__rr6)rrrr__repr__szHandle.__repr__N) rrrr3_winapir4r5r8r:__del__rrrrrr2s   r2c Csfx_tddD]M}|jdtj}|dk rytj|Wqtk r]YqXqWdS)N _deadstate)_active_internal_pollsysmaxsizeremover7)ZinstZresrrr_cleanups  rCcCsddddddddd d d d d dddddi }g}xM|jD]?\}}ttj|}|dkrO|jd||qOWx"tjD]}|jd|qW|S)znReturn a list of command-line arguments reproducing the current settings in sys.flags and sys.warnoptions.debugdoptimizeOdont_write_bytecodeB no_user_sitesno_siteSignore_environmentEverbosev bytes_warningbquietqr-z-W)itemsgetattrr@flagsappend warnoptions)Z flag_opt_mapargsZflagZoptrTrrr_args_from_interpreter_flagss"  r`rcOsSt||=}y|jd|SWn|j|jYnXWdQRXdS)zRun command with arguments. Wait for command to complete or timeout, then return the returncode attribute. The arguments are the same as for the Popen constructor. Example: retcode = call(["ls", "-l"]) rN)rwaitkill)r popenargskwargsprrrr"s  cOsMt||}|rI|jd}|dkr:|d}t||dS)aORun command with arguments. Wait for command to complete. If the exit code was zero then return, otherwise raise CalledProcessError. The CalledProcessError object will have the return code in the returncode attribute. The arguments are the same as for the call function. Example: check_call(["ls", "-l"]) r_Nr)r"getr )rcrdretcoder rrrr#s   c Osxd|krtdd|krV|ddkrV|jddrLdnd|d>> check_output(["ls", "-l", "/dev/null"]) b'crw-rw-rw- 1 root root 1, 3 Oct 18 2007 /dev/null\n' The stdout argument is not allowed as it is used internally. To capture standard error in the result, use stderr=STDOUT. >>> check_output(["/bin/sh", "-c", ... "ls -l non_existent_file ; exit 0"], ... stderr=STDOUT) b'ls: non_existent_file: No such file or directory\n' There is an additional optional argument, "input", allowing you to pass a string to the subprocess's stdin. If you use this argument you may not also use the Popen constructor's "stdin" argument, as it too will be used internally. Example: >>> check_output(["sed", "-e", "s/foo/bar/"], ... input=b"when in the course of fooman events\n") b'when in the course of barman events\n' If universal_newlines=True is passed, the "input" argument must be a string and the return value will be a string rather than bytes. rz3stdout argument not allowed, it will be overridden.inputNuniversal_newlinesFrcheckT)r7rfr'r r)rrcrdrrrr&s  "c@s@eZdZdZddddZddZddZdS) r)aEA process that has finished running. This is returned by run(). Attributes: args: The list or str args passed to run(). returncode: The exit code of the process, negative for signals. stdout: The standard output (None if not captured). stderr: The standard error (None if not captured). NcCs(||_||_||_||_dS)N)r_r rr )rr_r rr rrrrJs   zCompletedProcess.__init__cCsdj|jdj|jg}|jdk rL|jdj|j|jdk rt|jdj|jdjt|jdj|S)Nz args={!r}zreturncode={!r}z stdout={!r}z stderr={!r}z{}({})z, ) formatr_r rr]r typerjoin)rr_rrrr:PszCompletedProcess.__repr__cCs.|jr*t|j|j|j|jdS)z6Raise CalledProcessError if the exit code is non-zero.N)r r r_rr )rrrrcheck_returncodeYs z!CompletedProcess.check_returncode)rrrrrr:rprrrrr)?s  rhrlFc Os|dk r.d|kr$tdt|d>> import subprocess >>> subprocess.getstatusoutput('ls /bin/ls') (0, '/bin/ls') >>> subprocess.getstatusoutput('cat /bin/junk') (256, 'cat: /bin/junk: No such file or directory') >>> subprocess.getstatusoutput('/bin/junk') (256, 'sh: /bin/junk: not found') shellTrir rNrD r)r&r!r r r )r dataZstatusZexrrrr$s  cCst|dS)a%Return output (stdout or stderr) of executing cmd in a shell. Like getstatusoutput(), except the exit status is ignored and the return value is a string containing the command's output. Example: >>> import subprocess >>> subprocess.getoutput('ls /bin/ls') '/bin/ls' rD)r$)r rrrr%s c@s)eZdZdZdZd>dddddeddddddddfddZd d Zd d Zd dZ e j ddZ ddZ ddZddddZddZddZddZernddZdd Zd!d"Zdejejejd#d$Zddd%d&Zd'd(Zd)d*Zd+d,Zd-d.Z e Z!nd/dZd0d"Ze"j#e"j$e"j%e"j&d1d2Z'de"j(e"j)e*j+d3d$Zd4d5Z,ddd6d&Zd7d*Zd8d9Z-d:d,Zd;d.Z d<d=Z!dS)?ra Execute a child program in a new process. For a complete description of the arguments see the Python documentation. Arguments: args: A string, or a sequence of program arguments. bufsize: supplied as the buffering argument to the open() function when creating the stdin/stdout/stderr pipe file objects executable: A replacement program to execute. stdin, stdout and stderr: These specify the executed programs' standard input, standard output and standard error file handles, respectively. preexec_fn: (POSIX only) An object to be called in the child process just before the child is executed. close_fds: Controls closing or inheriting of file descriptors. shell: If true, the command will be executed through the shell. cwd: Sets the current directory before the child is executed. env: Defines the environment variables for the new process. universal_newlines: If true, use universal line endings for file objects stdin, stdout and stderr. startupinfo and creationflags (Windows only) restore_signals (POSIX only) start_new_session (POSIX only) pass_fds (POSIX only) Attributes: stdin, stdout, stderr, pid, returncode FrDNrTcCs@ttj|_d|_d|_|dkr:d}t|tsUtdt r|dk rst d|dk p|dk p|dk }|t kr|rd}qd}q;|r;|r;t dne|t krd}|r | r t j dtd}| dk r#t d |d kr;t d ||_d|_d|_d|_d|_d|_| |_|j|||\}}}}}}t r|dkrtj|jd }|dkrtj|jd }|dkrtj|jd }|dkrdtj|d ||_| rdtj|jd dd|dk|_|dkrtj|d||_| rtj|j|_|dkrtj|d||_| rtj|j|_d|_yD|j|||||| | | || ||||||||Wn xLtd|j|j|jfD])}y|j WqWt!k rYqWXqWW|js4g}|t"kr|j#||t"kr|j#||t"kr|j#|t$|dr|j#|j%x4|D],}yt&j |Wqt!k r/YqXqWYnXdS)zCreate new Popen instance.NFrDzbufsize must be an integerz0preexec_fn is not supported on Windows platformsTzSclose_fds is not supported on Windows platforms if you redirect stdin/stdout/stderrzpass_fds overriding close_fds.z2startupinfo is only supported on Windows platformsrz4creationflags is only supported on Windows platformswbZ write_throughline_bufferingrb_devnullrrrrrrr)'rC threadingZLock _waitpid_lock_input_communication_started isinstancer6 TypeError _mswindowsr7_PLATFORM_DEFAULT_CLOSE_FDSwarningswarnRuntimeWarningr_rqrr pidr ri _get_handlesmsvcrtZopen_osfhandler8ioopen TextIOWrapper_closed_child_pipe_fds_execute_childfiltercloseOSErrorr r]hasattrros)rr_bufsize executablerqrr preexec_fn close_fdsrcwdenvri startupinfo creationflagsrestore_signalsstart_new_sessionpass_fdsZ any_stdio_setp2creadp2cwritec2preadc2pwriteerrreaderrwritefZto_closefdrrrr3s                         '         (            zPopen.__init__cCs+|j|}|jddjddS)Nz r )decodereplace)rrencodingrrr_translate_newlinesszPopen._translate_newlinescCs|S)Nr)rrrr __enter__szPopen.__enter__c CsX|jr|jj|jr,|jjz|jrE|jjWd|jXdS)N)rrr rqra)rrnr tracebackrrr__exit__s     zPopen.__exit__cCsI|js dS|jd||jdkrEtdk rEtj|dS)Nr=)_child_createdr?r r>r])rZ_maxsizerrrr<s  z Popen.__del__cCs1t|ds*tjtjtj|_|jS)Nr)rrrdevnullO_RDWRr)rrrr _get_devnullszPopen._get_devnullcCs|r}y|jj|Wn`tk r.YnOtk r|}z/|jtjkrg|jdk rgnWYdd}~XnXy|jjWn`tk rYnOtk r}z/|jtjkr|jdk rnWYdd}~XnXdS)N)rqwriteBrokenPipeErrorrerrnoZEINVALrsr)rrherrr _stdin_writes" $ $zPopen._stdin_writec CsJ|jr|rtd|dkr|j r|j|j|jgjddkrd}d}|jr}|j|nM|jr|jj}|jjn%|jr|jj}|jj|j ni|dk rt |}nd}z|j |||\}}Wdd|_X|j d|j |}||fS)a4Interact with process: Send data to stdin. Read data from stdout and stderr, until end-of-file is reached. Wait for process to terminate. The optional "input" argument should be data to be sent to the child process (if self.universal_newlines is True, this should be a string; if it is False, "input" should be bytes), or None, if no data should be sent to the child. communicate() returns a tuple (stdout, stderr). These will be bytes or, if self.universal_newlines was True, a string. z.Cannot send input after starting communicationNrETr) rr7rqrr countrreadrra_time _communicate_remaining_time)rrhrrr endtimestsrrrrrs. '       zPopen.communicatecCs |jS)zSCheck if child process has terminated. Set and return returncode attribute.)r?)rrrrrs*sz Popen.pollcCs|dkrdS|tSdS)z5Convenience for _communicate when computing timeouts.N)r)rrrrrr0s zPopen._remaining_timecCs5|dkrdSt|kr1t|j|dS)z2Convenience for checking if a timeout has expired.N)rrr_)rr orig_timeoutrrr_check_timeout8s zPopen._check_timeoutc Cs|dkr(|dkr(|dkr(d Sd \}}d\}}d\}} |dkrtjtj}|dkrDtjdd\}} t|}tj| n|tkrtjdd\}}t|t|}}nZ|tkrtj |j }n6t |t r/tj |}ntj |j }|j|}|dkrtjtj}|dkrKtjdd\} }t|}tj| n|tkrtjdd\}}t|t|}}nZ|tkrtj |j }n6t |t r6tj |}ntj |j }|j|}|dkrtjtj} | dkrgtjdd\} } t| } tj| n|tkrtjdd\}} t|t| }} no|tkr |} nZ|tkr1tj |j } n6t |t rRtj |} ntj |j } |j| } |||||| fS)z|Construct and return tuple with IO objects: p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite NrDrrrrrrr)rrrrrrrr)rrrr)rrrr)rr)r;Z GetStdHandler,Z CreatePiper2r4r r(rZ get_osfhandlerrr6fileno_make_inheritabler-r.r!) rrqrr rrrrrr_rrrrDsn$                    zPopen._get_handlescCs7tjtj|tjddtj}t|S)z2Return a duplicate of handle, which is inheritablerrD)r;ZDuplicateHandleZGetCurrentProcessZDUPLICATE_SAME_ACCESSr2)rZhandlehrrrrs   zPopen._make_inheritablecCs| stdt|ts.t|}|dkrCt}d | ||fkr|jtjO_| |_||_ ||_ | r|jtj O_tj |_ tjjdd}dj||}z>tj||ddt| | ||| \}}}}Wd| d kr'| j|d kr=|j|d krS|jt|drrtj|jXd|_t||_||_tj|dS) z$Execute program (MS Windows version)z"pass_fds not supported on Windows.NrDZCOMSPECzcmd.exez {} /c "{}"rTrrrr)AssertionErrorrstrr~rrr;r0rrrr1r/rrenvironrfrmZ CreateProcessr6r5rrrrr2_handlerr4)rr_rrrrrrrrrrrrrrrZunused_restore_signalsZunused_start_new_sessionZcomspecZhpZhtrtidrrrrsF                  zPopen._execute_childcCs@|jdkr9||jd|kr9||j|_|jS)zCheck if child process has terminated. Returns returncode attribute. This method is called by __del__, so it can only refer to objects in its local scope. Nr)r r)rr=Z_WaitForSingleObjectZ_WAIT_OBJECT_0Z_GetExitCodeProcessrrrr?s zPopen._internal_pollcCs|dk r|j|}|dkr3tj}nt|d}|jdkrtj|j|}|tjkrt|j |tj |j|_|jS)zOWait for child process to terminate. Returns returncode attribute.Ni) rr;ZINFINITEr6r WaitForSingleObjectrZ WAIT_TIMEOUTrr_GetExitCodeProcess)rrrZtimeout_millisr{rrrras     z Popen.waitcCs!|j|j|jdS)N)r]rr)rZfhbufferrrr _readerthreadszPopen._readerthreadcCs|jret|d reg|_tjd|jd|j|jf|_d|j_|jj|j rt|d rg|_ tjd|jd|j |j f|_ d|j _|j j|j r|j ||jdk r)|jj|j||jjr)t|j||j dk rr|j j|j||j jrrt|j|d}d}|jr|j}|jj|j r|j }|j j|dk r|d}|dk r|d}||fS)N _stdout_bufftargetr_T _stderr_buffr)rrrrZThreadrZ stdout_threadZdaemonstartr rZ stderr_threadrqrrorZis_aliverr_r)rrhrrrr rrrrsF                  zPopen._communicatecCs|jdk rdS|tjkr/|jne|tjkrWtj|jtjn=|tjkrtj|jtjnt dj |dS)zSend a signal to the process.NzUnsupported signal: {}) r signalSIGTERM terminateZ CTRL_C_EVENTrrbrZCTRL_BREAK_EVENTr7rm)rsigrrr send_signal.s zPopen.send_signalc Csp|jdk rdSytj|jdWn?tk rktj|j}|tjkr^||_YnXdS)zTerminates the process.NrD)r r;ZTerminateProcessrPermissionErrorrZ STILL_ACTIVE)rZrcrrrr<s zPopen.terminatec Csd\}}d\}}d \}} |dkr3n`|tkrTtj\}}n?|tkro|j}n$t|tr|}n |j}|dkrn`|tkrtj\}}n?|tkr|j}n$t|tr|}n |j}|dkrn|tkr2tj\}} nr|tkre|d krS|} qt j j} n?|tkr|j} n$t|tr|} n |j} |||||| fS) z|Construct and return tuple with IO objects: p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite rDNrr)rrrr)rrrr)rrr) r rpiper(rrr6rr!r@ __stdout__) rrqrr rrrrrrrrrrQsJ                     c$"s]t|ttfr!|g}n t|}| rSddg|}rS|d<dkri|d}tj\}}g}x,|dkr|j|tj|}qWx|D]}tj|qWzz|dk rdd|j D}nd}tj tj j r5f}n(t fdd tj|D}t|}|j|tj|||t|||| | | |||||||||_d |_Wdtj|Xt|d d}| dkr| dkr| |krtj| |dkrM| dkrM||krMtj||dkr~|dkr~||kr~tj||dk rtj|d |_t}x:tj|d }||7}| st|d krPqWWdtj|X|rYytj|jdWntk r)YnXy|jdd\}}} Wn.tk ryd}d}dt|} YnXtt |j!dt"}!| j!dd} t#|!t$rM|rMt%|d}"| dk}#|#rd} |"dkr>tj&|"} |"t'j(kr>|#r*| dt|7} n| dt|7} |!|"| |!| dS) zExecute program (POSIX version)z/bin/shz-crNrFcSs6g|],\}}tj|dtj|qS)=)rfsencode).0krTrrr s z(Popen._execute_child..c3s-|]#}tjjtj|VqdS)N)rpathror)rdir)rrr sz'Popen._execute_child..TrrDiP:rEsSubprocessError0sBad exception data from child: asciierrors surrogatepassZnoexecrjz: rrrrrr))rrbyteslistrrr]duprrZrrdirnametuple get_exec_pathsetadd_posixsubprocessZ fork_execsortedrrr[r bytearrayrrxwaitpidChildProcessErrorsplitr7reprbuiltinsrr issubclassrr6strerrorrENOENT)$rr_rrrrrrrrrrrrrrrrrZorig_executableZ errpipe_readZ errpipe_writeZlow_fds_to_closeZlow_fdZenv_listZexecutable_listZ fds_to_keepZ devnull_fdZ errpipe_datapartZexception_nameZ hex_errnoZerr_msgZchild_exception_typeZ errno_numZchild_exec_never_calledr)rrrs               $ $ $            cCsM||r|| |_n*||r=|||_n tddS)z:All callers to this function MUST hold self._waitpid_lock.zUnknown child exit status!N)r r)rrZ _WIFSIGNALEDZ _WTERMSIGZ _WIFEXITEDZ _WEXITSTATUSrrr_handle_exitstatuss   zPopen._handle_exitstatuscCs|jdkr|jjds%dSzyN|jdk rA|jS||j|\}}||jkru|j|WnUtk r}z5|dk r||_n|j|krd|_WYdd}~XnXWd|jjX|jS)zCheck if child process has terminated. Returns returncode attribute. This method is called by __del__, so it cannot reference anything outside of the local scope (nor can any methods it calls). NFr)r racquirerrrrrelease)rr=Z_waitpidZ_WNOHANGZ_ECHILDrrrrrrr?s    c CsMytj|j|\}}Wn!tk rB|j}d}YnX||fS)z:All callers to this function MUST hold self._waitpid_lock.r)rrrr)rZ wait_flagsrrrrr _try_wait8s    zPopen._try_waitc Cs|jdk r|jS|dk s.|dk re|dkrJt|}n|dkre|j|}|dk r^d}xL|jjdrzj|jdk rP|jtj\}}||jks|dkst ||jkr|j |PWd|jj X|j|}|dkr4t |j |t|d|d}tj|qzWnhxe|jdkr|jF|jdk rP|jd\}}||jkr|j |WdQRXqaW|jS)zOWait for child process to terminate. Returns returncode attribute.NgMb@?FrrEg?)r rrrrr rWNOHANGrrrr rr_mintimeZsleep)rrrZdelayrrZ remainingrrrraEsB   !   c#Cs|jre|j rey|jjWntk r8YnX|sey|jjWntk rdYnXd}d}|jsi|_|jrg|j|j<|jrg|j|j<|jr|j|j}|jr|j|j}|j||j r t |j }t }|jr=|r=|j |jt j|jr\|j |jt j|jr{|j |jt jx|jr|j|}|dk r|dkrt|j||j|} |j||x6| D].\} } | j|jkr||j|jt} y"|jtj| j| 7_Wn/tk rt|j| j| jjYqX|jt|j kr|j| j| jjq| j|j|jfkrtj| jd} | s|j| j| jj|j| jj| qWq~WWdQRX|j d|j||dk rZdj!|}|dk rudj!|}|j"r|dk r|j#||jj$}|dk r|j#||jj$}||fS)Nrirrk)%rqrflushrrZ_fileobj2outputrr _save_inputr memoryview_PopenSelectorregister selectorsZ EVENT_WRITEZ EVENT_READZget_maprrr_selectrZfileobj _input_offset _PIPE_BUFrrrZ unregisterrxrr]rarorirr)rrhrrrr Z input_viewZselectorrZreadykeyZeventschunkrrrrrvs              "  &       cCs^|jrZ|jdkrZd|_||_|jrZ|dk rZ|jj|jj|_dS)Nr)rqrrriencoder)rrhrrrrs   zPopen._save_inputcCs&|jdkr"tj|j|dS)zSend a signal to the process.N)r rrbr)rrrrrrscCs|jtjdS)z/Terminate the process with SIGTERM N)rrr)rrrrrscCs|jtjdS)z*Kill the process with SIGKILL N)rrSIGKILL)rrrrrbsz Popen.killr).rrrrrrrrrrr@rAr<rrrrrsrrrrrrr;rZ WAIT_OBJECT_0rr?rarrrrrbr WIFSIGNALEDWTERMSIG WIFEXITED WEXITSTATUSrrr rZECHILDr rrrrrrs^ (       1    H =  4    6   " 1 _  r)=rr@platformrrrr rrrrrr Exceptionrr rrrr;rrrr ImportErrorZdummy_threadingr[rrrrZSelectSelector__all__r*r+r,r-r.r/r0r1ryr6r2r>rCr r!r(r`r"r#r&objectr)r'r~r$r%rrrrrr*sn                 :     ,!2 I