13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
17
"""RemoteTransport client for the smart-server.
36
36
from bzrlib.smart import client, medium
37
from bzrlib.symbol_versioning import (
37
from bzrlib.symbol_versioning import (deprecated_method, one_four)
42
40
class _SmartStat(object):
55
53
The connection has a notion of the current directory to which it's
56
54
connected; this is incorporated in filenames passed to the server.
58
This supports some higher-level RPC operations and can also be treated
56
This supports some higher-level RPC operations and can also be treated
59
57
like a Transport to do file-like operations.
61
59
The connection can be made over a tcp socket, an ssh pipe or a series of
69
67
# IMPORTANT FOR IMPLEMENTORS: RemoteTransport MUST NOT be given encoding
70
68
# responsibilities: Put those on SmartClient or similar. This is vital for
71
69
# the ability to support multiple versions of the smart protocol over time:
72
# RemoteTransport is an adapter from the Transport object model to the
70
# RemoteTransport is an adapter from the Transport object model to the
73
71
# SmartClient model, not an encoder.
75
73
# FIXME: the medium parameter should be private, only the tests requires
92
90
should only be used for testing purposes; normally this is
93
91
determined from the medium.
95
super(RemoteTransport, self).__init__(
96
url, _from_transport=_from_transport)
93
super(RemoteTransport, self).__init__(url,
94
_from_transport=_from_transport)
98
96
# The medium is the connection, except when we need to share it with
99
97
# other objects (RemoteBzrDir, RemoteRepository etc). In these cases
100
98
# what we want to share is really the shared connection.
102
if (_from_transport is not None
103
and isinstance(_from_transport, RemoteTransport)):
104
_client = _from_transport._client
105
elif _from_transport is None:
100
if _from_transport is None:
106
101
# If no _from_transport is specified, we need to intialize the
108
103
credentials = None
139
134
return None, None
141
def _report_activity(self, bytes, direction):
142
"""See Transport._report_activity.
144
Does nothing; the smart medium will report activity triggered by a
149
136
def is_readonly(self):
150
137
"""Smart server transport can do read/write file operations."""
168
155
def get_smart_medium(self):
169
156
return self._get_connection()
158
@deprecated_method(one_four)
159
def get_shared_medium(self):
160
return self._get_shared_connection()
171
162
def _remote_path(self, relpath):
172
163
"""Returns the Unicode version of the absolute path for relpath."""
173
164
return self._combine_paths(self._path, relpath)
182
173
return self._client.call(method, *args)
183
174
except errors.ErrorFromSmartServer, err:
184
# The first argument, if present, is always a path.
186
context = {'relpath': args[0]}
189
self._translate_error(err, **context)
175
self._translate_error(err)
191
177
def _call_with_body_bytes(self, method, args, body):
192
178
"""Call a method on the remote server with body bytes."""
194
180
return self._client.call_with_body_bytes(method, args, body)
195
181
except errors.ErrorFromSmartServer, err:
196
# The first argument, if present, is always a path.
198
context = {'relpath': args[0]}
201
self._translate_error(err, **context)
182
self._translate_error(err)
203
184
def has(self, relpath):
204
185
"""Indicate whether a remote file of the given name exists or not.
301
282
def append_file(self, relpath, from_file, mode=None):
302
283
return self.append_bytes(relpath, from_file.read(), mode)
304
285
def append_bytes(self, relpath, bytes, mode=None):
305
286
resp = self._call_with_body_bytes(
332
313
sorted_offsets = sorted(offsets)
333
314
coalesced = list(self._coalesce_offsets(sorted_offsets,
334
315
limit=self._max_readv_combine,
335
fudge_factor=self._bytes_to_read_before_seek,
336
max_size=self._max_readv_bytes))
316
fudge_factor=self._bytes_to_read_before_seek))
338
318
# now that we've coallesced things, avoid making enormous requests
367
347
[(c.start, c.length) for c in cur_request])
368
348
resp, response_handler = result
369
349
except errors.ErrorFromSmartServer, err:
370
self._translate_error(err, relpath)
350
self._translate_error(err)
372
352
if resp[0] != 'readv':
373
353
# This should raise an exception
430
410
def _ensure_ok(self, resp):
431
411
if resp[0] != 'ok':
432
412
raise errors.UnexpectedSmartServerResponse(resp)
434
def _translate_error(self, err, relpath=None):
435
remote._translate_error(err, path=relpath)
414
def _translate_error(self, err, orig_path=None):
415
remote._translate_error(err, path=orig_path)
437
417
def disconnect(self):
438
418
self.get_smart_medium().disconnect()
523
503
class RemoteHTTPTransport(RemoteTransport):
524
504
"""Just a way to connect between a bzr+http:// url and http://.
526
506
This connection operates slightly differently than the RemoteSSHTransport.
527
507
It uses a plain http:// transport underneath, which defines what remote
528
508
.bzr/smart URL we are connected to. From there, all paths that are sent are
577
557
_from_transport=self,
578
558
http_transport=self._http_transport)
580
def _redirected_to(self, source, target):
581
"""See transport._redirected_to"""
582
redirected = self._http_transport._redirected_to(source, target)
583
if (redirected is not None
584
and isinstance(redirected, type(self._http_transport))):
585
return RemoteHTTPTransport('bzr+' + redirected.external_url(),
586
http_transport=redirected)
588
# Either None or a transport for a different protocol
592
class HintingSSHTransport(transport.Transport):
593
"""Simple transport that handles ssh:// and points out bzr+ssh://."""
595
def __init__(self, url):
596
raise errors.UnsupportedProtocol(url,
597
'bzr supports bzr+ssh to operate over ssh, use "bzr+%s".' % url)
600
561
def get_test_permutations():
601
562
"""Return (transport, server) permutations for testing."""
602
563
### We may need a little more test framework support to construct an
603
564
### appropriate RemoteTransport in the future.
604
from bzrlib.tests import test_server
605
return [(RemoteTCPTransport, test_server.SmartTCPServer_for_testing)]
565
from bzrlib.smart import server
566
return [(RemoteTCPTransport, server.SmartTCPServer_for_testing)]