U
    Rhz                      @  sn  U d dl mZ d dlZd dlmZmZmZmZ d dlm	Z	 d dl
mZ d dlmZmZmZmZ d dlmZmZmZmZ dd	lmZ d
dlmZ d
dlmZ ejdkrd dlmZ nd dlmZ eee ddf Z!de"d< deeddddd dddddddddddddddddd d!d!d"dd#d$d%d&Z#eeedddd ddddddd'dddddddddd d!d!d"dd(d)d*d+Z$dS ),    )annotationsN)AsyncIterableIterableMappingSequence)BytesIO)PathLike)DEVNULLPIPECalledProcessErrorCompletedProcess)IOAnyUnioncast   )Process   )get_async_backend)create_task_group)   
   )	TypeAliaszPathLike[str]zPathLike[bytes]r   StrOrBytesPathTF )inputstdoutstderrcheckcwdenvstartupinfocreationflagsstart_new_sessionpass_fdsusergroupextra_groupsumaskz)StrOrBytesPath | Sequence[StrOrBytesPath]zbytes | Nonezint | IO[Any] | NoneboolzStrOrBytesPath | NonezMapping[str, str] | Noner   intzSequence[int]zstr | int | NonezIterable[str | int] | NonezCompletedProcess[bytes])commandr   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   returnc                  s6  dddd fdd}t | |r"tnt|||||||	|
||||dI dH 4 I dH }ddg t 4 I dH l}|jr|||jd	 |jr|||jd
 |jr|r|j|I dH  |j	 I dH  |
 I dH  W 5 Q I dH R X W 5 Q I dH R X  \}}|r |jd	kr ttt|j| ||t| tt|j||S )aq  
    Run an external command in a subprocess and wait until it completes.

    .. seealso:: :func:`subprocess.run`

    :param command: either a string to pass to the shell, or an iterable of strings
        containing the executable name or path and its arguments
    :param input: bytes passed to the standard input of the subprocess
    :param stdout: one of :data:`subprocess.PIPE`, :data:`subprocess.DEVNULL`,
        a file-like object, or `None`
    :param stderr: one of :data:`subprocess.PIPE`, :data:`subprocess.DEVNULL`,
        :data:`subprocess.STDOUT`, a file-like object, or `None`
    :param check: if ``True``, raise :exc:`~subprocess.CalledProcessError` if the
        process terminates with a return code other than 0
    :param cwd: If not ``None``, change the working directory to this before running the
        command
    :param env: if not ``None``, this mapping replaces the inherited environment
        variables from the parent process
    :param startupinfo: an instance of :class:`subprocess.STARTUPINFO` that can be used
        to specify process startup parameters (Windows only)
    :param creationflags: flags that can be used to control the creation of the
        subprocess (see :class:`subprocess.Popen` for the specifics)
    :param start_new_session: if ``true`` the setsid() system call will be made in the
        child process prior to the execution of the subprocess. (POSIX only)
    :param pass_fds: sequence of file descriptors to keep open between the parent and
        child processes. (POSIX only)
    :param user: effective user to run the process as (Python >= 3.9, POSIX only)
    :param group: effective group to run the process as (Python >= 3.9, POSIX only)
    :param extra_groups: supplementary groups to set in the subprocess (Python >= 3.9,
        POSIX only)
    :param umask: if not negative, this umask is applied in the child process before
        running the given command (Python >= 3.9, POSIX only)
    :return: an object representing the completed process
    :raises ~subprocess.CalledProcessError: if ``check`` is ``True`` and the process
        exits with a nonzero return code

    zAsyncIterable[bytes]r+   None)streamindexr-   c                   s4   t  }| 2 z3 d H W }|| q
6 |  |< d S )N)r   writegetvalue)r/   r0   bufferchunkZstream_contentsr   ?./venv/lib/python3.8/site-packages/anyio/_core/_subprocesses.pydrain_streamN   s    z!run_process.<locals>.drain_streamstdinr   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   Nr   r   )open_processr
   r	   r   r   Z
start_soonr   r9   sendaclosewait
returncoder   r   r+   r   )r,   r   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r7   Zprocesstgoutputerrorsr   r5   r6   run_process   s@    8

.rB   r8   r   )r,   r9   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r-   c                  s   i }|
dk	r&t jdk rtd|
|d< |dk	rHt jdk r@td||d< |dk	rjt jdk rbtd||d< |d	krt jdk rtd
||d< t j| f|||||||||	d	|I dH S )ac  
    Start an external command in a subprocess.

    .. seealso:: :class:`subprocess.Popen`

    :param command: either a string to pass to the shell, or an iterable of strings
        containing the executable name or path and its arguments
    :param stdin: one of :data:`subprocess.PIPE`, :data:`subprocess.DEVNULL`, a
        file-like object, or ``None``
    :param stdout: one of :data:`subprocess.PIPE`, :data:`subprocess.DEVNULL`,
        a file-like object, or ``None``
    :param stderr: one of :data:`subprocess.PIPE`, :data:`subprocess.DEVNULL`,
        :data:`subprocess.STDOUT`, a file-like object, or ``None``
    :param cwd: If not ``None``, the working directory is changed before executing
    :param env: If env is not ``None``, it must be a mapping that defines the
        environment variables for the new process
    :param creationflags: flags that can be used to control the creation of the
        subprocess (see :class:`subprocess.Popen` for the specifics)
    :param startupinfo: an instance of :class:`subprocess.STARTUPINFO` that can be used
        to specify process startup parameters (Windows only)
    :param start_new_session: if ``true`` the setsid() system call will be made in the
        child process prior to the execution of the subprocess. (POSIX only)
    :param pass_fds: sequence of file descriptors to keep open between the parent and
        child processes. (POSIX only)
    :param user: effective user to run the process as (Python >= 3.9; POSIX only)
    :param group: effective group to run the process as (Python >= 3.9; POSIX only)
    :param extra_groups: supplementary groups to set in the subprocess (Python >= 3.9;
        POSIX only)
    :param umask: if not negative, this umask is applied in the child process before
        running the given command (Python >= 3.9; POSIX only)
    :return: an asynchronous process object

    N)r   	   z0the 'user' argument requires Python 3.9 or laterr&   z1the 'group' argument requires Python 3.9 or laterr'   z8the 'extra_groups' argument requires Python 3.9 or laterr(   r   z1the 'umask' argument requires Python 3.9 or laterr)   )	r9   r   r   r    r!   r"   r#   r$   r%   )sysversion_info	TypeErrorr   r:   )r,   r9   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   kwargsr   r   r6   r:   z   s@    2



r:   )%Z
__future__r   rD   Zcollections.abcr   r   r   r   ior   osr   
subprocessr	   r
   r   r   typingr   r   r   r   abcr   Z
_eventloopr   Z_tasksr   rE   r   Ztyping_extensionsstrbytesr   __annotations__rB   r:   r   r   r   r6   <module>   sV    
0g