/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/tests/stub_sftp.py

  • Committer: John Arbash Meinel
  • Date: 2006-06-29 19:57:25 UTC
  • mto: (1711.4.39 win32-accepted)
  • mto: This revision was merged to the branch mainline in revision 1836.
  • Revision ID: john@arbash-meinel.com-20060629195725-a536f5de2c7c1141
Call finalize() though it doesn't help to release resources to allow cleanup.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2005 Robey Pointer <robey@lag.net>, Canonical Ltd
 
2
 
 
3
# This program is free software; you can redistribute it and/or modify
 
4
# it under the terms of the GNU General Public License as published by
 
5
# the Free Software Foundation; either version 2 of the License, or
 
6
# (at your option) any later version.
 
7
 
 
8
# This program is distributed in the hope that it will be useful,
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
# GNU General Public License for more details.
 
12
 
 
13
# You should have received a copy of the GNU General Public License
 
14
# along with this program; if not, write to the Free Software
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
16
 
 
17
"""
 
18
A stub SFTP server for loopback SFTP testing.
 
19
Adapted from the one in paramiko's unit tests.
 
20
"""
 
21
 
 
22
import os
 
23
from paramiko import ServerInterface, SFTPServerInterface, SFTPServer, SFTPAttributes, \
 
24
    SFTPHandle, SFTP_OK, AUTH_SUCCESSFUL, OPEN_SUCCEEDED
 
25
import sys
 
26
 
 
27
from bzrlib.osutils import pathjoin
 
28
from bzrlib.trace import mutter
 
29
 
 
30
 
 
31
class StubServer (ServerInterface):
 
32
 
 
33
    def __init__(self, test_case):
 
34
        ServerInterface.__init__(self)
 
35
        self._test_case = test_case
 
36
 
 
37
    def check_auth_password(self, username, password):
 
38
        # all are allowed
 
39
        self._test_case.log('sftpserver - authorizing: %s' % (username,))
 
40
        return AUTH_SUCCESSFUL
 
41
 
 
42
    def check_channel_request(self, kind, chanid):
 
43
        self._test_case.log('sftpserver - channel request: %s, %s' % (kind, chanid))
 
44
        return OPEN_SUCCEEDED
 
45
 
 
46
 
 
47
class StubSFTPHandle (SFTPHandle):
 
48
    def stat(self):
 
49
        try:
 
50
            return SFTPAttributes.from_stat(os.fstat(self.readfile.fileno()))
 
51
        except OSError, e:
 
52
            return SFTPServer.convert_errno(e.errno)
 
53
 
 
54
    def chattr(self, attr):
 
55
        # python doesn't have equivalents to fchown or fchmod, so we have to
 
56
        # use the stored filename
 
57
        mutter('Changing permissions on %s to %s', self.filename, attr)
 
58
        try:
 
59
            SFTPServer.set_file_attr(self.filename, attr)
 
60
        except OSError, e:
 
61
            return SFTPServer.convert_errno(e.errno)
 
62
 
 
63
 
 
64
class StubSFTPServer (SFTPServerInterface):
 
65
 
 
66
    def __init__(self, server, root, home=None):
 
67
        SFTPServerInterface.__init__(self, server)
 
68
        # All paths are actually relative to 'root'.
 
69
        # this is like implementing chroot().
 
70
        self.root = root
 
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'?
 
75
            self.home = self.root
 
76
        else:
 
77
            assert home.startswith(self.root), \
 
78
                    "home must be a subdirectory of root (%s vs %s)" \
 
79
                    % (home, root)
 
80
            self.home = home[len(self.root):]
 
81
        if self.home.startswith('/'):
 
82
            self.home = self.home[1:]
 
83
        server._test_case.log('sftpserver - new connection')
 
