~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/transport/sftp.py

  • Committer: John Arbash Meinel
  • Date: 2005-11-20 17:00:43 UTC
  • mto: (1185.50.1 jam-integration)
  • mto: This revision was merged to the branch mainline in revision 1518.
  • Revision ID: john@arbash-meinel.com-20051120170043-52cdb7aefe86140e
Use a weakref dictionary to enable re-use of a connection (for sftp).

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
import urllib
26
26
import time
27
27
import random
 
28
import weakref
28
29
 
29
30
from bzrlib.errors import (FileExists, 
30
31
                           TransportNotPossible, NoSuchFile, NonRelativePath,
50
51
SYSTEM_HOSTKEYS = {}
51
52
BZR_HOSTKEYS = {}
52
53
 
 
54
# This is a weakref dictionary, so that we can reuse connections
 
55
# that are still active. Long term, it might be nice to have some
 
56
# sort of expiration policy, such as disconnect if inactive for
 
57
# X seconds. But that requires a lot more fanciness.
 
58
_connected_hosts = weakref.WeakValueDictionary()
 
59
 
53
60
def load_host_keys():
54
61
    """
55
62
    Load system host keys (probably doesn't work on windows) and any
292
299
                except IOError, e:
293
300
                    self._translate_io_exception(e, relpath)
294
301
                except paramiko.SSHException, x:
295
 
                    raise SFTPTransportError('Unable to rename into file %r' 
 
302
                    raise SFTPTransportError('Unable to rename into file %r' % (path,), x) 
296
303
                else:
297
304
                    success = True
298
305
            finally:
473
480
        global SYSTEM_HOSTKEYS, BZR_HOSTKEYS
474
481
        
475
482
        load_host_keys()
 
483
 
 
484
        idx = (self._host, self._port, self._username)
 
485
        try:
 
486
            self._sftp = _connected_hosts[idx]
 
487
            return
 
488
        except KeyError:
 
489
            pass
476
490
        
477
491
        try:
478
492
            t = paramiko.Transport((self._host, self._port))
511
525
        except paramiko.SSHException:
512
526
            raise BzrError('Unable to find path %s on SFTP server %s' % \
513
527
                (self._path, self._host))
 
528
        else:
 
529
            _connected_hosts[idx] = self._sftp
514
530
 
515
531
    def _sftp_auth(self, transport, username, host):
516
532
        agent = paramiko.Agent()