/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/transport/remote.py

  • Committer: Jelmer Vernooij
  • Date: 2018-08-14 01:15:02 UTC
  • mto: This revision was merged to the branch mainline in revision 7078.
  • Revision ID: jelmer@jelmer.uk-20180814011502-5zaydaq02vc2qxo1
Fix tests.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006 Canonical Ltd
 
1
# Copyright (C) 2006-2012, 2016 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
17
17
"""RemoteTransport client for the smart-server.
18
18
 
19
19
This module shouldn't be accessed directly.  The classes defined here should be
20
 
imported from bzrlib.smart.
 
20
imported from breezy.bzr.smart.
21
21
"""
22
22
 
 
23
from __future__ import absolute_import
 
24
 
23
25
__all__ = ['RemoteTransport', 'RemoteTCPTransport', 'RemoteSSHTransport']
24
26
 
25
 
from cStringIO import StringIO
 
27
from io import BytesIO
26
28
 
27
 
from bzrlib import (
 
29
from .. import (
28
30
    config,
29
31
    debug,
30
32
    errors,
31
 
    remote,
32
33
    trace,
33
34
    transport,
34
35
    urlutils,
35
36
    )
36
 
from bzrlib.smart import client, medium
37
 
from bzrlib.symbol_versioning import (
38
 
    deprecated_method,
 
37
from ..bzr import (
 
38
    remote,
39
39
    )
 
40
from ..sixish import PY3
 
41
from ..bzr.smart import client, medium
40
42
 
41
43
 
42
44
class _SmartStat(object):
149
151
    def is_readonly(self):
150
152
        """Smart server transport can do read/write file operations."""
151
153
        try:
152
 
            resp = self._call2('Transport.is_readonly')
 
154
            resp = self._call2(b'Transport.is_readonly')
153
155
        except errors.UnknownSmartMethod:
154
156
            # XXX: nasty hack: servers before 0.16 don't have a
155
157
            # 'Transport.is_readonly' verb, so we do what clients before 0.16
156
158
            # did: assume False.
157
159
            return False
158
 
        if resp == ('yes', ):
 
160
        if resp == (b'yes', ):
159
161
            return True
160
 
        elif resp == ('no', ):
 
162
        elif resp == (b'no', ):
161
163
            return False
162
164
        else:
163
165
            raise errors.UnexpectedSmartServerResponse(resp)
170
172
 
171
173
    def _remote_path(self, relpath):
172
174
        """Returns the Unicode version of the absolute path for relpath."""
173
 
        return self._combine_paths(self._path, relpath)
 
175
        path = urlutils.URL._combine_paths(self._parsed_url.path, relpath)
 
176
        if not isinstance(path, bytes):
 
177
            path = path.encode()
 
178
        return path
174
179
 
175
180
    def _call(self, method, *args):
176
181
        resp = self._call2(method, *args)
180
185
        """Call a method on the remote server."""
181
186
        try:
182
187
            return self._client.call(method, *args)
183
 
        except errors.ErrorFromSmartServer, err:
 
188
        except errors.ErrorFromSmartServer as err:
184
189
            # The first argument, if present, is always a path.
185
190
            if args:
186
 
                context = {'relpath': args[0]}
 
191
                context = {'relpath': args[0].decode('utf-8')}
187
192
            else:
188
193
                context = {}
189
194
            self._translate_error(err, **context)
192
197
        """Call a method on the remote server with body bytes."""
193
198
        try:
194
199
            return self._client.call_with_body_bytes(method, args, body)
195
 
        except errors.ErrorFromSmartServer, err:
 
200
        except errors.ErrorFromSmartServer as err:
196
201
            # The first argument, if present, is always a path.
197
202
            if args:
198
203
                context = {'relpath': args[0]}
205
210
 
206
211
        :see: Transport.has()
207
212
        """
208
 
        resp = self._call2('has', self._remote_path(relpath))
209
 
        if resp == ('yes', ):
 
213
        resp = self._call2(b'has', self._remote_path(relpath))
 
214
        if resp == (b'yes', ):
210
215
            return True
211
 
        elif resp == ('no', ):
 
216
        elif resp == (b'no', ):
212
217
            return False
213
218
        else:
214
219
            raise errors.UnexpectedSmartServerResponse(resp)
218
223
 
219
224
        :see: Transport.get_bytes()/get_file()
220
225
        """
221
 
        return StringIO(self.get_bytes(relpath))
 
226
        return BytesIO(self.get_bytes(relpath))
222
227
 
223
228
    def get_bytes(self, relpath):
224
229
        remote = self._remote_path(relpath)
225
230
        try:
226
 
            resp, response_handler = self._client.call_expecting_body('get', remote)
227
 
        except errors.ErrorFromSmartServer, err:
 
231
            resp, response_handler = self._client.call_expecting_body(b'get', remote)
 
232
        except errors.ErrorFromSmartServer as err:
228
233
            self._translate_error(err, relpath)
229
 
        if resp != ('ok', ):
 
234
        if resp != (b'ok', ):
230
235
            response_handler.cancel_read_body()
231
236
            raise errors.UnexpectedSmartServerResponse(resp)
232
237
        return response_handler.read_body_bytes()
233
238
 
234
239
    def _serialise_optional_mode(self, mode):
235
240
        if mode is None:
236
 
            return ''
 
241
            return b''
237
242
        else:
238
 
            return '%d' % mode
 
243
            return ('%d' % mode).encode('ascii')
239
244
 
240
245
    def mkdir(self, relpath, mode=None):
241
 
        resp = self._call2('mkdir', self._remote_path(relpath),
 
246
        resp = self._call2(b'mkdir', self._remote_path(relpath),
242
247
            self._serialise_optional_mode(mode))
243
248
 
244
249
    def open_write_stream(self, relpath, mode=None):
245
250
        """See Transport.open_write_stream."""
246
 
        self.put_bytes(relpath, "", mode)
 
251
        self.put_bytes(relpath, b"", mode)
247
252
        result = transport.AppendBasedFileStream(self, relpath)
248
253
        transport._file_streams[self.abspath(relpath)] = result
249
254
        return result
250
255
 
251
 
    def put_bytes(self, relpath, upload_contents, mode=None):
252
 
        # FIXME: upload_file is probably not safe for non-ascii characters -
253
 
        # should probably just pass all parameters as length-delimited
254
 
        # strings?
255
 
        if type(upload_contents) is unicode:
256
 
            # Although not strictly correct, we raise UnicodeEncodeError to be
257
 
            # compatible with other transports.
258
 
            raise UnicodeEncodeError(
259
 
                'undefined', upload_contents, 0, 1,
260
 
                'put_bytes must be given bytes, not unicode.')
261
 
        resp = self._call_with_body_bytes('put',
 
256
    def put_bytes(self, relpath, raw_bytes, mode=None):
 
257
        if not isinstance(raw_bytes, bytes):
 
258
            raise TypeError(
 
259
                'raw_bytes must be bytes string, not %s' % type(raw_bytes))
 
260
        resp = self._call_with_body_bytes(
 
261
            b'put',
262
262
            (self._remote_path(relpath), self._serialise_optional_mode(mode)),
263
 
            upload_contents)
 
263
            raw_bytes)
264
264
        self._ensure_ok(resp)
265
 
        return len(upload_contents)
 
265
        return len(raw_bytes)
266
266
 
267
 
    def put_bytes_non_atomic(self, relpath, bytes, mode=None,
 
267
    def put_bytes_non_atomic(self, relpath, raw_bytes, mode=None,
268
268
                             create_parent_dir=False,
269
269
                             dir_mode=None):
270
270
        """See Transport.put_bytes_non_atomic."""
271
271
        # FIXME: no encoding in the transport!
272
 
        create_parent_str = 'F'
 
272
        create_parent_str = b'F'
273
273
        if create_parent_dir:
274
 
            create_parent_str = 'T'
 
274
            create_parent_str = b'T'
275
275
 
276
276
        resp = self._call_with_body_bytes(
277
 
            'put_non_atomic',
 
277
            b'put_non_atomic',
278
278
            (self._remote_path(relpath), self._serialise_optional_mode(mode),
279
279
             create_parent_str, self._serialise_optional_mode(dir_mode)),
280
 
            bytes)
 
280
            raw_bytes)
281
281
        self._ensure_ok(resp)
282
282
 
283
283
    def put_file(self, relpath, upload_file, mode=None):
303
303
 
304
304
    def append_bytes(self, relpath, bytes, mode=None):
305
305
        resp = self._call_with_body_bytes(
306
 
            'append',
 
306
            b'append',
307
307
            (self._remote_path(relpath), self._serialise_optional_mode(mode)),
308
308
            bytes)
309
 
        if resp[0] == 'appended':
 
309
        if resp[0] == b'appended':
310
310
            return int(resp[1])
311
311
        raise errors.UnexpectedSmartServerResponse(resp)
312
312
 
313
313
    def delete(self, relpath):
314
 
        resp = self._call2('delete', self._remote_path(relpath))
 
314
        resp = self._call2(b'delete', self._remote_path(relpath))
315
315
        self._ensure_ok(resp)
316
316
 
317
317
    def external_url(self):
318
 
        """See bzrlib.transport.Transport.external_url."""
 
318
        """See breezy.transport.Transport.external_url."""
319
319
        # the external path for RemoteTransports is the base
320
320
        return self.base
321
321
 
359
359
        # turn the list of offsets into a single stack to iterate
360
360
        offset_stack = iter(offsets)
361
361
        # using a list so it can be modified when passing down and coming back
362
 
        next_offset = [offset_stack.next()]
 
362
        next_offset = [next(offset_stack)]
363
363
        for cur_request in requests:
364
364
            try:
365
365
                result = self._client.call_with_body_readv_array(
366
 
                    ('readv', self._remote_path(relpath),),
 
366
                    (b'readv', self._remote_path(relpath),),
367
367
                    [(c.start, c.length) for c in cur_request])
368
368
                resp, response_handler = result
369
 
            except errors.ErrorFromSmartServer, err:
 
369
            except errors.ErrorFromSmartServer as err:
370
370
                self._translate_error(err, relpath)
371
371
 
372
 
            if resp[0] != 'readv':
 
372
            if resp[0] != b'readv':
373
373
                # This should raise an exception
374
374
                response_handler.cancel_read_body()
375
375
                raise errors.UnexpectedSmartServerResponse(resp)
403
403
                #       not have a real string.
404
404
                if key == cur_offset_and_size:
405
405
                    yield cur_offset_and_size[0], this_data
406
 
                    cur_offset_and_size = next_offset[0] = offset_stack.next()
 
406
                    try:
 
407
                        cur_offset_and_size = next_offset[0] = next(offset_stack)
 
408
                    except StopIteration:
 
409
                        return
407
410
                else:
408
411
                    data_map[key] = this_data
409
412
            data_offset += c_offset.length
412
415
            while cur_offset_and_size in data_map:
413
416
                this_data = data_map.pop(cur_offset_and_size)
414
417
                yield cur_offset_and_size[0], this_data
415
 
                cur_offset_and_size = next_offset[0] = offset_stack.next()
 
418
                cur_offset_and_size = next_offset[0] = next(offset_stack)
416
419
 
417
420
    def rename(self, rel_from, rel_to):
418
 
        self._call('rename',
 
421
        self._call(b'rename',
419
422
                   self._remote_path(rel_from),
420
423
                   self._remote_path(rel_to))
421
424
 
422
425
    def move(self, rel_from, rel_to):
423
 
        self._call('move',
 
426
        self._call(b'move',
424
427
                   self._remote_path(rel_from),
425
428
                   self._remote_path(rel_to))
426
429
 
427
430
    def rmdir(self, relpath):
428
 
        resp = self._call('rmdir', self._remote_path(relpath))
 
431
        resp = self._call(b'rmdir', self._remote_path(relpath))
429
432
 
430
433
    def _ensure_ok(self, resp):
431
 
        if resp[0] != 'ok':
 
434
        if resp[0] != b'ok':
432
435
            raise errors.UnexpectedSmartServerResponse(resp)
433
436
 
434
437
    def _translate_error(self, err, relpath=None):
435
438
        remote._translate_error(err, path=relpath)
436
439
 
437
440
    def disconnect(self):
438
 
        self.get_smart_medium().disconnect()
 
441
        m = self.get_smart_medium()
 
442
        if m is not None:
 
443
            m.disconnect()
439
444
 
440
445
    def stat(self, relpath):
441
 
        resp = self._call2('stat', self._remote_path(relpath))
442
 
        if resp[0] == 'stat':
 
446
        resp = self._call2(b'stat', self._remote_path(relpath))
 
447
        if resp[0] == b'stat':
443
448
            return _SmartStat(int(resp[1]), int(resp[2], 8))
444
449
        raise errors.UnexpectedSmartServerResponse(resp)
445
450
 
460
465
        return True
461
466
 
462
467
    def list_dir(self, relpath):
463
 
        resp = self._call2('list_dir', self._remote_path(relpath))
464
 
        if resp[0] == 'names':
465
 
            return [name.encode('ascii') for name in resp[1:]]
 
468
        resp = self._call2(b'list_dir', self._remote_path(relpath))
 
469
        if resp[0] == b'names':
 
470
            return [name.decode('utf-8') if PY3 else name for name in resp[1:]]
466
471
        raise errors.UnexpectedSmartServerResponse(resp)
467
472
 
468
473
    def iter_files_recursive(self):
469
 
        resp = self._call2('iter_files_recursive', self._remote_path(''))
470
 
        if resp[0] == 'names':
471
 
            return resp[1:]
 
474
        resp = self._call2(b'iter_files_recursive', self._remote_path(''))
 
475
        if resp[0] == b'names':
 
476
            return [name.decode('utf-8') if PY3 else name for name in resp[1:]]
472
477
        raise errors.UnexpectedSmartServerResponse(resp)
473
478
 
474
479
 
481
486
 
482
487
    def _build_medium(self):
483
488
        client_medium = medium.SmartTCPClientMedium(
484
 
            self._host, self._port, self.base)
 
489
            self._parsed_url.host, self._parsed_url.port, self.base)
485
490
        return client_medium, None
486
491
 
487
492
 
494
499
 
495
500
    def _build_medium(self):
496
501
        client_medium = medium.SmartTCPClientMedium(
497
 
            self._host, self._port, self.base)
 
502
            self._parsed_url.host, self._parsed_url.port, self.base)
498
503
        client_medium._protocol_version = 2
499
504
        client_medium._remember_remote_is_before((1, 6))
500
505
        return client_medium, None
510
515
    def _build_medium(self):
511
516
        location_config = config.LocationConfig(self.base)
512
517
        bzr_remote_path = location_config.get_bzr_remote_path()
513
 
        user = self._user
 
518
        user = self._parsed_url.user
514
519
        if user is None:
515
520
            auth = config.AuthenticationConfig()
516
 
            user = auth.get_user('ssh', self._host, self._port)
517
 
        client_medium = medium.SmartSSHClientMedium(self._host, self._port,
518
 
            user, self._password, self.base,
519
 
            bzr_remote_path=bzr_remote_path)
520
 
        return client_medium, (user, self._password)
 
521
            user = auth.get_user('ssh', self._parsed_url.host,
 
522
                self._parsed_url.port)
 
523
        ssh_params = medium.SSHParams(self._parsed_url.host,
 
524
                self._parsed_url.port, user, self._parsed_url.password,
 
525
                bzr_remote_path)
 
526
        client_medium = medium.SmartSSHClientMedium(self.base, ssh_params)
 
527
        return client_medium, (user, self._parsed_url.password)
521
528
 
522
529
 
523
530
class RemoteHTTPTransport(RemoteTransport):
537
544
            # url only for an intial construction (when the url came from the
538
545
            # command-line).
539
546
            http_url = base[len('bzr+'):]
540
 
            self._http_transport = transport.get_transport(http_url)
 
547
            self._http_transport = transport.get_transport_from_url(http_url)
541
548
        else:
542
549
            self._http_transport = http_transport
543
550
        super(RemoteHTTPTransport, self).__init__(
601
608
    """Return (transport, server) permutations for testing."""
602
609
    ### We may need a little more test framework support to construct an
603
610
    ### appropriate RemoteTransport in the future.
604
 
    from bzrlib.tests import test_server
 
611
    from ..tests import test_server
605
612
    return [(RemoteTCPTransport, test_server.SmartTCPServer_for_testing)]