/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: 2017-06-10 01:35:53 UTC
  • mto: (6670.4.8 move-bzr)
  • mto: This revision was merged to the branch mainline in revision 6681.
  • Revision ID: jelmer@jelmer.uk-20170610013553-560y7mn3su4pp763
Fix remaining 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.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
26
 
 
27
 
from bzrlib import (
 
27
from .. import (
28
28
    config,
29
29
    debug,
30
30
    errors,
33
33
    transport,
34
34
    urlutils,
35
35
    )
36
 
from bzrlib.smart import client, medium
37
 
from bzrlib.symbol_versioning import (
38
 
    deprecated_method,
 
36
from ..sixish import (
 
37
    BytesIO,
39
38
    )
 
39
from ..smart import client, medium
40
40
 
41
41
 
42
42
class _SmartStat(object):
170
170
 
171
171
    def _remote_path(self, relpath):
172
172
        """Returns the Unicode version of the absolute path for relpath."""
173
 
        return self._combine_paths(self._path, relpath)
 
173
        return urlutils.URL._combine_paths(self._parsed_url.path, relpath)
174
174
 
175
175
    def _call(self, method, *args):
176
176
        resp = self._call2(method, *args)
180
180
        """Call a method on the remote server."""
181
181
        try:
182
182
            return self._client.call(method, *args)
183
 
        except errors.ErrorFromSmartServer, err:
 
183
        except errors.ErrorFromSmartServer as err:
184
184
            # The first argument, if present, is always a path.
185
185
            if args:
186
186
                context = {'relpath': args[0]}
192
192
        """Call a method on the remote server with body bytes."""
193
193
        try:
194
194
            return self._client.call_with_body_bytes(method, args, body)
195
 
        except errors.ErrorFromSmartServer, err:
 
195
        except errors.ErrorFromSmartServer as err:
196
196
            # The first argument, if present, is always a path.
197
197
            if args:
198
198
                context = {'relpath': args[0]}
218
218
 
219
219
        :see: Transport.get_bytes()/get_file()
220
220
        """
221
 
        return StringIO(self.get_bytes(relpath))
 
221
        return BytesIO(self.get_bytes(relpath))
222
222
 
223
223
    def get_bytes(self, relpath):
224
224
        remote = self._remote_path(relpath)
225
225
        try:
226
226
            resp, response_handler = self._client.call_expecting_body('get', remote)
227
 
        except errors.ErrorFromSmartServer, err:
 
227
        except errors.ErrorFromSmartServer as err:
228
228
            self._translate_error(err, relpath)
229
229
        if resp != ('ok', ):
230
230
            response_handler.cancel_read_body()
248
248
        transport._file_streams[self.abspath(relpath)] = result
249
249
        return result
250
250
 
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',
 
251
    def put_bytes(self, relpath, raw_bytes, mode=None):
 
252
        if not isinstance(raw_bytes, str):
 
253
            raise TypeError(
 
254
                'raw_bytes must be a plain string, not %s' % type(raw_bytes))
 
255
        resp = self._call_with_body_bytes(
 
256
            'put',
262
257
            (self._remote_path(relpath), self._serialise_optional_mode(mode)),
263
 
            upload_contents)
 
258
            raw_bytes)
264
259
        self._ensure_ok(resp)
265
 
        return len(upload_contents)
 
260
        return len(raw_bytes)
266
261
 
267
 
    def put_bytes_non_atomic(self, relpath, bytes, mode=None,
 
262
    def put_bytes_non_atomic(self, relpath, raw_bytes, mode=None,
268
263
                             create_parent_dir=False,
269
264
                             dir_mode=None):
270
265
        """See Transport.put_bytes_non_atomic."""
277
272
            'put_non_atomic',
278
273
            (self._remote_path(relpath), self._serialise_optional_mode(mode),
279
274
             create_parent_str, self._serialise_optional_mode(dir_mode)),
280
 
            bytes)
 
275
            raw_bytes)
281
276
        self._ensure_ok(resp)
282
277
 
283
278
    def put_file(self, relpath, upload_file, mode=None):
315
310
        self._ensure_ok(resp)
316
311
 
317
312
    def external_url(self):
318
 
        """See bzrlib.transport.Transport.external_url."""
 
313
        """See breezy.transport.Transport.external_url."""
319
314
        # the external path for RemoteTransports is the base
320
315
        return self.base
321
316
 
359
354
        # turn the list of offsets into a single stack to iterate
360
355
        offset_stack = iter(offsets)
361
356
        # using a list so it can be modified when passing down and coming back
362
 
        next_offset = [offset_stack.next()]
 
357
        next_offset = [next(offset_stack)]
363
358
        for cur_request in requests:
364
359
            try:
365
360
                result = self._client.call_with_body_readv_array(
366
361
                    ('readv', self._remote_path(relpath),),
367
362
                    [(c.start, c.length) for c in cur_request])
368
363
                resp, response_handler = result
369
 
            except errors.ErrorFromSmartServer, err:
 
364
            except errors.ErrorFromSmartServer as err:
370
365
                self._translate_error(err, relpath)
371
366
 
372
367
            if resp[0] != 'readv':
403
398
                #       not have a real string.
404
399
                if key == cur_offset_and_size:
405
400
                    yield cur_offset_and_size[0], this_data
406
 
                    cur_offset_and_size = next_offset[0] = offset_stack.next()
 
401
                    cur_offset_and_size = next_offset[0] = next(offset_stack)
407
402
                else:
408
403
                    data_map[key] = this_data
409
404
            data_offset += c_offset.length
412
407
            while cur_offset_and_size in data_map:
413
408
                this_data = data_map.pop(cur_offset_and_size)
414
409
                yield cur_offset_and_size[0], this_data
415
 
                cur_offset_and_size = next_offset[0] = offset_stack.next()
 
410
                cur_offset_and_size = next_offset[0] = next(offset_stack)
416
411
 
417
412
    def rename(self, rel_from, rel_to):
418
413
        self._call('rename',
435
430
        remote._translate_error(err, path=relpath)
436
431
 
437
432
    def disconnect(self):
438
 
        self.get_smart_medium().disconnect()
 
433
        m = self.get_smart_medium()
 
434
        if m is not None:
 
435
            m.disconnect()
439
436
 
440
437
    def stat(self, relpath):
441
438
        resp = self._call2('stat', self._remote_path(relpath))
481
478
 
482
479
    def _build_medium(self):
483
480
        client_medium = medium.SmartTCPClientMedium(
484
 
            self._host, self._port, self.base)
 
481
            self._parsed_url.host, self._parsed_url.port, self.base)
485
482
        return client_medium, None
486
483
 
487
484
 
494
491
 
495
492
    def _build_medium(self):
496
493
        client_medium = medium.SmartTCPClientMedium(
497
 
            self._host, self._port, self.base)
 
494
            self._parsed_url.host, self._parsed_url.port, self.base)
498
495
        client_medium._protocol_version = 2
499
496
        client_medium._remember_remote_is_before((1, 6))
500
497
        return client_medium, None
510
507
    def _build_medium(self):
511
508
        location_config = config.LocationConfig(self.base)
512
509
        bzr_remote_path = location_config.get_bzr_remote_path()
513
 
        user = self._user
 
510
        user = self._parsed_url.user
514
511
        if user is None:
515
512
            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)
 
513
            user = auth.get_user('ssh', self._parsed_url.host,
 
514
                self._parsed_url.port)
 
515
        ssh_params = medium.SSHParams(self._parsed_url.host,
 
516
                self._parsed_url.port, user, self._parsed_url.password,
 
517
                bzr_remote_path)
 
518
        client_medium = medium.SmartSSHClientMedium(self.base, ssh_params)
 
519
        return client_medium, (user, self._parsed_url.password)
521
520
 
522
521
 
523
522
class RemoteHTTPTransport(RemoteTransport):
537
536
            # url only for an intial construction (when the url came from the
538
537
            # command-line).
539
538
            http_url = base[len('bzr+'):]
540
 
            self._http_transport = transport.get_transport(http_url)
 
539
            self._http_transport = transport.get_transport_from_url(http_url)
541
540
        else:
542
541
            self._http_transport = http_transport
543
542
        super(RemoteHTTPTransport, self).__init__(
601
600
    """Return (transport, server) permutations for testing."""
602
601
    ### We may need a little more test framework support to construct an
603
602
    ### appropriate RemoteTransport in the future.
604
 
    from bzrlib.tests import test_server
 
603
    from ..tests import test_server
605
604
    return [(RemoteTCPTransport, test_server.SmartTCPServer_for_testing)]