
    ii                        d Z ddlmZ ddlZddlZddl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 dd
lmZ ddlmZ ddlmZ ddlmZ  G d dej                  Z G d d          ZeZddZddZd dZdS )!zhGateway code for initiating popen, socket and ssh connections.

(c) 2004-2013, Holger Krekel and others
    )annotationsN)TYPE_CHECKING)Any)Callable   )gateway_base)IO)Channel)Message)Group)XSpecc                       e Zd ZU dZded<   d% fd
Zed&d            Zd&dZd'dZ		 d(d)dZ
d*d+dZd,dZd-dZd.d Zd/d0d$Z xZS )1Gatewayz0Gateway to a local or remote Python Interpreter.r   _groupior	   specr   returnNonec                    t                                          ||j        d           || _        |                                  dS )z	:private:r   )r   id_startcountN)super__init__r   r   _initreceive)selfr   r   	__class__s      V/home/ubuntu/.hermes/hermes-agent/venv/lib/python3.11/site-packages/execnet/gateway.pyr   zGateway.__init__   sC    B47:::	    strc                    | j         j        S N)_ioremoteaddressr   s    r   r#   zGateway.remoteaddress#   s     x%%r   c                   	 |                                  rdpd}t          t          | j                                                            }n# t
          $ r d}d}Y nw xY wd| j        j         d| j        d| d| j	        j
         d	| d
