o
    "ift                     @  sH  d Z ddlmZ ddlZddlZddlmZ ddlmZ er0ddlm	Z	 ddlm
Z
 ddlmZ zdd	lmZ dd
lmZ W n eyS   dd	lmZ dd
lmZ Y nw ddlmZ G dd deZdZd/ddZd0ddZd0ddZd0ddZd1dd Zd!Zd"Zd#Zd$ZG d%d& d&ZG d'd( d(Zd2d,d-Z e!d.kre e" dS dS )3zKexecnet IO initialization code.

Creates IO instances used for gateway IO.
    )annotationsN)TYPE_CHECKING)cast)Channel)	ExecModel)XSpec)Message)Popen2IO)partialc                      s<   e Zd ZU ded< d fddZdd
dZdddZ  ZS )Popen2IOMasterstrremoteaddress	execmodelr   returnNonec                   s:   |j j}|j j|||d | _}t j|j|j|d d S )N)stdoutstdin)r   )
subprocessPIPEPopenpopensuper__init__r   r   )selfargsr   r   p	__class__ K/var/www/html/corbot_env/lib/python3.10/site-packages/execnet/gateway_io.pyr       s   zPopen2IOMaster.__init__
int | Nonec                 C  s"   z| j  W S  ty   Y d S w N)r   waitOSErrorr   r   r   r   r"   %   s
   zPopen2IOMaster.waitc              
   C  sR   z| j   W d S  ty( } ztjd|  tj  W Y d }~d S d }~ww )NzERROR killing: %s
)r   killr#   sysstderrwriteflush)r   er   r   r   r%   +   s   zPopen2IOMaster.kill)r   r   r   r   r   r    r   r   )__name__
__module____qualname____annotations__r   r"   r%   __classcell__r   r   r   r   r      s
   
 
r   z+import sys;exec(eval(sys.stdin.readline()))pathr   r   	list[str]c                 C  s"   t jdr| dd} t| S )z}
    Use shell lexer to split the given path into a list of components,
    taking care to handle Windows' '' correctly.
    win\/)r&   platform
startswithreplaceshlexsplit)r2   r   r   r   shell_split_path6   s   
r<   specr   c                 C  sD   | j rt| j ntjg}|d | jr|d |dtg |S )Nz-uz-Bz-c)pythonr<   r&   
executableappenddont_write_bytecodeextendpopen_bootstrapline)r=   r   r   r   r   
popen_argsA   s   

rD   c                 C  sl   | j pd}ddg}| jd ur|dt| jg | jd usJ || j  | dt d}|| |S )Nr>   ssh-C-F -c "")r>   
ssh_configrB   r   rE   r;   rC   r@   r=   remotepythonr   	remotecmdr   r   r   ssh_argsJ   s   


rN   c                 C  sf   | j d usJ | jpd}dd| j ddg}| jd ur#|dt| jg | dt d}||g |S )	Nr>   vagrantrE   z--rF   rG   rH   rI   )vagrant_sshr>   rJ   rB   r   rC   rK   r   r   r   vagrant_ssh_argsZ   s   

rQ   r   r   c                 C  sd   | j rt| }t||S | jrt| }t||}| j|_|S | jr0t| }t||}| j|_|S J r!   )r   rD   r   rE   rN   r   rP   rQ   )r=   r   r   ior   r   r   	create_iok   s   


rS               c                   @  sx   e Zd ZdZd%dd	Zd&ddZd'ddZd(ddZd)ddZd)ddZ	d)ddZ
d*ddZed+d d!Zd+d"d#Zd$S ),ProxyIOaL  A Proxy IO object allows to instantiate a Gateway
    through another "via" gateway.

    A master:ProxyIO object provides an IO object effectively connected to the
    sub via the forwarder. To achieve this, master:ProxyIO interacts with
    forwarder:serve_proxy_io() which itself instantiates and interacts with the
    sub.
    proxy_channelr   r   r   r   r   c                 C  s6   |j  | _|| j || _| jd| _|| _d S )Nr)gateway
