
    6h`                    B   S SK Jr  S SKrS SKrS SKrS SKJrJr  SSKJr  SSK	J
r
JrJrJrJrJr  SSKJrJrJr  \R(                  (       a  S SKJr  S	 rSS
 jr      SS jr " S S5      rS r SS jrS rS r " S S\R<                  5      r  SS jr  SS jr!g)    )annotationsN)CallableOptional   )_)errorpycompatutilwireprototypeswireprotov1peerwireprotov1server)procutil
stringutilurlutil)Setc                z    U (       d  U $ [         R                  " SU 5      (       a  U $ SU R                  SS5      -  $ )z=quote a string for the remote shell ... which we assume is shs   [a-zA-Z0-9@%_+=:,./-]*$s   '%s'   's   '\'')rematchreplace)ss    3/usr/lib/python3/dist-packages/mercurial/sshpeer.py_serverquoter   %   s7    	xx*A..QYYtX...    c                   U(       az  UR                   (       dh  [        R                  " U5      nU(       aJ  U(       a  U R                  OU R                  nUR                  5        H  nU" [        S5      US5        M     gggg)zUdisplay all data currently available on pipe as remote output.

This is non blocking.   remote:    
N)closedr   readpipewarnstatus
splitlinesr   )uipiper    r   displayls         r   _forwardoutputr'   .   s]     DKKd#!%bgg299G\\^+51 $   tr   c                    [        U5      nU " U5      nUc  SnX2:  a(  [        U5      nX2:  a  U " XS 5      nUb  X4-  nX2:  a  M  U$ )zvwrite data with a non blocking function

In case not all data were written, keep writing until everything is
written.
Nr   )len
memoryview)
write_oncedatato_writewrittenwrotes        r   
_write_allr0   :   sd     4yHG$ tH~.E      Nr   c                  p    \ rS rSrSrS rS rSS jrSS jrS r	S r
S	 rSS jrS r\S 5       rS rSrg
)
doublepipeS   a  Operate a side-channel pipe in addition of a main one

The side-channel pipe contains server output to be forwarded to the user
input. The double pipe will behave as the "main" pipe, but will ensure the
content of the "side" pipe is properly processed while we wait for blocking
call on the "main" pipe.

If large amounts of data are read from "main", the forward will cease after
the first bytes start to appear. This simplifies the implementation
without affecting actual output of sshpeer too much as we rarely issue
large read for data not yet emitted by the server.

The main pipe is expected to be a 'bufferedinputpipe' from the util module
that handle all the os specific bits. This class lives in this module
because it focus on behavior specific to the ssh protocol.c                (    Xl         X l        X0l        g N)_ui_main_side)selfr#   mainsides       r   __init__doublepipe.__init__d   s    

r   c                   [        U R                  [        R                  5      (       a  U R                  R                  (       a  gU R                  R                  5       U R                  R                  5       /n [        R                  " U5      nU R                  R                  5       U;   U R                  R                  5       U;   4$ ! [         a    Un NHf = f)zwait until some data are available on main or side

return a pair of boolean (ismainready, issideready)

(This will only wait for data if the setup is supported by `util.poll`)
)TT)	
isinstancer7   r
   bufferedinputpipe	hasbufferfilenor8   pollNotImplementedError)r9   fdsacts      r   _waitdoublepipe._waiti   s     tzz4#9#9::

$$ zz  "DJJ$5$5$78	))C.C 

!!#s*DJJ,=,=,?3,FGG # 	C	s   ;C CCc                &    U R                  SU5      $ )z4Write as much data as possible in a non blocking way   write_callr9   r,   s     r   _write_oncedoublepipe._write_once   s    zz(D))r   c                .    [        U R                  U5      $ )z write all data in a blocking way)r0   rN   rM   s     r   writedoublepipe.write   s    $**D11r   c                    U R                  SU5      nUS:w  a'  U(       d   [        U R                  U R                  5        U$ )Ns   readr   rL   r'   r6   r8   r9   sizers      r   readdoublepipe.read   s4    JJw%19Q 488TZZ0r   c                    U R                  SU5      nUS:w  a'  U(       d   [        U R                  U R                  5        U$ )Ns   unbufferedreadr   rT   rU   s      r   unbufferedreaddoublepipe.unbufferedread   s5    JJ($/19Q 488TZZ0r   c                $    U R                  S5      $ )Ns   readlinerK   r9   s    r   readlinedoublepipe.readline   s    zz+&&r   Nc                   Ub  U(       a  U R                   R                  (       a(  [        U R                  U R                  5        US:X  a  gg U R                  5       u  p4U(       a   [        U R                  U R                  5        U(       a<  [        U R                   [        R                  " U5      5      nUc  U" 5       $ U" U5      $ M~  )zBcall <methname> on "main", forward output of "side" while blockingrJ   r   r   )	r7   r   r'   r6   r8   rG   getattrr	   sysstr)r9   methnamer,   	mainready	sidereadymeths         r   rL   doublepipe._call   s     Tdjj.?.?488TZZ08##'::< Itxx4tzz8??8+DE<6M:% r   c                6    U R                   R                  5       $ r5   )r7   closer^   s    r   rj   doublepipe.close       zz!!r   c                .    U R                   R                  $ r5   )r7   r   r^   s    r   r   doublepipe.closed   s    zz   r   c                6    U R                   R                  5       $ r5   )r7   flushr^   s    r   rp   doublepipe.flush   rl   r   )r7   r8   r6   )r,   bytesreturnOptional[int]r5   )__name__
__module____qualname____firstlineno____doc__r<   rG   rN   rQ   rX   r[   r_   rL   rj   propertyr   rp   __static_attributes__ r   r   r2   r2   S   sO    B 
H,*2		'&&" ! !"r   r2   c                   SnU(       a#  UR                   (       d  SnUR                  5         U(       a#  UR                   (       d  SnUR                  5         U(       aH  UR                   (       d7  Sn U H  nU R                  [        S5      U5        M      UR                  5         U(       a  Ub  U R                  SU-  5        ggg! [        [
        4 a     NCf = f)z)Clean up pipes used by an SSH connection.FTr   Ns.   missing close on SSH connection created at:
%s)r   rj   r!   r   OSError
ValueError	develwarn)r#   pipeipipeopipeer    didsomethingr&   s          r   _cleanuppipesr      s    LU\\U\\U\\			!K.!, 
 	( 	G$NO )| $ 		s   2$C CCc           
         SUU[         R                  " S[        U5      [        U5      U(       a  SOS4-  5      4-  nU R                  SU-  5        [         R                  " USUS9u  ppXX4$ )ztCreate an SSH connection to a server.

Returns a tuple of (process, stdin, stdout, stderr) for the
spawned process.
   %s %s %ss   %s -R %s serve --stdio%ss	    --hiddenr      running %s
r   )bufsizeenv)r   
shellquoter   debugpopen4)r#   sshcmdargs	remotecmdpathsshenvremotehiddencmdstdinstdoutstderrprocs               r   _makeconnectionr      s     'Y'T" ,#	
 C HH_s"#
 #+//#qf"ME6&&r   c                    S1n [         R                  R                  [         R                  5       Vs/ s H  nUR	                  5       R
                  PM     nnU R                  SSR                  U5      -  5        U $ s  snf )zoReturn list of capabilities of this client.

Returns a list of capabilities that are supported by this client.
s   partial-pulls   comp=%s   ,)r
   compenginessupportedwireengines
CLIENTROLEwireprotosupportnameaddjoin)protoparamsecompss      r   _clientcapabilitiesr      sy    
 ##K !!66tGGA 	
!!G 
  OOJ5!112s   $A>c                  ^ ^ UU 4S jnT R                  SS5      n[        R                  " [        [        R
                  " 5       5      5      n SS-  nSSS[        U5      -  U/nU(       a.  T R                  S	5        T R                  S
[        U5      -  5        T R                  S5        T R                  S5        [        UR                  SR                  U5      5        UR                  5         [        R                  n	[        R                   " S["        R$                  " U5      -  5      n
SS/nSnUS   (       a  U(       a   UR'                  5       n[)        T TSS9  U
R+                  U5      nU(       a&  UR-                  S5      n	T R                  SU	-  5        OYUS   S:X  a  US:X  a  OIU(       a  T R                  SU5        UR/                  U5        US-  nUS   (       a	  U(       a  M  U" 5         [1        5       nU	[        R                  :X  a^  [3        U5       HO  nUR5                  S5      (       d  M  UR7                  US S R9                  S5      S   R9                  5       5          O   U(       d  U" 5         [)        T TSS9  X4$ ! [         a    U" 5          GNf = f! [         a
    U" 5          Nf = f)Nc                 |   > [        TTSS9  [        S5      n TR                  SS5      n[        R                  " XS9e)NTr    s#   no suitable response from remote hg   uis   ssherrorhinthint)r'   r   configr   	RepoError)msgr   r   r#   s     r   badresponse&_performhandshake.<locals>.badresponse
  s;     	r6-67yy0ooc--r      devel   debug.peer-requests   %s-%s)(   0000000000000000000000000000000000000000r   s   hello
s   between
s	   pairs %d
s"   devel-peer-request: hello+between
s&   devel-peer-request:   pairs: %d bytes
s   sending hello command
s   sending between command
r   s   ^upgraded %s (.*)$s   dummyi  Tr   r   s   protocol upgraded to %s
s   1
r   r   s   capabilities:   :)
configboolr	   sysbytesstruuiduuid4r)   r   r0   rQ   r   rp   r~   r   SSHV1r   compiler   reescaper_   r'   r   groupappendsetreversed
startswithupdatesplit)r#   r   r   r   r   
requestlogtokenpairsarg	handshake	protoname
reupgradedlines	max_noiser&   mcapss   `  `            r   _performhandshaker   	  s`   
.@ x)>?J c$**,/0E44CM)	
	 HH;<HH?#h-OP
