2018.5.26
by Andrew Bennetts
Extract a simple SmartClient class from RemoteTransport, and a hack to avoid VFS operations when probing for a bzrdir over a smart transport. |
1 |
# Copyright (C) 2006 Canonical Ltd
|
2 |
#
|
|
3 |
# This program is free software; you can redistribute it and/or modify
|
|
4 |
# it under the terms of the GNU General Public License as published by
|
|
5 |
# the Free Software Foundation; either version 2 of the License, or
|
|
6 |
# (at your option) any later version.
|
|
7 |
#
|
|
8 |
# This program is distributed in the hope that it will be useful,
|
|
9 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
10 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
11 |
# GNU General Public License for more details.
|
|
12 |
#
|
|
13 |
# You should have received a copy of the GNU General Public License
|
|
14 |
# along with this program; if not, write to the Free Software
|
|
15 |
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
16 |
||
3192.2.2
by Andrew Bennetts
Just use urllib.unquote, as suggested by John's review. |
17 |
import urllib |
2414.1.1
by Andrew Bennetts
Add some unicode-related tests from the hpss branch, and a few other nits (also from the hpss branch). |
18 |
from urlparse import urlparse |
19 |
||
2018.5.26
by Andrew Bennetts
Extract a simple SmartClient class from RemoteTransport, and a hack to avoid VFS operations when probing for a bzrdir over a smart transport. |
20 |
from bzrlib.smart import protocol |
3297.3.1
by Andrew Bennetts
Raise UnknownSmartMethod automatically from read_response_tuple. |
21 |
from bzrlib import ( |
22 |
errors, |
|
23 |
urlutils, |
|
24 |
)
|
|
2018.5.26
by Andrew Bennetts
Extract a simple SmartClient class from RemoteTransport, and a hack to avoid VFS operations when probing for a bzrdir over a smart transport. |
25 |
|
26 |
||
2414.1.4
by Andrew Bennetts
Rename SmartClient to _SmartClient. |
27 |
class _SmartClient(object): |
2018.5.26
by Andrew Bennetts
Extract a simple SmartClient class from RemoteTransport, and a hack to avoid VFS operations when probing for a bzrdir over a smart transport. |
28 |
|
3313.2.1
by Andrew Bennetts
Change _SmartClient's API to accept a medium and a base, rather than a _SharedConnection. |
29 |
def __init__(self, medium, base): |
3104.4.4
by Andrew Bennetts
Rename parameter to _SmartClient.__init__ to be less confusing. |
30 |
"""Constructor.
|
31 |
||
3313.2.1
by Andrew Bennetts
Change _SmartClient's API to accept a medium and a base, rather than a _SharedConnection. |
32 |
:param medium: a SmartClientMedium
|
33 |
:param base: a URL
|
|
3104.4.4
by Andrew Bennetts
Rename parameter to _SmartClient.__init__ to be less confusing. |
34 |
"""
|
3313.2.1
by Andrew Bennetts
Change _SmartClient's API to accept a medium and a base, rather than a _SharedConnection. |
35 |
self._medium = medium |
36 |
self._base = base |
|
2018.5.26
by Andrew Bennetts
Extract a simple SmartClient class from RemoteTransport, and a hack to avoid VFS operations when probing for a bzrdir over a smart transport. |
37 |
|
38 |
def call(self, method, *args): |
|
39 |
"""Call a method on the remote server."""
|
|
2414.1.2
by Andrew Bennetts
Deal with review comments. |
40 |
result, protocol = self.call_expecting_body(method, *args) |
2414.1.1
by Andrew Bennetts
Add some unicode-related tests from the hpss branch, and a few other nits (also from the hpss branch). |
41 |
protocol.cancel_read_body() |
42 |
return result |
|
43 |
||
2414.1.2
by Andrew Bennetts
Deal with review comments. |
44 |
def call_expecting_body(self, method, *args): |
45 |
"""Call a method and return the result and the protocol object.
|
|
46 |
|
|
47 |
The body can be read like so::
|
|
48 |
||
49 |
result, smart_protocol = smart_client.call_expecting_body(...)
|
|
50 |
body = smart_protocol.read_body_bytes()
|
|
51 |
"""
|
|
3313.2.1
by Andrew Bennetts
Change _SmartClient's API to accept a medium and a base, rather than a _SharedConnection. |
52 |
request = self._medium.get_request() |
2535.4.2
by Andrew Bennetts
Nasty hackery to make stream_knit_data_for_revisions response use streaming. |
53 |
smart_protocol = protocol.SmartClientRequestProtocolTwo(request) |
2018.5.26
by Andrew Bennetts
Extract a simple SmartClient class from RemoteTransport, and a hack to avoid VFS operations when probing for a bzrdir over a smart transport. |
54 |
smart_protocol.call(method, *args) |
2414.1.1
by Andrew Bennetts
Add some unicode-related tests from the hpss branch, and a few other nits (also from the hpss branch). |
55 |
return smart_protocol.read_response_tuple(expect_body=True), smart_protocol |
2018.5.26
by Andrew Bennetts
Extract a simple SmartClient class from RemoteTransport, and a hack to avoid VFS operations when probing for a bzrdir over a smart transport. |
56 |
|
57 |
def call_with_body_bytes(self, method, args, body): |
|
58 |
"""Call a method on the remote server with body bytes."""
|
|
2018.5.131
by Andrew Bennetts
Be strict about unicode passed to transport.put_{bytes,file} and SmartClient.call_with_body_bytes, fixing part of TestLockableFiles_RemoteLockDir.test_read_write. |
59 |
if type(method) is not str: |
60 |
raise TypeError('method must be a byte string, not %r' % (method,)) |
|
61 |
for arg in args: |
|
62 |
if type(arg) is not str: |
|
63 |
raise TypeError('args must be byte strings, not %r' % (args,)) |
|
64 |
if type(body) is not str: |
|
65 |
raise TypeError('body must be byte string, not %r' % (body,)) |
|
3313.2.1
by Andrew Bennetts
Change _SmartClient's API to accept a medium and a base, rather than a _SharedConnection. |
66 |
request = self._medium.get_request() |
2018.5.26
by Andrew Bennetts
Extract a simple SmartClient class from RemoteTransport, and a hack to avoid VFS operations when probing for a bzrdir over a smart transport. |
67 |
smart_protocol = protocol.SmartClientRequestProtocolOne(request) |
68 |
smart_protocol.call_with_body_bytes((method, ) + args, body) |
|
69 |
return smart_protocol.read_response_tuple() |
|
2414.1.1
by Andrew Bennetts
Add some unicode-related tests from the hpss branch, and a few other nits (also from the hpss branch). |
70 |
|
3184.1.10
by Robert Collins
Change the smart server verb for Repository.stream_revisions_chunked to use SearchResults as the request mechanism for downloads. |
71 |
def call_with_body_bytes_expecting_body(self, method, args, body): |
72 |
"""Call a method on the remote server with body bytes."""
|
|
73 |
if type(method) is not str: |
|
74 |
raise TypeError('method must be a byte string, not %r' % (method,)) |
|
75 |
for arg in args: |
|
76 |
if type(arg) is not str: |
|
77 |
raise TypeError('args must be byte strings, not %r' % (args,)) |
|
78 |
if type(body) is not str: |
|
79 |
raise TypeError('body must be byte string, not %r' % (body,)) |
|
3313.2.1
by Andrew Bennetts
Change _SmartClient's API to accept a medium and a base, rather than a _SharedConnection. |
80 |
request = self._medium.get_request() |
3184.1.10
by Robert Collins
Change the smart server verb for Repository.stream_revisions_chunked to use SearchResults as the request mechanism for downloads. |
81 |
smart_protocol = protocol.SmartClientRequestProtocolTwo(request) |
82 |
smart_protocol.call_with_body_bytes((method, ) + args, body) |
|
83 |
return smart_protocol.read_response_tuple(expect_body=True), smart_protocol |
|
84 |
||
2414.1.1
by Andrew Bennetts
Add some unicode-related tests from the hpss branch, and a few other nits (also from the hpss branch). |
85 |
def remote_path_from_transport(self, transport): |
2414.1.2
by Andrew Bennetts
Deal with review comments. |
86 |
"""Convert transport into a path suitable for using in a request.
|
87 |
|
|
88 |
Note that the resulting remote path doesn't encode the host name or
|
|
89 |
anything but path, so it is only safe to use it in requests sent over
|
|
90 |
the medium from the matching transport.
|
|
91 |
"""
|
|
3313.2.1
by Andrew Bennetts
Change _SmartClient's API to accept a medium and a base, rather than a _SharedConnection. |
92 |
base = self._base |
3313.3.1
by Andrew Bennetts
Treat http:// and https:// URLs correctly in _SmartClient.remote_path_for_transport. |
93 |
if (base.startswith('bzr+http://') or base.startswith('bzr+https://') |
94 |
or base.startswith('http://') or base.startswith('https://')): |
|
3313.2.1
by Andrew Bennetts
Change _SmartClient's API to accept a medium and a base, rather than a _SharedConnection. |
95 |
medium_base = self._base |
3104.4.2
by Andrew Bennetts
All tests passing. |
96 |
else: |
3313.2.1
by Andrew Bennetts
Change _SmartClient's API to accept a medium and a base, rather than a _SharedConnection. |
97 |
medium_base = urlutils.join(self._base, '/') |
3104.4.2
by Andrew Bennetts
All tests passing. |
98 |
|
3192.2.1
by Andrew Bennetts
Don't transmit URL-escaped relpaths in the smart protocol, which is back to how things worked in bzr 1.1 and earlier. |
99 |
rel_url = urlutils.relative_url(medium_base, transport.base) |
3192.2.2
by Andrew Bennetts
Just use urllib.unquote, as suggested by John's review. |
100 |
return urllib.unquote(rel_url) |
3297.3.1
by Andrew Bennetts
Raise UnknownSmartMethod automatically from read_response_tuple. |
101 |