a
    ø£Beô  ã                   @   s"  d Z ddl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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 G dd„ deƒZdd„ Zdd„ Zdd„ ZzHejjjZeeeƒge_ eeƒe_!ejj"j#Z#e	e#_$e	ge#_ ddd„ZW n e%�y   eZY n0 dd„ Z&dS )zWindows-specific implementation of process utilities.

This file is only meant to be imported by process.py, not by end-users.
é    N)Úc_intÚPOINTER)ÚLPCWSTRÚHLOCAL)ÚSTDOUTÚTimeoutExpired)ÚThreadé   )Úread_no_interruptÚprocess_handlerÚ	arg_split)Ú	py3compat)ÚDEFAULT_ENCODINGc                   @   s    e Zd ZdZdd„ Zdd„ ZdS )ÚAvoidUNCPathaÂ  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   s6   t  ¡ | _| j d¡| _| jr.t  d¡ | jS d S d S )Nz\\zC:)ÚosÚgetcwdÚpathÚ
startswithÚis_unc_pathÚchdir)Úself© r   ú_/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/IPython/utils/_process_win32.pyÚ	__enter__9   s    

zAvoidUNCPath.__enter__c                 C   s   | j rt | j¡ d S )N)r   r   r   r   )r   Úexc_typeÚ	exc_valueÚ	tracebackr   r   r   Ú__exit__E   s    zAvoidUNCPath.__exit__N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r   r   %   s   r   c                    sb   t ‰ ‡ ‡fdd„}‡ ‡fdd„}t|d� ¡  t|d� ¡  ˆ ¡ }|du rXt d¡ q<|S q<dS )zCallback for _system.c                     s2   t ˆjƒ ¡ D ]} |  ˆ d¡} t| tjd� qd S ©NÚreplace)Úfile)r
   ÚstdoutÚ
splitlinesÚdecodeÚprintÚsys©Úline©ÚencÚpr   r   Ústdout_readN   s    z!_system_body.<locals>.stdout_readc                     s2   t ˆjƒ ¡ D ]} |  ˆ d¡} t| tjd� qd S r"   )r
   Ústderrr&   r'   r(   r)   r*   r,   r   r   Ústderr_readS   s    z!_system_body.<locals>.stderr_read)ÚtargetNg{®Gáz„?)r   r   ÚstartÚpollÚtimeÚsleep)r.   r/   r1   Úresultr   r,   r   Ú_system_bodyJ   s    r8   c                 C   sH   t ƒ �.}|durd|| f } t| tƒW  d  ƒ S 1 s:0    Y  dS )a7  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
    -------
    int : child process' exit code.
    Nú"pushd %s &&"%s)r   r   r8   )Úcmdr   r   r   r   Úsystemf   s    r;   c                 C   s`   t ƒ �4}|durd|| f } t| dd„ tƒ}W d  ƒ n1 s@0    Y  |du rVd}t |¡S )zý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
    Nr9   c                 S   s   |   ¡ d S )Nr   )Úcommunicate)r.   r   r   r   Ú<lambda>�   ó    zgetoutput.<locals>.<lambda>r>   )r   r   r   r   r'   )r:   r   Úoutr   r   r   Ú	getoutput~   s    .r@   FTc                 C   st   |   ¡ dkrg S |s"t| ||d�S tƒ }tt |  ¡ ¡t |¡ƒ}t	|j
 }dd„ | t |j¡¡D ƒ}t|ƒ}|S )a4  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 parameter is ignored.

        If strict=False, process_common.arg_split(...strict=False) is used instead.
        Ú )ÚposixÚstrictc                 S   s   g | ]}|‘qS r   r   )Ú.0Úargr   r   r   Ú
<listcomp>¯   r>   zarg_split.<locals>.<listcomp>)ÚstripÚpy_arg_splitr   ÚCommandLineToArgvWr   Zcast_unicodeÚlstripÚctypesZbyrefr   ÚvalueZfrom_addressZ	addressofÚcontentsÚ	LocalFree)ÚcommandlinerB   rC   ZargvnZresult_pointerZresult_array_typer7   Úretvalr   r   r   r   ž   s    	
r   c                 C   s   t tjj dd| ¡ƒS )Nr	   r   )ÚboolrK   ÚwindllÚkernel32ZOpenProcess)Úpidr   r   r   Ú	check_pidµ   s    rU   )FT)'r!   r   r)   rK   r5   r   r   Zctypes.wintypesr   r   Ú
subprocessr   r   Ú	threadingr   Z_process_commonr
   r   r   rH   rA   r   Úencodingr   Úobjectr   r8   r;   r@   rR   Zshell32rI   Z	arg_typesÚrestyperS   rN   Zres_typeÚAttributeErrorrU   r   r   r   r   Ú<module>   s4   %



