ó
æNXc           @  s}  d  Z  d d l m Z d d l Z d d l Z d d l Z d d l m Z m Z d d l m	 Z	 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 e f d „  ƒ  YZ d „  Z d „  Z d „  Z d „  Z yj e j j j Z e	 e e ƒ g e _ e e	 ƒ e _  e j j! j" Z" e
 e" _# e
 g e" _ e$ e% d „ Z Wn e& k
 roe Z n Xd „  Z' d S(   s   Windows-specific implementation of process utilities.

This file is only meant to be imported by process.py, not by end-users.
iÿÿÿÿ(   t   print_functionN(   t   c_intt   POINTER(   t   LPCWSTRt   HLOCAL(   t   STDOUTi   (   t   read_no_interruptt   process_handlert	   arg_split(   t	   py3compat(   t   DEFAULT_ENCODINGt   AvoidUNCPathc           B  s    e  Z d  Z d „  Z d „  Z RS(   sÂ  A context manager to protect command execution from UNC paths.

    In the Win32 API, commands can't be invoked with the cwd being a UNC path.
    This context manager temporarily changes directory to the 'C:' drive on
    entering, and restores the original working directory on exit.

    The context manager returns the starting working directory *if* it made a
    change and None otherwise, so that users can apply the necessary adjustment
    to their system calls in the event of a change.

    Examples
    --------
    ::
        cmd = 'dir'
        with AvoidUNCPath() as path:
            if path is not None:
                cmd = '"pushd %s &&"%s' % (path, cmd)
            os.system(cmd)
    c         C  sI   t  j ƒ  |  _ |  j j d ƒ |  _ |  j rA t j d ƒ |  j Sd  Sd  S(   Ns   \\s   C:(   R	   t   getcwdt   patht
   startswitht   is_unc_patht   ost   chdirt   None(   t   self(    (    sT   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/utils/_process_win32.pyt	   __enter__8   s    	c         C  s    |  j  r t j |  j ƒ n  d  S(   N(   R   R   R   R   (   R   t   exc_typet	   exc_valuet	   traceback(    (    sT   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/utils/_process_win32.pyt   __exit__D   s    	(   t   __name__t
   __module__t   __doc__R   R   (    (    (    sT   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/utils/_process_win32.pyR   $   s   	c         C  sµ   y d d l  m } Wn t k
 r3 t d ƒ ‚ n~ Xt j d } d d d d g } d } x2 | D]* } y | | |  | ƒ d	 } Wq` q` Xq` W| d k r­ t d
 |  ƒ ‚ n | Sd S(   s?   Find the full path to a .bat or .exe using the win32api module.iÿÿÿÿ(   t
   SearchPaths3   you need to have pywin32 installed for this to workt   PATHs   .exes   .coms   .bats   .pyi    s   command %r not foundN(   t   win32apiR   t   ImportErrorR   t   environR   t   OSError(   t   cmdR   R   t
   extensionsR   t   ext(    (    sT   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/utils/_process_win32.pyt	   _find_cmdI   s    c         C  sš   t  } xB t |  j ƒ j ƒ  D]+ } | j | d ƒ } t | d t j ƒq WxB t |  j ƒ j ƒ  D]+ } | j | d ƒ } t | d t j ƒqa W|  j ƒ  S(   s   Callback for _system.t   replacet   file(	   R
   R   t   stdoutt
   splitlinest   decodet   printt   syst   stderrt   wait(   t   pt   enct   line(    (    sT   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/utils/_process_win32.pyt   _system_body^   s    c         C  sB   t  ƒ  3 } | d k	 r+ d | |  f }  n  t |  t ƒ SWd QXd S(   sÜ  Win32 version of os.system() that works with network shares.

    Note that this implementation returns None, as meant for use in IPython.

    Parameters
    ----------
    cmd : str or list
      A command to be executed in the system shell.

    Returns
    -------
    None : we explicitly do NOT return the subprocess status code, as this
    utility is meant to be used extensively in IPython, where any return value
    would trigger :func:`sys.displayhook` calls.
    s   "pushd %s &&"%sN(   R   R   R   R2   (   R"   R   (    (    sT   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/utils/_process_win32.pyt   systeml   s    c         C  sh   t  ƒ  ; } | d k	 r+ d | |  f }  n  t |  d „  t ƒ } Wd QX| d k r[ d } n  t j | ƒ S(   sû   Return standard output of executing cmd in a shell.

    Accepts the same arguments as os.system().

    Parameters
    ----------
    cmd : str or list
      A command to be executed in the system shell.

    Returns
    -------
    stdout : str
    s   "pushd %s &&"%sc         S  s   |  j  ƒ  d S(   Ni    (   t   communicate(   R/   (    (    sT   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/utils/_process_win32.pyt   <lambda>˜   s    Nt    (   R   R   R   R   R	   t   bytes_to_str(   R"   R   t   out(    (    sT   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/utils/_process_win32.pyt	   getoutput†   s    	c   	      C  s­   |  j  ƒ  d k r g  S| s2 t |  d | d | ƒSt ƒ  } t t j |  j ƒ  ƒ t j | ƒ ƒ } t	 | j
 } g  | j t j | j ƒ ƒ D] } | ^ q‹ } t | ƒ } | S(   s;  Split a command line's arguments in a shell-like manner.

        This is a special version for windows that use a ctypes call to CommandLineToArgvW
        to do the argv splitting. The posix paramter is ignored.
        
        If strict=False, process_common.arg_split(...strict=False) is used instead.
        R6   t   posixt   strict(   t   stript   py_arg_splitR   t   CommandLineToArgvWR	   t   cast_unicodet   lstript   ctypest   byrefR   t   valuet   from_addresst	   addressoft   contentst	   LocalFree(	   t   commandlineR:   R;   t   argvnt   result_pointert   result_array_typet   argt   resultt   retval(    (    sT   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/utils/_process_win32.pyR   ¦   s    		'.c         C  s   t  t j j j d d |  ƒ ƒ S(   Ni   i    (   t   boolRA   t   windllt   kernel32t   OpenProcess(   t   pid(    (    sT   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/utils/_process_win32.pyt	   check_pid½   s    ((   R   t
   __future__R    R   R,   RA   R   R   t   ctypes.wintypesR   R   t
   subprocessR   t   _process_commonR   R   R   R=   R6   R	   t   encodingR
   t   objectR   R%   R2   R3   R9   RP   t   shell32R>   t	   arg_typest   restypeRQ   RG   t   res_typet   Falset   Truet   AttributeErrorRT   (    (    (    sT   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/utils/_process_win32.pyt   <module>   s4   %					
