/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: Breezy landing bot
  • Author(s): Jelmer Vernooij
  • Date: 2018-07-04 12:50:55 UTC
  • mfrom: (7027.2.8 git-fixes)
  • Revision ID: breezy.the.bot@gmail.com-20180704125055-8nni25pn2439p48v
Fix eol handling in knits on Python 3, port fastimport plugin to Python 3.

Merged from https://code.launchpad.net/~jelmer/brz/fastimport-fixes/+merge/348924

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