S )z.A string representing gateway type and status.zreceive-liveznot-receivinguninitializedno<z id= , z model, z active channels>)hasreceiverr   len_channelfactorychannelsAttributeErrorr   __name__r   	execmodelbackend)r   ris      r   __repr__zGateway.__repr__(   s    	&&((;^OAC,55778899AA 	 	 	AAAA	 v4>*uuuuAuuAWuuabuuuus   AA AAc                   |                      d           | | j        vr|                      d           dS | j                            |            	 |                      d           |                     t          j                   |                      d           | j                                         dS # t          t          t          f$ r8}|                      d           |                      d|z             Y d}~dS d}~ww xY w)zTrigger gateway exit.

        Defer waiting for finishing of receiver-thread and subprocess activity
        to when group.terminate() is called.
        zgateway.exit() calledz'gateway already unregistered with groupNz--> sending GATEWAY_TERMINATEz--> io.close_writez-io-error: could not send termination sequencez exception: %r)_tracer   _unregister_sendr   GATEWAY_TERMINATEr"   close_write
ValueErrorEOFErrorOSError)r   excs     r   exitzGateway.exit2   s    	+,,,t{""KKABBBF%%%	0KK7888JJw0111KK,---H  """""Hg. 	0 	0 	0KKGHHHKK(3./////////	0s   A"B5 5D-C??DTFpy2str_as_py3strboolpy3str_as_py2strc                    ||f| _         t          j        | j                   }|                     t          j        |           dS )zSet the string coercion for this gateway.

        The default is to try to convert py2 str as py3 str, but not to try and
        convert py3 str to py2 str.
        )dataN)
_strconfigr   dumps_internalr9   r   RECONFIGURE)r   rA   rC   rE   s       r   reconfigurezGateway.reconfigureF   sB     ,-=>*4?;;

7&T
22222r   updateRInfoc                   |st          | d          so|                     t                    }	 t          |                                          | _        |                                 n# |                                 w xY w| j        S )z,Return some sys/env information from remote._cache_rinfo)hasattrremote_execrinfo_sourcerK   receiverM   	waitclose)r   rJ   chs      r   _rinfozGateway._rinfoR   su     	~66 	!!,//B$)"**,,$7$7!  s   &A) )A?c                <    | j                                         dk    S )z(Whether gateway is able to receive data.r   )_receivepoolactive_countr$   s    r   r+   zGateway.hasreceiver\   s     --//!33r   RemoteStatusc                    |                                  }|                     t          j        |j                   |                                }| j                            |j                   t          |          S )z5Obtain information about the remote execution status.)	
newchannelr9   r   STATUSr   rQ   r-   _local_closerX   )r   channel
statusdicts      r   remote_statuszGateway.remote_status`   sa    //##

7>7:...__&&
 	))'*555J'''r   sourceCstr | types.FunctionType | Callable[..., object] | types.ModuleTypekwargsobjectr
   c           
     N   d}d}t          |t          j                  r=t          j        |          }t          j        |           t          j        |          }nft          |t          j                  r+|j	        }t          j        |          }t          |          }n!t          j        t          |                    }|s|rt          d          |                                 }|                     t"          j        |j        t)          j        ||||f                     |S )a  Return channel object and connect it to a remote
        execution thread where the given ``source`` executes.

        * ``source`` is a string: execute source string remotely
          with a ``channel`` put into the global namespace.
        * ``source`` is a pure function: serialize source and
          call function with ``**kwargs``, adding a
          ``channel`` object to the keyword arguments.
        * ``source`` is a pure module: execute source of module
          with a ``channel`` in its global namespace.

        In all cases the binding ``__name__='__channelexec__'``
        will be available in the global namespace of the remotely
        executing code.
        Nz-can't pass kwargs to non-function remote_exec)
isinstancetypes
ModuleTypeinspectgetsourcefile	linecacheupdatecache	getsourceFunctionTyper0   _source_of_functiontextwrapdedentr   	TypeErrorrZ   r9   r   CHANNEL_EXECr   r   rG   )r   r`   rb   	call_name	file_namer]   s         r   rO   zGateway.remote_execj   s   ( 		fe.// 		2-f55I!),,,&v..FF 233 	2I-f55I(00FF_S[[11F 	MV 	MKLLL//##

 J'Iv(NOO	
 	
 	

 r   Nnum
int | Nonec                $    t          d           dS )z1DEPRECATED.  Is currently a NO-OPERATION already.z?WARNING: remote_init_threads() is a no-operation in execnet-1.2N)print)r   ru   s     r   remote_init_threadszGateway.remote_init_threads   s    OPPPPPr   )r   r	   r   r   r   r   r   r   r   r   )TF)rA   rB   rC   rB   r   r   )F)rJ   rB   r   rK   )r   rB   )r   rX   )r`   ra   rb   rc   r   r
   r!   )ru   rv   r   r   )r0   
__module____qualname____doc____annotations__r   propertyr#   r5   r@   rI   rT   r+   r_   rO   ry   __classcell__)r   s   @r   r   r      s2        ::MMM      & & & X&v v v v0 0 0 0* GL
3 
3 
3 
3 
3! ! ! ! !4 4 4 4( ( ( (* * * *XQ Q Q Q Q Q Q Q Qr   r   c                  .    e Zd Zd
dZddZerddZd	S d	S )rK   r   r   c                :    | j                             |           d S r!   )__dict__rJ   )r   rb   s     r   r   zRInfo.__init__   s    V$$$$$r   r   c                    d                     d t          | j                                                  D                       }d|z  S )Nr*   c              3  *   K   | ]\  }}| d | V  dS )=N ).0kvs      r   	<genexpr>z!RInfo.__repr__.<locals>.<genexpr>   s0      NN1ANNNNNNr   z
<RInfo %r>)joinsortedr   items)r   infos     r   r5   zRInfo.__repr__   sC    yyNNt}7J7J7L7L0M0MNNNNNd""r   namer   c                    d S r!   r   )r   r   s     r   __getattr__zRInfo.__getattr__   s      r   Nr{   rz   )r   r   r   r   )r0   r|   r}   r   r5   r   r   r   r   r   rK   rK      sX        % % % %# # # #  54444445 5r   rK   r   r   c           
         dd l }dd l}|                     t          |j        |j        d d         |j        |                                |                                                     d S )Nr      )
executableversion_infoplatformcwdpid)	ossyssenddictr   r   r   getcwdgetpid)r]   r   r   s      r   rP   rP      su    IIIJJJLL~)"1"-\					
 	
 	
    r   r`   r   codeobjtypes.CodeType	list[str]c                    dd l dd lt                              |j                  fd                                        |                     D             S )Nr   c                v    g | ]5}t          |j                  r|j        vr|j        j        v.|j        6S r   )re   Namer   r   )r   nodeastbuiltinsvarss     r   
<listcomp>z-_find_non_builtin_globals.<locals>.<listcomp>   sZ       dCH%% G4G8,,,	 	 -,,r   )r   r   r   fromkeysco_varnameswalkparse)r`   r   r   r   r   s     @@@r   _find_non_builtin_globalsr      sx    JJJOOO==,--D     HHSYYv..//   r   function*types.FunctionType | Callable[..., object]c                B   | j         dk    rt          d          	 t          j        |           }|j        }n*# t
          $ r t          j        |           d         }Y nw xY w|r|d         dk    rt          d          | j        }| j        }|t          d          	 t          j	        |           }n%# t          $ r}t          d| z            |d }~ww xY wt          j        |          }t          ||          }|rt          d|          d	|j        d
z
  z  }||z   S )Nz<lambda>z can't evaluate lambda functions'r   r]   z0expected first function argument to be `channel`z'functions with closures can't be passedzcan't find source file for %sz.the use of non-builtin globals isn't supported
r   )r0   r<   rh   getfullargspecargsr/   
getargspec__closure____code__rl   r>   ro   rp   r   co_firstlineno)	r   sigr   closurer   r`   eused_globals
leading_wss	            r   rn   rn      sj   J&&;<<<$X.. x  / / /!(++A./  M47i''KLLL"GGBCCCL"8,, L L L88CDD!KL _V$$F,VW==L YI<XXX/!34Js'   8 $AAB4 4
C>CCr{   )r`   r   r   r   r   r   )r   r   r   r   )r~   
__future__r   rh   rj   ro   rf   typingr   r   r    r   r	   r
   r   multir   xspecr   BaseGatewayr   rK   rX   rP   r   rn   r   r   r   <module>r      s   
 # " " " " "                                            ! ! ! ! ! ! ! ! ! ! ! !            @Q @Q @Q @Q @Ql& @Q @Q @QF
5 
5 
5 
5 
5 
5 
5 
5                  r   