3 \@s8dZddlZejdkZddlZddlZddlZddlZddlZddl Z ddl Z ddlm Z Gddde ZGdddeZGd d d eZerddlZddlZddlZGd d d ZnhddlZddlZddlZy ddlZWnek rddlZYnXeed dZeedrejZnejZddddddddddddd dgZ erddlm!Z!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z(e j)ddddd d!d"d#d g Gd$d%d%e*Z+gZ,d&d'Z-dZ0d+d,Z1d-d.Z2dd/d0dZ3d1dZ4dd/d2dZ5Gd3dde6Z7ddd4d5d6dZ8d7d8Z9d9dZ:d:dZ;e6Z}|jtjd}|dk rytj|Wqtk rJYqXqWdS)N) _deadstate)_active_internal_pollsysmaxsizeremover)Zinstresrrr_cleanupsrEcCs*g}tjj}|dkr&|jdd||S)zgReturn a list of command-line arguments reproducing the current optimization settings in sys.flags.r-O)rAflagsoptimizeappend)argsrrrr"_optim_args_from_interpreter_flagss rOcCsddddddd}t}x:|jD].\}}ttj|}|dkr"|jd ||q"Wtjjrh|jd n$tjjrz|jd tjjr|jd xtj D]}|jd |qWttdi}xDdD]<}||kr||}|dkr|}n d||f}|j d|fqW|S)z}Return a list of command-line arguments reproducing the current settings in sys.flags, sys.warnoptions and sys._xoptions.dBSvbq)debugdont_write_bytecodeno_siteverbose bytes_warningquietrrIz-Iz-Ez-sz-W _xoptions faulthandler tracemallocshowalloccount showrefcountutf8Tz%s=%sz-X)r]r^r_r`ra) rOitemsgetattrrArKrMisolatedignore_environment no_user_site warnoptionsextend)Z flag_opt_maprNflagoptrSZxoptionsrargrrr_args_from_interpreter_flagss:       rl)rcOsFt||2}y |j|dS|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)r!waitkill)r popenargskwargsprrrr$s  cOs6t||}|r2|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"]) rNNr)r$getr )rorpretcoder rrrr%(s   cOsTd|krtdd|kr<|ddkr<|jddr4dnd|d<t|t|d d |jS) a(Run command with arguments and return its output. If the exit code was non-zero it raises a CalledProcessError. The CalledProcessError object will have the return code in the returncode attribute and output in the output attribute. The arguments are the same as for the Popen constructor. Example: >>> 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_newlinesFT)rrcheck)rrrr)r"r)rrorprrrr(;s c@s*eZdZdZd ddZddZddZdS) 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)rNr rr )rrNr rr rrrrrszCompletedProcess.__init__cCshdj|jdj|jg}|jdk r4|jdj|j|jdk rP|jdj|jdjt|jdj|S)Nz args={!r}zreturncode={!r}z stdout={!r}z stderr={!r}z{}({})z, ) formatrNr rrMr typerjoin)rrNrrrr;xs   zCompletedProcess.__repr__cCs |jrt|j|j|j|jdS)z6Raise CalledProcessError if the exit code is non-zero.N)r r rNrr )rrrrcheck_returncodesz!CompletedProcess.check_returncode)NN)rrrrrr;r|rrrrr+gs   F)rtrrxc Os|dk r d|krtdt|d<t||}y|j||d\}}WnVtk r||j|j\}}t|j|||dYn|j|jYnX|j}|r|rt ||j||dWdQRXt |j|||S)afRun command with arguments and return a CompletedProcess instance. The returned instance will have attributes args, returncode, stdout and stderr. By default, stdout and stderr are not captured, and those attributes will be None. Pass stdout=PIPE and/or stderr=PIPE in order to capture them. If check is True and the exit code was non-zero, it raises a CalledProcessError. The CalledProcessError object will have the return code in the returncode attribute, and output & stderr attributes if those streams were captured. If timeout is given, and the process takes too long, a TimeoutExpired exception will be raised. There is an 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 will be used internally. The other arguments are the same as for the Popen constructor. If universal_newlines=True is passed, the "input" argument must be a string and stdout/stderr in the returned object will be strings rather than bytes. Nstdinz/stdin and input arguments may not both be used.)r)r r ) rr"r! communicaterrnrNrmpollr r+) rtrrxrorpZprocessrr rsrrrr)s*    cCsg}d}x|D]}g}|r$|jdd|kp8d|kp8| }|rH|jdxj|D]b}|dkrf|j|qN|dkr|jdt|dg}|jdqN|r|j|g}|j|qNW|r|j||r|j||jdqWdj|S) a Translate a sequence of arguments into a command line string, using the same rules as the MS C runtime: 1) Arguments are delimited by white space, which is either a space or a tab. 2) A string surrounded by double quotation marks is interpreted as a single argument, regardless of white space contained within. A quoted string can be embedded in an argument. 3) A double quotation mark preceded by a backslash is interpreted as a literal double quotation mark. 4) Backslashes are interpreted literally, unless they immediately precede a double quotation mark. 5) If backslashes immediately precede a double quotation mark, every pair of backslashes is interpreted as a literal backslash. If the number of backslashes is odd, the last backslash escapes the next double quotation mark as described in rule 3. F  "\rGz\"rv)rMlenrhr{)seqresultZ needquoterkZbs_bufcrrr list2cmdlines4         rcCslyt|ddtd}d}Wn.tk rF}z|j}|j}WYdd}~XnX|dddkrd|dd}||fS) aReturn (exitcode, output) of executing cmd in a shell. Execute the string 'cmd' in a shell with 'check_output' and return a 2-tuple (status, output). The locale encoding is used to decode the output and process newlines. A trailing newline is stripped from the output. The exit status for the command can be interpreted according to the rules for the function 'wait'. Example: >>> import subprocess >>> subprocess.getstatusoutput('ls /bin/ls') (0, '/bin/ls') >>> subprocess.getstatusoutput('cat /bin/junk') (1, 'cat: /bin/junk: No such file or directory') >>> subprocess.getstatusoutput('/bin/junk') (127, 'sh: /bin/junk: not found') >>> subprocess.getstatusoutput('/bin/kill $$') (-15, '') T)shellrur rNrF r)r(r#r r r )r dataZexitcodeZexrrrr&s cCs t|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' rF)r&)r rrrr'"s c@seZdZdZdZd?dddddeddddddddffddddd Zd d Zd d ZddZ e j e j fddZddZddZd@ddZddZddZddZerddZd d!Zd"d#Zdejejejfd$d%ZdAd&d'Zd(d)Zd*d+Z d,d-Z!d.d/Z"e"Z#nd0dZd1d#Ze$j%e$j&e$j'e$j(e$j)e$j*fd2d3Z+de$j,e$j-e.j/fd4d%Zd5d6Z0dBd7d'Zd8d+Z d9d:Z1d;d-Z!dZ#dS)Cr!a 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) encoding and errors: Text mode encoding and error handling to use for file objects stdin, stdout and stderr. Attributes: stdin, stdout, stderr, pid, returncode FrFNrT)encodingerrorscCsrttj|_d|_d|_|dkr(d}t|ts:tdt r|dk rNt d|dk pd|dk pd|dk }|t kr~|rxd}qd}q|r|rt dnF|t krd}|r| rt j dtd}| dk rt d |d krt d ||_d|_d|_d|_d|_d|_| |_||_||_|j|||\}}}}}}t rz|dkrFtj|jd }|dkr`tj|jd }|dkrztj|jd }|p|p| }d|_y|dkrtj|d ||_|rtj|jd|dk||d |_|dkrtj|d||_|rtj|j||d|_|dkr6tj|d||_|r6tj|j||d|_|j |||||| | | || ||||||||WnxDt!d|j|j|jfD]*}y |j"Wnt#k rYnXqW|jsfg}|t$kr|j%||t$kr|j%||t$kr|j%|t&|dr|j%|j'xP|D]H}y*t r)_child_createdr rResourceWarningr@r?rM)rZ_maxsizeZ_warnrrrr= s    z Popen.__del__cCs"t|dstjtjtj|_|jS)Nr)rrrdevnullO_RDWRr)rrrr _get_devnulls zPopen._get_devnullcCs|rZy|jj|WnDtk r(Yn2tk rX}z|jtjkrFnWYdd}~XnXy|jjWnDtk r|Yn2tk r}z|jtjkrnWYdd}~XnXdS)N)r}writeBrokenPipeErrorrerrnoZEINVALr)rrtexcrrr _stdin_writes"  zPopen._stdin_writec Cs|jr|rtd|dkr|j r|j|j|jgjddkrd}d}|jrV|j|n6|jrr|jj}|jjn|jr|jj}|jj|j nJ|dk rt |}nd}z|j |||\}}Wdd|_X|j |j |d}||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 communicationNrGT)r) rrr}rr countrreadrrm_time _communicate_remaining_time)rrtrrr endtimestsrrrr~7s.        zPopen.communicatecCs|jS)zSCheck if child process has terminated. Set and return returncode attribute.)r@)rrrrrhsz Popen.pollcCs|dkr dS|tSdS)z5Convenience for _communicate when computing timeouts.N)r)rrrrrrnszPopen._remaining_timecCs&|dkr dSt|kr"t|j|dS)z2Convenience for checking if a timeout has expired.N)rrrN)rr orig_timeoutrrr_check_timeoutvs zPopen._check_timeoutc Cs|dkr|dkr|dkrd Sd \}}d\}}d\}} |dkrttjtj}|dkrrtjdd\}} t|}tj| nj|tkrtjdd\}}t|t|}}n>|tkrtj |j }n&t |t rtj |}ntj |j }|j|}|dkr,tjtj}|dkrtjdd\} }t|}tj| nn|tkrZtjdd\}}t|t|}}n@|tkrttj |j }n&t |t rtj |}ntj |j }|j|}|dkrtjtj} | dkrftjdd\} } t| } tj| n~|tkrtjdd\}} t|t| }} nP|tkr&|} n@|tkr@tj |j } n&t |t rXtj |} ntj |j } |j| } |||||| fS)z|Construct and return tuple with IO objects: p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite NrFrrrrrrr)rrrrrrrr)rrrr)rrrr)rr)r<Z GetStdHandler.Z CreatePiper4r6r"r*rZ get_osfhandlerrr8fileno_make_inheritabler/r0r#) rr}rr rrrrrr_rrrrsn                        zPopen._get_handlescCs&tjtj|tjddtj}t|S)z2Return a duplicate of handle, which is inheritablerrF)r<ZDuplicateHandleZGetCurrentProcessZDUPLICATE_SAME_ACCESSr4)rZhandlehrrrrs  zPopen._make_inheritablecCs<t|tst|}|dkr t}d| ||fkrP|jtjO_| |_||_||_ | r|jtj O_tj |_ t jjdd}dj||}z>tj||ddt| | ||dk rt j|nd| \}}}}Wd| d kr| j|d kr|j|d kr|jt|drt j|jd|_Xd|_t||_||_tj|dS) z$Execute program (MS Windows version)NrFZCOMSPECzcmd.exez {} /c "{}"rTrrrr)rstrrrrr<r2rrrr3r1rrenvironrrryZ CreateProcessr8fspathr7rrrrrr4_handlerr6)rrNrrrrrrrrrrrrrrrZunused_restore_signalsZunused_start_new_sessionZcomspecZhpZhtrtidrrrrsF      zPopen._execute_childcCs,|jdkr&||jd|kr&||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"tjdtdd|j|}|dkr2tj}n t|d}|jdkrztj|j |}|tj krlt |j |tj |j |_|jS)zOWait for child process to terminate. Returns returncode attribute.Nz0'endtime' argument is deprecated; use 'timeout'.rG) stackleveli)rrDeprecationWarningrr<ZINFINITEr8r WaitForSingleObjectrZ WAIT_TIMEOUTrrNGetExitCodeProcess)rrrZtimeout_millisrrrrrm$s      z Popen.waitcCs|j|j|jdS)N)rMrr)rZfhbufferrrr _readerthread:szPopen._readerthreadcCs`|jrDt|d rDg|_tj|j|j|jfd|_d|j_|jj|j rt|d rg|_ tj|j|j |j fd|_ d|j _|j j|j r|j ||jdk r|jj|j||jjrt|j||j dk r|j j|j||j jrt|j|d}d}|jr|j}|jj|j r4|j }|j j|dk rF|d}|dk rX|d}||fS)N _stdout_buff)targetrNT _stderr_buffr)rrrrZThreadrZ stdout_threadZdaemonstartr rZ stderr_threadr}rr{rZis_aliverrNr)rrtrrrr rrrr?sF             zPopen._communicatecCsl|jdk rdS|tjkr"|jnF|tjkr>tj|jtjn*|tjkrZtj|jtjnt dj |dS)zSend a signal to the process.NzUnsupported signal: {}) r rSIGTERM terminateZ CTRL_C_EVENTrrnrZCTRL_BREAK_EVENTrry)rsigrrr send_signalss     zPopen.send_signalc CsX|jdk rdSytj|jdWn2tk rRtj|j}|tjkrH||_YnXdS)zTerminates the process.NrF)r r<ZTerminateProcessrPermissionErrorrZ STILL_ACTIVE)rZrcrrrrs   zPopen.terminatec Cs.d\}}d\}}d \}} |dkr"n@|tkr8tj\}}n*|tkrJ|j}nt|trZ|}n|j}|dkrln@|tkrtj\}}n*|tkr|j}nt|tr|}n|j}|dkrnh|tkrtj\}} nR|tkr|d kr|} n t j j} n.|tkr|j} nt|tr|} n|j} |||||| fS) z|Construct and return tuple with IO objects: p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite rFNrr)rrrr)rrrr)rrr) r"rpiper*rrr8rr#rA __stdout__) rr}rr rrrrrrrrrrsJ         c)"st|ttfr|g}nt|}| r:ddg|}r:|d<dkrJ|d}tj\}}g}x |dkr~|j|tj|}q`Wx|D]}tj|qWzz|dk rg}xL|j D]:\}}tj |}d|krt d|j|dtj |qWnd}tj tj j rf}ntfdd tj|D}t|}|j|tj|||tttt|||| | | |||||||||_d |_Wdtj|Xt|d d}| dkr| dkr| |krtj| |dkr| dkr||krtj||dkr|dkr||krtj||dk r.tj|d |_t}x2tj|d }||7}| sft|d krsz'Popen._execute_child..TrrFiP:rGsSubprocessError0z#Bad exception data from child: {!r}asciiZnoexecrvz: rrrrrr)/rrbyteslistrrrMduprrbrrrdirnametuple get_exec_pathsetadd_posixsubprocessZ fork_execsortedmapr8rrrcr bytearrayrrwaitpid_handle_exitstatusrArBr ChildProcessErrorsplitrrybuiltinsr issubclassrstrerrorrENOENTrepr))rrNrrrrrrrrrrrrrrrrrZorig_executableZ errpipe_readZ errpipe_writeZlow_fds_to_closeZlow_fdZenv_listkrSZexecutable_listZ fds_to_keepZ devnull_fdZ errpipe_datapartrrZexception_nameZ hex_errnoZerr_msgZchild_exception_typeZ errno_numZchild_exec_never_calledZ err_filenamer)rrrs                                cCsL||r|| |_n2||r*|||_n||r@|| |_ntddS)z:All callers to this function MUST hold self._waitpid_lock.zUnknown child exit status!N)r r)rrZ _WIFSIGNALEDZ _WTERMSIGZ _WIFEXITEDZ _WEXITSTATUSZ _WIFSTOPPEDZ _WSTOPSIGrrrr Xs zPopen._handle_exitstatuscCs|jdkr|jjdsdSzy8|jdk r.|jS||j|\}}||jkrR|j|WnBtk r}z&|dk rv||_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 racquirerr rrrelease)rr>Z_waitpidZ_WNOHANGZ_ECHILDrrerrrr@js      c Cs>ytj|j|\}}Wntk r4|j}d}YnX||fS)z:All callers to this function MUST hold self._waitpid_lock.r)rr rr )rZ wait_flagsrrrrr _try_waits  zPopen._try_waitc CsV|jdk r|jS|dk r(tjdtdd|dk s8|dk r^|dkrLt|}n|dkr^|j|}|dk rd}x|jjdrz6|jdk rP|jt j \}}||j kr|j |PWd|jj X|j|}|dkrt|j|t|d|d}tj|qlWnVxT|jdkrN|j8|jdk rP|jd\}}||j krB|j |WdQRXqW|jS) zOWait for child process to terminate. Returns returncode attribute.Nz0'endtime' argument is deprecated; use 'timeout'.rG)rgMb@?Frg?)r rrrrrrrrrWNOHANGrr rrrNmintimeZsleep)rrrZdelayrrZ remainingrrrrmsJ            c#Cs|jrZ|j rZy|jjWntk r0YnX|sZy|jjWntk rXYnXd}d}|jsi|_|jrg|j|j<|jrg|j|j<|jr|j|j}|jr|j|j}|j||j rt |j }t }|jr|r|j |jt j|jr |j |jt j|jr$|j |jt jxJ|jrp|j|}|dk r\|dkr\t|j||j|} |j||x| D]\} } | j|jkr||j|jt} y|jtj| j| 7_Wn,tk r|j| j| jjYn*X|jt|j krf|j| j| jjnP| j|j|jfkrxtj| jd} | sT|j| j| jj|j| jj| qxWq(WWdQRX|j |j|d|dk rdj!|}|dk rdj!|}|j"s|j#s|j$r|dk r|j%||jj"|jj#}|dk r|j%||jj"|jj#}||fS)Nri)rrw)&r}rflushrrZ_fileobj2outputrr _save_inputr memoryview_PopenSelectorregister selectorsZ EVENT_WRITEZ EVENT_READZget_maprrrNselectrZfileobj _input_offset _PIPE_BUFrrrZ unregisterrrrMrmr{rrrur)rrtrrrr Z input_viewZselectorrZreadykeyZeventschunkrrrrrs                 (        cCsR|jrN|jdkrNd|_||_|dk rN|js6|js6|jrN|jj|jj|jj|_dS)Nr)r}rr#rrruencode)rrtrrrr/s zPopen._save_inputcCs|jdkrtj|j|dS)zSend a signal to the process.N)r rrnr)rrrrrr<s cCs|jtjdS)z/Terminate the process with SIGTERM N)rrr)rrrrrBscCs|jtjdS)z*Kill the process with SIGKILL N)rrSIGKILL)rrrrrnGsz Popen.killr)NN)NN)NN)2rrrrrrrrrrrArBrrr=rrr~rrrrrrrr<rZ WAIT_OBJECT_0rr@rmrrrrrnr WIFSIGNALEDWTERMSIG WIFEXITED WEXITSTATUS WIFSTOPPEDWSTOPSIGr r rrZECHILDrrrrrrr!2sd+  1H @ 46 " 4a r)>rrAplatformrrrrrrrrrr Exceptionrr rrrr<rrr"r! ImportErrorZdummy_threadingrcr$rr rZSelectSelector__all__r,r-r.r/r0r1r2r3rhr8r4r?rEr"r#r*rOrlr$r%r(objectr+r)rr&r'rr!rrrr*sr  %      (  .,!2I