~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/stub_sftp.py

  • Committer: John Arbash Meinel
  • Date: 2006-06-29 04:08:32 UTC
  • mto: (1711.5.3 win32)
  • mto: This revision was merged to the branch mainline in revision 1827.
  • Revision ID: john@arbash-meinel.com-20060629040832-929ebb7408247e38
Get most SFTP tests to pass. StubSFTPServer now talks the same path protocol that SFTPTransport talks. on win32

Show diffs side-by-side

added added

removed removed

Lines of Context:
22
22
import os
23
23
from paramiko import ServerInterface, SFTPServerInterface, SFTPServer, SFTPAttributes, \
24
24
    SFTPHandle, SFTP_OK, AUTH_SUCCESSFUL, OPEN_SUCCEEDED
 
25
import sys
25
26
 
26
27
from bzrlib.osutils import pathjoin
27
28
from bzrlib.trace import mutter
64
65
 
65
66
    def __init__(self, server, root, home=None):
66
67
        SFTPServerInterface.__init__(self, server)
 
68
        # All paths are actually relative to 'root'.
 
69
        # this is like implementing chroot().
67
70
        self.root = root
68
71
        if home is None:
 
72
            # XXX: if 'home' is None, shouldn't it
 
73
            #       be set to '', since it should
 
74
            #       be relative to 'root'?
69
75
            self.home = self.root
70
76
        else:
 
77
            assert home.startswith(self.root), \
 
78
                    "home must be a subdirectory of root (%s vs %s)" \
 
79
                    % (home, root)
71
80
            self.home = home[len(self.root):]
72
 
        if (len(self.home) > 0) and (self.home[0] == '/'):
 
81
        if self.home.startswith('/'):
73
82
            self.home = self.home[1:]
74
83
        server._test_case.log('sftpserver - new connection')
75
84
 
76
85
    def _realpath(self, path):
77
 
        return self.root + self.canonicalize(path)
 
86
        if sys.platform == 'win32':
 
87
            # Win32 sftp paths end up looking like
 
88
            # sftp://host@foo/h:/foo/bar
 
89
            # which gets translated here to:
 
90
            # /h:/foo/bar
 
91
            # Local paths stay 'foo/bar', though.
 
92
            # Also, win32 needs to use the Unicode APIs.
 
93
            thispath = path.decode('utf8')
 
94
            if path.startswith('/'):
 
95
                # Abspath
 
96
                realpath = os.path.normpath(thispath[1:])
 
97
            else:
 
98
                realpath = os.path.normpath(os.path.join(self.home, thispath))
 
99
        else:
 
100
            realpath = self.root + self.canonicalize(path)
 
101
        return realpath
78
102
 
79
103
    def canonicalize(self, path):
80
104
        if os.path.isabs(path):
96
120
            # TODO: win32 incorrectly lists paths with non-ascii if path is not
97
121
            # unicode. However on Linux the server should only deal with
98
122
            # bytestreams and posix.listdir does the right thing 
99
 
            flist = os.listdir(path)
 
123
            if sys.platform == 'win32':
 
124
                flist = [f.encode('utf8') for f in os.listdir(path)]
 
125
            else:
 
126
                flist = os.listdir(path)
100
127
            for fname in flist:
101
128
                attr = SFTPAttributes.from_stat(os.stat(pathjoin(path, fname)))
102
129
                attr.filename = fname