
H/\c           @@ s  d  Z  d d l m Z m Z d d l Z d d l Z d d l Z d d l Z d d l Z d d l	 Z	 d d l
 m Z d d l m Z d d l m Z d d l m Z d d l m Z d d	 l m Z m Z m Z d d
 l m Z d d l m Z d d l m Z d d l m Z d d l m Z d d l m Z d d l m Z m Z e j  Z  d d l! Z" d d d d g Z# e oe j$ j% d  re# j& d  e' Z( n  d Z) d d d d d d d d d  d! d" g Z* d# d$ d% d& d' d( d) d* d+ d, d- d. d/ d0 d1 d2 d3 d4 d5 d6 d7 d8 d9 g Z+ e j, d:  d; d; f k rIe* d< d= d> d? d@ g 7Z* n e+ j& d@  e j, d:  d; dA f k re+ j- d8  e+ j- d9  e# j& d8  e# j& d9  e+ j- d#  y e j. dB  Z/ Wn dC Z/ n Xn  e j, d:  d; dD f k re+ j- d%  e* j& d%  n  e j, d:  d; dE f k rSe* j0 dF dG dH dI dJ dK dL dM dN dO g
  n  e e" e1   dP e* dQ e2 Z3 e+ j0 e4 e*  j5 e4 e3    e3 Z* [3 e6 e" d( e  Z7 e6 e" d) e  Z8 e" e7 e8 g Z9 x e: e+  D]} Z; e; e1   k rqn  e Z< x0 e9 D]( Z= e6 e= e; e  Z< e< e k	 r,Pn  qWe< e k rLe+ j- e;  n e< e1   e; <qW[9 e# e* Z> x3 d8 d9 d@ f D]" Z? e? e> k re> j& e?  n  qzWe j$ d k Z@ e@ rd d lA ZA e rdR eB f dS     YZC n  nJ d d lD ZD d d lE ZE d dT lF mG ZG eG jH dU dV  ZI d dW lJ mK ZK dX   ZL dY   ZM dZ   ZN eO   ZP d@ e1   k rd d[ lQ mR ZS d@ eS f d\     YZT n  eU e d]  re jV ZW n	 d^   ZW d_   ZX d eO f d`     YZY da   ZZ db   Z[ d9 eO f dc     YZ\ dd   Z] d S(e   s%  
Cooperative ``subprocess`` module.

.. caution:: On POSIX platforms, this module is not usable from native
   threads other than the main thread; attempting to do so will raise
   a :exc:`TypeError`. This module depends on libev's fork watchers.
   On POSIX systems, fork watchers are implemented using signals, and
   the thread to which process-directed signals are delivered `is not
   defined`_. Because each native thread has its own gevent/libev
   loop, this means that a fork watcher registered with one loop
   (thread) may never see the signal about a child it spawned if the
   signal is sent to a different thread.

.. note:: The interface of this module is intended to match that of
   the standard library :mod:`subprocess` module (with many backwards
   compatible extensions from Python 3 backported to Python 2). There
   are some small differences between the Python 2 and Python 3
   versions of that module (the Python 2 ``TimeoutExpired`` exception,
   notably, extends ``Timeout`` and there is no ``SubprocessError``) and between the
   POSIX and Windows versions. The HTML documentation here can only
   describe one version; for definitive documentation, see the
   standard library or the source code.

.. _is not defined: http://www.linuxprogrammingblog.com/all-about-linux-signals?page=11
i    (   t   absolute_importt   print_functionN(   t   AsyncResult(   t   _get_hub_noargs(   t	   linkproxy(   t   sleep(   t
   getcurrent(   t   integer_typest   string_typest   xrange(   t   PY3(   t   reraise(   t   fspath(   t   fsencode(   t   _NONE(   t   copy_globals(   t   Greenlett   joinallt   Popent   callt
   check_callt   check_outputt   win32t   _posixsubprocesss   PIPE should be importedt   PIPEt   STDOUTt   CalledProcessErrort   CREATE_NEW_CONSOLEt   CREATE_NEW_PROCESS_GROUPt   STD_INPUT_HANDLEt   STD_OUTPUT_HANDLEt   STD_ERROR_HANDLEt   SW_HIDEt   STARTF_USESTDHANDLESt   STARTF_USESHOWWINDOWt   MAXFDt   _eintr_retry_callt   STARTUPINFOt
   pywintypest   list2cmdlinet   _subprocesst   _winapit   WAIT_OBJECT_0t   WaitForSingleObjectt   GetExitCodeProcesst   GetStdHandlet
   CreatePipet   DuplicateHandlet   GetCurrentProcesst   DUPLICATE_SAME_ACCESSt   GetModuleFileNamet
   GetVersiont   CreateProcesst   INFINITEt   TerminateProcesst   STILL_ACTIVEt   runt   CompletedProcessi   i   t   DEVNULLt   getstatusoutputt	   getoutputt   SubprocessErrort   TimeoutExpiredi   t   SC_OPEN_MAXi   i   i   t   ABOVE_NORMAL_PRIORITY_CLASSt   BELOW_NORMAL_PRIORITY_CLASSt   HIGH_PRIORITY_CLASSt   IDLE_PRIORITY_CLASSt   NORMAL_PRIORITY_CLASSt   REALTIME_PRIORITY_CLASSt   CREATE_NO_WINDOWt   DETACHED_PROCESSt   CREATE_DEFAULT_ERROR_MODEt   CREATE_BREAKAWAY_FROM_JOBt
   only_namest   ignore_missing_namest   Handlec           B@ s5   e  Z e Z d    Z d   Z d   Z e Z e Z RS(   c         C@ s&   |  j  s" t |  _  t j |   n  d  S(   N(   t   closedt   TrueR)   t   CloseHandle(   t   self(    (    s0   lib/python2.7/site-packages/gevent/subprocess.pyt   Close   s    		c         C@ s,   |  j  s t |  _  t |   St d   d  S(   Ns   already closed(   RM   RN   t   intt
   ValueError(   RP   (    (    s0   lib/python2.7/site-packages/gevent/subprocess.pyt   Detach   s    		
c         C@ s   d t  |   S(   Ns
   Handle(%d)(   RR   (   RP   (    (    s0   lib/python2.7/site-packages/gevent/subprocess.pyt   __repr__   s    (	   t   __name__t
   __module__t   FalseRM   RQ   RT   RU   t   __del__t   __str__(    (    (    s0   lib/python2.7/site-packages/gevent/subprocess.pyRL      s   			(   t   monkeyt   ost   fork(   t   fork_and_watchc       	   O@ si   | j  d d  } t |  |   B } y | j d | d t  SWn | j   | j     n XWd QXd S(   s^  
    call(args, *, stdin=None, stdout=None, stderr=None, shell=False, timeout=None) -> returncode

    Run command with arguments. Wait for command to complete or
    timeout, then return the returncode attribute.

    The arguments are the same as for the Popen constructor.  Example::

        retcode = call(["ls", "-l"])

    .. versionchanged:: 1.2a1
       The ``timeout`` keyword argument is now accepted on all supported
       versions of Python (not just Python 3) and if it expires will raise a
       :exc:`TimeoutExpired` exception (under Python 2 this is a subclass of :exc:`~.Timeout`).
    t   timeoutt
   _raise_excN(   t   popt   NoneR   t   waitRN   t   kill(   t	   popenargst   kwargsR_   t   p(    (    s0   lib/python2.7/site-packages/gevent/subprocess.pyR      s    

c          O@ sS   t  |  |   } | rO | j d  } | d k r= |  d } n  t | |   n  d S(   s  
    check_call(args, *, stdin=None, stdout=None, stderr=None, shell=False, timeout=None) -> 0

    Run command with arguments.  Wait for command to complete.  If
    the exit code was zero then return, otherwise raise
    :exc:`CalledProcessError`.  The ``CalledProcessError`` object will have the
    return code in the returncode attribute.

    The arguments are the same as for the Popen constructor.  Example::

        retcode = check_call(["ls", "-l"])
    t   argsi    N(   R   t   getRb   R   (   Re   Rf   t   retcodet   cmd(    (    s0   lib/python2.7/site-packages/gevent/subprocess.pyR     s    c       	   O@ sK  | j  d d  } d | k r- t d   n  d | k rr d | k rT t d   n  | d } | d =t | d <n d } t d t |  |   } y | j | d | \ } } Wnb t k
 r | j   | j   \ } } t | j | d |  n | j   | j	     n X| j
   } | rAt | | j d |  n  Wd QX| S(	   sX  
    check_output(args, *, input=None, stdin=None, stderr=None, shell=False, universal_newlines=False, timeout=None) -> output

    Run command with arguments and return its output.

    If the exit code was non-zero it raises a :exc:`CalledProcessError`.  The
    ``CalledProcessError`` object will have the return code in the returncode
    attribute and output in the output attribute.


    The arguments are the same as for the Popen constructor.  Example::

        >>> check_output(["ls", "-1", "/dev/null"])
        '/dev/null\n'

    The ``stdout`` argument is not allowed as it is used internally.

    To capture standard error in the result, use ``stderr=STDOUT``::

        >>> print(check_output(["/bin/sh", "-c",
        ...               "ls -l non_existent_file ; exit 0"],
        ...              stderr=STDOUT).decode('ascii').strip())
        ls: non_existent_file: No such file or directory

    There is an additional optional argument, "input", allowing you to
    pass a string to the subprocess's stdin.  If you use this argument
    you may not also use the Popen constructor's "stdin" argument, as
    it too will be used internally.  Example::

        >>> check_output(["sed", "-e", "s/foo/bar/"],
        ...              input=b"when in the course of fooman events\n")
        'when in the course of barman events\n'

    If ``universal_newlines=True`` is passed, the return value will be a
    string rather than bytes.

    .. versionchanged:: 1.2a1
       The ``timeout`` keyword argument is now accepted on all supported
       versions of Python (not just Python 3) and if it expires will raise a
       :exc:`TimeoutExpired` exception (under Python 2 this is a subclass of :exc:`~.Timeout`).
    .. versionchanged:: 1.2a1
       The ``input`` keyword argument is now accepted on all supported
       versions of Python, not just Python 3
    R_   t   stdouts3   stdout argument not allowed, it will be overridden.t   inputt   stdins/   stdin and input arguments may not both be used.t   outputN(   Ra   Rb   RS   R   R   t   communicateR>   Rd   Rh   Rc   t   pollR   (   Re   Rf   R_   t	   inputdatat   processRo   t
   unused_errRj   (    (    s0   lib/python2.7/site-packages/gevent/subprocess.pyR     s2    -



!(   t   Timeoutc           B@ s2   e  Z d  Z d d  Z e d    Z d   Z RS(   s  
        This exception is raised when the timeout expires while waiting for
        a child process in `communicate`.

        Under Python 2, this is a gevent extension with the same name as the
        Python 3 class for source-code forward compatibility. However, it extends
        :class:`gevent.timeout.Timeout` for backwards compatibility (because
        we used to just raise a plain ``Timeout``); note that ``Timeout`` is a
        ``BaseException``, *not* an ``Exception``.

        .. versionadded:: 1.2a1
        c         C@ s/   t  j |  d   | |  _ | |  _ | |  _ d  S(   N(   t   _Timeoutt   __init__Rb   Rk   t   secondsRo   (   RP   Rk   R_   Ro   (    (    s0   lib/python2.7/site-packages/gevent/subprocess.pyRw   y  s    		c         C@ s   |  j  S(   N(   Rx   (   RP   (    (    s0   lib/python2.7/site-packages/gevent/subprocess.pyR_     s    c         C@ s   d |  j  |  j f S(   Ns'   Command '%s' timed out after %s seconds(   Rk   R_   (   RP   (    (    s0   lib/python2.7/site-packages/gevent/subprocess.pyRZ     s    N(   RV   RW   t   __doc__Rb   Rw   t   propertyR_   RZ   (    (    (    s0   lib/python2.7/site-packages/gevent/subprocess.pyR>   k  s   t   set_inheritablec         C@ s   t  S(   N(   RN   (   t   it   v(    (    s0   lib/python2.7/site-packages/gevent/subprocess.pyt   <lambda>  s    c          G@ s'   d d l  m } | t   d <| |    S(   Ni    (   t
   FileObjectR   (   t   gevent.fileobjectR   t   globals(   Rh   t   _FileObject(    (    s0   lib/python2.7/site-packages/gevent/subprocess.pyR     s    c           B@ s  e  Z d  Z d Z e r d n d d* d* d* d* d* e e d* d* d* d* d e e d+ d* d* d* d* d  Z d   Z	 d   Z
 d   Z d* Z d* Z d* d* d  Z d	   Z d
   Z d   Z d* e d  Z e rDd   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d* e d  Z d   Z d   Z e Z  n d   Z d   Z e! d  Z" d   Z# d   Z$ d, Z% e& d     Z' e& d!    Z( e& d"    Z) d#   Z d$   Z* d%   Z d* e d&  Z d'   Z d(   Z d)   Z  RS(-   sQ  
    The underlying process creation and management in this module is
    handled by the Popen class. It offers a lot of flexibility so that
    developers are able to handle the less common cases not covered by
    the convenience functions.

    .. seealso:: :class:`subprocess.Popen`
       This class should have the same interface as the standard library class.

    .. versionchanged:: 1.2a1
       Instances can now be used as context managers under Python 2.7. Previously
       this was restricted to Python 3.

    .. versionchanged:: 1.2a1
       Instances now save the ``args`` attribute under Python 2.7. Previously this was
       restricted to Python 3.

    .. versionchanged:: 1.2b1
        Add the ``encoding`` and ``errors`` parameters for Python 3.

    .. versionchanged:: 1.3a1
       Accept "path-like" objects for the *cwd* parameter on all platforms.
       This was added to Python 3.6. Previously with gevent, it only worked
       on POSIX platforms on 3.6.

    .. versionchanged:: 1.3a1
       Add the ``text`` argument as a synonym for ``universal_newlines``,
       as added on Python 3.7.

    .. versionchanged:: 1.3a2
       Allow the same keyword arguments under Python 2 as Python 3:
       ``pass_fds``, ``start_new_session``, ``restore_signals``, ``encoding``
       and ``errors``. Under Python 2, ``encoding`` and ``errors`` are ignored
       because native handling of universal newlines is used.

    .. versionchanged:: 1.3a2
       Under Python 2, ``restore_signals`` defaults to ``False``. Previously it
       defaulted to ``True``, the same as it did in Python 3.
    t    ii    c   $      C@ s  | |  _  | |  _ t   } | d  k r< t r3 d n d } n  t | t  sZ t d   n  t r6| d  k	 r{ t	 d   n  t
 j d  d k r | t k r	t } q	nc | d  k	 p | d  k	 p | d  k	 } | t k r | r t } q	t } n | r	| r	t	 d   n  | d  k r!| j } n  | |  _ t |  _ n | t k rZt rQt } qZt } n  | r| rd d  l } | j d	 t  t } n  | d  k	 rt	 d
   n  | d k rt	 d   n  | d  k st  | j |  _ | d  k	 r| d  k	 rt |  t |  k rt d   n  | |  _ d  |  _ d  |  _ d  |  _ d  |  _ d  |  _ | |  _ t    |  _! |  j" | | |  \ } } } } } } t r| d k rt# j$ | j%   d  } n  | d k rt# j$ | j%   d  } n  | d k rt# j$ | j%   d  } qn  t o0|  j  p0|  j p0| p0| } | s?| rKd |  _& n  | d k rt r| rt' | d |  |  _ |  j j( d  d t d | d k d |  j  d |  j qt' | d |  |  _ n  | d k rO| s| r7t rt' | d |  |  _ |  j j( d d |  j  d |  j qLt' | d |  |  _ qOt' | d |  |  _ n  | d k r| sg| rt rt' | d |  |  _ |  j j( d  d | d | qt' | d |  |  _ qt' | d |  |  _ n  t |  _) |
 d  k	 rt* |
  n d  }
 yG |  j+ | | | | | |
 | | | | |	 | | | | | | | |  WnUt sZt
 j,   }  n  xQ t- d  |  j |  j |  j f  D]. }! y |! j.   Wq|t/ t0 f k
 rq|Xq|W|  j) srg  }" | t1 k r|" j2 |  n  | t1 k r|" j2 |  n  | t1 k r|" j2 |  n  t3 |  d  r3|" j2 |  j4  n  x< |" D]1 }# y t5 j. |#  Wq:t/ t0 f k
 rjq:Xq:Wn  t sz t6 |    Wd  ~  Xn    n Xd  S(   Nii    s   bufsize must be an integers0   preexec_fn is not supported on Windows platformsi   i   i   sS   close_fds is not supported on Windows platforms if you redirect stdin/stdout/stderrs   pass_fds overriding close_fds.s2   startupinfo is only supported on Windows platformss4   creationflags is only supported on Windows platformssl   Cannot disambiguate when both text and universal_newlines are supplied but different. Pass one or the other.R   t   wbt   write_throught   line_bufferingi   t   encodingt   errorst   rbt   rt   rUt   _devnull(   i   i   (7   R   R   t   get_hubRb   R
   t
   isinstanceR   t	   TypeErrort	   mswindowsRS   t   syst   version_infot   _PLATFORM_DEFAULT_CLOSE_FDSRN   RX   t
   threadpoolt   _waitingt   warningst   warnt   RuntimeWarningt   AssertionErrort   loopt   _loopt   boolR=   Rh   Rn   Rl   t   stderrt   pidt
   returncodet   universal_newlinesR   t   resultt   _get_handlest   msvcrtt   open_osfhandleRT   t   _communicate_empty_valueR   t   translate_newlinest   _closed_child_pipe_fdsR   t   _execute_childt   exc_infot   filtert   closet   OSErrort   IOErrorR   t   appendt   hasattrR   R\   R   ($   RP   Rh   t   bufsizet
   executableRn   Rl   R   t
   preexec_fnt	   close_fdst   shellt   cwdt   envR   t   startupinfot   creationflagst   restore_signalst   start_new_sessiont   pass_fdsR   R   t   textR   t   hubt   any_stdio_setR   t   p2creadt   p2cwritet   c2preadt   c2pwritet   errreadt   errwritet	   text_modeR   t   ft   to_closet   fd(    (    s0   lib/python2.7/site-packages/gevent/subprocess.pyRw     s    																'$%		(	c         C@ s&   d |  j  j t |   |  j |  j f S(   Ns!   <%s at 0x%x pid=%r returncode=%r>(   t	   __class__RV   t   idR   R   (   RP   (    (    s0   lib/python2.7/site-packages/gevent/subprocess.pyRU     s    c         C@ sa   | j    | j } t j |  r8 t j |  |  _ n t j |  |  _ |  j j |  j  d  S(   N(	   t   stopt   rstatusR\   t   WIFSIGNALEDt   WTERMSIGR   t   WEXITSTATUSR   t   set(   RP   t   watchert   status(    (    s0   lib/python2.7/site-packages/gevent/subprocess.pyt	   _on_child  s    
	c         C@ s4   t  |  d  s- t j t j t j  |  _ n  |  j S(   NR   (   R   R\   t   opent   devnullt   O_RDWRR   (   RP   (    (    s0   lib/python2.7/site-packages/gevent/subprocess.pyt   _get_devnull  s    c         @ s  g  }   j  r. | j t t   j  |   n    f d   }   j rn | d  } t |  } | j |  n d }   j r | d  } t |  } | j |  n d } | r | d k	 r   j d | d t  n  t	 | d | }	 | d k	 r"t
 |	  t
 |  k r"t   j |   n  xE   j   j f D]1 }
 |
 r5y |
 j   Wqft k
 rbqfXq5q5W  j     f d   } | d  } | d  } | d k rd n | | d k rd n | f S(   sC  Interact with process: Send data to stdin.  Read data from
        stdout and stderr, until end-of-file is reached.  Wait for
        process to terminate.  The optional input argument should be a
        string to be sent to the child process, or None, if no data
        should be sent to the child.

        communicate() returns a tuple (stdout, stderr).

        :keyword timeout: Under Python 2, this is a gevent extension; if
           given and it expires, we will raise :exc:`TimeoutExpired`, which
           extends :exc:`gevent.timeout.Timeout` (note that this only extends :exc:`BaseException`,
           *not* :exc:`Exception`)
           Under Python 3, this raises the standard :exc:`TimeoutExpired` exception.

        .. versionchanged:: 1.1a2
           Under Python 2, if the *timeout* elapses, raise the :exc:`gevent.timeout.Timeout`
           exception. Previously, we silently returned.
        .. versionchanged:: 1.1b5
           Honor a *timeout* even if there's no way to communicate with the child
           (stdin, stdout, and stderr are not pipes).
        c         @ s6   t   |    d |  d       f d   } | S(   Nt   _t   _bufferc          @ sk   y  j    }  Wn t k
 r$ d  SX|  s/ d  St     } | rT | j |   n t    |  g  d  S(   N(   t   readt   RuntimeErrort   getattrR   t   setattr(   t   datat
   the_buffer(   t   buf_namet   pipeRP   (    s0   lib/python2.7/site-packages/gevent/subprocess.pyt   _read  s    (   R   (   t	   pipe_nameR   (   RP   (   R   R   s0   lib/python2.7/site-packages/gevent/subprocess.pyt   _make_pipe_reader  s    Rl   R   R_   R`   c         @ sU   d |  d } t    |  } t   | d   | rH   j j |  } n	   j } | S(   NR   R   (   R   R   Rb   R   t   join(   R   R   t	   buf_value(   RP   (    s0   lib/python2.7/site-packages/gevent/subprocess.pyt   _get_output_value  s    	N(   Rn   R   t   spawnt   write_and_closeRl   Rb   R   Rc   RN   R   t   lenR>   Rh   R   R   (   RP   Rm   R_   t	   greenletsR   t	   _read_outRl   t	   _read_errR   t   doneR   R   t   stdout_valuet   stderr_value(    (   RP   s0   lib/python2.7/site-packages/gevent/subprocess.pyRp     s>    			$

c         C@ s
   |  j    S(   sS   Check if child process has terminated. Set and return :attr:`returncode` attribute.(   t   _internal_poll(   RP   (    (    s0   lib/python2.7/site-packages/gevent/subprocess.pyRq     s    c         C@ s   |  S(   N(    (   RP   (    (    s0   lib/python2.7/site-packages/gevent/subprocess.pyt	   __enter__  s    c         C@ sa   |  j  r |  j  j   n  |  j r2 |  j j   n  z |  j rN |  j j   n  Wd  |  j   Xd  S(   N(   Rl   R   R   Rn   Rc   (   RP   t   tR}   t   tb(    (    s0   lib/python2.7/site-packages/gevent/subprocess.pyt   __exit__  s    			c         C@ sP   |  j  j d |  } | rL | d  k	 rL |  j  j   rL t |  j |   n  | S(   NR_   (   R   Rc   Rb   t   readyR>   Rh   (   RP   R_   t	   raise_excR   (    (    s0   lib/python2.7/site-packages/gevent/subprocess.pyt   _gevent_result_wait*  s    "c         C@ s  | d k r( | d k r( | d k r( d Sd \ } } d \ } } d \ } }	 y t Wn t k
 rp t   }
 n Xt }
 | d k r t t  } | d k rxt d d  \ } } t r t |  } t	 j
 |  q qxn | t k rt d d  \ } } t rxt |  t |  } } qxnZ | |
 k rBt j |  j    } n6 t | t  rct j |  } n t j | j    } |  j |  } | d k rt t  } | d k rt d d  \ } } t rt |  } t	 j
 |  qqn | t k r.t d d  \ } } t rt |  t |  } } qnZ | |
 k rRt j |  j    } n6 t | t  rst j |  } n t j | j    } |  j |  } | d k rt t  }	 |	 d k rt d d  \ } }	 t rt |	  }	 t	 j
 |  qqn | t k r>t d d  \ } }	 t rt |  t |	  } }	 qno | t k rS| }	 nZ | |
 k rwt j |  j    }	 n6 t | t  rt j |  }	 n t j | j    }	 |  j |	  }	 | | | | | |	 f S(   s|   Construct and return tuple with IO objects:
            p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite
            ii    N(   iiiiii(   ii(   ii(   ii(   Rb   R:   t	   NameErrort   objectR-   R   R.   R
   RL   R)   RO   R   R   t   get_osfhandleR   R   RR   t   filenot   _make_inheritableR   R   R   (   RP   Rn   Rl   R   R   R   R   R   R   R   R   R   (    (    s0   lib/python2.7/site-packages/gevent/subprocess.pyR   5  s    $	c         C@ s   t  t   | t   d d t  S(   s2   Return a duplicate of handle, which is inheritablei    i   (   R/   R0   R1   (   RP   t   handle(    (    s0   lib/python2.7/site-packages/gevent/subprocess.pyR     s    	c         C@ s   t  j j t  j j t d   d  } t  j j |  s t  j j t  j j t j  d  } t  j j |  s t d   q n  | S(   s-   Find and return absolute path to w9xpopen.exei    s   w9xpopen.exesZ   Cannot locate w9xpopen.exe, which is needed for Popen to work with your shell or platform.(	   R\   t   pathR   t   dirnameR2   t   existsR   t   exec_prefixR   (   RP   t   w9xpopen(    (    s0   lib/python2.7/site-packages/gevent/subprocess.pyt   _find_w9xpopen  s    		c         C@ s   t  d   | D  S(   s   Filter out console handles that can't be used
            in lpAttributeList["handle_list"] and make sure the list
            isn't empty. This also removes duplicate handles.c         S@ s>   h  |  ]4 } | d  @d  k s4 t  j |  t  j k r |  q S(   i   (   R)   t   GetFileTypet   FILE_TYPE_CHAR(   t   .0R   (    (    s0   lib/python2.7/site-packages/gevent/subprocess.pys	   <setcomp>  s   	 (   t   list(   RP   t   handle_list(    (    s0   lib/python2.7/site-packages/gevent/subprocess.pyt   _filter_handle_list  s    c   !   
   C@ sN  | s t  d   t | t  s1 t |  } n  |	 d k rI t   }	 n  d | | | f k } | r |	 j t O_ | |	 _ | |	 _	 | |	 _
 n  t |	 d  r|	 j } t | o d | k o | d  } | s | r| r| d k r i  } |	 _ n  t | j d g    } | d <| rG| t |  t |  t |  g 7} n  |  j |  | (| r| sd d l } | j d t  n  t } qqn  | r/|	 j t O_ t |	 _ t j j d d	  } d
 j | |  } t   d k st j j |  j   d k r/|  j    } d | | f } |
 t! O}
 q/n  zp y; t" | | d d t |  |
 | | |	 	 \ } } } } Wn. t# k
 r} t$ r  n  t% | j&    n XWd d   }  |  |  |  |  |  |  t |  d  rt j' |  j(  n  Xt) |  _* t | d  st+ |  n | |  _, | |  _- t | d  s@t. j/ |  n	 | j0   d S(   s$   Execute program (MS Windows version)s"   pass_fds not supported on Windows.it   lpAttributeListR	  i    Ns?   startupinfo.lpAttributeList['handle_list'] overriding close_fdst   COMSPECs   cmd.exes
   {} /c "{}"I       s   command.coms   "%s" %sc         S@ sH   |  d  k	 rD |  d k rD t |  d  r4 |  j   qD t j |   n  d  S(   NiRQ   (   Rb   R   RQ   R)   RO   (   t   x(    (    s0   lib/python2.7/site-packages/gevent/subprocess.pyt   _close  s    R   RQ   (1   R   R   R   R'   Rb   R%   t   dwFlagsR!   t	   hStdInputt
   hStdOutputt	   hStdErrorR   R  R   R  Ri   RR   R
  R   R   R   RX   R"   R    t   wShowWindowR\   t   environt   formatR3   R   t   basenamet   lowerR  R   R4   R   R
   t   WindowsErrorRh   R   R   RN   t   _child_createdRL   t   _handleR   R)   RO   RQ   (!   RP   Rh   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   t   unused_restore_signalst   unused_start_new_sessiont   use_std_handlest   attribute_listt   have_handle_listR	  R   t   comspecR  t   hpt   htR   t   tidt   eR  (    (    s0   lib/python2.7/site-packages/gevent/subprocess.pyR     sz    					 (	
	- 	
	


	$	c         C@ sY   |  j  d k rR t |  j d  t k rR t |  j  |  _  |  j j |  j   qR n  |  j  S(   s^   Check if child process has terminated.  Returns returncode
            attribute.
            i    N(   R   Rb   R+   R  R*   R,   R   R   (   RP   (    (    s0   lib/python2.7/site-packages/gevent/subprocess.pyR   '  s
    c         C@ sS   |  j  j   r6 |  j r6 t |  _ t j |  j  n  |  j  j t | |    d  S(   N(	   R   R   R   RN   R   R   t   _waitt   rawlinkR   (   RP   t   callback(    (    s0   lib/python2.7/site-packages/gevent/subprocess.pyR&  2  s    	c         C@ s)   t  |  j t  t |  j  |  _ |  j S(   N(   R+   R  R5   R,   R   (   RP   (    (    s0   lib/python2.7/site-packages/gevent/subprocess.pyt   _blocking_wait9  s    c         C@ s#   |  j  j |  j  j |  j  d  S(   N(   R   R   R(  R&  R   (   RP   (    (    s0   lib/python2.7/site-packages/gevent/subprocess.pyR%  ?  s    c         C@ sA   |  j  d k r1 |  j s1 t |  _ |  j   q1 n  |  j | |  S(   sO   Wait for child process to terminate.  Returns returncode
            attribute.N(   R   Rb   R   RN   R%  R   (   RP   R_   R`   (    (    s0   lib/python2.7/site-packages/gevent/subprocess.pyRc   B  s
    		c         C@ s   | t  j k r |  j   ne | t  j k rD t j |  j t  j  n= | t  j k rl t j |  j t  j  n t d j	 |    d S(   s)   Send a signal to the process
            s   Unsupported signal: {}N(
   t   signalt   SIGTERMt	   terminatet   CTRL_C_EVENTR\   Rd   R   t   CTRL_BREAK_EVENTRS   R  (   RP   t   sig(    (    s0   lib/python2.7/site-packages/gevent/subprocess.pyt   send_signalK  s    c         C@ s   |  j  d k	 r d Sy t |  j d  Wne t k
 r } | j d k rN   n  t |  j  } | t k ro   n  | |  _  |  j j	 |  j   n Xd S(   s#   Terminates the process
            Ni   i   (
   R   Rb   R6   R  R   t   winerrorR,   R7   R   R   (   RP   R$  t   rc(    (    s0   lib/python2.7/site-packages/gevent/subprocess.pyR+  W  s    	c         C@ s   |  j  j t | |    d  S(   N(   R   R&  R   (   RP   R'  (    (    s0   lib/python2.7/site-packages/gevent/subprocess.pyR&  r  s    c         C@ s  d \ } } d \ } } d \ } }	 y t  Wn t k
 rH t   }
 n Xt  }
 | d k r^ n` | t k r |  j   \ } } n? | |
 k r |  j   } n$ t | t  r | } n | j	   } | d k r n` | t k r |  j   \ } } n? | |
 k r	|  j   } n$ t | t  r!| } n | j	   } | d k r<n | t k r]|  j   \ } }	 nr | t
 k r| d k r~| }	 qt j j	   }	 n? | |
 k r|  j   }	 n$ t | t  r| }	 n | j	   }	 | | | | | |	 f S(   s|   Construct and return tuple with IO objects:
            p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite
            i(   ii(   ii(   iiN(   R:   R   R   Rb   R   t   pipe_cloexecR   R   RR   R   R   R   t
   __stdout__(   RP   Rn   Rl   R   R   R   R   R   R   R   R   (    (    s0   lib/python2.7/site-packages/gevent/subprocess.pyR   w  sT    				c         C@ s~   y t  j } Wn t k
 r& d } n Xt  j  | t  j  } | r_ t  j  | t  j | | B n t  j  | t  j | | @ d  S(   Ni   (   t   fcntlt
   FD_CLOEXECt   AttributeErrort   F_GETFDt   F_SETFD(   RP   R   t   cloexect   cloexec_flagt   old(    (    s0   lib/python2.7/site-packages/gevent/subprocess.pyt   _set_cloexec_flag  s    
c         C@ s7   t  j  | t  j  t j @} t  j  | t  j |  d  S(   N(   R4  t   F_GETFLR\   t
   O_NONBLOCKt   F_SETFL(   RP   R   t   flags(    (    s0   lib/python2.7/site-packages/gevent/subprocess.pyt   _remove_nonblock_flag  s    c         C@ s6   t  j   \ } } |  j |  |  j |  | | f S(   s#   Create a pipe with FDs set CLOEXEC.(   R\   R   R<  (   RP   R   t   w(    (    s0   lib/python2.7/site-packages/gevent/subprocess.pyR2    s    s   /proc/self/fds   /dev/fdc         C@ sI   x6 |  j  D]+ } t j j |  r
 |  j | | |  Sq
 W|  j | |  S(   N(   t   _POSSIBLE_FD_DIRSR\   R   t   isdirt   _close_fds_from_patht   _close_fds_brute_force(   t   clst   keept   errpipe_writeR   (    (    s0   lib/python2.7/site-packages/gevent/subprocess.pyt
   _close_fds  s    c         C@ s   y, g  t  j |  D] } t |  ^ q } Wn' t t f k
 rU |  j | |  n{ Xx- | D]% } | | k ru q] n  t | t  q] WxG | D]? } | | k s | d k  r q n  y t  j |  Wq q Xq Wd  S(   Ni   (	   R\   t   listdirRR   RS   R   RF  t   _set_inheritableRN   R   (   RG  R   RH  RI  t   fnamet   fdsR|   R   (    (    s0   lib/python2.7/site-packages/gevent/subprocess.pyRE    s    ,c         C@ s   t  t d  s t  t |  } t |  } t |  } t j d |  t j | d t  xQ t | |  D]@ } | | k r t	 | t
  qm n  y t j |  Wqm qm Xqm Wd  S(   Nt
   closerangei   i   (   R   R\   R   t   sortedt   mint   maxRO  R#   R	   RL  RN   R   (   RG  RH  RI  t   min_keept   max_keepR|   (    (    s0   lib/python2.7/site-packages/gevent/subprocess.pyRF    s    c   &      @ sz  t  r' t | t t f  r' | g } nU t  rI t | t  rI | g } n3 y t |  } Wn  t k
 r{ t |  g } n X| r d d g | } | r | | d <q n  | d k r | d } n    j	 j
     j   \ } } g  } x, | d k  r| j |  t j |  } q Wx | D] } t j |  qWzpzmt j   } t j   y" t   j   j	 t t    _ Wn | rt j   n    n X  j d k rzyo| d k rt j |  n  | d k rt j |  n  | d k rt j |  n  t j |  | d k r%t j |  } n  x | d k rFt j |  } q(W  f d   } | | d  | | d  | | d  t d g  } xL | | | g D]; } | | k r| d k rt j |  | j |  qqW| d k	 ry t j |  Wqt k
 r} t | _   qXn  | r-|   n  | r_t |  } | j |    j | |  n  | rxE d D]: } t t  | d  } | d k	 rlt  j  | t  j!  qlqlWn  | rt j"   n  | d k rt j# | |  n2 t  rd   | j$   D } n  t j% | | |  Wn\ t& j'   \ } }  }! t( j) | |  |!  }" d j* |"  |  _+ t j, | t- j. |    n XWd t j/ d  Xn  t   _0 | rt j   n  Wd t j |  Xt   d d  }# | d k r| d k r| |# k rt j |  n  | d k r.| d k r.| |# k r.t j |  n  | d k rb| d k rb| |# k rbt j |  n  |# d k	 r~t j |#  n  t   _1 t2 | d  } | j3   }$ Wd t4 | d  r| j   n t j |  X|$ d k rv  j5   t- j6 |$  }% x? | | | f D]. } | d k	 r| d k rt j |  qqWt |% t  rm| |% _7 t4 |% d  rm| |% _7 qmn  |%  n  d S(   s   Execute program (POSIX version)s   /bin/shs   -ci    i   ii   c         @ sg   |  | k r   j  |  t  n |  d k r> t j |  |  n  y   j |  Wn t k
 rb n Xd  S(   Ni(   R<  RX   R\   t   dup2RA  R   (   t   existingt   desired(   RP   (    s0   lib/python2.7/site-packages/gevent/subprocess.pyt   _dup2c  s    i   t   SIGPIPEt   SIGXFZt   SIGXFSZc         S@ s[   i  |  ]Q \ } } t  | t  r0 t j |  n | t  | t  rQ t j |  n |  q S(    (   R   t   bytesR\   t   fsdecode(   R  t   kR}   (    (    s0   lib/python2.7/site-packages/gevent/subprocess.pys
   <dictcomp>  s   	R   NR   R   R   t   _failed_chdir(   i    i   (   RY  RZ  R[  (8   R
   R   t   strR\  R   R  R   R   Rb   R   t   install_sigchldR2  R   R\   t   dupR   t   gct	   isenabledt   disableR^   R   RN   R]   R   t   enableR   t   addt   chdirR   R_  RJ  R   R)  t   SIG_DFLt   setsidt   execvpt   itemst   execvpeR   R   t	   tracebackt   format_exceptionR   t   child_tracebackt   writet   picklet   dumpst   _exitR  R   R   R   R   Rc   t   loadst   filename(&   RP   Rh   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   t   errpipe_readRI  t   low_fds_to_closet   low_fdt   gc_was_enabledRX  RM   R   R$  t   fds_to_keepR.  t   exc_typet	   exc_valueR   t	   exc_linest
   devnull_fdR   t   child_exception(    (   RP   s0   lib/python2.7/site-packages/gevent/subprocess.pyR     s    	
"	 	

		!	$$$	
	c         C@ sY   t  j |  r% t  j |  |  _ n0 t  j |  rI t  j |  |  _ n t d   d  S(   Ns   Unknown child exit status!(   R\   R   R   R   t	   WIFEXITEDR   R   (   RP   t   sts(    (    s0   lib/python2.7/site-packages/gevent/subprocess.pyt   _handle_exitstatus  s
    c         C@ sV   |  j  d k rO t   t   k	 rO t |  j d t  } | rL t d  qL qO n  |  j  S(   s^   Check if child process has terminated.  Returns returncode
            attribute.
            t   sig_pendinggh㈵>N(   R   Rb   R   R   R   R   RN   R   (   RP   R  (    (    s0   lib/python2.7/site-packages/gevent/subprocess.pyR     s    c         C@ s   |  j  | |  S(   s  
            Wait for child process to terminate.  Returns :attr:`returncode`
            attribute.

            :keyword timeout: The floating point number of seconds to
                wait. Under Python 2, this is a gevent extension, and
                we simply return if it expires. Under Python 3, if
                this time elapses without finishing the process,
                :exc:`TimeoutExpired` is raised.
            (   R   (   RP   R_   R`   (    (    s0   lib/python2.7/site-packages/gevent/subprocess.pyRc     s    c         C@ s)   |  j  d k r% t j |  j |  n  d S(   s)   Send a signal to the process
            N(   R   Rb   R\   Rd   R   (   RP   R.  (    (    s0   lib/python2.7/site-packages/gevent/subprocess.pyR/    s    c         C@ s   |  j  t j  d S(   s/   Terminate the process with SIGTERM
            N(   R/  R)  R*  (   RP   (    (    s0   lib/python2.7/site-packages/gevent/subprocess.pyR+    s    c         C@ s   |  j  t j  d S(   s*   Kill the process with SIGKILL
            N(   R/  R)  t   SIGKILL(   RP   (    (    s0   lib/python2.7/site-packages/gevent/subprocess.pyRd     s    N(    (   s   /proc/self/fds   /dev/fd(+   RV   RW   Ry   R   R
   Rb   R   RX   Rw   RU   R   R   t   _stdout_buffert   _stderr_bufferRp   Rq   R   R   R   R   R   R   R  R
  R   R   R&  R(  R%  Rc   R/  R+  Rd   RN   R<  RA  R2  RC  t   classmethodRJ  RE  RF  R  (    (    (    s0   lib/python2.7/site-packages/gevent/subprocess.pyR     sl   '							j				U				w										<		 						c         C@ s   z y6 | r8 |  j  |  t |  d  r8 |  j   q8 n  WnC t t f k
 r~ } | j t j k r | j t j k r   q n XWd  y |  j   Wn t	 k
 r n XXd  S(   Nt   flush(
   Rq  R   R  R   R   t   errnot   EPIPEt   EINVALR   t   EnvironmentError(   t   fobjR   t   ex(    (    s0   lib/python2.7/site-packages/gevent/subprocess.pyR     s     $c         C@ s   |  j  |  _ | |  _ |  S(   N(   Ro   Rl   R   (   t   excR   (    (    s0   lib/python2.7/site-packages/gevent/subprocess.pyt   _with_stdout_stderr&  s    	c           B@ s/   e  Z d  Z d d d  Z d   Z d   Z RS(   s  
    A process that has finished running.

    This is returned by run().

    Attributes:
      - args: The list or str args passed to run().
      - returncode: The exit code of the process, negative for signals.
      - stdout: The standard output (None if not captured).
      - stderr: The standard error (None if not captured).

    .. versionadded:: 1.2a1
       This first appeared in Python 3.5 and is available to all
       Python versions in gevent.
    c         C@ s(   | |  _  | |  _ | |  _ | |  _ d  S(   N(   Rh   R   Rl   R   (   RP   Rh   R   Rl   R   (    (    s0   lib/python2.7/site-packages/gevent/subprocess.pyRw   >  s    			c         C@ s   d j  |  j  d j  |  j  g } |  j d  k	 rO | j d j  |  j   n  |  j d  k	 rz | j d j  |  j   n  d j  t |   j d j	 |   S(   Ns	   args={!r}s   returncode={!r}s   stdout={!r}s   stderr={!r}s   {}({})s   , (
   R  Rh   R   Rl   Rb   R   R   t   typeRV   R   (   RP   Rh   (    (    s0   lib/python2.7/site-packages/gevent/subprocess.pyRU   D  s    c         C@ s7   |  j  r3 t t |  j  |  j |  j  |  j   n  d S(   s6   Raise CalledProcessError if the exit code is non-zero.N(   R   R  R   Rh   Rl   R   (   RP   (    (    s0   lib/python2.7/site-packages/gevent/subprocess.pyt   check_returncodeM  s    	N(   RV   RW   Ry   Rb   Rw   RU   R  (    (    (    s0   lib/python2.7/site-packages/gevent/subprocess.pyR9   .  s   		c    
   
   O@ s  | j  d d  } | j  d d  } | j  d t  } | j  d t  } | d k	 r| d | k ro t d   n  t | d <n  | r d | k s d | k r t d	   n  t | d <t | d <n  t |  |    } y | j | d | \ } } Wnk t k
 rA| j   | j   \ } } t	 t | j
 | d
 | |   n | j   | j     n X| j   }	 | r|	 rt	 t |	 | j
 |  |   n  Wd QXt | j
 |	 | |  S(   sm  
    run(args, *, stdin=None, input=None, stdout=None, stderr=None, shell=False, timeout=None, check=False) -> CompletedProcess

    Run command with arguments and return a CompletedProcess instance.

    The returned instance will have attributes args, returncode, stdout and
    stderr. By default, stdout and stderr are not captured, and those attributes
    will be None. Pass stdout=PIPE and/or stderr=PIPE in order to capture them.
    If check is True and the exit code was non-zero, it raises a
    CalledProcessError. The CalledProcessError object will have the return code
    in the returncode attribute, and output & stderr attributes if those streams
    were captured.

    If timeout is given, and the process takes too long, a TimeoutExpired
    exception will be raised.

    There is an optional argument "input", allowing you to
    pass a string to the subprocess's stdin.  If you use this argument
    you may not also use the Popen constructor's "stdin" argument, as
    it will be used internally.
    The other arguments are the same as for the Popen constructor.
    If universal_newlines=True is passed, the "input" argument must be a
    string and stdout/stderr in the returned object will be strings rather than
    bytes.

    .. versionadded:: 1.2a1
       This function first appeared in Python 3.5. It is available on all Python
       versions gevent supports.

    .. versionchanged:: 1.3a2
       Add the ``capture_output`` argument from Python 3.7. It automatically sets
       ``stdout`` and ``stderr`` to ``PIPE``. It is an error to pass either
       of those arguments along with ``capture_output``.
    Rm   R_   t   checkt   capture_outputRn   s/   stdin and input arguments may not both be used.Rl   R   s@   stdout and stderr arguments may not be used with capture_output.Ro   N(   Ra   Rb   RX   RS   R   R   Rp   R>   Rd   R  Rh   Rc   Rq   R   R9   (
   Re   Rf   Rm   R_   R  R  Rs   Rl   R   Rj   (    (    s0   lib/python2.7/site-packages/gevent/subprocess.pyR8   T  s8    #

$

'(^   Ry   t
   __future__R    R   R  Rc  R\   R)  R   Rn  t   gevent.eventR   t
   gevent.hubR   R   R   R   R   t   gevent._compatR   R   R	   R
   R   R   R   t   gevent._utilR   R   t   gevent.greenletR   R   R   t
   subprocesst   __subprocess__t   __implements__t   platformt
   startswithR   Rb   R   R   t   __imports__t	   __extra__R   t   removet   sysconfR#   t   extendR   RN   t   actually_importedR   t
   differenceR   R(   R)   t   _attr_resolution_orderR  t   namet   valuet   placet   __all__t   _xR   R   RR   RL   R4  Rr  t   geventR[   t   get_originalR]   t	   gevent.osR^   R   R   R   R   R   t   gevent.timeoutRu   Rv   R>   R   R{   RL  R   R   R   R  R9   R8   (    (    (    s0   lib/python2.7/site-packages/gevent/subprocess.pyt   <module>   s*  						"
			I			    		&