~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/transport/ssh.py

  • Committer: Robert Collins
  • Date: 2010-07-04 06:22:11 UTC
  • mto: This revision was merged to the branch mainline in revision 5332.
  • Revision ID: robertc@robertcollins.net-20100704062211-tk9hw6bnsn5x47fm
``bzrlib.lsprof.profile`` will no longer silently generate bad threaded
profiles when concurrent profile requests are made. Instead the profile
requests will be serialised. Reentrant requests will now deadlock.
(Robert Collins)

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:
652
652
            pass
653
653
 
654
654
 
655
 
class SSHSubprocess(object):
656
 
    """A socket-like object that talks to an ssh subprocess via pipes."""
657
 
 
658
 
    def __init__(self, proc):
 
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
        """
659
687
        self.proc = proc
 
688
        self._sock = sock
660
689
        # Add a weakref to proc that will attempt to do the same as self.close
661
690
        # to avoid leaving processes lingering indefinitely.
662
691
        def terminate(ref):
665
694
        _subproc_weakrefs.add(weakref.ref(self, terminate))
666
695
 
667
696
    def send(self, data):
668
 
        return os.write(self.proc.stdin.fileno(), 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)
669
701
 
670
702
    def recv(self, count):
671
 
        return os.read(self.proc.stdout.fileno(), 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)
672
707
 
673
708
    def close(self):
674
709
        _close_ssh_proc(self.proc)
675
710
 
676
 
    def get_filelike_channels(self):
677
 
        return (self.proc.stdout, self.proc.stdin)
 
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
 
678
730