o
    jZh                     @   s   d Z ddlZddlZddlZz
ddlmZmZ W n ey)   ddlmZmZ Y nw ddl	m
Z
 G dd deZdd Zd	Zd
d Zdd Zdd Zdd Zdd ZdZdZdZdZG dd dZG dd dZdd Zedkrvee dS dS )zJ
execnet io initialization code

creates io instances used for gateway io
    N)Popen2IOMessage)partialc                       s,   e Zd Z fddZdd Zdd Z  ZS )Popen2IOMasterc                    s:   |j j}|j j|||d | _}t j|j|j|d d S )N)stdoutstdin)	execmodel)
subprocessPIPEPopenpopensuper__init__r   r   )selfargsr   r
   p	__class__ I/var/www/html/lang_env/lib/python3.10/site-packages/execnet/gateway_io.pyr      s   zPopen2IOMaster.__init__c                 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   s   t | j d S r   )	killpopenr   r   r   r   r   kill      zPopen2IOMaster.kill)__name__
__module____qualname__r   r   r   __classcell__r   r   r   r   r      s    r   c              
   C   sP   z|    W d S  ty' } ztjd|  tj  W Y d }~d S d }~ww )NzERROR killing: %s
)r   r   sysstderrwriteflush)r   er   r   r   r   "   s   r   z+import sys;exec(eval(sys.stdin.readline()))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)pathr   r   r   shell_split_path-   s   
r/   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)specr   r   r   r   
popen_args8   s   

r7   c                 C   s^   | j pd}ddg}| jd ur|dt| jg || j  | dt d}|| |S )Nr0   ssh-C-F -c "")r0   
ssh_configr4   strr8   r-   r5   r2   r6   Zremotepythonr   Z	remotecmdr   r   r   ssh_argsA   s   


r@   c                 C   sX   | j pd}dd| jddg}| jd ur|dt| jg | dt d}||g |S )	Nr0   Zvagrantr8   z--r9   r:   r;   r<   )r0   vagrant_sshr=   r4   r>   r5   r?   r   r   r   vagrant_ssh_argsP   s   

rB   c                 C   sd   | j rt| }t||S | jrt| }t||}| j|_|S | jr0t| }t||}| j|_|S d S r   )r   r7   r   r8   r@   remoteaddressrA   rB   )r6   r   r   ior   r   r   	create_io`   s   


rE               c                   @   s\   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dd Z
edd Zdd ZdS )ProxyIOaM  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.
    c                 C   s6   |j  | _|| j || _| jd| _|| _d S )Nr)gatewayZ
newchannelcontrolchansendiochanmakefileiochan_filer   )r   Zproxy_channelr   r   r   r   r      s
   
zProxyIO.__init__c                 C      | j |S r   )rQ   read)r   nbytesr   r   r   rS         zProxyIO.readc                 C   rR   r   )rO   rN   )r   datar   r   r   r#      rU   zProxyIO.writec                 C   s   | j | | j  S r   )rM   rN   receive)r   eventr   r   r   	_controll   s   
zProxyIO._controllc                 C      |  t d S r   )rY   RIO_CLOSE_WRITEr   r   r   r   close_write   r   zProxyIO.close_writec                 C   rZ   r   )rY   RIO_KILLr   r   r   r   r      r   zProxyIO.killc                 C   
   |  tS r   )rY   RIO_WAITr   r   r   r   r      s   
zProxyIO.waitc                 C   r^   r   )rY   RIO_REMOTEADDRESSr   r   r   r   rC      s   
zProxyIO.remoteaddressc                 C   s   d| j jj dS )Nz<RemoteIO via >)rO   rL   idr   r   r   r   __repr__   s   zProxyIO.__repr__N)r   r   r   __doc__r   rS   r#   rY   r\   r   r   propertyrC   rc   r   r   r   r   rJ   }   s    	
rJ   c                   @   s   e Zd Zdd Zdd ZdS )
PseudoSpecc                 C   s   | j | d S r   )__dict__update)r   varsr   r   r   r      s   zPseudoSpec.__init__c                 C   s   d S r   r   )r   namer   r   r   __getattr__   s   zPseudoSpec.__getattr__N)r   r   r   r   rk   r   r   r   r   rf      s    rf   c                    s   | j j}t| j jd| j t|  }t|||   d  fdd}| |  fdd} | | 	d}d|j 
d	}|d
ksRJ |d|j || 	 zt}W n tyu   d|j Y d S w || q^)Nzserve_proxy_io:%szgot control chanc                    s    dt |   |  d S )Nzforward data to sub, size %s)lenr#   rV   )logsub_ior   r   forward_to_sub   s   z&serve_proxy_io.<locals>.forward_to_subc                    sj   | t kr   d S | tkr   d S | tkr& j d S | tkr3   d S d S r   )	r_   rN   r   r]   r   r`   rC   r[   r\   rm   )control_chanro   r   r   control   s   zserve_proxy_io.<locals>.controlwzreading bootstrap byte from subrF      1z"forwarding bootstrap byte from subTz'EOF from sub, terminating proxying loop)rL   r   r   _tracerb   rf   rW   rE   ZsetcallbackrP   rS   r#   r   Zfrom_ioEOFErrorZto_io)Zproxy_channelXr   r6   rp   rr   Zforward_to_master_fileinitialmessager   )rq   rn   ro   r   serve_proxy_io   s6   








ry   Z__channelexec__)rd   osr,   r!   Zexecnet.gateway_baser   r   ImportError__main__	functoolsr   r   r   r5   r/   r7   r@   rB   rE   r]   r_   r`   r[   rJ   rf   ry   r   Zchannelr   r   r   r   <module>   s8    	-4