
    	^cY                         d dl Z d dlZddlmZ ddlmZ ddlmZmZmZm	Z	m
Z
mZ ddlmZmZmZ d Zdd	Z G d
 d          Zd ZddZd Zd Z G d de
j                  ZddZddZdS )    N   )_)getattr)errorpycompatutilwireprototypeswireprotov1peerwireprotov1server)procutil
stringutilurlutilc                 j    | s| S t          j        d|           r| S d|                     dd          z  S )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      sB     	x*A.. QYYtX....    Fc                     |r^|j         sYt          j        |          }|rE|r| j        n| j        }|                                D ]"} |t          d          |d           dS dS dS dS )zYdisplay 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      2DK 2d## 	2!%4bgg29G\\^^ 2 2+511112 2 2 2	2 	22 2r   c                   `    e Zd ZdZd Zd Zd Zd Zd Zd Z	dd	Z
d
 Zed             Zd ZdS )
doublepipea3  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                 0    || _         || _        || _        d S N)_ui_main_side)selfr    mainsides       r   __init__zdoublepipe.__init__C   s    



r   c                 |   t          | j        t          j                  r| j        j        rdS | j                                        | j                                        g}	 t          j        |          }n# t          $ r |}Y nw xY w| j                                        |v | j                                        |v fS )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)	
isinstancer*   r   bufferedinputpipe	hasbufferfilenor+   pollNotImplementedError)r,   fdsacts      r   _waitzdoublepipe._waitH   s     tz4#9::	
$	
 :z  ""DJ$5$5$7$78	)C..CC" 	 	 	CCC	 
!!##s*DJ,=,=,?,?3,FGGs   !A6 6BBc                 .    |                      d|          S )Ns   write_call)r,   datas     r   writezdoublepipe.write^   s    zz(D)))r   c                 v    |                      d|          }|dk    r|st          | j        | j                   |S )Ns   readr   r<   r$   r)   r+   r,   sizers      r   readzdoublepipe.reada   s=    JJw%%199Q9 48TZ000r   c                 v    |                      d|          }|dk    r|st          | j        | j                   |S )Ns   unbufferedreadr   r@   rA   s      r   unbufferedreadzdoublepipe.unbufferedreadl   s>    JJ($//199Q9 48TZ000r   c                 ,    |                      d          S )Ns   readliner;   r,   s    r   readlinezdoublepipe.readlinew   s    zz+&&&r   Nc                     ||r| j         j        rt          | j        | j                   dS 	 |                                 \  }}|rt          | j        | j                   |r,t          | j         |          }|
 |            S  ||          S b)zBcall <methname> on "main", forward output of "side" while blockingNr   )r*   r   r$   r)   r+   r9   r   )r,   methnamer=   	mainready	sidereadymeths         r   r<   zdoublepipe._callz   s     Tdj.?48TZ0003		&#'::<< Iy 5tx444 &tz844<466M4::%		&r   c                 4    | j                                         S r(   )r*   closerH   s    r   rP   zdoublepipe.close       z!!!r   c                     | j         j        S r(   )r*   r   rH   s    r   r   zdoublepipe.closed   s    z  r   c                 4    | j                                         S r(   )r*   flushrH   s    r   rT   zdoublepipe.flush   rQ   r   r(   )__name__
__module____qualname____doc__r/   r9   r>   rD   rF   rI   r<   rP   propertyr   rT    r   r   r&   r&   2   s        B B   
H H H,* * *	 	 		 	 	' ' '& & & &"" " " ! ! X!" " " " "r   r&   c                    d}|r|j         sd}|                                 |r|j         sd}|                                 |r^|j         sWd}	 |D ]%}|                     t          d          |           &n# t          t
          f$ r Y nw xY w|                                 |r||                     d|z             dS dS dS )z)Clean up pipes used by an SSH connection.FTr   Ns.   missing close on SSH connection created at:
%s)r   rP   r   r   IOError
ValueError	develwarn)r    pipeipipeopipeer   didsomethingr#   s          r   _cleanuppipesrc      s   L U\  U\  	U\ 		 - -		!K..!,,,,-$ 	 	 	D	 	 P( 	G$NOOOOOP P((s   (A6 6B
	B
