/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/transport/ssh.py

  • Committer: John Arbash Meinel
  • Date: 2010-09-29 20:18:37 UTC
  • mfrom: (5450 +trunk)
  • mto: This revision was merged to the branch mainline in revision 5452.
  • Revision ID: john@arbash-meinel.com-20100929201837-6d9jhvjokfe3ubvk
Merge bzr.dev 5450 to resolve NEWS and criss-cross merge.

Show diffs side-by-side

added added

removed removed

Lines of Context:
131
131
        # is given in BZR_SSH. See https://bugs.launchpad.net/bugs/414743
132
132
        elif 'plink' in version and progname == 'plink':
133
133
            # Checking if "plink" was the executed argument as Windows
134
 
            # sometimes reports 'ssh -V' incorrectly with 'plink' in it's
 
134
            # sometimes reports 'ssh -V' incorrectly with 'plink' in its
135
135
            # version.  See https://bugs.launchpad.net/bzr/+bug/107155
136
136
            trace.mutter("ssh implementation is Putty's plink.")
137
137
            vendor = PLinkSubprocessVendor()
239
239
    def connect_ssh(self, username, password, host, port, command):
240
240
        """Make an SSH connection.
241
241
 
242
 
        :returns: something with a `close` method, and a `get_filelike_channels`
243
 
            method that returns a pair of (read, write) filelike objects.
 
242
        :returns: an SSHConnection.
244
243
        """
245
244
        raise NotImplementedError(self.connect_ssh)
246
245
 
269
268
register_ssh_vendor('loopback', LoopbackVendor())
270
269
 
271
270
 
272
 
class _ParamikoSSHConnection(object):
273
 
    def __init__(self, channel):
274
 
        self.channel = channel
275
 
 
276
 
    def get_filelike_channels(self):
277
 
        return self.channel.makefile('rb'), self.channel.makefile('wb')
278
 
 
279
 
    def close(self):
280
 
        return self.channel.close()
281
 
 
282
 
 
283
271
class ParamikoVendor(SSHVendor):
284
272
    """Vendor that uses paramiko."""
285
273
 
348
336
            self._raise_connection_error(host, port=port, orig_error=e,
349
337
                                         msg='Unable to invoke remote bzr')
350
338
 
 
339
_ssh_connection_errors = (EOFError, OSError, IOError, socket.error)
351
340
if paramiko is not None:
352
341
    vendor = ParamikoVendor()
353
342
    register_ssh_vendor('paramiko', vendor)
354
343
    register_ssh_vendor('none', vendor)
355
344
    register_default_ssh_vendor(vendor)
356
 
    _sftp_connection_errors = (EOFError, paramiko.SSHException)
 
345
    _ssh_connection_errors += (paramiko.SSHException,)
357
346
    del vendor
358
 
else:
359
 
    _sftp_connection_errors = (EOFError,)
360
347
 
361
348
 
362
349
class SubprocessVendor(SSHVendor):
363
350
    """Abstract base class for vendors that use pipes to a subprocess."""
364
351
 
365
352
    def _connect(self, argv):
366
 
        proc = subprocess.Popen(argv,
367
 
                                stdin=subprocess.PIPE,
368
 
                                stdout=subprocess.PIPE,
 
353
        # Attempt to make a socketpair to use as stdin/stdout for the SSH
 
354
        # subprocess.  We prefer sockets to pipes because they support
 
355
        # non-blocking short reads, allowing us to optimistically read 64k (or
 
356
        # whatever) chunks.
 
357
        try:
 
358
            my_sock, subproc_sock = socket.socketpair()
 
359
        except (AttributeError, socket.error):
 
360
            # This platform doesn't support socketpair(), so just use ordinary
 
361
            # pipes instead.
 
362
            stdin = stdout = subprocess.PIPE
 
363
            sock = None
 
364
        else:
 
365
            stdin = stdout = subproc_sock
 
366
            sock = my_sock
 
367
        proc = subprocess.Popen(argv, stdin=stdin, stdout=stdout,
369
368
                                **os_specific_subprocess_params())
370
 
        return SSHSubprocess(proc)
 
369
        return SSHSubprocessConnection(proc, sock=sock)
371
370
 
372
371
    def connect_sftp(self, username, password, host, port):
373
372
        try:
375
374
                                                  subsystem='sftp')
376
375
            sock = self._connect(argv)
377
376
            return SFTPClient(SocketAsChannelAdapter(sock))
378
 
        except _sftp_connection_errors, e:
379
 
            self._raise_connection_error(host, port=port, orig_error=e)
380
 
        except (OSError, IOError), e:
381
 
            # If the machine is fast enough, ssh can actually exit
382
 
            # before we try and send it the sftp request, which
383
 
            # raises a Broken Pipe
384
 
            if e.errno not in (errno.EPIPE,):
385
 
                raise
 
377
        except _ssh_connection_errors, e:
386
378
            self._raise_connection_error(host, port=port, orig_error=e)
387
379
 
388
380
    def connect_ssh(self, username, password, host, port, command):
390
382
            argv = self._get_vendor_specific_argv(username, host, port,
391
383
                                                  command=command)
392
384
            return self._connect(argv)