+,
-.5;; 34
 $$I1J4G4G4NNOJ(OEI
)		!A2vD1   #AGGAJ	5	AB 
 RyF"qEza(LLONI1 )		8 	5D N(((%A ||,--AcrFLL.q1779: !  2vD)?  D  	M	s2   B#J AJ)  J) 0/J) J&%J&)J=<J=c                     ^  \ rS rSr  SU 4S jjrS1rS rS rS rS r	SS jr
S	 rS
 rSS jrS rSS jrS rS rS rS rS rS rS rSS jrSrU =r$ )	sshv1peeri  c
                2  > [         T
U ]  XU	S9  X0l        U(       a-  [        U[        R
                  " U5      U5      n[        XU5      nX@l        XPl        X`l        Xpl	        Xl
        SR                  [        R                  " S5      5      U l        Xl        g)aV  Create a peer from an existing SSH connection.

``proc`` is a handle on the underlying SSH process.
``stdin``, ``stdout``, and ``stderr`` are handles on the stdio
pipes for that process.
``caps`` is a set of capabilities supported by the remote.
``autoreadstderr`` denotes whether to automatically read from
stderr and to forward its output.
)r   r   r   r   N)superr<   _subprocessr2   r
   r@   _pipeo_pipei_pipee_caps_autoreadstderrr   getstackframes
_initstack_remotehidden)r9   r#   r   r   r   r   r   r   autoreadstderrr   	__class__s             r   r<   sshv1peer.__init__  s    * 	\B   D$:$:6$BFKFr&1E
-((4#6#6q#9:)r   s   batchc                .    U R                   R                  $ r5   )r   locr^   s    r   urlsshv1peer.url  s    yy}}r   c                    g r5   r|   r^   s    r   localsshv1peer.local      r   c                    g)NTr|   r^   s    r   canpushsshv1peer.canpush  r   r   c                $    U R                  5         g r5   _cleanupr^   s    r   rj   sshv1peer.close  s    r   c                    U R                   $ r5   )r   r^   s    r   capabilitiessshv1peer.capabilities  s    zzr   c                D    [        U R                  U R                  5        g r5   )r'   r#   r   r^   s    r   _readerrsshv1peer._readerr  s    tww,r   c                &    U R                  5         Uer5   r   )r9   	exceptions     r   _abortsshv1peer._abort  s    r   c                n    [        U R                  U R                  U R                  U R                  US9  g Nr   )r   r#   r   r   r   )r9   r    s     r   r   sshv1peer._cleanup  s"    dggt{{DKK4Pr   c                6    U R                  U R                  S9  g r   )r   r   r^   s    r   __del__sshv1peer.__del__  s    4??+r   c           
     H   U R                   R                  (       a  U R                   R                  SS5      (       a  U R                   R                  nSnU" XQ-  5        [	        UR                  5       5       Hp  u  pg[        U[        5      (       d  U" US-  U[        U5      4-  5        M5  [	        UR                  5       5       H  u  pU" US-  Xh[        U	5      4-  5        M      Mr     U R                   R                  SU-  5        U R                  R                  SU-  5        [        R                  U   u  pUR                  5       n0 nU H  nUS:X  a  X-S'     OX.   X'   X.	 M     [	        UR                  5       5       H  u  pU R                  R                  S	U[        U5      4-  5        [        U[        5      (       a_  UR                  5        HI  u  pU R                  R                  S	U[        U	5      4-  5        U R                  R                  U	5        MK     M  U R                  R                  U5        M     U R                  R                  5         U(       a1  U R                  5       n[         R"                  " U R$                  U5      $ U R$                  $ )
Nr   r   s   devel-peer-request: %s
s     %s: %d bytess     %s-%s: %ds   sending %s command
s   %s
   *s   %s %d
)r#   	debugflagr   r   sorteditemsr?   dictr)   r   rQ   r   commandsr   rp   
_getamountr
   cappedreaderr   )r9   r   r   frameddbglinekeyvaluedkdv_funcnameskeyswireargskvamounts                    r   _sendrequestsshv1peer._sendrequest  s   77!3!3+"
 "
 ''--C.D
O$TZZ\2
!%..00CU3DDE"("7D>1Sc"g4FFG #8	 3 	-34'C-((11#6{{}ADy!%"gG  8>>+,DAKKjAs1v;67!T""ggiFBKK%%jBB=&@AKK%%b) ( !!!$ - 	 __&F$$T[[&99{{r   c                f    [         R                  " U5      nU R                  XXR                  ;   S9$ Nr	  r	   byteskwargsr  _FRAMED_COMMANDSr9   r   r   s      r   _callstreamsshv1peer._callstream#  1    ##D)  3:O:O3O PPr   c                f    [         R                  " U5      nU R                  XXR                  ;   S9$ r  r  r  s      r   _callcompressablesshv1peer._callcompressable'  r"  r   c                j    [         R                  " U5      nU R                  XSS9R                  5       $ )NTr  )r	   r  r  rX   r  s      r   rL   sshv1peer._call+  s1    ##D)  4 8==??r   c                
  ^ U R                   " U40 UD6nU(       a  SU4$ [        U4S jS5       H  nU R                  U5        M     U R                  SSS9  U R                  5       nU(       a  SU4$ U R                  5       S4$ )Nr   c                 &   > T R                  S5      $ Ni   rX   fps   r   <lambda>%sshv1peer._callpush.<locals>.<lambda>8      bggdmr   Trp   )rL   iter_writeframed_readframedr9   r   r-  r   rW   ds     `   r   	_callpushsshv1peer._callpush/  s     JJs#d#6M +S1Aa  2#T*
 6M!3&&r   c                  ^ U R                   " U40 UD6nU(       a"  [        R                  " [        S5      U-  5      e[	        U4S jS5       H  nU R                  U5        M     U R                  SSS9  U R                  $ )Ns   unexpected remote reply: %sc                 &   > T R                  S5      $ r*  r+  r,  s   r   r.  -sshv1peer._calltwowaystream.<locals>.<lambda>N  r0  r   r   Tr1  )rL   r   Abortr   r2  r3  r   r5  s     `   r   _calltwowaystreamsshv1peer._calltwowaystreamD  st     JJs#d#++a >?!CDD +S1Aa  2#T*{{r   c                   U R                   R                  5       nUS:X  aO  U R                  (       a  U R                  5         [	        S5      nU R                  [        R                  " US95        U R                  (       a  U R                  5          [        U5      $ ! [         a2    U R                  [        R                  " [	        S5      U5      5         g f = f)Nr   s   check previous remote outputr   s   unexpected response:)r   r_   r   r   r   r   r   OutOfBandErrorintr   ResponseError)r9   r&   r   s      r   r  sshv1peer._getamountT  s    KK  ":##34CKK,,#67MMO	Lq6M 	LKK++A.E,FJK	Ls   
B 9CCc                h    U R                  5       nU(       d  gU R                  R                  U5      $ )Nr   )r  r   rX   )r9   rV   s     r   r4  sshv1peer._readframedb  s)     {{%%r   c                   U R                   R                  S[        U5      -  5        U(       a  U R                   R                  U5        U(       a  U R                   R                  5         U R                  (       a  U R                  5         g g )Ns   %d
)r   rQ   r)   rp   r   r   )r9   r,   rp   s      r   r3  sshv1peer._writeframedi  s\    'CI-.KKd#KKMMO  r   )r   r   r   r   r   r   r   r   TF)rs   z
Set[bytes]r5   F)ru   rv   rw   rx   r<   r  r   r   r   rj   r   r   r   r   r   r  r   r$  rL   r7  r=  r  r4  r3  r{   __classcell__)r   s   @r   r   r     s     &*V 	
-Q,+ZQQ@'* L& r   r   c                     [        XXE5      u  pU[        R                  :X  a  [        U UUUUUU	UUS9	$ [        XX5SS9  [        R                  " [        S5      U-  5      e! [         a    [        XX5SS9  e f = f)a  Make a peer instance from existing pipes.

``path`` and ``proc`` are stored on the eventual peer instance and may
not be used for anything meaningful.

``stdin``, ``stdout``, and ``stderr`` are the pipes connected to the
SSH server's stdio handles.

This function is factored out to allow creating peers that don't
actually spawn a new process. It is useful for starting SSH protocol
servers and clients via non-standard means, which can be useful for
testing.
Nr   )r   r   s#   unknown version of SSH protocol: %s)	r   	Exceptionr   r   r   r   r   r   r   )
r#   r   r   r   r   r   r   r   r   r   s
             r   
_make_peerrM  s  s    .+BvF	
 N((()%

 
	
 	b%d;oo45	A
 	
%  b%d;s   A" "A9c           
     p   [         R                  " UR                  SSS9nUR                  S:w  d  UR                  (       a  UR
                  c,  [        R                  " [        S5      UR                  -  5      e[         R                  " UR                  5        UR                  b  [        R                  " [        S5      5      eU R                  SS5      nU R                  SS5      n[        U R                  S5      5      n	[        R                  " U	5      n
UR
                  =(       d    S	n[        R                   " XvR                  UR"                  UR$                  5      nU(       a  U(       a  [        R                  " [        S
5      5      eSUU[        R&                  " S[)        U5      [)        U5      4-  5      4-  nU R+                  SU-  5        U R-                  USU
S9nUS:w  a  [        R                  " [        S5      5      e[/        U UUUUU
US9u  nnnn[1        XUUUUUS9nSUR3                  5       ;   a3   UR5                  SSR7                  [9        [;        5       5      5      S9  U$ U$ ! [<         a0    UR?                  5         [        R                  " [        S5      5      ef = f)zaCreate an SSH peer.

The returned object conforms to the ``wireprotov1peer.wirepeer`` interface.
F)
parsequeryparsefragments   sshs   couldn't parse location %ss   password in URL not supportedr   s	   remotecmds   sshenv   .s8   cannot create remote SSH repositories with extra optionsr   s
   %s init %sr   s   sshpeer)
blockedtagenvironr   s   could not create remote repo)r   s	   protocaps    )r   s   capability exchange failed) r   r   r   schemehostr   r   r   r   checksafesshpasswdr   r  configitemsr   shellenvironsshargsuserportr   r   r   systemr   rM  r   rL   r   r  r   r~   r   )r#   r   createintents
createoptsr   ur   r   	sshaddenvr   
remotepathr   r   resr   r   r   r   peers                       r   	make_peerrg    sZ    	DHHeDAxx6166>ooa =>IJJ"xxooa @ABBYYuf%F		%.IR^^I./I""9-F4JFFFAFFAFF;D
 //*  	*L,DEF
 
 	3&'ii
FiC!8//!$C"DEE"1
!#D% 
$vvLD t((**	DJJ499V4G4I-J#K   K4K	  	DMMO//!$A"BCC	Ds   0I; ;:J5rI  )r+   z Callable[[bytes], Optional[int]]r,   rr   rs   rt   )NFrH  )NNF)"
__future__r   r   typingr   r   r   i18nr    r   r	   r
   r   r   r   utilsr   r   r   TYPE_CHECKINGr   r   r'   r0   r2   r   r   r   r   wirepeerr   rM  rg  r|   r   r   <module>ro     s    # 	   %    

/	20
 2h" h"VP> BG'>WtM(( Mn -
b CHKr   