~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-02-11 04:02:41 UTC
  • mfrom: (5017.2.2 tariff)
  • Revision ID: pqm@pqm.ubuntu.com-20100211040241-w6n021dz0uus341n
(mbp) add import-tariff tests

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006-2010 Robey Pointer <robey@lag.net>
 
1
# Copyright (C) 2005 Robey Pointer <robey@lag.net>
2
2
# Copyright (C) 2005, 2006, 2007 Canonical Ltd
3
3
#
4
4
# This program is free software; you can redistribute it and/or modify
173
173
register_ssh_vendor = _ssh_vendor_manager.register_vendor
174
174
 
175
175
 
176
 
def _ignore_signals():
 
176
def _ignore_sigint():
177
177
    # TODO: This should possibly ignore SIGHUP as well, but bzr currently
178
178
    # doesn't handle it itself.
179
179
    # <https://launchpad.net/products/bzr/+bug/41433/+index>
180
180
    import signal
181
181
    signal.signal(signal.SIGINT, signal.SIG_IGN)
182
 
    # GZ 2010-02-19: Perhaps make this check if breakin is installed instead
183
 
    if signal.getsignal(signal.SIGQUIT) != signal.SIG_DFL:
184
 
        signal.signal(signal.SIGQUIT, signal.SIG_IGN)
185
182
 
186
183
 
187
184
class SocketAsChannelAdapter(object):
239
236
    def connect_ssh(self, username, password, host, port, command):
240
237
        """Make an SSH connection.
241
238
 
242
 
        :returns: an SSHConnection.
 
239
        :returns: something with a `close` method, and a `get_filelike_channels`
 
240
            method that returns a pair of (read, write) filelike objects.
243
241
        """
244
242
        raise NotImplementedError(self.connect_ssh)
245
243
 
268
266
register_ssh_vendor('loopback', LoopbackVendor())
269
267
 
270
268
 
 
269
class _ParamikoSSHConnection(object):
 
270
    def __init__(self, channel):
 
271
        self.channel = channel
 
272
 
 
273
    def get_filelike_channels(self):
 
274
        return self.channel.makefile('rb'), self.channel.makefile('wb')
 
275
 
 
276
    def close(self):
 
277
        return self.channel.close()
 
278
 
 
279
 
271
280
class ParamikoVendor(SSHVendor):
272
281
    """Vendor that uses paramiko."""
273
282
 
351
360
    """Abstract base class for vendors that use pipes to a subprocess."""
352
361
 
353
362
    def _connect(self, argv):
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,
 
363
        proc = subprocess.Popen(argv,
 
364
                                stdin=subprocess.PIPE,
 
365
                                stdout=subprocess.PIPE,
369
366
                                **os_specific_subprocess_params())
370
 
        return SSHSubprocessConnection(proc, sock=sock)
 
367
        return SSHSubprocess(proc)
371
368
 
372
369
    def connect_sftp(self, username, password, host, port):
373
370
        try:
637
634
        # Running it in a separate process group is not good because then it
638
635
        # can't get non-echoed input of a password or passphrase.
639
636
        # <https://launchpad.net/products/bzr/+bug/40508>
640
 
        return {'preexec_fn': _ignore_signals,
 
637
        return {'preexec_fn': _ignore_sigint,
641
638
                'close_fds': True,
642
639
                }
643
640
 
652
649
            pass
653
650
 
654
651
 
655
 
class SSHConnection(object):
656
 
    """Abstract base class for SSH connections."""
657
 
 
658
 
    def get_sock_or_pipes(self):
659
 
        """Returns a (kind, io_object) pair.
660
 
 
661
 
        If kind == 'socket', then io_object is a socket.
662
 
 
663
 
        If kind == 'pipes', then io_object is a pair of file-like objects
664
 
        (read_from, write_to).
665
 
        """
666
 
        raise NotImplementedError(self.get_sock_or_pipes)
667
 
 
668
 
    def close(self):
669
 
        raise NotImplementedError(self.close)
670
 
 
671
 
 
672
 
class SSHSubprocessConnection(SSHConnection):
673
 
    """A connection to an ssh subprocess via pipes or a socket.
674
 
 
675
 
    This class is also socket-like enough to be used with
676
 
    SocketAsChannelAdapter (it has 'send' and 'recv' methods).
677
 
    """
678
 
 
679
 
    def __init__(self, proc, sock=None):
680
 
        """Constructor.
681
 
 
682
 
        :param proc: a subprocess.Popen
683
 
        :param sock: if proc.stdin/out is a socket from a socketpair, then sock
684
 
            should bzrlib's half of that socketpair.  If not passed, proc's
685
 
            stdin/out is assumed to be ordinary pipes.
686
 
        """
 
652
class SSHSubprocess(object):
 
653
    """A socket-like object that talks to an ssh subprocess via pipes."""
 
654
 
 
655
    def __init__(self, proc):
687
656
        self.proc = proc
688
 
        self._sock = sock
689
657
        # Add a weakref to proc that will attempt to do the same as self.close
690
658
        # to avoid leaving processes lingering indefinitely.
691
659
        def terminate(ref):
694
662
        _subproc_weakrefs.add(weakref.ref(self, terminate))
695
663
 
696
664
    def send(self, data):
697
 
        if self._sock is not None:
698
 
            return self._sock.send(data)
699
 
        else:
700
 
            return os.write(self.proc.stdin.fileno(), data)
 
665
        return os.write(self.proc.stdin.fileno(), data)
701
666
 
702
667
    def recv(self, count):
703
 
        if self._sock is not None:
704
 
            return self._sock.recv(count)
705
 
        else:
706
 
            return os.read(self.proc.stdout.fileno(), count)
 
668
        return os.read(self.proc.stdout.fileno(), count)
707
669
 
708
670
    def close(self):
709
671
        _close_ssh_proc(self.proc)
710
672
 
711
 
    def get_sock_or_pipes(self):
712
 
        if self._sock is not None:
713
 
            return 'socket', self._sock
714
 
        else:
715
 
            return 'pipes', (self.proc.stdout, self.proc.stdin)
716
 
 
717
 
 
718
 
class _ParamikoSSHConnection(SSHConnection):
719
 
    """An SSH connection via paramiko."""
720
 
 
721
 
    def __init__(self, channel):
722
 
        self.channel = channel
723
 
 
724
 
    def get_sock_or_pipes(self):
725
 
        return ('socket', self.channel)
726
 
 
727
 
    def close(self):
728
 
        return self.channel.close()
729
 
 
 
673
    def get_filelike_channels(self):
 
674
        return (self.proc.stdout, self.proc.stdin)
730
675