a
    ƹDf                     @   sd   d Z ddgZddlZddlZddlZddlZddlmZ G dd deZ	G dd de
Zedkr`dS )	a  
This module contains the base class for secure tunnel connections, and
describes the pathos tunnel interface.  See the following for an example.


Usage
=====

A typical call to a pathos 'tunnel' will roughly follow this example:

    >>> # instantiate the tunnel, providing it with a unique identifier
    >>> tunnel = Tunnel('tunnel')
    >>>
    >>> # establish a tunnel to the remote host and port
    >>> remotehost = 'remote.host.edu'
    >>> remoteport = 12345
    >>> localport = tunnel.connect(remotehost, remoteport)
    >>> print("Tunnel connected at local port: %s" % tunnel._lport)
    >>>
    >>> # pause script execution to maintain the tunnel (i.e. do something)
    >>> sys.stdin.readline()
    >>>
    >>> # tear-down the tunneled connection
    >>> tunnel.disconnect()
 
TunnelTunnelException    N)Pipec                   @   s   e Zd ZdZdS )r   z-Exception for failure to establish ssh tunnelN)__name__
__module____qualname____doc__ r	   r	   a/nfs/NAS7/SABIOD/METHODE/ermites/ermites_venv/lib/python3.9/site-packages/pathos/secure/tunnel.pyr   ,   s   c                   @   sR   e Zd ZdZdZdZdZdddZdd	 Zd
d Z	dddZ
dd ZdddZdS )r   z=a ssh-tunnel launcher for parallel and distributed computing.i   i  TNc           	   
   C   s   ddl m} |du r4ddlm} |r,||n||}|| j| j}| }|dk rXtdz| j||||d W nX ty } z@|jd dkr| 	  W Y d}~qBn| 
  tdW Y d}~n
d}~0 0 d	| _|S )
a   establish a secure shell tunnel between local and remote host

Input:
    host     -- remote hostname  [user@host:path is also valid]
    port     -- remote port number

Additional Input:
    through  -- 'tunnel-through' hostname  [default = None]
        r   )
portnumberN)
randomportzNo available local port)throughbindzConnection failedT)Zpathos.portpickerr   Zpathos.corer   MINPORTMAXPORTr   _connectargs
disconnect_Tunnel__disconnect	connected)	selfhostportr   r   r   Zpick	localporter	   r	   r
   connect7   s$    
zTunnel.connectc                 C   sH   | j dkrD| jrtd| j   t| j tj t| j d |   dS )zdestroy the ssh tunnelr   zKill ssh pid=%dN)	_pidverboseprintoskillsignalSIGTERMwaitpidr   r   r	   r	   r
   r   [   s    
zTunnel.disconnectc                 C   s"   d| _ d| _d| _d| _d| _dS )zdisconnect tunnel internalsr   FN)r   r   _lport_rport_hostr$   r	   r	   r
   Z__disconnecte   s    zTunnel.__disconnectc                    sZ   t j |du r*d fddtdD n|| _td| _|   |rV| jf i | dS )zocreate a ssh tunnel launcher

Inputs:
    name        -- a unique identifier (string) for the launcher
        N c                 3   s   | ]}t  V  qd S )N)randomchoice).0iZxyzr	   r
   	<genexpr>u       z"Tunnel.__init__.<locals>.<genexpr>   launcher)	stringascii_lettersjoinrangenamer   	_launcherr   r   )r   r6   kwdsr	   r-   r
   __init__n   s    
zTunnel.__init__c                 C   sP   | j sd| j S z"| jjddd d }W n   | jj}Y n0 d| S )NzTunnel('%s')    ")r   r6   r7   messagesplitrstrip)r   msgr	   r	   r
   __repr__}   s    
"zTunnel.__repr__c           	      C   s   d|||f }d}|r|}n|}| j |||dd | j   || _|| _|| _| j  | _| j  }|r|dr|t	dnt
| t	dd S )Nz-q -N -L %d:%s:%dr(   T)r   commandoptionsZ
backgroundr   Zfailure)r7   Zlaunchr%   r&   r'   pidr   response
startswithr   r   )	r   r   Z
remotehostZ
remoteportr   rD   rC   Zrhostliner	   r	   r
   r      s&    




zTunnel._connect)NN)N)N)r   r   r   r   r   r   r   r   r   r   r9   rB   r   r	   r	   r	   r
   r   0   s   
$
	
__main__)r   __all__r   r!   r)   r2   Zpathos.securer   	Exceptionr   objectr   r   r	   r	   r	   r
   <module>
   s   k