
    Ϫf!                     8   d Z ddlZddlZddlmZ ddlmZ ddlmZm	Z	 ddl
mZ 	 ddZ G d	 d
e      Z G d de	j                        Zdi dddfdZ G d de	j                        Zdi ddfdZ G d de	j                        Zdi dddfdZd Zd Zd Zg dZy)z
Utility methods.
    Nwraps)BytesIO)deferprotocol)failure c                     |ddl m} t        j                         } | |g| }|j	                  |||ft        |      z   ||       |S )Nr   )reactor)twisted.internetr   r   DeferredspawnProcesstuple)	r   
executableargsenvpathr   	protoArgsdps	            8/usr/lib/python3/dist-packages/twisted/internet/utils.py_callProtocolWithDeferredr      sM     ,AYAJd(CS$OH    c                       e Zd ZdZd Zy)_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).
    c                 D    t         j                  | d|       || _        y )Nzgot stderr: )IOError__init__processEnded)selftextr   s      r   r   z_UnexpectedErrorOutput.__init__*   s!    dX67(r   N)__name__
__module____qualname____doc__r   r	   r   r   r   r      s    )r   r   c                   0    e Zd ZdZddZd Zd Zd Zd Zy)	
_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.
    c                 z    || _         t               | _        |r| j                  | _        y | j
                  | _        y N)deferredr   serrReceivedIsGooderrReceivederrReceivedIsBad)r    r*   errortoos      r   r   z_BackRelay.__init__B   s1     #55D#44Dr   c                    | j                   t        j                         | _        t	        || j                        }| j                   j                  t        j                  |             d | _         | j                  j                          y y r)   )
r*   r   r   onProcessEndedr   errbackr   Failure	transportloseConnection)r    r!   errs      r   r.   z_BackRelay.errReceivedIsBadJ   sd    ==$"'.."2D(t/B/BCCMM!!'//#"67 DMNN))+ %r   c                 :    | j                   j                  |       y r)   r+   writer    r!   s     r   r,   z_BackRelay.errReceivedIsGoodR       Tr   c                 :    | j                   j                  |       y r)   r8   r:   s     r   outReceivedz_BackRelay.outReceivedU   r;   r   c                     | j                   4| j                   j                  | j                  j                                y | j                  | j                  j                  |       y y r)   )r*   callbackr+   getvaluer1   r2   r    reasons     r   r   z_BackRelay.processEndedX   sN    ==$MM""466??#45  ,''/ -r   N)r   )	r"   r#   r$   r%   r   r.   r,   r=   r   r	   r   r   r'   r'   /   s     $5,0r   r'   c                 *    t        fd| ||||      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.
    c                     t        |       S )N)r/   )r'   )r   r/   s    r   <lambda>z"getProcessOutput.<locals>.<lambda>y   s    *Q2 r   )r   )r   r   r   r   r   r/   s        `r   getProcessOutputrF   _   s     2 %2Jc4QX r   c                       e Zd Zd Zd Zy)_ValueGetterc                     || _         y r)   )r*   )r    r*   s     r   r   z_ValueGetter.__init__~   s	     r   c                 b    | j                   j                  |j                  j                         y r)   )r*   r?   valueexitCoderA   s     r   r   z_ValueGetter.processEnded   s    v||445r   N)r"   r#   r$   r   r   r	   r   r   rH   rH   }   s    !6r   rH   c                 *    t        t        | ||||      S )z7Spawn a process and return its exit code as a Deferred.)r   rH   )r   r   r   r   r   s        r   getProcessValuerN      s    $\:tS$PWXXr   c                        e Zd ZddZd Zd Zy)_EverythingGetterNc                     || _         t               | _        t               | _        | j                  j                  | _        | j                  j                  | _        || _        y r)   )r*   r   outBuferrBufr9   r=   r-   
stdinBytes)r    r*   rT   s      r   r   z_EverythingGetter.__init__   sF     ii;;,,;;,,$r   c                     | j                   A| j                  j                  d| j                          | j                  j                          y y )Nr   )rT   r4   writeToChild
closeStdin)r    s    r   connectionMadez _EverythingGetter.connectionMade   s;    ??&NN''4??; NN%%'	 'r   c                 B   | j                   j                         }| j                  j                         }|j                  }|j                  }|j
                  r)| j                  j                  |||j
                  f       y | j                  j                  |||f       y r)   )	rR   r@   rS   rK   rL   signalr*   r2   r?   )r    rB   outr6   ecodes         r   r   z_EverythingGetter.processEnded   su    kk""$kk""$LLzz88MM!!3QXX"67MM""Cd#34r   r)   )r"   r#   r$   r   rX   r   r	   r   r   rP   rP      s    %(5r   rP   c           	      0    t        t        | |||||f      S )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   )r   rP   )r   r   r   r   r   rT   s         r   getProcessOutputAndValuer_      s(     %- r   c                 r    |D ]"  }	 t         j                  j                  |       $ | S # t        $ r Y 2w xY wr)   )warningsfiltersremove
ValueError)passthroughaddedFiltersfs      r   _resetWarningFiltersrh      sF     	##A&
   		s   *	66c                    | D ]  \  }}t        j                  |i |  t         j                  dt        |        }	  ||i |}t	        |t
        j                        r|j                  t        |       |S t        d|       |S # t        $ r8 t        j                         }t        d|       |d   j                  |d         w xY w)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      )ra   filterwarningsrb   len
isinstancer   r   addBothrh   BaseExceptionsysexc_infowith_traceback)	suppressedWarningsrg   akwr   kwargsrf   resultrr   s	            r   runWithWarningsSuppressedry      s    4 + 1f001##$=c*<&=>LA fenn-NN/>  !|4  6<<>T<0qk((!556s   B ACc                 2     t                fd       }|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.
    c                  $    t        g| i |S r)   )ry   )ru   rv   rg   rt   s     r   warningSuppressingWrapperz3suppressWarnings.<locals>.warningSuppressingWrapper   s    ();QIIbIIr   r   )rg   rt   r|   s   `` r   suppressWarningsr}      s%     1XJ J %$r   )ry   r}   rF   rN   r_   )Nr	   )r%   rq   ra   	functoolsr   ior   r   r   r   twisted.pythonr   r   r   r   ProcessProtocolr'   rF   rH   rN   rP   r_   rh   ry   r}   __all__r	   r   r   <module>r      s   

     , " DF	)W ) -0)) -0` ')btTTU <68++ 6 &(RdD Y
500 58 RdDT&(V%r   