ó
B[~Mc           @   sG  d  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 Z d d l Z d d l	 Z	 e j
 ƒ  Z d e _ 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
 „  Z d e j f d „  ƒ  YZ d „  Z d „  Z d e j e f d „  ƒ  YZ  d S(   s4   
http://isometric.sixsided.org/_/gates_in_the_head/
iÿÿÿÿNi   (   t
   implements(   t   IProcessTransportt	   IConsumert	   IProducer(   t   quoteArguments(   t   error(   t   _pollingfile(   t   BaseProcessc         C   s"   d d  l  } |  GH| j j ƒ  d  S(   Niÿÿÿÿ(   t   syst   stdoutt   flush(   t   msgR   (    (    sC   /usr/lib/python2.7/dist-packages/twisted/internet/_dumbwin32proc.pyt   debug$   s    t   _Reaperc           B   s   e  Z d  „  Z d „  Z RS(   c         C   s   | |  _  d  S(   N(   t   proc(   t   selfR   (    (    sC   /usr/lib/python2.7/dist-packages/twisted/internet/_dumbwin32proc.pyt   __init__+   s    c         C   sX   t  j |  j j d ƒ t  j k r% d St j |  j j ƒ } |  j ƒ  |  j j | ƒ d S(   Ni    (	   t
   win32eventt   WaitForSingleObjectR   t   hProcesst   WAIT_OBJECT_0t   win32processt   GetExitCodeProcesst
   deactivatet   processEnded(   R   t   exitCode(    (    sC   /usr/lib/python2.7/dist-packages/twisted/internet/_dumbwin32proc.pyt	   checkWork.   s    !
(   t   __name__t
   __module__R   R   (    (    (    sC   /usr/lib/python2.7/dist-packages/twisted/internet/_dumbwin32proc.pyR   )   s   	c         C   sD   t  |  d ƒ } | j d ƒ d k r@ | j d ƒ j d ƒ } | Sd S(   s   
    Look for a #! line, and return the value following the #! if one exists, or
    None if this file is not a script.

    I don't know if there are any conventions for quoting in Windows shebang
    lines, so this doesn't support any; therefore, you may not pass any
    arguments to scripts invoked as filters.  That's probably wrong, so if
    somebody knows more about the cultural expectations on Windows, please feel
    free to fix.

    This shebang line support was added in support of the CGI tests;
    appropriately enough, I determined that shebang lines are culturally
    accepted in the Windows world through this page::

        http://www.cgi101.com/learn/connect/winxp.html

    @param filename: str representing a filename

    @return: a str representing another filename.
    t   rUi   s   #!i   s   
N(   t   filet   readt   readlinet   strip(   t   filenamet   ft   exe(    (    sC   /usr/lib/python2.7/dist-packages/twisted/internet/_dumbwin32proc.pyt   _findShebang7   s    c         C   s   |  j  d d k S(   sù   
    Determine if a pywintypes.error is telling us that the given process is
    'not a valid win32 application', i.e. not a PE format executable.

    @param pywinerr: a pywintypes.error instance raised by CreateProcess

    @return: a boolean
    i    iÁ   (   t   args(   t   pywinerr(    (    sC   /usr/lib/python2.7/dist-packages/twisted/internet/_dumbwin32proc.pyt   _invalidWin32AppQ   s    t   Processc           B   sá   e  Z d  Z e e e e ƒ d Z d „  Z d „  Z	 d „  Z
 d „  Z d „  Z d „  Z d „  Z d	 „  Z d
 „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z RS(   s•  A process that integrates with the Twisted event loop.

    If your subprocess is a python program, you need to:

     - Run python.exe with the '-u' command line option - this turns on
       unbuffered I/O. Buffering stdout/err/in can cause problems, see e.g.
       http://support.microsoft.com/default.aspx?scid=kb;EN-US;q1903

     - If you don't want Windows messing with data passed over
       stdin/out/err, set the pipes to be in binary mode::

        import os, sys, mscvrt
        msvcrt.setmode(sys.stdin.fileno(), os.O_BINARY)
        msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
        msvcrt.setmode(sys.stderr.fileno(), os.O_BINARY)

    i    c            s6  t  j j ˆ | ƒ t j ˆ | ƒ t j ƒ  } d | _ t j | d ƒ \ ˆ _	 } t j | d ƒ \ ˆ _
 }	 t j | d ƒ \ }
 ˆ _ t j ˆ j t j d	 d	 ƒ t j ƒ  ‰ | ˆ _ |	 ˆ _ |
 ˆ _ t j ˆ _ t j ƒ  } t j | ˆ j	 | d d t j ƒ } t j ˆ j	 ƒ | ˆ _	 t j | ˆ j
 | d d t j ƒ } t j ˆ j
 ƒ | ˆ _
 t j | ˆ j | d d t j ƒ } t j ˆ j ƒ | ˆ _ t j j ƒ  ‰ ˆ j  | p¹i  ƒ t! | ƒ ‰  ‡ ‡ ‡  ‡ ‡ ‡ f d †  } y€ y | ƒ  Wnn t" k
 re} | j# d
 k r‚  n  i  } x0 ˆ j$ ƒ  D]" \ } } t% | ƒ | t% | ƒ <q/W| ‰ | ƒ  n XWnå t& j' k
 rN} t( | ƒ s—t) | ƒ ‚ qOt* ˆ ƒ } | d	 k rÂt) d ˆ ƒ ‚ qOt+ | ƒ } | j, d ˆ ƒ t! | ƒ ‰  ˆ } | ‰ y | ƒ  WqOt& j' k
 rJ} t( | ƒ r;t) d | | f ƒ ‚ n  t) | ƒ ‚ qOXn Xt j |	 ƒ t j | ƒ t j |
 ƒ t  j- ˆ j	 ‡ f d †  ˆ j. ƒ ˆ _/ t  j- ˆ j
 ‡ f d †  ˆ j0 ƒ ˆ _1 t  j2 ˆ j ˆ j3 ƒ ˆ _4 x- ˆ j/ ˆ j1 ˆ j4 f D] } ˆ j5 | ƒ qøWˆ j6 j7 ˆ ƒ ˆ j5 t8 ˆ ƒ ƒ d	 S(   s-   
        Create a new child process.
        i   i    c       
      s@   t  j ˆ ˆ d  d  d d ˆ ˆ ˆ ƒ	 \ ˆ  _ ˆ  _ ˆ  _ }  d  S(   Ni   i    (   R   t   CreateProcesst   NoneR   t   hThreadt   pid(   t   dwTid(   R   t   StartupInfot   cmdlinet   commandt   envt   path(    sC   /usr/lib/python2.7/dist-packages/twisted/internet/_dumbwin32proc.pyt   doCreate®   s    s<   All dictionary items must be strings, or all must be unicodesD   %r is neither a Windows executable, nor a script with a shebang lines<   %r has an invalid shebang line: %r is not a valid executablec            s   ˆ  j  j d |  ƒ S(   Ni   (   t   protot   childDataReceived(   t   data(   R   (    sC   /usr/lib/python2.7/dist-packages/twisted/internet/_dumbwin32proc.pyt   <lambda>æ   s    c            s   ˆ  j  j d |  ƒ S(   Ni   (   R5   R6   (   R7   (   R   (    sC   /usr/lib/python2.7/dist-packages/twisted/internet/_dumbwin32proc.pyR8   ë   s    N(   s<   All dictionary items must be strings, or all must be unicode(9   R   t   _PollingTimerR   R   t   win32securityt   SECURITY_ATTRIBUTESt   bInheritHandlet	   win32pipet
   CreatePipet   hStdoutRt   hStderrRt   hStdinWt   SetNamedPipeHandleStatet   PIPE_NOWAITR+   R   t   STARTUPINFOt
   hStdOutputt	   hStdErrort	   hStdInputt   STARTF_USESTDHANDLESt   dwFlagst   win32apit   GetCurrentProcesst   DuplicateHandlet   win32cont   DUPLICATE_SAME_ACCESSt	   win32filet   CloseHandlet   ost   environt   copyt   updateR   t	   TypeErrorR&   t   itemst   unicodet
   pywintypesR   R(   t   OSErrorR%   t   listt   insertt   _PollableReadPipet   outConnectionLostR	   t   errConnectionLostt   stderrt   _PollableWritePipet   inConnectionLostt   stdint   _addPollableResourceR5   t   makeConnectionR   (   R   t   reactort   protocolR1   R&   t   environmentR3   t   sAttrst   hStdoutWt   hStderrWt   hStdinRt
   currentPidt   tmpR4   t   et   newenvt   keyt   valuet   pwtet   shebt   origcmdt   pwte2t   pipewatcher(    (   R0   R/   R   R2   R1   R3   sC   /usr/lib/python2.7/dist-packages/twisted/internet/_dumbwin32proc.pyR   w   s¤    							c         C   sD   |  j  d  k r t j ƒ  ‚ n  | d k r@ t j |  j d ƒ n  d  S(   Nt   INTt   TERMt   KILLi   (   Rw   Rx   Ry   (   R-   R+   R   t   ProcessExitedAlreadyR   t   TerminateProcessR   (   R   t   signalID(    (    sC   /usr/lib/python2.7/dist-packages/twisted/internet/_dumbwin32proc.pyt   signalProcessû   s    c         C   s&   | d k r t  j | ƒ St  j | ƒ S(   Ni    (   R   t   ProcessDonet   ProcessTerminated(   R   t   status(    (    sC   /usr/lib/python2.7/dist-packages/twisted/internet/_dumbwin32proc.pyt
   _getReason  s    c         C   s   |  j  j | ƒ d S(   s!   Write data to the process' stdin.N(   Rb   t   write(   R   R7   (    (    sC   /usr/lib/python2.7/dist-packages/twisted/internet/_dumbwin32proc.pyR‚     s    c         C   s   |  j  j | ƒ d S(   s!   Write data to the process' stdin.N(   Rb   t   writeSequence(   R   t   seq(    (    sC   /usr/lib/python2.7/dist-packages/twisted/internet/_dumbwin32proc.pyRƒ     s    c         C   s[   | d k r |  j  ƒ  n> | d k r2 |  j ƒ  n% | d k rK |  j ƒ  n t d ƒ ‚ d  S(   Ni    i   i   s4   Only standard-IO file descriptors available on win32(   t
   closeStdint   closeStdoutt   closeStderrt   NotImplementedError(   R   t   fd(    (    sC   /usr/lib/python2.7/dist-packages/twisted/internet/_dumbwin32proc.pyt   closeChildFD  s    c         C   s   |  j  j ƒ  d S(   s"   Close the process' stdin.
        N(   Rb   t   close(   R   (    (    sC   /usr/lib/python2.7/dist-packages/twisted/internet/_dumbwin32proc.pyR…     s    c         C   s   |  j  j ƒ  d  S(   N(   R_   R‹   (   R   (    (    sC   /usr/lib/python2.7/dist-packages/twisted/internet/_dumbwin32proc.pyR‡     s    c         C   s   |  j  j ƒ  d  S(   N(   R	   R‹   (   R   (    (    sC   /usr/lib/python2.7/dist-packages/twisted/internet/_dumbwin32proc.pyR†   "  s    c         C   s"   |  j  ƒ  |  j ƒ  |  j ƒ  d S(   s&   Close the process' stdout, in and err.N(   R…   R†   R‡   (   R   (    (    sC   /usr/lib/python2.7/dist-packages/twisted/internet/_dumbwin32proc.pyt   loseConnection%  s    

c         C   s   |  j  j d ƒ |  j ƒ  d  S(   Ni   (   R5   t   childConnectionLostt   connectionLostNotify(   R   (    (    sC   /usr/lib/python2.7/dist-packages/twisted/internet/_dumbwin32proc.pyR]   ,  s    c         C   s   |  j  j d ƒ |  j ƒ  d  S(   Ni   (   R5   R   RŽ   (   R   (    (    sC   /usr/lib/python2.7/dist-packages/twisted/internet/_dumbwin32proc.pyR^   1  s    c         C   s   |  j  j d ƒ |  j ƒ  d  S(   Ni    (   R5   R   RŽ   (   R   (    (    sC   /usr/lib/python2.7/dist-packages/twisted/internet/_dumbwin32proc.pyRa   6  s    c         C   s   |  j  d 7_  |  j ƒ  d S(   sS   
        Will be called 3 times, by stdout/err threads and process handle.
        i   N(   t   closedNotifiest   maybeCallProcessEnded(   R   (    (    sC   /usr/lib/python2.7/dist-packages/twisted/internet/_dumbwin32proc.pyRŽ   ;  s    c         C   s^   |  j  d k rZ |  j rZ t j |  j ƒ t j |  j ƒ d  |  _ d  |  _ t j |  ƒ n  d  S(   Ni   (	   R   t   lostProcessRO   RP   R   R,   R+   R   R   (   R   (    (    sC   /usr/lib/python2.7/dist-packages/twisted/internet/_dumbwin32proc.pyR   C  s    		c         C   s   |  j  j | | ƒ d  S(   N(   Rb   t   registerProducer(   R   t   producert	   streaming(    (    sC   /usr/lib/python2.7/dist-packages/twisted/internet/_dumbwin32proc.pyR’   M  s    c         C   s   |  j  j ƒ  d  S(   N(   Rb   t   unregisterProducer(   R   (    (    sC   /usr/lib/python2.7/dist-packages/twisted/internet/_dumbwin32proc.pyR•   P  s    c         C   s   |  j  ƒ  d  S(   N(   t   _pause(   R   (    (    sC   /usr/lib/python2.7/dist-packages/twisted/internet/_dumbwin32proc.pyt   pauseProducingT  s    c         C   s   |  j  ƒ  d  S(   N(   t   _unpause(   R   (    (    sC   /usr/lib/python2.7/dist-packages/twisted/internet/_dumbwin32proc.pyt   resumeProducingW  s    c         C   s   |  j  ƒ  d  S(   N(   RŒ   (   R   (    (    sC   /usr/lib/python2.7/dist-packages/twisted/internet/_dumbwin32proc.pyt   stopProducingZ  s    c         C   s   d |  j  j |  j f S(   s@   
        Return a string representation of the process.
        s   <%s pid=%s>(   t	   __class__R   R-   (   R   (    (    sC   /usr/lib/python2.7/dist-packages/twisted/internet/_dumbwin32proc.pyt   __repr__^  s    (   R   R   t   __doc__R    R   R   R   R   R   R}   R   R‚   Rƒ   RŠ   R…   R‡   R†   RŒ   R]   R^   Ra   RŽ   R   R’   R•   R—   R™   Rš   Rœ   (    (    (    sC   /usr/lib/python2.7/dist-packages/twisted/internet/_dumbwin32proc.pyR)   a   s0   	„					
									
					(!   R   RQ   RJ   RM   R   RO   R=   R   R:   RX   R;   t   PIPE_ATTRS_INHERITABLER<   t   zope.interfaceR    t   twisted.internet.interfacesR   R   R   t   twisted.python.win32R   t   twisted.internetR   R   t   twisted.internet._baseprocessR   R   t   _PollableResourceR   R%   R(   R9   R)   (    (    (    sC   /usr/lib/python2.7/dist-packages/twisted/internet/_dumbwin32proc.pyt   <module>   s,   				