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

  • Committer: Breezy landing bot
  • Author(s): Colin Watson
  • Date: 2020-11-16 21:47:08 UTC
  • mfrom: (7521.1.1 remove-lp-workaround)
  • Revision ID: breezy.the.bot@gmail.com-20201116214708-jos209mgxi41oy15
Remove breezy.git workaround for bazaar.launchpad.net.

Merged from https://code.launchpad.net/~cjwatson/brz/remove-lp-workaround/+merge/393710

Show diffs side-by-side

added added

removed removed

Lines of Context:
22
22
import os
23
23
import paramiko
24
24
import socket
25
 
import SocketServer
 
25
import socketserver
26
26
import sys
27
27
import time
28
28
 
29
 
from bzrlib import (
 
29
from .. import (
30
30
    osutils,
31
31
    trace,
32
32
    urlutils,
33
33
    )
34
 
from bzrlib.transport import (
 
34
from ..transport import (
35
35
    ssh,
36
36
    )
37
 
from bzrlib.tests import test_server
 
37
from . import test_server
38
38
 
39
39
 
40
40
class StubServer(paramiko.ServerInterface):
59
59
        try:
60
60
            return paramiko.SFTPAttributes.from_stat(
61
61
                os.fstat(self.readfile.fileno()))
62
 
        except OSError, e:
 
62
        except OSError as e:
63
63
            return paramiko.SFTPServer.convert_errno(e.errno)
64
64
 
65
65
    def chattr(self, attr):
68
68
        trace.mutter('Changing permissions on %s to %s', self.filename, attr)
69
69
        try:
70
70
            paramiko.SFTPServer.set_file_attr(self.filename, attr)
71
 
        except OSError, e:
 
71
        except OSError as e:
72
72
            return paramiko.SFTPServer.convert_errno(e.errno)
73
73
 
74
74
 
125
125
    def chattr(self, path, attr):
126
126
        try:
127
127
            paramiko.SFTPServer.set_file_attr(path, attr)
128
 
        except OSError, e:
 
128
        except OSError as e:
129
129
            return paramiko.SFTPServer.convert_errno(e.errno)
130
130
        return paramiko.SFTP_OK
131
131
 
132
132
    def list_folder(self, path):
133
133
        path = self._realpath(path)
134
134
        try:
135
 
            out = [ ]
 
135
            out = []
136
136
            # TODO: win32 incorrectly lists paths with non-ascii if path is not
137
137
            # unicode. However on unix the server should only deal with
138
138
            # bytestreams and posix.listdir does the right thing
146
146
                attr.filename = fname
147
147
                out.append(attr)
148
148
            return out
149
 
        except OSError, e:
 
149
        except OSError as e:
150
150
            return paramiko.SFTPServer.convert_errno(e.errno)
151
151
 
152
152
    def stat(self, path):
153
153
        path = self._realpath(path)
154
154
        try:
155
155
            return paramiko.SFTPAttributes.from_stat(os.stat(path))
156
 
        except OSError, e:
 
156
        except OSError as e:
157
157
            return paramiko.SFTPServer.convert_errno(e.errno)
158
158
 
159
159
    def lstat(self, path):
160
160
        path = self._realpath(path)
161
161
        try:
162
162
            return paramiko.SFTPAttributes.from_stat(os.lstat(path))
163
 
        except OSError, e:
 
163
        except OSError as e:
164
164
            return paramiko.SFTPServer.convert_errno(e.errno)
165
165
 
166
166
    def open(self, path, flags, attr):
172
172
            else:
173
173
                # os.open() defaults to 0777 which is
174
174
                # an odd default mode for files
175
 
                fd = os.open(path, flags, 0666)
176
 
        except OSError, e:
 
175
                fd = os.open(path, flags, 0o666)
 
176
        except OSError as e:
177
177
            return paramiko.SFTPServer.convert_errno(e.errno)
178
178
 
179
179
        if (flags & os.O_CREAT) and (attr is not None):
188
188
            fstr = 'rb'
189
189
        try:
190
190
            f = os.fdopen(fd, fstr)
191
 
        except (IOError, OSError), e:
 
191
        except (IOError, OSError) as e:
192
192
            return paramiko.SFTPServer.convert_errno(e.errno)
193
193
        fobj = StubSFTPHandle()
194
194
        fobj.filename = path
200
200
        path = self._realpath(path)
201
201
        try:
202
202
            os.remove(path)
203
 
        except OSError, e:
 
203
        except OSError as e:
204
204
            return paramiko.SFTPServer.convert_errno(e.errno)
205
205
        return paramiko.SFTP_OK
206
206
 
209
209
        newpath = self._realpath(newpath)
210
210
        try:
211
211
            os.rename(oldpath, newpath)
212
 
        except OSError, e:
213
 
            return paramiko.SFTPServer.convert_errno(e.errno)
214
 
        return paramiko.SFTP_OK
 
212
        except OSError as e:
 
213
            return paramiko.SFTPServer.convert_errno(e.errno)
 
214
        return paramiko.SFTP_OK
 
215
 
 
216
    def symlink(self, target_path, path):
 
217
        path = self._realpath(path)
 
218
        try:
 
219
            os.symlink(target_path, path)
 
220
        except OSError as e:
 
221
            return paramiko.SFTPServer.convert_errno(e.errno)
 
222
        return paramiko.SFTP_OK
 
223
 
 
224
    def readlink(self, path):
 
225
        path = self._realpath(path)
 
226
        try:
 
227
            target_path = os.readlink(path)
 
228
        except OSError as e:
 
229
            return paramiko.SFTPServer.convert_errno(e.errno)
 
230
        return target_path
215
231
 
216
232
    def mkdir(self, path, attr):
217
233
        path = self._realpath(path)
225
241
            if attr is not None:
226
242
                attr._flags &= ~attr.FLAG_PERMISSIONS
227
243
                paramiko.SFTPServer.set_file_attr(path, attr)
228
 
        except OSError, e:
 
244
        except OSError as e:
229
245
            return paramiko.SFTPServer.convert_errno(e.errno)
230
246
        return paramiko.SFTP_OK
231
247
 
233
249
        path = self._realpath(path)
234
250
        try:
235
251
            os.rmdir(path)
236
 
        except OSError, e:
 
252
        except OSError as e:
237
253
            return paramiko.SFTPServer.convert_errno(e.errno)
238
254
        return paramiko.SFTP_OK
239
255
 
240
 
    # removed: chattr, symlink, readlink
 
256
    # removed: chattr
241
257
    # (nothing in bzr's sftp transport uses those)
242
258
 
243
259
 
244
260
# ------------- server test implementation --------------
245
261
 
246
 
STUB_SERVER_KEY = """
 
262
STUB_SERVER_KEY = """\
247
263
-----BEGIN RSA PRIVATE KEY-----
248
264
MIICWgIBAAKBgQDTj1bqB4WmayWNPB+8jVSYpZYk80Ujvj680pOTh2bORBjbIAyz
249
265
oWGW+GUjzKxTiiPvVmxFgx5wdsFvF03v34lEVVhMpouqPAYQ15N37K/ir5XY+9m/
337
353
        return bytes_sent
338
354
 
339
355
 
340
 
class TestingSFTPConnectionHandler(SocketServer.BaseRequestHandler):
 
356
class TestingSFTPConnectionHandler(socketserver.BaseRequestHandler):
341
357
 
342
358
    def setup(self):
343
359
        self.wrap_for_latency()
345
361
        ptrans = paramiko.Transport(self.request)
346
362
        self.paramiko_transport = ptrans
347
363
        # Set it to a channel under 'bzr' so that we get debug info
348
 
        ptrans.set_log_channel('bzr.paramiko.transport')
 
364
        ptrans.set_log_channel('brz.paramiko.transport')
349
365
        ptrans.add_server_key(tcs.get_host_key())
350
366
        ptrans.set_subsystem_handler('sftp', paramiko.SFTPServer,
351
367
                                     StubSFTPServer, root=tcs._root,
377
393
        # Re-import these as locals, so that they're still accessible during
378
394
        # interpreter shutdown (when all module globals get set to None, leading
379
395
        # to confusing errors like "'NoneType' object has no attribute 'error'".
 
396
 
380
397
        class FakeChannel(object):
381
398
            def get_transport(self):
382
399
                return self
 
400
 
383
401
            def get_log_channel(self):
384
 
                return 'bzr.paramiko'
 
402
                return 'brz.paramiko'
 
403
 
385
404
            def get_name(self):
386
405
                return '1'
 
406
 
387
407
            def get_hexdump(self):
388
408
                return False
 
409
 
389
410
            def close(self):
390
411
                pass
391
412
 
394
415
            FakeChannel(), 'sftp', StubServer(tcs), StubSFTPServer,
395
416
            root=tcs._root, home=tcs._server_homedir)
396
417
        self.sftp_server = sftp_server
397
 
        sys_stderr = sys.stderr # Used in error reporting during shutdown
 
418
        sys_stderr = sys.stderr  # Used in error reporting during shutdown
398
419
        try:
399
420
            sftp_server.start_subsystem(
400
421
                'sftp', None, ssh.SocketAsChannelAdapter(self.request))
401
 
        except socket.error, e:
 
422
        except socket.error as e:
402
423
            if (len(e.args) > 0) and (e.args[0] == errno.EPIPE):
403
424
                # it's okay for the client to disconnect abruptly
404
425
                # (bug in paramiko 1.6: it should absorb this exception)
405
426
                pass
406
427
            else:
407
428
                raise
408
 
        except Exception, e:
 
429
        except Exception as e:
409
430
            # This typically seems to happen during interpreter shutdown, so
410
431
            # most of the useful ways to report this error won't work.
411
432
            # Writing the exception type, and then the text of the exception,
474
495
    def start_server(self, backing_server=None):
475
496
        # XXX: TODO: make sftpserver back onto backing_server rather than local
476
497
        # disk.
477
 
        if not (backing_server is None or
478
 
                isinstance(backing_server, test_server.LocalURLServer)):
 
498
        if not (backing_server is None
 
499
                or isinstance(backing_server, test_server.LocalURLServer)):
479
500
            raise AssertionError(
480
501
                'backing_server should not be %r, because this can only serve '
481
502
                'the local current working directory.' % (backing_server,))
482
503
        self._original_vendor = ssh._ssh_vendor_manager._cached_ssh_vendor
483
504
        ssh._ssh_vendor_manager._cached_ssh_vendor = self._vendor
 
505
        self._homedir = osutils.getcwd()
484
506
        if sys.platform == 'win32':
485
 
            # Win32 needs to use the UNICODE api
486
 
            self._homedir = os.getcwdu()
487
507
            # Normalize the path or it will be wrongly escaped
488
508
            self._homedir = osutils.normpath(self._homedir)
489
509
        else:
490
 
            # But unix SFTP servers should just deal in bytestreams
491
 
            self._homedir = os.getcwd()
 
510
            self._homedir = self._homedir
492
511
        if self._server_homedir is None:
493
512
            self._server_homedir = self._homedir
494
513
        self._root = '/'
503
522
            ssh._ssh_vendor_manager._cached_ssh_vendor = self._original_vendor
504
523
 
505
524
    def get_bogus_url(self):
506
 
        """See bzrlib.transport.Server.get_bogus_url."""
 
525
        """See breezy.transport.Server.get_bogus_url."""
507
526
        # this is chosen to try to prevent trouble with proxies, weird dns, etc
508
527
        # we bind a random socket, so that we get a guaranteed unused port
509
528
        # we just never listen on that port
516
535
    """A test server for sftp transports, using absolute urls and ssh."""
517
536
 
518
537
    def get_url(self):
519
 
        """See bzrlib.transport.Server.get_url."""
 
538
        """See breezy.transport.Server.get_url."""
520
539
        homedir = self._homedir
521
540
        if sys.platform != 'win32':
522
541
            # Remove the initial '/' on all platforms but win32
540
559
    """A test server for sftp transports, using absolute urls."""
541
560
 
542
561
    def get_url(self):
543
 
        """See bzrlib.transport.Server.get_url."""
 
562
        """See breezy.transport.Server.get_url."""
544
563
        homedir = self._homedir
545
564
        if sys.platform != 'win32':
546
565
            # Remove the initial '/' on all platforms but win32
552
571
    """A test server for sftp transports, using homedir relative urls."""
553
572
 
554
573
    def get_url(self):
555
 
        """See bzrlib.transport.Server.get_url."""
 
574
        """See breezy.transport.Server.get_url."""
556
575
        return self._get_sftp_url("%7E/")
557
576
 
558
577
 
567
586
        server = super(SFTPSiblingAbsoluteServer, self).create_server()
568
587
        server._server_homedir = '/dev/noone/runs/tests/here'
569
588
        return server
570