o
    éT•jí  ã                   @   s¬   d Z ddlZddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ej	Z	ej
Z
ejZG d	d
„ d
ejejƒZG dd„ dƒZdddejfdd„Zdddejdœdd„ZdS ))Úcreate_subprocess_execÚcreate_subprocess_shellé    Né   )Úevents)Ú	protocols)Ústreams)Útasks)Úloggerc                       sX   e Zd ZdZ‡ fdd„Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dd„ Z
dd„ Z‡  ZS )ÚSubprocessStreamProtocolz0Like StreamReaderProtocol, but for a subprocess.c                    sH   t ƒ j|d� || _d  | _ | _| _d | _d| _g | _| j	 
¡ | _d S )N)ÚloopF)ÚsuperÚ__init__Ú_limitÚstdinÚstdoutÚstderrÚ
_transportÚ_process_exitedÚ	_pipe_fdsÚ_loopÚcreate_futureÚ_stdin_closed)ÚselfÚlimitr   ©Ú	__class__© ú)/usr/lib/python3.10/asyncio/subprocess.pyr      s   z!SubprocessStreamProtocol.__init__c                 C   sn   | j jg}| jd ur| d| j›�¡ | jd ur!| d| j›�¡ | jd ur/| d| j›�¡ d d |¡¡S )Nzstdin=zstdout=zstderr=z<{}>ú )r   Ú__name__r   Úappendr   r   ÚformatÚjoin)r   Úinfor   r   r   Ú__repr__   s   



z!SubprocessStreamProtocol.__repr__c                 C   s²   || _ | d¡}|d ur"tj| j| jd�| _| j |¡ | j 	d¡ | d¡}|d urAtj| j| jd�| _
| j
 |¡ | j 	d¡ | d¡}|d urWtj|| d | jd�| _d S d S )Nr   ©r   r   é   r   )ÚprotocolÚreaderr   )r   Úget_pipe_transportr   ÚStreamReaderr   r   r   Úset_transportr   r    r   ÚStreamWriterr   )r   Ú	transportÚstdout_transportÚstderr_transportÚstdin_transportr   r   r   Úconnection_made(   s.   
ÿ
ÿ
ýÿz(SubprocessStreamProtocol.connection_madec                 C   s>   |dkr| j }n
|dkr| j}nd }|d ur| |¡ d S d S ©Nr   r&   )r   r   Ú	feed_data)r   ÚfdÚdatar(   r   r   r   Úpipe_data_received@   s   ÿz+SubprocessStreamProtocol.pipe_data_receivedc                 C   sº   |dkr(| j }|d ur| ¡  |  |¡ |d u r | j d ¡ d S | j |¡ d S |dkr0| j}n
|dkr8| j}nd }|d urL|d u rG| ¡  n| |¡ || j	v rW| j	 
|¡ |  ¡  d S )Nr   r   r&   )r   ÚcloseÚconnection_lostr   Ú
set_resultÚset_exceptionr   r   Úfeed_eofr   ÚremoveÚ_maybe_close_transport)r   r4   ÚexcÚpiper(   r   r   r   Úpipe_connection_lostJ   s,   
ÿ


z-SubprocessStreamProtocol.pipe_connection_lostc                 C   s   d| _ |  ¡  d S )NT)r   r=   ©r   r   r   r   Úprocess_exitede   s   z'SubprocessStreamProtocol.process_exitedc                 C   s0   t | jƒdkr| jr| j ¡  d | _d S d S d S )Nr   )Úlenr   r   r   r7   rA   r   r   r   r=   i   s   

þz/SubprocessStreamProtocol._maybe_close_transportc                 C   s   || j u r| jS d S ©N)r   r   )r   Ústreamr   r   r   Ú_get_close_waitern   s   
ÿz*SubprocessStreamProtocol._get_close_waiter)r   Ú
__module__Ú__qualname__Ú__doc__r   r$   r1   r6   r@   rB   r=   rF   Ú__classcell__r   r   r   r   r
      s    	

r
   c                   @   sj   e Zd Zdd„ Zdd„ Zedd„ ƒZdd„ Zd	d
