
    \d!                     4   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dS )z
Utility methods.
    Nwraps)BytesIO)deferprotocol)failure c                     |ddl m} t          j                    } | |g|R  }|                    |||ft          |          z   ||           |S )Nr   )reactor)twisted.internetr   r   DeferredspawnProcesstuple)	r   
executableargsenvpathr   	protoArgsdps	            6lib/python3.11/site-packages/twisted/internet/utils.py_callProtocolWithDeferredr      sm     ,,,,,,AYAJd(CS$OOOH    c                       e Zd ZdZd ZdS )_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                 P    t                               | d|           || _        d S )Nzgot stderr: )IOError__init__processEnded)selftextr   s      r   r   z_UnexpectedErrorOutput.__init__*   s/    6d66777(r   N)__name__
__module____qualname____doc__r   r	   r   r   r   r      s-         ) ) ) ) )r   r   c                   2    e Zd ZdZd	dZd Zd Zd Zd Zd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.
    r   c                 r    || _         t                      | _        |r| j        | _        d S | j        | _        d S N)deferredr   serrReceivedIsGooderrReceivederrReceivedIsBad)r    r*   errortoos      r   r   z_BackRelay.__init__B   s>      	5#5D#4Dr   c                 
   | j         {t          j                    | _        t	          || j                  }| j                             t          j        |                     d | _         | j        	                                 d S d S r)   )
r*   r   r   onProcessEndedr   errbackr   Failure	transportloseConnection)r    r!   errs      r   r.   z_BackRelay.errReceivedIsBadJ   su    =$"'."2"2D(t/BCCCM!!'/#"6"6777 DMN))+++++ %$r   c                 :    | j                             |           d S r)   r+   writer    r!   s     r   r,   z_BackRelay.errReceivedIsGoodR       Tr   c                 :    | j                             |           d S r)   r8   r:   s     r   outReceivedz_BackRelay.outReceivedU   r;   r   c                     | j         3| j                             | j                                                   d S | j        | j                            |           d S d S r)   )r*   callbackr+   getvaluer1   r2   r    reasons     r   r   z_BackRelay.processEndedX   s`    =$M""46??#4#455555 ,''///// -,r   N)r   )	r"   r#   r$   r%   r   r.   r,   r=   r   r	   r   r   r'   r'   /   sn         $5 5 5 5, , ,    0 0 0 0 0r   r'   c                 2    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22 r   )r   )r   r   r   r   r   r/   s        `r   getProcessOutputrF   _   s.    2 %2222Jc4QX  r   c                       e Zd Zd Zd ZdS )_ValueGetterc                     || _         d S r)   )r*   )r    r*   s     r   r   z_ValueGetter.__init__~   s     r   c                 N    | j                             |j        j                   d S r)   )r*   r?   valueexitCoderA   s     r   r   z_ValueGetter.processEnded   s#    v|455555r   N)r"   r#   r$   r   r   r	   r   r   rH   rH   }   s2        ! ! !6 6 6 6 6r   rH   c                 4    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XXr   c                   "    e Zd ZddZd Zd ZdS )_EverythingGetterNc                     || _         t                      | _        t                      | _        | j        j        | _        | j        j        | _        || _        d S r)   )r*   r   outBuferrBufr9   r=   r-   
stdinBytes)r    r*   rT   s      r   r   z_EverythingGetter.__init__   sD     iiii;,;,$r   c                     | j         ;| j                            d| j                    | j                                         d S d S )Nr   )rT   r4   writeToChild
closeStdin)r    s    r   connectionMadez _EverythingGetter.connectionMade   sH    ?&N''4?;;; N%%'''''	 '&r   c                    | j                                         }| j                                        }|j        }|j        }|j        r$| j                            |||j        f           d S | j                            |||f           d S r)   )	rR   r@   rS   rK   rL   signalr*   r2   r?   )r    rB   outr6   ecodes         r   r   z_EverythingGetter.processEnded   s    k""$$k""$$Lz8 	5M!!3QX"677777M""Cd#344444r   r)   )r"   r#   r$   r   rX   r   r	   r   r   rP   rP      sF        % % % %( ( (5 5 5 5 5r   rP   c           	      :    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_      s2     %-   r   c                 p    |D ]2}	 t           j                            |           ## t          $ r Y /w xY w| S r)   )warningsfiltersremove
ValueError)passthroughaddedFiltersfs      r   _resetWarningFiltersrh      sV      	##A&&&& 	 	 	D	s   &
33c                    | D ]\  }}t          j        |i | t           j        dt          |                    }	  ||i |}t	          |t
          j                  r|                    t          |           nt          d|           |S # t          $ rE t          j                    }t          d|           |d                             |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 1f00000#$=c*<&=&=$=>LA fen-- 	5NN/>>>> |444  6 6 6<>>T<000qk((!5556s   B ACc                 @     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| R i |S r)   )ry   )ru   rv   rg   rt   s     r   warningSuppressingWrapperz3suppressWarnings.<locals>.warningSuppressingWrapper   s$    ();QIIIIbIIIr   r   )rg   rt   r|   s   `` r   suppressWarningsr}      sB     1XXJ J J J J X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 -0 -0 -0) -0 -0 -0` ')btTTU    <6 6 6 6 68+ 6 6 6 &(RdD Y Y Y Y
5 5 5 5 50 5 5 58 RdDT   &  ( ( (V% % %  r   