comparison mercurial/sshpeer.py @ 36487:dabf86721200

sshpeer: factor out code for creating peers from pipes An upcoming commit will want to instantiate an SSH peer via an alternate mechanism that doesn't require running a new `ssh` command. To facilitate that, we extract the code for creating a peer from pipes to its own function. Differential Revision: https://phab.mercurial-scm.org/D2391
author Gregory Szorc <gregory.szorc@gmail.com>
date Sun, 25 Feb 2018 11:00:53 -0800
parents b8d0761a85c7
children 1a36ef7df70a
comparison
equal deleted inserted replaced
36486:fb9b8004e1b0 36487:dabf86721200
529 """A peer that speakers version 2 of the transport protocol.""" 529 """A peer that speakers version 2 of the transport protocol."""
530 # Currently version 2 is identical to version 1 post handshake. 530 # Currently version 2 is identical to version 1 post handshake.
531 # And handshake is performed before the peer is instantiated. So 531 # And handshake is performed before the peer is instantiated. So
532 # we need no custom code. 532 # we need no custom code.
533 533
534 def makepeer(ui, path, proc, stdin, stdout, stderr):
535 """Make a peer instance from existing pipes.
536
537 ``path`` and ``proc`` are stored on the eventual peer instance and may
538 not be used for anything meaningful.
539
540 ``stdin``, ``stdout``, and ``stderr`` are the pipes connected to the
541 SSH server's stdio handles.
542
543 This function is factored out to allow creating peers that don't
544 actually spawn a new process. It is useful for starting SSH protocol
545 servers and clients via non-standard means, which can be useful for
546 testing.
547 """
548 try:
549 protoname, caps = _performhandshake(ui, stdin, stdout, stderr)
550 except Exception:
551 _cleanuppipes(ui, stdout, stdin, stderr)
552 raise
553
554 if protoname == wireprotoserver.SSHV1:
555 return sshv1peer(ui, path, proc, stdin, stdout, stderr, caps)
556 elif protoname == wireprotoserver.SSHV2:
557 return sshv2peer(ui, path, proc, stdin, stdout, stderr, caps)
558 else:
559 _cleanuppipes(ui, stdout, stdin, stderr)
560 raise error.RepoError(_('unknown version of SSH protocol: %s') %
561 protoname)
562
534 def instance(ui, path, create): 563 def instance(ui, path, create):
535 """Create an SSH peer. 564 """Create an SSH peer.
536 565
537 The returned object conforms to the ``wireproto.wirepeer`` interface. 566 The returned object conforms to the ``wireproto.wirepeer`` interface.
538 """ 567 """
563 raise error.RepoError(_('could not create remote repo')) 592 raise error.RepoError(_('could not create remote repo'))
564 593
565 proc, stdin, stdout, stderr = _makeconnection(ui, sshcmd, args, remotecmd, 594 proc, stdin, stdout, stderr = _makeconnection(ui, sshcmd, args, remotecmd,
566 remotepath, sshenv) 595 remotepath, sshenv)
567 596
568 try: 597 return makepeer(ui, path, proc, stdin, stdout, stderr)
569 protoname, caps = _performhandshake(ui, stdin, stdout, stderr)
570 except Exception:
571 _cleanuppipes(ui, stdout, stdin, stderr)
572 raise
573
574 if protoname == wireprotoserver.SSHV1:
575 return sshv1peer(ui, path, proc, stdin, stdout, stderr, caps)
576 elif protoname == wireprotoserver.SSHV2:
577 return sshv2peer(ui, path, proc, stdin, stdout, stderr, caps)
578 else:
579 _cleanuppipes(ui, stdout, stdin, stderr)
580 raise error.RepoError(_('unknown version of SSH protocol: %s') %
581 protoname)