
[XMc           @   s]  d  Z  d d l Z d d l Z d d l m Z m Z d d l m Z m Z	 y d d l
 Z Wn e k
 ry d d l Z n Xd d  Z d e f d     YZ d e j f d	     YZ d i  d d d
 d  Z d e j f d     YZ d i  d d d  Z d e j f d     YZ d i  d d d  Z d   Z d   Z d   Z d d d d d g Z d S(   s   
Utility methods.
iN(   t   protocolt   defer(   t   failuret   utilc         C   sa   | d  k r d d l m } n  t j   } |  |  } | j | | | f t |  | |  | S(   Ni(   t   reactor(   t   Nonet   twisted.internetR   R   t   Deferredt   spawnProcesst   tuple(   R    t
   executablet   argst   envt   pathR   t   dt   p(    (    s:   /usr/lib/python2.7/dist-packages/twisted/internet/utils.pyt   _callProtocolWithDeferred   s    &t   _UnexpectedErrorOutputc           B   s   e  Z d  Z d   Z RS(   sy  
    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         C   s$   t  j |  d | f  | |  _ d  S(   Ns   got stderr: %r(   t   IOErrort   __init__t   processEnded(   t   selft   textR   (    (    s:   /usr/lib/python2.7/dist-packages/twisted/internet/utils.pyR   (   s    (   t   __name__t
   __module__t   __doc__R   (    (    (    s:   /usr/lib/python2.7/dist-packages/twisted/internet/utils.pyR      s   	t
   _BackRelayc           B   s>   e  Z d  Z d d  Z d   Z d   Z d   Z d   Z RS(   s  
    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 C{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.
    i    c         C   s=   | |  _  t j   |  _ | r- |  j |  _ n |  j |  _ d  S(   N(   t   deferredt   StringIOt   st   errReceivedIsGoodt   errReceivedt   errReceivedIsBad(   R   R   t   errortoo(    (    s:   /usr/lib/python2.7/dist-packages/twisted/internet/utils.pyR   A   s
    	c         C   sf   |  j  d  k	 rb t j   |  _ t | |  j  } |  j  j t j |   d  |  _  |  j	 j
   n  d  S(   N(   R   R   R   R   t   onProcessEndedR   t   errbackR   t   Failuret	   transportt   loseConnection(   R   R   t   err(    (    s:   /usr/lib/python2.7/dist-packages/twisted/internet/utils.pyR    I   s    	c         C   s   |  j  j |  d  S(   N(   R   t   write(   R   R   (    (    s:   /usr/lib/python2.7/dist-packages/twisted/internet/utils.pyR   Q   s    c         C   s   |  j  j |  d  S(   N(   R   R(   (   R   R   (    (    s:   /usr/lib/python2.7/dist-packages/twisted/internet/utils.pyt   outReceivedT   s    c         C   sQ   |  j  d  k	 r+ |  j  j |  j j    n" |  j d  k	 rM |  j j |  n  d  S(   N(   R   R   t   callbackR   t   getvalueR"   R#   (   R   t   reason(    (    s:   /usr/lib/python2.7/dist-packages/twisted/internet/utils.pyR   W   s    (   R   R   R   R   R    R   R)   R   (    (    (    s:   /usr/lib/python2.7/dist-packages/twisted/internet/utils.pyR   .   s   			i    c            s"   t    f d   |  | | | |  S(   s  
    Spawn a process and return its output as a deferred returning a string.

    @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 *NOT* be the
                 executable's name.

    @param env: the environment variables to pass to the processs; 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            s   t  |  d   S(   NR!   (   R   (   R   (   R!   (    s:   /usr/lib/python2.7/dist-packages/twisted/internet/utils.pyt   <lambda>y   s    (   R   (   R
   R   R   R   R   R!   (    (   R!   s:   /usr/lib/python2.7/dist-packages/twisted/internet/utils.pyt   getProcessOutput_   s    t   _ValueGetterc           B   s   e  Z d    Z d   Z RS(   c         C   s   | |  _  d  S(   N(   R   (   R   R   (    (    s:   /usr/lib/python2.7/dist-packages/twisted/internet/utils.pyR      s    c         C   s   |  j  j | j j  d  S(   N(   R   R*   t   valuet   exitCode(   R   R,   (    (    s:   /usr/lib/python2.7/dist-packages/twisted/internet/utils.pyR      s    (   R   R   R   R   (    (    (    s:   /usr/lib/python2.7/dist-packages/twisted/internet/utils.pyR/      s   	c         C   s   t  t |  | | | |  S(   s7   Spawn a process and return its exit code as a Deferred.(   R   R/   (   R
   R   R   R   R   (    (    s:   /usr/lib/python2.7/dist-packages/twisted/internet/utils.pyt   getProcessValue   s    t   _EverythingGetterc           B   s   e  Z d    Z d   Z RS(   c         C   sI   | |  _  t j   |  _ t j   |  _ |  j j |  _ |  j j |  _ d  S(   N(   R   R   t   outBuft   errBufR(   R)   R   (   R   R   (    (    s:   /usr/lib/python2.7/dist-packages/twisted/internet/utils.pyR      s
    	c         C   su   |  j  j   } |  j j   } | j } | j } | j rX |  j j | | | j f  n |  j j | | | f  d  S(   N(	   R4   R+   R5   R0   R1   t   signalR   R#   R*   (   R   R,   t   outR'   t   et   code(    (    s:   /usr/lib/python2.7/dist-packages/twisted/internet/utils.pyR      s    			(   R   R   R   R   (    (    (    s:   /usr/lib/python2.7/dist-packages/twisted/internet/utils.pyR3      s   	c         C   s   t  t |  | | | |  S(   s.  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   R3   (   R
   R   R   R   R   (    (    s:   /usr/lib/python2.7/dist-packages/twisted/internet/utils.pyt   getProcessOutputAndValue   s    c         C   s=   x6 | D]. } y t  j j |  Wq t k
 r4 q Xq W|  S(   N(   t   warningst   filterst   removet
   ValueError(   t   passthrought   addedFilterst   f(    (    s:   /usr/lib/python2.7/dist-packages/twisted/internet/utils.pyt   _resetWarningFilters   s    c   	      O   s   x$ |  D] \ } } t  j | |   q Wt  j t |    } y | | |   } Wn8 t j   } t d |  | d | d | d  n7 Xt | t	 j
  r | j t |  n t d |  | Sd S(   s%  Run the function C{f}, but with some warnings suppressed.

    @param suppressedWarnings: A list of arguments to pass to filterwarnings.
                               Must be a sequence of 2-tuples (args, kwargs).
    @param f: A callable, followed by its arguments and keyword arguments
    i    i   i   N(   R;   t   filterwarningsR<   t   lent   syst   exc_infoRB   R   t
   isinstanceR   R   t   addBoth(	   t   suppressedWarningsRA   t   at   kwR   t   kwargsR@   t   resultRF   (    (    s:   /usr/lib/python2.7/dist-packages/twisted/internet/utils.pyt   runWithWarningsSuppressed   s    c            s"      f d   } t  j  |  S(   s   
    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             s   t     |  |  S(   N(   RN   (   RJ   RK   (   RI   RA   (    s:   /usr/lib/python2.7/dist-packages/twisted/internet/utils.pyt   warningSuppressingWrapper   s    (   t   tputilt   mergeFunctionMetadata(   RA   RI   RO   (    (   RI   RA   s:   /usr/lib/python2.7/dist-packages/twisted/internet/utils.pyt   suppressWarnings   s    RN   RR   R.   R2   R:   (    (    (    (   R   RE   R;   R   R    R   t   twisted.pythonR   R   RP   t	   cStringIOR   t   ImportErrorR   R   R   R   t   ProcessProtocolR   R.   R/   R2   R3   R:   RB   RN   RR   t   __all__(    (    (    s:   /usr/lib/python2.7/dist-packages/twisted/internet/utils.pyt   <module>   s,   1							