o
    û›hÜ  ã                   @   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z$e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w 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   s2   t  ¡ | _| j d¡| _| jrt  d¡ | jS d S )Nz\\zC:)ÚosÚgetcwdÚpathÚ
startswithÚis_unc_pathÚchdir)Úself© r   úRc:\Users\shubh\Videos\agent\nova\Lib\site-packages\IPython/utils/_process_win32.pyÚ	__enter__9   s   

zAvoidUNCPath.__enter__c                 C   s   | j rt | j¡ d S 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                    s`   t ‰ ‡ ‡fdd„}‡ ‡fdd„}t|d� ¡  t|d� ¡  	 ˆ ¡ }|du r-t d¡ n|S q)	zCallback for _system.c                     ó2   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   ó   þz!_system_body.<locals>.stdout_readc                     r"   r#   )r
   Ústderrr'   r(   r)   r*   r+   r-   r   r   Ústderr_readS   r1   z!_system_body.<locals>.stderr_read)ÚtargetTNg{®Gáz„?)r   r   ÚstartÚpollÚtimeÚsleep)r/   r0   r3   Úresultr   r-   r   Ú_system_bodyJ   s   ûr:   c                 C   sH   t ƒ �}|durd|| f } t| tƒW  d  ƒ S 1 sw   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   r:   )Úcmdr   r   r   r   Úsystemf   s
   $ýr=   c                 C   s`   t ƒ �}|durd|| f } t| dd„ tƒ}W d  ƒ n1 s w   Y  |du r+d}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
    Nr;   c                 S   s   |   ¡ d S )Nr   )Úcommunicate)r/   r   r   r   Ú<lambda>�   s    zgetoutput.<locals>.<lambda>ó    )r   r   r   r   r(   )r<   r   Úoutr   r   r   Ú	getoutput~   s   ý
rB   FTc                 C   sn   |   ¡ dkrg S |st| ||d�S 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>¯   s    zarg_split.<locals>.<listcomp>)ÚstripÚpy_arg_splitr   ÚCommandLineToArgvWÚlstripÚctypesÚbyrefr   ÚvalueÚfrom_addressÚ	addressofÚcontentsÚ	LocalFree)ZcommandlinerD   rE   ZargvnZresult_pointerZresult_array_typer9   Úretvalr   r   r   r   ž   s   	
r   c                 C   s   t tjj dd| ¡ƒS )Nr	   r   )ÚboolrM   ÚwindllÚkernel32ÚOpenProcess)Úpidr   r   r   Ú	check_pidµ   s   rZ   )FT)'r!   r   r*   rM   r7   r   r   Zctypes.wintypesr   r   Ú
subprocessr   r   Ú	threadingr   Z_process_commonr
   r   r   rJ   rC   r   Úencodingr   Úobjectr   r:   r=   rB   rV   Úshell32rK   Z	arg_typesÚrestyperW   rS   Zres_typeÚAttributeErrorrZ   r   r   r   r   Ú<module>   s8    %


ÿ