/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/sftp.py

Move register_ssh_vendor, _ssh_vendor and _get_ssh_vendor into ssh.py

Show diffs side-by-side

added added

removed removed

Lines of Context:
76
76
_default_do_prefetch = (_paramiko_version >= (1, 5, 5))
77
77
 
78
78
 
79
 
_ssh_vendors = {}
80
 
 
81
 
def register_ssh_vendor(name, vendor_class):
82
 
    """Register lazy-loaded SSH vendor class.""" 
83
 
    _ssh_vendors[name] = vendor_class
84
 
 
85
 
register_ssh_vendor('loopback', ssh.LoopbackVendor)
86
 
register_ssh_vendor('paramiko', ssh.ParamikoVendor)
87
 
register_ssh_vendor('none', ssh.ParamikoVendor)
88
 
register_ssh_vendor('openssh', ssh.OpenSSHSubprocessVendor)
89
 
register_ssh_vendor('ssh', ssh.SSHCorpSubprocessVendor)
90
 
    
91
 
_ssh_vendor = None
92
 
def _get_ssh_vendor():
93
 
    """Find out what version of SSH is on the system."""
94
 
    global _ssh_vendor
95
 
    if _ssh_vendor is not None:
96
 
        return _ssh_vendor
97
 
 
98
 
    if 'BZR_SSH' in os.environ:
99
 
        vendor_name = os.environ['BZR_SSH']
100
 
        try:
101
 
            klass = _ssh_vendors[vendor_name]
102
 
        except KeyError:
103
 
            raise UnknownSSH(vendor_name)
104
 
        else:
105
 
            _ssh_vendor = klass()
106
 
        return _ssh_vendor
107
 
 
108
 
    try:
109
 
        p = subprocess.Popen(['ssh', '-V'],
110
 
                             stdin=subprocess.PIPE,
111
 
                             stdout=subprocess.PIPE,
112
 
                             stderr=subprocess.PIPE,
113
 
                             **ssh.os_specific_subprocess_params())
114
 
        returncode = p.returncode
115
 
        stdout, stderr = p.communicate()
116
 
    except OSError:
117
 
        returncode = -1
118
 
        stdout = stderr = ''
119
 
    if 'OpenSSH' in stderr:
120
 
        mutter('ssh implementation is OpenSSH')
121
 
        _ssh_vendor = ssh.OpenSSHSubprocessVendor()
122
 
    elif 'SSH Secure Shell' in stderr:
123
 
        mutter('ssh implementation is SSH Corp.')
124
 
        _ssh_vendor = ssh.SSHCorpSubprocessVendor()
125
 
 
126
 
    if _ssh_vendor is not None:
127
 
        return _ssh_vendor
128
 
 
129
 
    # XXX: 20051123 jamesh
130
 
    # A check for putty's plink or lsh would go here.
131
 
 
132
 
    mutter('falling back to paramiko implementation')
133
 
    _ssh_vendor = ssh.ParamikoVendor()
134
 
    return _ssh_vendor
135
 
 
136
 
 
137
79
def clear_connection_cache():
138
80
    """Remove all hosts from the SFTP connection cache.
139
81
 
906
848
        event.wait(5.0)
907
849
    
908
850
    def setUp(self):
909
 
        global _ssh_vendor
910
 
        self._original_vendor = _ssh_vendor
911
 
        _ssh_vendor = self._vendor
 
851
        self._original_vendor = ssh._ssh_vendor
 
852
        ssh._ssh_vendor = self._vendor
912
853
        if sys.platform == 'win32':
913
854
            # Win32 needs to use the UNICODE api
914
855
            self._homedir = getcwd()
926
867
 
927
868
    def tearDown(self):
928
869
        """See bzrlib.transport.Server.tearDown."""
929
 
        global _ssh_vendor
930
870
        self._listener.stop()
931
 
        _ssh_vendor = self._original_vendor
 
871
        ssh._ssh_vendor = self._original_vendor
932
872
 
933
873
    def get_bogus_url(self):
934
874
        """See bzrlib.transport.Server.get_bogus_url."""
956
896
        self._vendor = ssh.LoopbackVendor()
957
897
 
958
898
    def _run_server(self, sock):
 
899
        # Re-import these as locals, so that they're still accessible during
 
900
        # interpreter shutdown (when all module globals get set to None, leading
 
901
        # to confusing errors like "'NoneType' object has no attribute 'error'".
 
902
        import socket, errno
959
903
        class FakeChannel(object):
960
904
            def get_transport(self):
961
905
                return self
1031
975
    return sftp
1032
976
 
1033
977
def _sftp_connect_uncached(host, port, username, password):
1034
 
    vendor = _get_ssh_vendor()
 
978
    vendor = ssh._get_ssh_vendor()
1035
979
    sftp = vendor.connect_sftp(username, password, host, port)
1036
980
    return sftp
1037
981