c           
          d||t          j        dt          |          t          |          fz            fz  }|                     d|z             t          j        |d|          \  }}}	}
|
|||	fS )zCreate 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   running %s
r   )bufsizeenv)r   
shellquoter   debugpopen4)r    sshcmdargs	remotecmdpathsshenvcmdstdinstdoutstderrprocs              r   _makeconnectionrv      s     %I&&T(:(:;<	
 	
 C HH_s"###
 #+/#qf"M"M"ME664&&r   c                      dh} d t           j                            t           j                  D             }|                     dd                    |          z             | S )zwReturn list of capabilities of this client.

    Returns a list of capabilities that are supported by this client.
    s   partial-pullc                 @    g | ]}|                                 j        S rZ   )wireprotosupportname).0es     r   
<listcomp>z'_clientcapabilities.<locals>.<listcomp>   s7        	
!  r   s   comp=%s   ,)r   compenginessupportedwireengines
CLIENTROLEaddjoin)protoparamscompss     r   _clientcapabilitiesr      sh    
 ##K !66tGG  E OOJ5!1!11222r   c                       fd}                      dd          }t          j        t          t	          j                                        }	 ddz  }dddt          |          z  |g}|r:                     d	                                d
t          |          z                                  d                                d           |                    d	                    |                     |
                                 n# t          $ r  |             Y nw xY wt          j        }	t          j        dt!          j        |          z            }
ddg}d}|d         r|r	 |                                }t'           d           |
                    |          }|r.|                    d          }	                     d|	z             nt|d         dk    r|dk    rna|r                     d|           |                    |           |dz  }n# t          $ r  |             Y nw xY w|d         r| |             t/                      }|	t          j        k    rqt1          |          D ]a}|                    d          rJ|                    |d d                             d          d                                                     nb|s
 |             t'           d           |	|fS )Nc                      t          d           t          d          }                     dd          }t          j        | |          )NTr   s#   no suitable response from remote hg   uis   ssherrorhinthint)r$   r   configr   	RepoError)msgr   rt   r    s     r   badresponsez&_performhandshake.<locals>.badresponse   sP     	r6----677yy00oc----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uuid4lenrj   r>   r   rT   r\   r	   SSHV1r   compiler   reescaperI   r$   r   groupappendsetreversed
startswithupdatesplit)r    rr   rs   rt   r   
requestlogtokenpairsarg	handshake	protoname
reupgradedlines	max_noiser#   mcapss   `  `            r   _performhandshaker      sW   
. 
. 
. 
. 
. 
.@ x)>??J c$*,,//00E44CMM)	
	  	QHH;<<<HH?#h--OPPP
+,,,
-...CHHY''(((    $I1J4G4N4NNOOJ(OEI
) 	 	!!A2vD1111   ##A GGAJJ	5	ABBB 
 RyF""qEzz )a(((LLOOONII 	 	 	KMMMMM	3 ) 	 8 	55D N(((% 	 	A ||,-- AcrcFLL..q17799:::   2vD))))d?s2   B=D D'&D'0A*H! H! .2H! !H87H8c                       e Zd Z	 ddZdhZd Zd Zd Zd Zd Z	d	 Z
d
 Zd ZddZd ZddZd Zd Zd Zd Zd Zd Zd ZddZdS )	sshv1peerTc	                 :   || _         || _        || _        |r4t          |t	          j        |          |          }t          |||          }|| _        || _        || _        || _	        || _
        d                    t	          j        d                    | _        dS )a  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   N)_urlr    _subprocessr&   r   r2   _pipeo_pipei_pipee_caps_autoreadstderrr   getstackframes
