3 \@sddgZddlZddlmZddlmZddlmZddlmZdd lmZdd l m Z ej Z ej Z ej Z Gd d d ejejZGd ddZeddddejfddZeddddejdddZdS)create_subprocess_execcreate_subprocess_shellN)events) protocols)streams)tasks) coroutine)loggercsPeZdZdZfddZddZddZdd Zd d Zd d Z ddZ Z S)SubprocessStreamProtocolz0Like StreamReaderProtocol, but for a subprocess.cs<tj|d||_d|_|_|_d|_d|_g|_dS)N)loopF) super__init___limitstdinstdoutstderr _transport_process_exited _pipe_fds)selflimitr ) __class__"/usr/lib64/python3.6/subprocess.pyrs z!SubprocessStreamProtocol.__init__cCsf|jjg}|jdk r$|jd|j|jdk r>|jd|j|jdk rX|jd|jddj|S)Nzstdin=%rz stdout=%rz stderr=%rz<%s> )r__name__rappendrrjoin)rinforrr__repr__s    z!SubprocessStreamProtocol.__repr__cCs||_|jd}|dk rDtj|j|jd|_|jj||jj d|jd}|dk rtj|j|jd|_ |j j||jj d|jd}|dk rtj ||d|jd|_ dS)Nr)rr r)protocolreaderr ) rget_pipe_transportr StreamReaderr_looprZ set_transportrrr StreamWriterr)r transportZstdout_transportZstderr_transportZstdin_transportrrrconnection_made(s&         z(SubprocessStreamProtocol.connection_madecCs:|dkr|j}n|dkr |j}nd}|dk r6|j|dS)Nrr!)rrZ feed_data)rfddatar#rrrpipe_data_received@sz+SubprocessStreamProtocol.pipe_data_receivedcCs|dkr,|j}|dk r|j|j|dS|dkr<|j}n|dkrL|j}nd}|dkrt|dkrj|jn |j|||jkr|jj||j dS)Nrrr!) rcloseZconnection_lostrrZfeed_eofZ set_exceptionrremove_maybe_close_transport)rr*excpiper#rrrpipe_connection_lostJs$     z-SubprocessStreamProtocol.pipe_connection_lostcCsd|_|jdS)NT)rr/)rrrrprocess_exitedasz'SubprocessStreamProtocol.process_exitedcCs(t|jdkr$|jr$|jjd|_dS)Nr)lenrrrr-)rrrrr/es z/SubprocessStreamProtocol._maybe_close_transport) r __module__ __qualname____doc__rr r)r,r2r3r/ __classcell__rr)rrr s   r c@s~eZdZddZddZeddZeddZd d Z d d Z d dZ eddZ eddZ eddZedddZdS)ProcesscCs8||_||_||_|j|_|j|_|j|_|j|_dS)N)rZ _protocolr&rrrZget_pidpid)rr(r"r rrrrlszProcess.__init__cCsd|jj|jfS)Nz<%s %s>)rrr:)rrrrr uszProcess.__repr__cCs |jjS)N)rZget_returncode)rrrr returncodexszProcess.returncodeccs|jjEdHS)zdWait until the process exit and return the process return code. This method is a coroutine.N)rZ_wait)rrrrwait|sz Process.waitcCs|jj|dS)N)r send_signal)rsignalrrrr=szProcess.send_signalcCs|jjdS)N)r terminate)rrrrr?szProcess.terminatecCs|jjdS)N)rkill)rrrrr@sz Process.killccs|jj}|jj||r,tjd|t|y|jjEdHWn8tt fk rx}z|rhtjd||WYdd}~XnX|rtjd||jj dS)Nz%%r communicate: feed stdin (%s bytes)z%r communicate: stdin got %rz%r communicate: close stdin) r& get_debugrwriter debugr4ZdrainBrokenPipeErrorConnectionResetErrorr-)rinputrCr0rrr _feed_stdins     zProcess._feed_stdincCsdS)Nr)rrrr_noopsz Process._noopccs|jj|}|dkr|j}n|dks(t|j}|jjrV|dkrDdnd}tjd|||j EdH}|jjr|dkrzdnd}tjd|||j |S)Nr!rrrz%r communicate: read %sz%r communicate: close %s) rr$rAssertionErrorrr&rAr rCreadr-)rr*r(streamnameoutputrrr _read_streams    zProcess._read_streamNccs|dk r|j|}n|j}|jdk r2|jd}n|j}|jdk rP|jd}n|j}tj||||jdEdH\}}}|jEdH||fS)Nrr!)r ) rGrHrrNrrZgatherr&r<)rrFrrrrrr communicates      zProcess.communicate)N)rr5r6rr propertyr;r r<r=r?r@rGrHrNrOrrrrr9ks      r9c +sPdkrtjfdd}j||f|||d|EdH\}} t|| S)Ncs tdS)N)rr )r r)rr rrsz)create_subprocess_shell..)rrr)rget_event_loopZsubprocess_shellr9) cmdrrrr rkwdsprotocol_factoryr(r"r)rr rrs)rrrr rc /sTdkrtjfdd}j||f||||d|EdH\} } t| | S)Ncs tdS)N)rr )r r)rr rrrQsz(create_subprocess_exec..)rrr)rrRZsubprocess_execr9) Zprogramrrrr rargsrTrUr(r"r)rr rrs)__all__ subprocessrrrrZ coroutinesr logr PIPEZSTDOUTZDEVNULLZFlowControlMixinZSubprocessProtocolr r9Z_DEFAULT_LIMITrrrrrrs(      X]