393
 
        except (EOFError), e:
394
 
            self._raise_connection_error(host, port=port, orig_error=e)
395
 
        except (OSError, IOError), e:
396
 
            # If the machine is fast enough, ssh can actually exit
397
 
            # before we try and send it the sftp request, which
398
 
            # raises a Broken Pipe
399
 
            if e.errno not in (errno.EPIPE,):
400
 
                raise
 
385
        except _ssh_connection_errors, e:
401
386
            self._raise_connection_error(host, port=port, orig_error=e)
402
387
 
403
388
    def _get_vendor_specific_argv(self, username, host, port, subsystem=None,
645
630
_subproc_weakrefs = set()
646
631
 
647
632
def _close_ssh_proc(proc):
648
 
    for func in [proc.stdin.close, proc.stdout.close, proc.wait]:
649
 
        try:
650
 
            func()
 
633
    """Carefully close stdin/stdout and reap the SSH process.
 
634
 
 
635
    If the pipes are already closed and/or the process has already been
 
636
    wait()ed on, that's ok, and no error is raised.  The goal is to do our best
 
637
    to clean up (whether or not a clean up was already tried).
 
638
    """
 
639
    dotted_names = ['stdin.close', 'stdout.close', 'wait']
 
640
    for dotted_name in dotted_names:
 
641
        attrs = dotted_name.split('.')
 
642
        try:
 
643
            obj = proc
 
644
            for attr in attrs:
 
645
                obj = getattr(obj, attr)
 
646
        except AttributeError:
 
647
            # It's ok for proc.stdin or proc.stdout to be None.
 
648
            continue
 
649
        try:
 
650
            obj()
651
651
        except OSError:
652
 
            pass
653
 
 
654
 
 
655
 
class SSHSubprocess(object):
656
 
    """A socket-like object that talks to an ssh subprocess via pipes."""
657
 
 
658
 
    def __init__(self, proc):
 
652
            # It's ok for the pipe to already be closed, or the process to
 
653
            # already be finished.
 
654
            continue
 
655
 
 
656
 
 
657
class SSHConnection(object):
 
658
    """Abstract base class for SSH connections."""
 
659
 
 
660
    def get_sock_or_pipes(self):
 
661
        """Returns a (kind, io_object) pair.
 
662
 
 
663
        If kind == 'socket', then io_object is a socket.
 
664
 
 
665
        If kind == 'pipes', then io_object is a pair of file-like objects
 
666
        (read_from, write_to).
 
667
        """
 
668
        raise NotImplementedError(self.get_sock_or_pipes)
 
669
 
 
670
    def close(self):
 
671
        raise NotImplementedError(self.close)
 
672
 
 
673
 
 
674
class SSHSubprocessConnection(SSHConnection):
 
675
    """A connection to an ssh subprocess via pipes or a socket.
 
676
 
 
677
    This class is also socket-like enough to be used with
 
678
    SocketAsChannelAdapter (it has 'send' and 'recv' methods).
 
679
    """
 
680
 
 
681
    def __init__(self, proc, sock=None):
 
682
        """Constructor.
 
683
 
 
684
        :param proc: a subprocess.Popen
 
685
        :param sock: if proc.stdin/out is a socket from a socketpair, then sock
 
686
            should bzrlib's half of that socketpair.  If not passed, proc's
 
687
            stdin/out is assumed to be ordinary pipes.
 
688
        """
659
689
        self.proc = proc
 
690
        self._sock = sock
660
691
        # Add a weakref to proc that will attempt to do the same as self.close
661
692
        # to avoid leaving processes lingering indefinitely.
662
693
        def terminate(ref):
665
696
        _subproc_weakrefs.add(weakref.ref(self, terminate))
666
697
 
667
698
    def send(self, data):
668
 
        return os.write(self.proc.stdin.fileno(), data)
 
699
        if self._sock is not None:
 
700
            return self._sock.send(data)
 
701
        else:
 
702
            return os.write(self.proc.stdin.fileno(), data)
669
703
 
670
704
    def recv(self, count):
671
 
        return os.read(self.proc.stdout.fileno(), count)
 
705
        if self._sock is not None:
 
706
            return self._sock.recv(count)
 
707
        else:
 
708
            return os.read(self.proc.stdout.fileno(), count)
672
709
 
673
710
    def close(self):
674
711
        _close_ssh_proc(self.proc)
675
712
 
676
 
    def get_filelike_channels(self):
677
 
        return (self.proc.stdout, self.proc.stdin)
 
713
    def get_sock_or_pipes(self):
 
714
        if self._sock is not None:
 
715
            return 'socket', self._sock
 
716
        else:
 
717
            return 'pipes', (self.proc.stdout, self.proc.stdin)
 
718
 
 
719
 
 
720
class _ParamikoSSHConnection(SSHConnection):
 
721
    """An SSH connection via paramiko."""
 
722
 
 
723
    def __init__(self, channel):
 
724
        self.channel = channel
 
725
 
 
726
    def get_sock_or_pipes(self):
 
727
        return ('socket', self.channel)
 
728
 
 
729
    def close(self):
 
730
        return self.channel.close()
 
731
 
678
732