_initstack)	r,   r    urlru   rr   rs   rt   r   autoreadstderrs	            r   r/   zsshv1peer.__init__v  s     	    	2D$:6$B$BFKKFr5&11E
-((4#6q#9#9::r   s   batchc                     | j         S r(   )r   rH   s    r   r   zsshv1peer.url  s
    yr   c                     d S r(   rZ   rH   s    r   localzsshv1peer.local      tr   c                     | S r(   rZ   rH   s    r   peerzsshv1peer.peer  s    r   c                     dS )NTrZ   rH   s    r   canpushzsshv1peer.canpush  r   r   c                 .    |                                   d S r(   _cleanuprH   s    r   rP   zsshv1peer.close  s    r   c                     | j         S r(   )r   rH   s    r   capabilitieszsshv1peer.capabilities  s
    zr   c                 :    t          | j        | j                   d S r(   )r$   r    r   rH   s    r   _readerrzsshv1peer._readerr  s    tw,,,,,r   c                 .    |                                   |r(   r   )r,   	exceptions     r   _abortzsshv1peer._abort  s    r   Nc                 V    t          | j        | j        | j        | j        |           d S Nr   )rc   r    r   r   r   )r,   r   s     r   r   zsshv1peer._cleanup  s)    dgt{DK4PPPPPPr   c                 <    |                      | j                   d S r   )r   r   rH   s    r   __del__zsshv1peer.__del__  s    4?+++++r   Fc           
      B   | j         j        r| j                             dd          r| j         j        }d} |||z             t	          |                                          D ]\  }}t          |t                    s! ||dz  |t          |          fz             ;t	          |                                          D ]&\  }}	 ||dz  ||t          |	          fz             '| j                             d|z             | j	        
                    d|z             t          j        |         \  }
}|                                }i }|D ]}|dk    r||d<    n||         ||<   ||= t	          |                                          D ]\  }}| j	        
                    d	|t          |          fz             t          |t                    ra|                                D ]K\  }}	| j	        
                    d	|t          |	          fz             | j	        
                    |	           L| j	        
                    |           | j	                                         |r.|                                 }t!          j        | j        |          S | j        S )
Nr   r   s   devel-peer-request: %s
s     %s: %d bytess     %s-%s: %ds   sending %s command
s   %s
   *s   %s %d
)r    	debugflagr   rj   sorteditemsr1   dictr   r   r>   r   commandsr   rT   
_getamountr   cappedreaderr   )r,   rq   rm   frameddbglinekeyvaluedkdv_funcnameskeyswireargskvamounts                    r   _sendrequestzsshv1peer._sendrequest  s   7 	H!3!3+"
 "
 	H '-C.DCs
