/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: Canonical.com Patch Queue Manager
  • Date: 2006-09-19 03:52:02 UTC
  • mfrom: (2018.1.11 bzr+ssh:// testing)
  • Revision ID: pqm@pqm.ubuntu.com-20060919035202-8174b4dc7ff91add
(Andrew Bennetts, Robert Collins) Add bzr+ssh:// url support and turn the smart server into a anonymous readonly server by default.

Show diffs side-by-side

added added

removed removed

Lines of Context:
154
154
        raise NotImplementedError(self.connect_sftp)
155
155
 
156
156
    def connect_ssh(self, username, password, host, port, command):
157
 
        """Make an SSH connection, and return a pipe-like object.
 
157
        """Make an SSH connection.
158
158
        
159
 
        (This is currently unused, it's just here to indicate future directions
160
 
        for this code.)
 
159
        :returns: something with a `close` method, and a `get_filelike_channels`
 
160
            method that returns a pair of (read, write) filelike objects.
161
161
        """
162
162
        raise NotImplementedError(self.connect_ssh)
163
163
        
177
177
register_ssh_vendor('loopback', LoopbackVendor())
178
178
 
179
179
 
 
180
class _ParamikoSSHConnection(object):
 
181
    def __init__(self, channel):
 
182
        self.channel = channel
 
183
 
 
184
    def get_filelike_channels(self):
 
185
        return self.channel.makefile('rb'), self.channel.makefile('wb')
 
186
 
 
187
    def close(self):
 
188
        return self.channel.close()
 
189
 
 
190
 
180
191
class ParamikoVendor(SSHVendor):
181
192
    """Vendor that uses paramiko."""
182
193
 
183
 
    def connect_sftp(self, username, password, host, port):
 
194
    def _connect(self, username, password, host, port):
184
195
        global SYSTEM_HOSTKEYS, BZR_HOSTKEYS
185
196
        
186
197
        load_host_keys()
218
229
                ['Try editing %s or %s' % (filename1, filename2)])
219
230
 
220
231
        _paramiko_auth(username, password, host, t)
 
232
        return t
221
233
        
 
234
    def connect_sftp(self, username, password, host, port):
 
235
        t = self._connect(username, password, host, port)
222
236
        try:
223
 
            sftp = t.open_sftp_client()
 
237
            return t.open_sftp_client()
224
238
        except paramiko.SSHException, e:
225
239
            raise ConnectionError('Unable to start sftp client %s:%d' %
226
240
                                  (host, port), e)
227
 
        return sftp
 
241
 
 
242
    def connect_ssh(self, username, password, host, port, command):
 
243
        t = self._connect(username, password, host, port)
 
244
        try:
 
245
            channel = t.open_session()
 
246
            cmdline = ' '.join(command)
 
247
            channel.exec_command(cmdline)
 
248
            return _ParamikoSSHConnection(channel)
 
249
        except paramiko.SSHException, e:
 
250
            raise ConnectionError('Unable to invoke remote bzr %s:%d' %
 
251
                                  (host, port), e)
228
252
 
229
253
register_ssh_vendor('paramiko', ParamikoVendor())
230
254
 
232
256
class SubprocessVendor(SSHVendor):
233
257
    """Abstract base class for vendors that use pipes to a subprocess."""
234
258
    
 
259
    def _connect(self, argv):
 
260
        proc = subprocess.Popen(argv,
 
261
                                stdin=subprocess.PIPE,
 
262
                                stdout=subprocess.PIPE,
 
263
                                **os_specific_subprocess_params())
 
264
        return SSHSubprocess(proc)
 
265
 
235
266
    def connect_sftp(self, username, password, host, port):
236
267
        try:
237
268
            argv = self._get_vendor_specific_argv(username, host, port,
238
269
                                                  subsystem='sftp')
239
 
            proc = subprocess.Popen(argv,
240
 
                                    stdin=subprocess.PIPE,
241
 
                                    stdout=subprocess.PIPE,
242
 
                                    **os_specific_subprocess_params())
243
 
            sock = SSHSubprocess(proc)
 
270
            sock = self._connect(argv)
244
271
            return SFTPClient(sock)
245
272
        except (EOFError, paramiko.SSHException), e:
246
273
            raise ConnectionError('Unable to connect to SSH host %s:%s: %s'
254
281
            raise ConnectionError('Unable to connect to SSH host %s:%s: %s'
255
282
                                  % (host, port, e))
256
283
 
 
284
    def connect_ssh(self, username, password, host, port, command):
 
285
        try:
 
286
            argv = self._get_vendor_specific_argv(username, host, port,
 
287
                                                  command=command)
 
288
            return self._connect(argv)
 
289
        except (EOFError), e:
 
290
            raise ConnectionError('Unable to connect to SSH host %s:%s: %s'
 
291
                                  % (host, port, e))
 
292
        except (OSError, IOError), e:
 
293
            # If the machine is fast enough, ssh can actually exit
 
294
            # before we try and send it the sftp request, which
 
295
            # raises a Broken Pipe
 
296
            if e.errno not in (errno.EPIPE,):
 
297
                raise
 
298
            raise ConnectionError('Unable to connect to SSH host %s:%s: %s'
 
299
                                  % (host, port, e))
 
300
 
257
301
    def _get_vendor_specific_argv(self, username, host, port, subsystem=None,
258
302
                                  command=None):
259
303
        """Returns the argument list to run the subprocess with.
469
513
        self.proc.stdout.close()
470
514
        self.proc.wait()
471
515
 
 
516
    def get_filelike_channels(self):
 
517
        return (self.proc.stdout, self.proc.stdin)
472
518