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
20
20
imported from bzrlib.smart.
23
from __future__ import absolute_import
23
25
__all__ = ['RemoteTransport', 'RemoteTCPTransport', 'RemoteSSHTransport']
25
27
from cStringIO import StringIO
171
170
def _remote_path(self, relpath):
172
171
"""Returns the Unicode version of the absolute path for relpath."""
173
return self._combine_paths(self._path, relpath)
172
return urlutils.URL._combine_paths(self._parsed_url.path, relpath)
175
174
def _call(self, method, *args):
176
175
resp = self._call2(method, *args)
180
179
"""Call a method on the remote server."""
182
181
return self._client.call(method, *args)
183
except errors.ErrorFromSmartServer, err:
182
except errors.ErrorFromSmartServer as err:
184
183
# The first argument, if present, is always a path.
186
185
context = {'relpath': args[0]}
192
191
"""Call a method on the remote server with body bytes."""
194
193
return self._client.call_with_body_bytes(method, args, body)
195
except errors.ErrorFromSmartServer, err:
194
except errors.ErrorFromSmartServer as err:
196
195
# The first argument, if present, is always a path.
198
197
context = {'relpath': args[0]}
224
223
remote = self._remote_path(relpath)
226
225
resp, response_handler = self._client.call_expecting_body('get', remote)
227
except errors.ErrorFromSmartServer, err:
226
except errors.ErrorFromSmartServer as err:
228
227
self._translate_error(err, relpath)
229
228
if resp != ('ok', ):
230
229
response_handler.cancel_read_body()
248
247
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',
250
def put_bytes(self, relpath, raw_bytes, mode=None):
251
if not isinstance(raw_bytes, str):
253
'raw_bytes must be a plain string, not %s' % type(raw_bytes))
254
resp = self._call_with_body_bytes(
262
256
(self._remote_path(relpath), self._serialise_optional_mode(mode)),
264
258
self._ensure_ok(resp)
265
return len(upload_contents)
259
return len(raw_bytes)
267
def put_bytes_non_atomic(self, relpath, bytes, mode=None,
261
def put_bytes_non_atomic(self, relpath, raw_bytes, mode=None,
268
262
create_parent_dir=False,
270
264
"""See Transport.put_bytes_non_atomic."""
277
271
'put_non_atomic',
278
272
(self._remote_path(relpath), self._serialise_optional_mode(mode),
279
273
create_parent_str, self._serialise_optional_mode(dir_mode)),
281
275
self._ensure_ok(resp)
283
277
def put_file(self, relpath, upload_file, mode=None):
366
360
('readv', self._remote_path(relpath),),
367
361
[(c.start, c.length) for c in cur_request])
368
362
resp, response_handler = result
369
except errors.ErrorFromSmartServer, err:
363
except errors.ErrorFromSmartServer as err:
370
364
self._translate_error(err, relpath)
372
366
if resp[0] != 'readv':
435
429
remote._translate_error(err, path=relpath)
437
431
def disconnect(self):
438
self.get_smart_medium().disconnect()
432
m = self.get_smart_medium()
440
436
def stat(self, relpath):
441
437
resp = self._call2('stat', self._remote_path(relpath))
482
478
def _build_medium(self):
483
479
client_medium = medium.SmartTCPClientMedium(
484
self._host, self._port, self.base)
480
self._parsed_url.host, self._parsed_url.port, self.base)
485
481
return client_medium, None
495
491
def _build_medium(self):
496
492
client_medium = medium.SmartTCPClientMedium(
497
self._host, self._port, self.base)
493
self._parsed_url.host, self._parsed_url.port, self.base)
498
494
client_medium._protocol_version = 2
499
495
client_medium._remember_remote_is_before((1, 6))
500
496
return client_medium, None
510
506
def _build_medium(self):
511
507
location_config = config.LocationConfig(self.base)
512
508
bzr_remote_path = location_config.get_bzr_remote_path()
509
user = self._parsed_url.user
515
511
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)
512
user = auth.get_user('ssh', self._parsed_url.host,
513
self._parsed_url.port)
514
ssh_params = medium.SSHParams(self._parsed_url.host,
515
self._parsed_url.port, user, self._parsed_url.password,
517
client_medium = medium.SmartSSHClientMedium(self.base, ssh_params)
518
return client_medium, (user, self._parsed_url.password)
523
521
class RemoteHTTPTransport(RemoteTransport):
537
535
# url only for an intial construction (when the url came from the
539
537
http_url = base[len('bzr+'):]
540
self._http_transport = transport.get_transport(http_url)
538
self._http_transport = transport.get_transport_from_url(http_url)
542
540
self._http_transport = http_transport
543
541
super(RemoteHTTPTransport, self).__init__(