OOO$TZZ\\22 H H
U!%.. HC00CU3DDEEEE"("7"7 H HBD>1S"c"gg4FFGGGGH-3444'C-((((1#6u{{}} 	 	ADyy!%"1gGG8>>++,, 	% 	%DAqKjAs1vv;6777!T"" %ggii * *FBK%%jBB=&@AAAK%%b))))* !!!$$$$  	:__&&F$T[&999{r   c                 h    t          j        |          }|                     |||| j        v           S Nr   r   byteskwargsr   _FRAMED_COMMANDSr,   rq   rm   s      r   _callstreamzsshv1peer._callstream  5    #D))  d3$:O3O PPPr   c                 h    t          j        |          }|                     |||| j        v           S r   r   r   s      r   _callcompressablezsshv1peer._callcompressable  r   r   c                 ~    t          j        |          }|                     ||d                                          S )NTr   )r   r   r   rD   r   s      r   r<   zsshv1peer._call  s9    #D))  d4 88==???r   c                     | j         |fi |}|rd|fS t          fdd          D ]}|                     |           |                     dd           |                                 }|rd|fS |                                 dfS )Nr   c                  .                          d          S Ni   rD   fps   r   <lambda>z%sshv1peer._callpush.<locals>.<lambda>      bggdmm r   TrT   )r<   iter_writeframed_readframedr,   rq   r   rm   rC   ds     `   r   	_callpushzsshv1peer._callpush  s     DJs##d## 	6M ++++S11 	! 	!Aa    #T***
  	6M!!3&&r   c                      | j         |fi |}|r$t          j        t          d          |z            t	          fdd          D ]}|                     |           |                     dd           | j        S )Ns   unexpected remote reply: %sc                  .                          d          S r   r   r   s   r   r   z-sshv1peer._calltwowaystream.<locals>.<lambda>  r   r   r   Tr  )r<   r   Abortr   r  r  r   r  s     `   r   _calltwowaystreamzsshv1peer._calltwowaystream  s     DJs##d## 	E+a >??!CDDD ++++S11 	! 	!Aa    #T***{r   c                    | j                                         }|dk    rR| j        r|                                  t	          d          }|                     t          j        |                     | j        r|                                  	 t          |          S # t          $ r9 |                     t          j
        t	          d          |                     Y d S w xY w)Nr   s   check previous remote outputr   s   unexpected response:)r   rI   r   r   r   r   r   OutOfBandErrorintr]   ResponseError)r,   r#   r   s      r   r   zsshv1peer._getamount   s    K  ""::#  344CKK,#666777 	MMOOO	Lq66M 	L 	L 	LKK+A.E,F,FJJKKKKKK	Ls   B ?C C c                 f    |                                  }|sdS | j                            |          S )Nr   )r   r   rD   )r,   rB   s     r   r  zsshv1peer._readframed.  s4       	3{%%%r   c                    | j                             dt          |          z             |r| j                             |           |r| j                                          | j        r|                                  d S d S )Ns   %d
)r   r>   r   rT   r   r   )r,   r=   rT   s      r   r  zsshv1peer._writeframed5  s    'CII-... 	$Kd### 	 K 	MMOOOOO	 	r   Tr(   F)rU   rV   rW   r/   r   r   r   r   r   rP   r   r   r   r   r   r   r   r   r<   r  r  r   r  r  rZ   r   r   r   r   u  sv       IM; ; ; ;D 	            
- - -  Q Q Q Q, , ,+ + + +ZQ Q QQ Q Q@ @ @' ' '*   L L L& & &     r   r   Tc           
      4   	 t          | |||          \  }}n## t          $ r t          | |||d            w xY w|t          j        k    rt          | |||||||          S t          | |||d           t          j        t          d          |z            )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   s#   unknown version of SSH protocol: %s)	r   	Exceptionrc   r	   r   r   r   r   r   )	r    ro   ru   rr   rs   rt   r   r   r   s	            r   makepeerr  ?  s    +BvvFF	44   b&%d;;;; N((()	
 	
 	
 		
 	b&%d;;;;o455	A
 
 	
s     8c           
      H   t          j        |dd          }|j        dk    s|j        r|j        $t          j        t          d          |z            t          j        |           |j	        !t          j        t          d                    | 
                    dd          }| 
                    dd          }t          |                     d	                    }t          j        |          }	|j        pd
}
t          j        ||j        |j        |j                  }|r|r!t          j        t          d                    d||t          j        dt'          |          t'          |
          fz            fz  }|                     d|z             |                     |d|	          }|dk    r!t          j        t          d                    t-          | ||||
|	          \  }}}}t/          | |||||          }d|                                v r	 |                    dd                    t7          t9                                                     nC# t:          $ r6 |                                 t          j        t          d                    w xY w|S )ziCreate an SSH peer.

    The returned object conforms to the ``wireprotov1peer.wirepeer`` interface.
    F)
parsequeryparsefragments   sshNs   couldn't parse location %ss   password in URL not supportedr   s	   remotecmds   sshenv   .s8   cannot create remote SSH repositories with extra optionsre   s
   %s init %srf   s   sshpeer)
blockedtagenvironr   s   could not create remote repos	   protocaps    )r   s   capability exchange failed)r   r   schemehostro   r   r   r   checksafesshpasswdr   r   configitemsr   shellenvironsshargsuserportri   r   rj   systemrv   r  r   r<   r   r   r   r\   r   )r    ro   createintents
createoptsurl   rn   	sshaddenvrp   
remotepathrm   rq   resru   rr   rs   rt   r   s                      r   instancer/  e  s   
 	DU%@@@Ax616>oa =>>EFFFxoa @AABBBYYuf%%F		%..IR^^I..//I"9--F4JFAFAFAF;;D F
  	/*    	**L,D,DEF 
 
 	3&'''ii
FiCC!88/!$C"D"DEEE"1
FD)Z# #D% BdE66::D t((****	DJJ499V4G4I4I-J-J#K#K       	D 	D 	DMMOOO/!$A"B"BCCC	D Ks   AI A Jr  r(   r  )NN)r   r   i18nr   r   r    r   r   r	   r
   r   utilsr   r   r   r   r$   r&   rc   rv   r   r   wirepeerr   r  r/  rZ   r   r   <module>r4     s   
			                                     / / /	2 	2 	2 	2a" a" a" a" a" a" a" a"HP P P<' ' ' '2  W W WtG G G G G( G G GT#
 #
 #
 #
LA A A A A Ar   