newchannelcontrolchansendiochanmakefileiochan_filer   )r   rY   r   r   r   r   r      s
   
zProxyIO.__init__nbytesintbytesc                 C  s   | j |S r!   )ra   read)r   rb   r   r   r   re      s   zProxyIO.readdatac                 C     | j | d S r!   )r_   r^   )r   rf   r   r   r   r(         zProxyIO.writeeventobjectc                 C  s   | j | | j  S r!   )r]   r^   receive)r   ri   r   r   r   	_controll   s   
zProxyIO._controllc                 C     |  t d S r!   )rl   RIO_CLOSE_WRITEr$   r   r   r   close_write      zProxyIO.close_writec                 C  s   t  r!   )NotImplementedErrorr$   r   r   r   
close_read   s   zProxyIO.close_readc                 C  rm   r!   )rl   RIO_KILLr$   r   r   r   r%      rp   zProxyIO.killr    c                 C  s$   |  t}|d u st|tsJ |S r!   )rl   RIO_WAIT
isinstancerc   r   responser   r   r   r"      s   
zProxyIO.waitr   c                 C  s   |  t}t|tsJ |S r!   )rl   RIO_REMOTEADDRESSru   r   rv   r   r   r   r      s   
zProxyIO.remoteaddressc                 C  s   d| j jj dS )Nz<RemoteIO via >)r_   r[   idr$   r   r   r   __repr__   s   zProxyIO.__repr__N)rY   r   r   r   r   r   )rb   rc   r   rd   rf   rd   r   r   )ri   rc   r   rj   r,   r+   )r   r   )r-   r.   r/   __doc__r   re   r(   rl   ro   rr   r%   r"   propertyr   r{   r   r   r   r   rX      s    
	
	





rX   c                   @  s    e Zd Zd
ddZdddZd	S )
PseudoSpecr   r   c                 C  rg   r!   )__dict__update)r   varsr   r   r   r      rh   zPseudoSpec.__init__namer   c                 C  s   d S r!   r   )r   r   r   r   r   __getattr__   s   zPseudoSpec.__getattr__Nr,   )r   r   r   r   )r-   r.   r/   r   r   r   r   r   r   r      s    
r   proxy_channelXr   r   c                   s  | j j}t| j jd| j tdt|  }t||td|   d  dfd	d
}| 	| d fdd} 	| | 
d}d|j d}|dksZJ |d|j || 	 zt}W n ty}   d|j Y d S w || qf)Nzserve_proxy_io:%sr   r   zgot control chanrf   rd   r   r   c                   s    dt |   |  d S )Nzforward data to sub, size %s)lenr(   rf   )logsub_ior   r   forward_to_sub   s   z&serve_proxy_io.<locals>.forward_to_subrc   c                   sr   | t kr   d S | tkr   d  d S | tkr( j d S | tkr7   d  d S d S r!   )	rt   r^   r"   rs   r%   rx   r   rn   ro   r   )control_chanr   r   r   control   s   zserve_proxy_io.<locals>.controlwzreading bootstrap byte from subrT      1z"forwarding bootstrap byte from subTz'EOF from sub, terminating proxying loopr|   )rf   rc   r   r   )r[   r   r
   _tracerz   r   r   rk   rS   setcallbackr`   re   r(   r   from_ioEOFErrorto_io)r   r   r=   r   r   forward_to_master_fileinitialmessager   )r   r   r   r   serve_proxy_io   s6   







r   __channelexec__)r2   r   r   r3   )r=   r   r   r3   )r=   r   r   r   r   r   )r   r   r   r   )#r}   
__future__r   r:   r&   typingr   r   execnet.gateway_baser   r   execnet.xspecr   r   r	   ImportError__main__	functoolsr
   r   rC   r<   rD   rN   rQ   rS   rs   rt   rx   rn   rX   r   r   r-   channelr   r   r   r   <module>   sF    


	

7
6