o b!@sdZddlZddlZddlmZddlmZddlmZm Z ddl m Z  ddd Z Gd d d e ZGd d d e jZdidddfddZGddde jZdiddfddZGddde jZdidddfddZddZddZddZgdZdS) z Utility methods. Nwraps)BytesIO)deferprotocol)failurec CsL|dur ddlm}t}||g|R}||||ft||||S)Nr)reactor)twisted.internetr rDeferred spawnProcesstuple) r executableargsenvpathr protoArgsdprr8/usr/lib/python3/dist-packages/twisted/internet/utils.py_callProtocolWithDeferreds  rc@seZdZdZddZdS)_UnexpectedErrorOutputay Standard error data was received where it was not expected. This is a subclass of L{IOError} to preserve backward compatibility with the previous error behavior of L{getProcessOutput}. @ivar processEnded: A L{Deferred} which will fire when the process which produced the data on stderr has ended (exited and all file descriptors closed). cCst|d|||_dS)Nz got stderr: )IOError__init__ processEnded)selftextrrrrr*s z_UnexpectedErrorOutput.__init__N)__name__ __module__ __qualname____doc__rrrrrrs rc@s:eZdZdZdddZddZddZd d Zd d Zd S) _BackRelaya Trivial protocol for communicating with a process and turning its output into the result of a L{Deferred}. @ivar deferred: A L{Deferred} which will be called back with all of stdout and, if C{errortoo} is true, all of stderr as well (mixed together in one string). If C{errortoo} is false and any bytes are received over stderr, this will fire with an L{_UnexpectedErrorOutput} instance and the attribute will be set to L{None}. @ivar onProcessEnded: If C{errortoo} is false and bytes are received over stderr, this attribute will refer to a L{Deferred} which will be called back when the process ends. This C{Deferred} is also associated with the L{_UnexpectedErrorOutput} which C{deferred} fires with earlier in this case so that users can determine when the process has actually ended, in addition to knowing when bytes have been received via stderr. rcCs*||_t|_|r|j|_dS|j|_dSN)deferredrserrReceivedIsGood errReceivederrReceivedIsBad)rr#errortoorrrrBs   z_BackRelay.__init__cCsJ|jdur#t|_t||j}|jt|d|_|j dSdSr") r#rr onProcessEndedrerrbackrFailure transportloseConnection)rrerrrrrr'Js   z_BackRelay.errReceivedIsBadcC|j|dSr"r$writerrrrrr%Rz_BackRelay.errReceivedIsGoodcCr/r"r0r2rrr outReceivedUr3z_BackRelay.outReceivedcCs>|jdur|j|jdS|jdur|j|dSdSr")r#callbackr$getvaluer)r*rreasonrrrrXs  z_BackRelay.processEndedN)r) rrrr rr'r%r4rrrrrr!/s  r!cstfdd|||||S)a Spawn a process and return its output as a deferred returning a L{bytes}. @param executable: The file name to run and get the output of - the full path should be used. @param args: the command line arguments to pass to the process; a sequence of strings. The first string should B{NOT} be the executable's name. @param env: the environment variables to pass to the process; a dictionary of strings. @param path: the path to run the subprocess in - defaults to the current directory. @param reactor: the reactor to use - defaults to the default reactor @param errortoo: If true, include stderr in the result. If false, if stderr is received the returned L{Deferred} will errback with an L{IOError} instance with a C{processEnded} attribute. The C{processEnded} attribute refers to a L{Deferred} which fires when the executed process ends. cs t|dS)Nr()r!)rr9rrys z"getProcessOutput..)r)rrrrr r(rr9rgetProcessOutput_sr;c@seZdZddZddZdS) _ValueGettercCs ||_dSr")r#)rr#rrrr~s z_ValueGetter.__init__cCs|j|jjdSr")r#r5valueexitCoder7rrrrsz_ValueGetter.processEndedN)rrrrrrrrrr<}s r<cCstt|||||S)z7Spawn a process and return its exit code as a Deferred.)rr<)rrrrr rrrgetProcessValuesr?c@s&eZdZdddZddZddZdS) _EverythingGetterNcCs4||_t|_t|_|jj|_|jj|_||_dSr")r#routBuferrBufr1r4r& stdinBytes)rr#rCrrrrs    z_EverythingGetter.__init__cCs,|jdur|jd|j|jdSdS)Nr)rCr, writeToChild closeStdin)rrrrconnectionMades z _EverythingGetter.connectionMadecCsT|j}|j}|j}|j}|jr|j|||jfdS|j|||fdSr") rAr6rBr=r>signalr#r*r5)rr8outr.ecoderrrrs  z_EverythingGetter.processEndedr")rrrrrFrrrrrr@s  r@c Cstt||||||fdS)a.Spawn a process and returns a Deferred that will be called back with its output (from stdout and stderr) and it's exit code as (out, err, code) If a signal is raised, the Deferred will errback with the stdout and stderr up to that point, along with the signal, as (out, err, signalNum) )r)rr@)rrrrr rCrrrgetProcessOutputAndValuesrKc Cs0|D]}ztj|WqtyYqw|Sr")warningsfiltersremove ValueError) passthrough addedFiltersfrrr_resetWarningFilterss rSc Os|D] \}}tj|i|qtjdt|}z ||i|}Wnty:t}td||d|dwt |t j rI| t||Std||S)a} Run the function I{f}, but with some warnings suppressed. This calls L{warnings.filterwarnings} to add warning filters before invoking I{f}. If I{f} returns a L{Deferred} then the added filters are removed once the deferred fires. Otherwise they are removed immediately. Note that the list of warning filters is a process-wide resource, so calling this function will affect all threads. @param suppressedWarnings: A list of arguments to pass to L{warnings.filterwarnings}, a sequence of (args, kwargs) 2-tuples. @param f: A callable, which may return a L{Deferred}. @param a: Positional arguments passed to I{f} @param kw: Keyword arguments passed to I{f} @return: The result of C{f(*a, **kw)} @seealso: L{twisted.python.util.runWithWarningsSuppressed} functions similarly, but doesn't handled L{Deferred}s. N) rLfilterwarningsrMlen BaseExceptionsysexc_inforSwith_traceback isinstancerr addBoth) suppressedWarningsrRakwrkwargsrQresultrZrrrrunWithWarningsSuppresseds      rccstfdd}|S)z Wrap C{f} in a callable which suppresses the indicated warnings before invoking C{f} and unsuppresses them afterwards. If f returns a Deferred, warnings will remain suppressed until the Deferred fires. cstg|Ri|Sr")rc)r_r`rRr^rrwarningSuppressingWrappersz3suppressWarnings..warningSuppressingWrapperr)rRr^rerrdrsuppressWarningssrf)rcrfr;r?rK)Nr)r rYrL functoolsriorr rrtwisted.pythonrrrrProcessProtocolr!r;r<r?r@rKrSrcrf__all__rrrrs*     0   +