U
    -e                     @   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%k
r   eZY nX 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   ]/var/www/html/Darija-Ai-Train/env/lib/python3.8/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k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{Gz?)r   r   startpolltimesleep)r.   r/   r1   resultr   r,   r   _system_bodyJ   s    r8   c              
   C   s<   t  ,}|dk	rd|| f } t| tW  5 Q R  S Q R X 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   sL   t  *}|dk	rd|| f } t| dd t}W 5 Q R X |dkrBd}t|S )zReturn 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>   s     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   %