„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zddd„ZdS )ÚProcessc                 C   s8   || _ || _|| _|j| _|j| _|j| _| ¡ | _d S rD   )r   Ú	_protocolr   r   r   r   Úget_pidÚpid)r   r-   r'   r   r   r   r   r   t   s   zProcess.__init__c                 C   s   d| j j› d| j› d�S )Nú<r   ú>)r   r   rN   rA   r   r   r   r$   }   s   zProcess.__repr__c                 C   s
   | j  ¡ S rD   )r   Úget_returncoderA   r   r   r   Ú
returncode€   s   
zProcess.returncodec                 Ã   s   �| j  ¡ I dH S )z?Wait until the process exit and return the process return code.N)r   Ú_waitrA   r   r   r   Úwait„   s   €zProcess.waitc                 C   s   | j  |¡ d S rD   )r   Úsend_signal)r   Úsignalr   r   r   rU   ˆ   s   zProcess.send_signalc                 C   ó   | j  ¡  d S rD   )r   Ú	terminaterA   r   r   r   rX   ‹   ó   zProcess.terminatec                 C   rW   rD   )r   ÚkillrA   r   r   r   rZ   Ž   rY   zProcess.killc              
   Ã   sœ   �| j  ¡ }| j |¡ |rt d| t|ƒ¡ z
| j ¡ I d H  W n tt	fy> } z|r4t d| |¡ W Y d }~nd }~ww |rGt d| ¡ | j 
¡  d S )Nz%%r communicate: feed stdin (%s bytes)z%r communicate: stdin got %rz%r communicate: close stdin)r   Ú	get_debugr   Úwriter	   ÚdebugrC   ÚdrainÚBrokenPipeErrorÚConnectionResetErrorr7   )r   Úinputr]   r>   r   r   r   Ú_feed_stdin‘   s"   €

ÿ€ýzProcess._feed_stdinc                 Ã   s   �d S rD   r   rA   r   r   r   Ú_noop¢   s   €zProcess._noopc                 Ã   sš   �| j  |¡}|dkr| j}n	|dksJ ‚| j}| j ¡ r,|dkr#dnd}t d| |¡ | ¡ I d H }| j ¡ rG|dkr>dnd}t d| |¡ | 	¡  |S )Nr&   r   r   r   z%r communicate: read %sz%r communicate: close %s)
r   r)   r   r   r   r[   r	   r]   Úreadr7   )r   r4   r-   rE   ÚnameÚoutputr   r   r   Ú_read_stream¥   s   €

zProcess._read_streamNc                 Ã   sŠ   �|d ur|   |¡}n|  ¡ }| jd ur|  d¡}n|  ¡ }| jd ur)|  d¡}n|  ¡ }t |||¡I d H \}}}|  ¡ I d H  ||fS r2   )rb   rc   r   rg   r   r   ÚgatherrT   )r   ra   r   r   r   r   r   r   Úcommunicate¶   s   €

zProcess.communicaterD   )r   rG   rH   r   r$   ÚpropertyrR   rT   rU   rX   rZ   rb   rc   rg   ri   r   r   r   r   rK   s   s    	
rK   c           	      ‹   sJ   �t  ¡ ‰‡ ‡fdd„}ˆj|| f|||dœ|¤ŽI d H \}}t||ˆƒS )Nc                      ó   t ˆ ˆd�S ©Nr%   ©r
   r   r%   r   r   Ú<lambda>Ë   ó    ÿz)create_subprocess_shell.<locals>.<lambda>©r   r   r   )r   Úget_running_loopÚsubprocess_shellrK   )	Úcmdr   r   r   r   ÚkwdsÚprotocol_factoryr-   r'   r   r%   r   r   È   s   €þýýr   )r   r   r   r   c          
      �   sP   �t  ¡ ‰‡ ‡fdd„}ˆj|| g|¢R |||dœ|¤ŽI d H \}}	t||	ˆƒS )Nc                      rk   rl   rm   r   r%   r   r   rn   Ø   ro   z(create_subprocess_exec.<locals>.<lambda>rp   )r   rq   Úsubprocess_execrK   )
Úprogramr   r   r   r   Úargsrt   ru   r-   r'   r   r%   r   r   Ô   s   €þþüür   )Ú__all__Ú
subprocessÚ r   r   r   r   Úlogr	   ÚPIPEÚSTDOUTÚDEVNULLÚFlowControlMixinÚSubprocessProtocolr
   rK   Ú_DEFAULT_LIMITr   r   r   r   r   r   Ú<module>   s(    ÿbU
ÿÿ