~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/transport/ssh.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2010-09-20 16:15:26 UTC
  • mfrom: (5430.4.1 work)
  • Revision ID: pqm@pqm.ubuntu.com-20100920161526-3r87u084xg7d3pd6
(vila) Tweak tools/check-newsbug.py and do some light editing in
 doc/developers/ppa.txt. (Vincent Ladeuil)

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
363
351
    """Abstract base class for vendors that use pipes to a subprocess."""
364
352
 
365
353
    def _connect(self, argv):
366
 
        proc = subprocess.Popen(argv,
367
 
                                stdin=subprocess.PIPE,
368
 
                                stdout=subprocess.PIPE,
 
354
        # Attempt to make a socketpair to use as stdin/stdout for the SSH
 
355
        # subprocess.  We prefer sockets to pipes because they support
 
356
        # non-blocking short reads, allowing us to optimistically read 64k (or
 
357
        # whatever) chunks.
 
358
        try:
 
359
            my_sock, subproc_sock = socket.socketpair()
 
360
        except (AttributeError, socket.error):
 
361
            # This platform doesn't support socketpair(), so just use ordinary
 
362
            # pipes instead.
 
363
            stdin = stdout = subprocess.PIPE
 
364
            sock = None
 
365
        else:
 
366
            stdin = stdout = subproc_sock
 
367
            sock = my_sock
 
368
        proc = subprocess.Popen(argv, stdin=stdin, stdout=stdout,
369
369
                                **os_specific_subprocess_params())
370
 
        return SSHSubprocess(proc)
 
370
        return SSHSubprocessConnection(proc, sock=sock)
371
371
 
372
372
    def connect_sftp(self, username, password, host, port):
373
373
        try:
377
377
            return SFTPClient(SocketAsChannelAdapter(sock))
378
378
        except _sftp_connection_errors, e:
379
379
            self._raise_connection_error(host, port=port, orig_error=e)
380
 
        except (OSError, IOError), e:
 
380
        except (OSError, IOError, socket.error), e:
381
381
            # If the machine is fast enough, ssh can actually exit
382
382
            # before we try and send it the sftp request, which
383
383
            # raises a Broken Pipe
392
392
            return self._connect(argv)
393
393
        except (EOFError), e:
394
394
            self._raise_connection_error(host, port=port, orig_error=e)
395
 
        except (OSError, IOError), e:
 
395
        except (OSError, IOError, socket.error), e:
396
396
            # If the machine is fast enough, ssh can actually exit
397
397
            # before we try and send it the sftp request, which
398
398
            # raises a Broken Pipe
645
645
_subproc_weakrefs = set()
646
646
 
647
647
def _close_ssh_proc(proc):
648
 
    for func in [proc.stdin.close, proc.stdout.close, proc.wait]:
649
 
        try:
650
 
            func()
 
648
    """Carefully close stdin/stdout and reap the SSH process.
 
649
 
 
650
    If the pipes are already closed and/or the process has already been
 
651
    wait()ed on, that's ok, and no error is raised.  The goal is to do our best
 
652
    to clean up (whether or not a clean up was already tried).
 
653
    """
 
654
    dotted_names = ['stdin.close', 'stdout.close', 'wait']
 
655
    for dotted_name in dotted_names:
 
656
        attrs = dotted_name.split('.')
 
657
        try:
 
658
            obj = proc
 
659
            for attr in attrs:
 
660
                obj = getattr(obj, attr)
 
661
        except AttributeError:
 
662
            # It's ok for proc.stdin or proc.stdout to be None.
 
663
            continue
 
664
        try:
 
665
            obj()
651
666
        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):
 
667
            # It's ok for the pipe to already be closed, or the process to
 
668
            # already be finished.
 
669
            continue
 
670
 
 
671
 
 
672
class SSHConnection(object):
 
673
    """Abstract base class for SSH connections."""
 
674
 
 
675
    def get_sock_or_pipes(self):
 
676
        """Returns a (kind, io_object) pair.
 
677
 
 
678
        If kind == 'socket', then io_object is a socket.
 
679
 
 
680
        If kind == 'pipes', then io_object is a pair of file-like objects
 
681
        (read_from, write_to).
 
682
        """
 
683
        raise NotImplementedError(self.get_sock_or_pipes)
 
684
 
 
685
    def close(self):
 
686
        raise NotImplementedError(self.close)
 
687
 
 
688
 
 
689
class SSHSubprocessConnection(SSHConnection):
 
690
    """A connection to an ssh subprocess via pipes or a socket.
 
691
 
 
692
    This class is also socket-like enough to be used with
 
693
    SocketAsChannelAdapter (it has 'send' and 'recv' methods).
 
694
    """
 
695
 
 
696
    def __init__(self, proc, sock=None):
 
697
        """Constructor.
 
698
 
 
699
        :param proc: a subprocess.Popen
 
700
        :param sock: if proc.stdin/out is a socket from a socketpair, then sock
 
701
            should bzrlib's half of that socketpair.  If not passed, proc's
 
702
            stdin/out is assumed to be ordinary pipes.
 
703
        """
659
704
        self.proc = proc
 
705
        self._sock = sock
660
706
        # Add a weakref to proc that will attempt to do the same as self.close
661
707
        # to avoid leaving processes lingering indefinitely.
662
708
        def terminate(ref):
665
711
        _subproc_weakrefs.add(weakref.ref(self, terminate))
666
712
 
667
713
    def send(self, data):
668
 
        return os.write(self.proc.stdin.fileno(), data)
 
714
        if self._sock is not None:
 
715
            return self._sock.send(data)
 
716
        else:
 
717
            return os.write(self.proc.stdin.fileno(), data)
669
718
 
670
719
    def recv(self, count):
671
 
        return os.read(self.proc.stdout.fileno(), count)
 
720
        if self._sock is not None:
 
721
            return self._sock.recv(count)
 
722
        else:
 
723
            return os.read(self.proc.stdout.fileno(), count)
672
724
 
673
725
    def close(self):
674
726
        _close_ssh_proc(self.proc)
675
727
 
676
 
    def get_filelike_channels(self):
677
 
        return (self.proc.stdout, self.proc.stdin)
 
728
    def get_sock_or_pipes(self):
 
729
        if self._sock is not None:
 
730
            return 'socket', self._sock
 
731
        else:
 
732
            return 'pipes', (self.proc.stdout, self.proc.stdin)
 
733
 
 
734
 
 
735
class _ParamikoSSHConnection(SSHConnection):
 
736
    """An SSH connection via paramiko."""
 
737
 
 
738
    def __init__(self, channel):
 
739
        self.channel = channel
 
740
 
 
741
    def get_sock_or_pipes(self):
 
742
        return ('socket', self.channel)
 
743
 
 
744
    def close(self):
 
745
        return self.channel.close()
 
746
 
678
747