1
# Copyright (C) 2006 Canonical Ltd
1
# Copyright (C) 2006-2012, 2016 Canonical Ltd
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.
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.
23
from __future__ import absolute_import
23
25
__all__ = ['RemoteTransport', 'RemoteTCPTransport', 'RemoteSSHTransport']
25
from cStringIO import StringIO
36
from bzrlib.smart import client, medium
37
from bzrlib.symbol_versioning import (
36
from ..sixish import (
39
from ..smart import client, medium
42
42
class _SmartStat(object):
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)
175
175
def _call(self, method, *args):
176
176
resp = self._call2(method, *args)
180
180
"""Call a method on the remote server."""
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.
186
186
context = {'relpath': args[0]}
192
192
"""Call a method on the remote server with body bytes."""
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.
198
198
context = {'relpath': args[0]}
219
219
:see: Transport.get_bytes()/get_file()
221
return StringIO(self.get_bytes(relpath))
221
return BytesIO(self.get_bytes(relpath))
223
223
def get_bytes(self, relpath):
224
224
remote = self._remote_path(relpath)
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
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
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):
254
'raw_bytes must be a plain string, not %s' % type(raw_bytes))
255
resp = self._call_with_body_bytes(
262
257
(self._remote_path(relpath), self._serialise_optional_mode(mode)),
264
259
self._ensure_ok(resp)
265
return len(upload_contents)
260
return len(raw_bytes)
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,
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)),
281
276
self._ensure_ok(resp)
283
278
def put_file(self, relpath, upload_file, mode=None):
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:
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)
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)
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)
417
412
def rename(self, rel_from, rel_to):
418
413
self._call('rename',
435
430
remote._translate_error(err, path=relpath)
437
432
def disconnect(self):
438
self.get_smart_medium().disconnect()
433
m = self.get_smart_medium()
440
437
def stat(self, relpath):
441
438
resp = self._call2('stat', self._remote_path(relpath))
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
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()
510
user = self._parsed_url.user
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,
518
client_medium = medium.SmartSSHClientMedium(self.base, ssh_params)
519
return client_medium, (user, self._parsed_url.password)
523
522
class RemoteHTTPTransport(RemoteTransport):
537
536
# url only for an intial construction (when the url came from the
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)
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)]