84
 
 
85
    def _realpath(self, 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
 
102
 
 
103
    def canonicalize(self, path):
 
104
        if os.path.isabs(path):
 
105
            return os.path.normpath(path)
 
106
        else:
 
107
            return os.path.normpath('/' + os.path.join(self.home, path))
 
108
 
 
109
    def chattr(self, path, attr):
 
110
        try:
 
111
            SFTPServer.set_file_attr(path, attr)
 
112
        except OSError, e:
 
113
            return SFTPServer.convert_errno(e.errno)
 
114
        return SFTP_OK
 
115
 
 
116
    def list_folder(self, path):
 
117
        path = self._realpath(path)
 
118
        try:
 
119
            out = [ ]
 
120
            # TODO: win32 incorrectly lists paths with non-ascii if path is not
 
121
            # unicode. However on Linux the server should only deal with
 
122
            # bytestreams and posix.listdir does the right thing 
 
123
            if sys.platform == 'win32':
 
124
                flist = [f.encode('utf8') for f in os.listdir(path)]
 
125
            else:
 
126
                flist = os.listdir(path)
 
127
            for fname in flist:
 
128
                attr = SFTPAttributes.from_stat(os.stat(pathjoin(path, fname)))
 
129
                attr.filename = fname
 
130
                out.append(attr)
 
131
            return out
 
132
        except OSError, e:
 
133
            return SFTPServer.convert_errno(e.errno)
 
134
 
 
135
    def stat(self, path):
 
136
        path = self._realpath(path)
 
137
        try:
 
138
            return SFTPAttributes.from_stat(os.stat(path))
 
139
        except OSError, e:
 
140
            return SFTPServer.convert_errno(e.errno)
 
141
 
 
142
    def lstat(self, path):
 
143
        path = self._realpath(path)
 
144
        try:
 
145
            return SFTPAttributes.from_stat(os.lstat(path))
 
146
        except OSError, e:
 
147
            return SFTPServer.convert_errno(e.errno)
 
148
 
 
149
    def open(self, path, flags, attr):
 
150
        path = self._realpath(path)
 
151
        try:
 
152
            if hasattr(os, 'O_BINARY'):
 
153
                flags |= os.O_BINARY
 
154
            if getattr(attr, 'st_mode', None):
 
155
                fd = os.open(path, flags, attr.st_mode)
 
156
            else:
 
157
                fd = os.open(path, flags)
 
158
        except OSError, e:
 
159
            return SFTPServer.convert_errno(e.errno)
 
160
 
 
161
        if (flags & os.O_CREAT) and (attr is not None):
 
162
            attr._flags &= ~attr.FLAG_PERMISSIONS
 
163
            SFTPServer.set_file_attr(path, attr)
 
164
        if flags & os.O_WRONLY:
 
165
            fstr = 'wb'
 
166
        elif flags & os.O_RDWR:
 
167
            fstr = 'rb+'
 
168
        else:
 
169
            # O_RDONLY (== 0)
 
170
            fstr = 'rb'
 
171
        try:
 
172
            f = os.fdopen(fd, fstr)
 
173
        except (IOError, OSError), e:
 
174
            return SFTPServer.convert_errno(e.errno)
 
175
        fobj = StubSFTPHandle()
 
176
        fobj.filename = path
 
177
        fobj.readfile = f
 
178
        fobj.writefile = f
 
179
        return fobj
 
180
 
 
181
    def remove(self, path):
 
182
        path = self._realpath(path)
 
183
        try:
 
184
            os.remove(path)
 
185
        except OSError, e:
 
186
            return SFTPServer.convert_errno(e.errno)
 
187
        return SFTP_OK
 
188
 
 
189
    def rename(self, oldpath, newpath):
 
190
        oldpath = self._realpath(oldpath)
 
191
        newpath = self._realpath(newpath)
 
192
        try:
 
193
            os.rename(oldpath, newpath)
 
194
        except OSError, e:
 
195
            return SFTPServer.convert_errno(e.errno)
 
196
        return SFTP_OK
 
197
 
 
198
    def mkdir(self, path, attr):
 
199
        path = self._realpath(path)
 
200
        try:
 
201
            # Using getattr() in case st_mode is None or 0
 
202
            # both evaluate to False
 
203
            if getattr(attr, 'st_mode', None):
 
204
                os.mkdir(path, attr.st_mode)
 
205
            else:
 
206
                os.mkdir(path)
 
207
            if attr is not None:
 
208
                attr._flags &= ~attr.FLAG_PERMISSIONS
 
209
                SFTPServer.set_file_attr(path, attr)
 
210
        except OSError, e:
 
211
            return SFTPServer.convert_errno(e.errno)
 
212
        return SFTP_OK
 
213
 
 
214
    def rmdir(self, path):
 
215
        path = self._realpath(path)
 
216
        try:
 
217
            os.rmdir(path)
 
218
        except OSError, e:
 
219
            return SFTPServer.convert_errno(e.errno)
 
220
        return SFTP_OK
 
221
 
 
222
    # removed: chattr, symlink, readlink
 
223
    # (nothing in bzr's sftp transport uses those)