
    hB                         S SK r S SKrS SKrS SKrS SKJrJrJrJrJr  SSK	J
r
JrJr  SSKJr  \R                  " \5      r\" S\ R$                  S9r " S S	\5      r " S
 S\ R,                  \ R.                  5      rg)    N)AnyCallableIterableSequenceTypeVar   )InteractionAgentInteractionHandlerget_running_loop)get_exception_for_ssh_stderrP)boundc                   N   ^  \ rS rSr% \\S'   \\S'   S\S\SS4U 4S jjrSrU =r	$ )SubprocessError    
returncodestderrreturnNc                 <   > [         TU ]  X5        Xl        X l        g N)super__init__r   r   )selfr   r   	__class__s      A/usr/lib/python3/dist-packages/cockpit/_vendor/ferny/transport.pyr   SubprocessError.__init__$   s    ,$    )r   r   )
__name__
__module____qualname____firstlineno__int__annotations__strr   __static_attributes____classcell__)r   s   @r   r   r       s,    OK3    r   r   c                   (   \ rS rSr% \\S'   S\S'   \\S'   \R                  \S'   Sr	\\S'   S	r
S
\S'   S	rS\S'   S	rS\S'   S	rS\S'   S	rS\S'   S	rS\S'   Sr\\S'   Sr\\S'   \   SMSSS\/ \4   S\\   SSS\\   S\S \S!S"4S# jj5       rS$\R                  S!S	4S% jrSNS& jrSOS' jrS(\R:                  S!S	4S) jrSPS+ jrS,\ S-\!S!S	4S. jr"S,\ S*SS!S	4S/ jr#SNS0 jr$SNS1 jr%SNS2 jr&SQSPS3 jjr'S!\4S4 jr(SQS5\S6\)S!\)4S7 jjr*S$\RV                  S!S	4S8 jr,S!\R                  4S9 jr-S!\4S: jr.SNS; jr/SNS< jr0SNS= jr1S!\4S> jr2S!\ 4S? jr3SRS@ jr4SSSTSA jjr5S-\!S!S	4SB jr6SC\7\!   S!S	4SD jr8SNSE jr9S!\ 4SF jr:SUSG jr;SNSH jr<SI\ S!S	4SJ jr=SNSK jr>SLr?g	)VFernyTransport*   _agentz@asyncio.Task[tuple[asyncio.SubprocessTransport, FernyTransport]]
_exec_task_is_ssh	_protocolF_protocol_disconnectedNz"asyncio.SubprocessTransport | None_subprocess_transportzasyncio.WriteTransport | None_stdin_transportzasyncio.ReadTransport | None_stdout_transportException | None
_exceptionz
str | None_stderr_output
int | None_returncode_transport_disconnected_closedclsztype[typing.Self]protocol_factoryargsloopz asyncio.AbstractEventLoop | Noneinteraction_handlersis_sshkwargsr   ztuple[typing.Self, P]c                   ^	 [         R                  SXU5        U" 5       nU " U5      m	UT	l        Uc
  [        5       n[	        XCT	R
                  5      T	l        UR                  ST	R                  R                  5       5        UR                  UR                  " U	4S j/UQ70 UD65      T	l        S[        R                  SS4U	4S jjnT	R                  R                  U5        T	U4$ )a  Connects a FernyTransport to a protocol, using the given command.

This spawns an external command and connects the stdin and stdout of
the command to the protocol returned by the factory.

An instance of ferny.InteractionAgent is created and attached to the
stderr of the spawned process, using the provided handlers.  It is the
responsibility of the caller to ensure that:
  - a `ferny-askpass` client program is installed somewhere; and
  - any relevant command-line arguments or environment variables are
    passed correctly to the program to be spawned

This function returns immediately and never raises exceptions, assuming
all preconditions are met.

If spawning the process fails then connection_lost() will be
called with the relevant OSError, even before connection_made() is
called.  This is somewhat non-standard behaviour, but is the easiest
way to report these errors without making this function async.

Once the process is successfully executed, connection_made() will be
called and the transport can be used as normal.  connection_lost() will
be called if the process exits or another error occurs.

The return value of this function is the transport, but it exists in a
semi-initialized state.  You can call .close() on it, but nothing else.
Once .connection_made() is called, you can call all the other
functions.

After you call this function, `.connection_lost()` will be called on
your Protocol, exactly once, no matter what.  Until that happens, you
are responsible for holding a reference to the returned transport.

:param args: the full argv of the command to spawn
:param loop: the event loop to use.  If none is provided, we use the
    one which is (read: must be) currently running.
:param interaction_handlers: the handlers passed to the
    InteractionAgent
:param is_ssh: whether we should attempt to interpret stderr as ssh
    error messages
:param kwargs: anything else is passed through to `subprocess_exec()`
:returns: the usual `(Transport, Protocol)` pair
zspawn(%r, %r, %r)Nr   c                     > T $ r    r   s   r   <lambda>&FernyTransport.spawn.<locals>.<lambda>   s    r   taskr   c                   > [         R                  STU 5        U TR                  L d   e U R                  5       u  pUTL d   e[         R                  S5        TR                  UL d   eTR                  c   eTR                  c   eTR                  R                  5         g ! [        R
                   a     g [         a1  n[         R                  SU5        TR                  U5         S nAg S nAff = f)Nzexec_completed(%r, %r)z
  success.z  OSError %r)loggerdebugr+   resultasyncioCancelledErrorOSErrorcloser/   r0   r1   r*   start)rF   	transportmeexcr   s       r   exec_completed,FernyTransport.spawn.<locals>.exec_completed   s    LL14>4??***	 $	Tz!z\* --:::((444))555 KK ))  ^S1

3s   .B# #C39	C3'C..C3)rH   rI   r,   r   r	   _interaction_completedr*   
setdefaultfilenocreate_tasksubprocess_execr+   rK   Taskadd_done_callback)
r9   r:   r;   r<   r=   r>   r?   protocolrS   r   s
            @r   spawnFernyTransport.spawn@   s    j 	(#F#%8}<#%D&';4C^C^_(DKK$6$6$89 **4+?+?+^t+^W]+^_	  	 $ 	 . 	)).9X~r   r\   c                     Xl         g r   r-   r   r\   s     r   r   FernyTransport.__init__   s    !r   c                    [         R                  SU 5        U R                  R                  5       (       d!  [         R                  SU R                  5        g U R                  b2  U R
                  (       d!  [         R                  SU R                  5        g U R                  c  [         R                  S5        g U R                  (       a  [         R                  S5        g SU l        U R                  bF  [         R                  SU R                  5        U R                  R                  U R                  5        g U R                  S:X  d  U R                  (       a1  [         R                  S	5        U R                  R                  S 5        g U R                  (       a_  U R                  S
:X  aO  [         R                  SU R                  5        U R                  R                  [        U R                  5      5        g [         R                  SU R                  U R                  5        U R                  c   eU R                  R                  [        U R                  U R                  5      5        g )Nz_consider_disconnect(%r)z  exec_task still running %rz  transport still connected %rz  agent still runningz  already disconnectedTz  disconnect with exception %rr   z  clean disconnect   z  disconnect with ssh error %rz)  disconnect with exit code %r, stderr %r)rH   rI   r+   doner/   r7   r4   r.   r3   r-   connection_lostr6   r8   r,   r   r   rC   s    r   _consider_disconnect#FernyTransport._consider_disconnect   s   /6##%%LL7I%%1$:V:VLL94;U;UV&LL01 &&LL12&*# ??&LL94??KNN**4??;"dll LL-.NN**40\\d..#5LL94;N;NONN**+GH[H[+\] LLDdFVFVX\XkXkl ##///NN**?4;K;KTM`M`+abr   c                 F   [         R                  SX5         UR                  5       U l        [         R                  SU R                  5        U R                  5         g ! [         a8  n[         R                  SU5        SU l        U R                  U5         S nANNS nAff = f)Nz_interaction_completed(%r, %r)z  stderr: %rz  exception: %r )rH   rI   rJ   r4   	ExceptionrN   rg   )r   futurerR   s      r   rU   %FernyTransport._interaction_completed   s|    5tD	"(--/DLL)<)<= 	!!#  	LL*C0"$DJJsOO	s   5A 
B (.BB rP   c                     [         R                  SX5        [        U[        R                  5      (       d   eXl        UR                  S5      n[        U[        R                  5      (       d   eX l        UR                  S5      n[        U[        R                  5      (       d   eX0l
        UR                  S5      nUb   e[         R                  SX R                  5        U R                  R                  U 5        g )Nzconnection_made(%r, %r)r   r      zcalling connection_made(%r, %r))rH   rI   
isinstancerK   SubprocessTransportr/   get_pipe_transportWriteTransportr0   ReadTransportr1   r-   connection_made)r   rP   stdin_transportstdout_transportstderr_transports        r   ru   FernyTransport.connection_made   s    .@)W%@%@AAAA%."#66q9/7+A+ABBBB /$77:*G,A,ABBBB!1$77:'''6nnM&&t,r   rR   c                     [         R                  SX5        U R                  c  Xl        SU l        U R	                  5         g )Nzconnection_lost(%r, %r)T)rH   rI   r3   r7   rg   r   rR   s     r   rf   FernyTransport.connection_lost   s4    .:??"!O'+$!!#r   fddatac                     [         R                  SX[        U5      5        US:X  d   eU R                  R	                  U5        g )Nzpipe_data_received(%r, %r, %r)r   )rH   rI   lenr-   data_received)r   r}   r~   s      r   pipe_data_received!FernyTransport.pipe_data_received   s4    5tTKQww$$T*r   c                 ,   [         R                  SXU5        US;   d   e[        U[        5      (       a  S nUb  U R	                  U5        g US:X  aC  U R
                  (       d1  U R                  R                  5       (       d  U R	                  5         g g g g )Nz pipe_connection_lost(%r, %r, %r))r   r   r   )rH   rI   rp   BrokenPipeErrorrN   r8   r-   eof_received)r   r}   rR   s      r   pipe_connection_lost#FernyTransport.pipe_connection_lost   sz    73GV|| c?++C ?JJsO1WT\\>>..00

 1 *Wr   c                     [         R                  SU 5        U R                  c   eU R                  R                  5       U l        [         R                  SU R                  5        U R
                  R                  5         g )Nzprocess_exited(%r)z  ._returncode = %r)rH   rI   r/   get_returncoder6   r*   force_completionrC   s    r   process_exitedFernyTransport.process_exited  s_    )40))55555DDF*D,<,<=$$&r   c                 d    [         R                  SU 5        U R                  R                  5         g )Nzpause_writing(%r))rH   rI   r-   pause_writingrC   s    r   r   FernyTransport.pause_writing  s!    ($/$$&r   c                 d    [         R                  SU 5        U R                  R                  5         g )Nzresume_writing(%r))rH   rI   r-   resume_writingrC   s    r   r   FernyTransport.resume_writing  s!    )40%%'r   c                 @   [         R                  SX5        SU l        U R                  c  [         R                  SU5        Xl        U R                  R                  5       (       d/  [         R                  S5        U R                  R                  5         U R                  bR  [         R                  S5        [        R                  " [        5         U R                  R                  5         S S S 5        U R                  R                  5         g ! , (       d  f       N)= f)Nzclose(%r, %r)Tz  setting exception %rz  cancelling _exec_taskz  closing _subprocess_transport)rH   rI   r8   r3   r+   re   cancelr/   
contextlibsuppressPermissionErrorrN   r*   r   r{   s     r   rN   FernyTransport.close   s    _d0??"LL137!O##%%LL23OO""$%%1LL:;$$_5**002 6$$& 65s   D
Dc                 T    U R                   c   eU R                   R                  5       $ r   )r/   
is_closingrC   s    r   r   FernyTransport.is_closing0  s)    ))555))4466r   namedefaultc                 V    U R                   c   eU R                   R                  X5      $ r   )r/   get_extra_info)r   r   r   s      r   r   FernyTransport.get_extra_info4  s+    ))555))88GGr   c                 R    [        U[        R                  5      (       d   eXl        g r   )rp   rK   Protocolr-   ra   s     r   set_protocolFernyTransport.set_protocol8  s     (G$4$45555!r   c                     U R                   $ r   r`   rC   s    r   get_protocolFernyTransport.get_protocol<  s    ~~r   c                 p   U R                   c   e U R                   R                  5       $ ! [         a    U R                   R                  (       + s $ [         aZ     U R                   R
                  R                  nUR                  U R                   R                  5         g! [         a      gf = ff = f)NTF)
r1   
is_readingNotImplementedError_pausedAttributeError_loop	_selectorget_key_filenoKeyError)r   selectors     r   r   FernyTransport.is_reading?  s    %%111	))4466" 	6--5555 	1177AA  !7!7!?!?@ 	s.   + %B5	B5AB##
B1-B50B11B5c                 V    U R                   c   eU R                   R                  5         g r   )r1   pause_readingrC   s    r   r   FernyTransport.pause_readingO  s&    %%111,,.r   c                 V    U R                   c   eU R                   R                  5         g r   )r1   resume_readingrC   s    r   r   FernyTransport.resume_readingS  s&    %%111--/r   c                     U R                   c   eU R                  c   eU R                   R                  5         U R                  R                  5         g r   )r0   r/   abortkillrC   s    r   r   FernyTransport.abortW  sJ    $$000))555##%""'')r   c                 T    U R                   c   eU R                   R                  5       $ r   )r0   can_write_eofrC   s    r   r   FernyTransport.can_write_eof]  s)    $$000$$2244r   c                 T    U R                   c   eU R                   R                  5       $ r   )r0   get_write_buffer_sizerC   s    r   r   $FernyTransport.get_write_buffer_sizea  s)    $$000$$::<<r   c                 T    U R                   c   eU R                   R                  5       $ r   )r0   get_write_buffer_limitsrC   s    r   r   &FernyTransport.get_write_buffer_limitse  s)    $$000$$<<>>r   c                 V    U R                   c   eU R                   R                  X5      $ r   )r0   set_write_buffer_limits)r   highlows      r   r   &FernyTransport.set_write_buffer_limitsi  s+    $$000$$<<TGGr   c                 V    U R                   c   eU R                   R                  U5      $ r   )r0   write)r   r~   s     r   r   FernyTransport.writem  s+    $$000$$**400r   list_of_datac                 V    U R                   c   eU R                   R                  U5      $ r   )r0   
writelines)r   r   s     r   r   FernyTransport.writelinesq  s+    $$000$$//==r   c                 T    U R                   c   eU R                   R                  5       $ r   )r0   	write_eofrC   s    r   r   FernyTransport.write_eofu  s)    $$000$$..00r   c                 T    U R                   c   eU R                   R                  5       $ r   )r/   get_pidrC   s    r   r   FernyTransport.get_pid{  s)    ))555))1133r   c                     U R                   $ r   )r6   rC   s    r   r   FernyTransport.get_returncode  s    r   c                 V    U R                   c   eU R                   R                  5         g r   )r/   r   rC   s    r   r   FernyTransport.kill  s&    ))555""'')r   numberc                 X    U R                   c   eU R                   R                  U5        g r   )r/   send_signal)r   r   s     r   r   FernyTransport.send_signal  s(    ))555""..v6r   c                 V    U R                   c   eU R                   R                  5         g r   )r/   	terminaterC   s    r   r   FernyTransport.terminate  s&    ))555"",,.r   )r*   r8   r3   r+   r,   r-   r.   r6   r4   r0   r1   r/   r7   )NrB   T)r   N)rl   zasyncio.Future[str]r   N)rR   r2   r   Nr   )r   ztuple[int, int])NN)r   r5   r   r5   r   N)r   r5   )@r   r   r    r!   r	   r#   boolrK   r   r.   r/   r0   r1   r3   r4   r6   r7   r8   classmethodr   r   r   r$   r
   r   r]   r   rg   rU   BaseTransportru   rf   r"   bytesr   r   r   r   r   rN   r   objectr   BaseProtocolr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r%   rB   r   r   r(   r(   *   s   RRM#(D( CG?F8<5<8<5< &*J")#'NL' $K$$)T)GT
 48=?a a"2q5/a sma 1	a
 ''9:a a a 
!a aF"!1!1 "d "*cX
$-)>)> -4 -&$+S + +$ +
s 1C   ''(
' 7D 7H3 H H6 H"W%9%9 "d "g.. D  /0*5t 5=s =?H1% 1D 1>x >4 >14 4 *7# 7$ 7/r   r(   )rK   r   loggingtypingr   r   r   r   r   interaction_agentr	   r
   r   
ssh_errorsr   	getLoggerr   rH   r   r   rk   r   	TransportSubprocessProtocolr(   rB   r   r   <module>r      sl   $     = = U U 4			8	$Cw''(i b/W&&(B(B b/r   