o
    :ήc                     @   s  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mZmZmZmZmZ ddlmZ ddlmZ dd	lmZ d
eedd
d
dddeeeeeeef  f dee deee
e d
f deee
e d
f dedeeedd
f deeeef  deddfddZeeed
d
dddeeeeeeef  f deee
e d
f deee
e d
f deee
e d
f deeedd
f deeeef  dedefddZd
S )    )BytesIO)PathLike)DEVNULLPIPECalledProcessErrorCompletedProcess)	IOAnyAsyncIterableListMappingOptionalSequenceUnioncast   )Process   )get_asynclib)create_task_groupNTF)inputstdoutstderrcheckcwdenvstart_new_sessioncommandr   r   r   r   r   zPathLike[str]r   r   returnzCompletedProcess[bytes]c             
      s  dt t dtddf fdd}t| |rtnt|||||dI dH 4 I dH n}	ddg zRt 4 I dH >}
|	jr?|
||	jd |	j	rJ|
||	j	d	 |	j
r`|r`|	j
|I dH  |	j
 I dH  |	 I dH  W d  I dH  n1 I dH sww   Y  W n ty   |	   w W d  I dH  n1 I dH sw   Y   \}}|r|	jdkrttt|	j| ||t| tt|	j||S )
a  
    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: either :data:`subprocess.PIPE` or :data:`subprocess.DEVNULL`
    :param stderr: one of :data:`subprocess.PIPE`, :data:`subprocess.DEVNULL` or
        :data:`subprocess.STDOUT`
    :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 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)
    :return: an object representing the completed process
    :raises ~subprocess.CalledProcessError: if ``check`` is ``True`` and the process exits with a
        nonzero return code

    streamindexr   Nc                    s6   t  }| 2 z3 d H W }|| q6 |  |< d S )N)r   writegetvalue)r   r    bufferchunkstream_contents @/tmp/pip-target-vg8gfxp4/lib/python/anyio/_core/_subprocesses.pydrain_stream8   s   z!run_process.<locals>.drain_streamstdinr   r   r   r   r   r   r   )r
   bytesintopen_processr   r   r   r   
start_soonr   r+   sendaclosewaitBaseExceptionkill
returncoder   r   r   )r   r   r   r   r   r   r   r   r)   processtgoutputerrorsr'   r%   r(   run_process   sF   #

(
(r:   r*   r+   c             
      s.   t | t}t j| |||||||dI dH S )a  
    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 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)
    :return: an asynchronous process object

    )shellr+   r   r   r   r   r   N)
isinstancestrr   r.   )r   r+   r   r   r   r   r   r;   r'   r'   r(   r.   _   s   
r.   )ior   osr   
subprocessr   r   r   r   typingr   r	   r
   r   r   r   r   r   r   abcr   
_eventloopr   _tasksr   r=   r,   r-   boolr:   r.   r'   r'   r'   r(   <module>   sp    ,	

M	