bzr branch
http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
|
2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
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 |
||
|
2018.5.19
by Andrew Bennetts
Add docstrings to all the new modules, and a few other places. |
17 |
"""The 'medium' layer for the smart servers and clients.
|
18 |
||
19 |
"Medium" here is the noun meaning "a means of transmission", not the adjective
|
|
20 |
for "the quality between big and small."
|
|
21 |
||
22 |
Media carry the bytes of the requests somehow (e.g. via TCP, wrapped in HTTP, or
|
|
23 |
over SSH), and pass them to and from the protocol logic. See the overview in
|
|
24 |
bzrlib/transport/smart/__init__.py.
|
|
25 |
"""
|
|
26 |
||
|
2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
27 |
import os |
28 |
import socket |
|
|
2018.5.162
by Andrew Bennetts
Add some missing _ensure_real calls, and a missing import. |
29 |
import sys |
|
3431.3.11
by Andrew Bennetts
Push remote_path_from_transport logic into SmartClientMedium, removing special-casing of bzr+http from _SmartClient. |
30 |
import urllib |
|
3731.2.1
by Andrew Bennetts
Show total HPSS calls (if any) on stderr when -Dhpss is active. |
31 |
import weakref |
|
2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
32 |
|
|
3530.1.1
by John Arbash Meinel
Make bzrlib.smart use lazy imports. |
33 |
from bzrlib.lazy_import import lazy_import |
34 |
lazy_import(globals(), """ |
|
|
1551.18.17
by Aaron Bentley
Introduce bzr_remote_path configuration variable |
35 |
from bzrlib import (
|
|
3731.2.1
by Andrew Bennetts
Show total HPSS calls (if any) on stderr when -Dhpss is active. |
36 |
debug,
|
|
1551.18.17
by Aaron Bentley
Introduce bzr_remote_path configuration variable |
37 |
errors,
|
|
3118.2.1
by Andrew Bennetts
(andrew) Fix #115781 by passing no more than 64k at a time to socket.sendall. |
38 |
osutils,
|
|
1551.18.17
by Aaron Bentley
Introduce bzr_remote_path configuration variable |
39 |
symbol_versioning,
|
|
3431.3.11
by Andrew Bennetts
Push remote_path_from_transport logic into SmartClientMedium, removing special-casing of bzr+http from _SmartClient. |
40 |
urlutils,
|
|
1551.18.17
by Aaron Bentley
Introduce bzr_remote_path configuration variable |
41 |
)
|
|
3731.2.1
by Andrew Bennetts
Show total HPSS calls (if any) on stderr when -Dhpss is active. |
42 |
from bzrlib.smart import client, protocol
|
|
3066.2.1
by John Arbash Meinel
We don't require paramiko for bzr+ssh. |
43 |
from bzrlib.transport import ssh
|
|
3530.1.1
by John Arbash Meinel
Make bzrlib.smart use lazy imports. |
44 |
""") |
|
2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
45 |
|
|
2018.5.17
by Andrew Bennetts
Paramaterise the commands handled by SmartServerRequestHandler. |
46 |
|
|
3565.1.3
by Andrew Bennetts
Define a _MAX_READ_SIZE constant as suggested by John's review. |
47 |
# We must not read any more than 64k at a time so we don't risk "no buffer
|
48 |
# space available" errors on some platforms. Windows in particular is likely
|
|
49 |
# to give error 10053 or 10055 if we read more than 64k from a socket.
|
|
50 |
_MAX_READ_SIZE = 64 * 1024 |
|
51 |
||
52 |
||
|
3245.4.16
by Andrew Bennetts
Remove duplication of request version identification logic in wsgi.py |
53 |
def _get_protocol_factory_for_bytes(bytes): |
54 |
"""Determine the right protocol factory for 'bytes'. |
|
55 |
||
56 |
This will return an appropriate protocol factory depending on the version
|
|
57 |
of the protocol being used, as determined by inspecting the given bytes.
|
|
58 |
The bytes should have at least one newline byte (i.e. be a whole line),
|
|
59 |
otherwise it's possible that a request will be incorrectly identified as
|
|
60 |
version 1.
|
|
61 |
||
62 |
Typical use would be::
|
|
63 |
||
64 |
factory, unused_bytes = _get_protocol_factory_for_bytes(bytes)
|
|
65 |
server_protocol = factory(transport, write_func, root_client_path)
|
|
66 |
server_protocol.accept_bytes(unused_bytes)
|
|
67 |
||
68 |
:param bytes: a str of bytes of the start of the request.
|
|
69 |
:returns: 2-tuple of (protocol_factory, unused_bytes). protocol_factory is
|
|
70 |
a callable that takes three args: transport, write_func,
|
|
71 |
root_client_path. unused_bytes are any bytes that were not part of a
|
|
72 |
protocol version marker.
|
|
73 |
"""
|
|
|
3530.1.1
by John Arbash Meinel
Make bzrlib.smart use lazy imports. |
74 |
if bytes.startswith(protocol.MESSAGE_VERSION_THREE): |
75 |
protocol_factory = protocol.build_server_protocol_three |
|
76 |
bytes = bytes[len(protocol.MESSAGE_VERSION_THREE):] |
|
77 |
elif bytes.startswith(protocol.REQUEST_VERSION_TWO): |
|
78 |
protocol_factory = protocol.SmartServerRequestProtocolTwo |
|
79 |
bytes = bytes[len(protocol.REQUEST_VERSION_TWO):] |
|
|
3245.4.16
by Andrew Bennetts
Remove duplication of request version identification logic in wsgi.py |
80 |
else: |
|
3530.1.1
by John Arbash Meinel
Make bzrlib.smart use lazy imports. |
81 |
protocol_factory = protocol.SmartServerRequestProtocolOne |
|
3245.4.16
by Andrew Bennetts
Remove duplication of request version identification logic in wsgi.py |
82 |
return protocol_factory, bytes |
83 |
||
84 |
||
|
3606.4.1
by Andrew Bennetts
Fix NotImplementedError when probing for smart protocol via HTTP. |
85 |
def _get_line(read_bytes_func): |
86 |
"""Read bytes using read_bytes_func until a newline byte. |
|
87 |
|
|
88 |
This isn't particularly efficient, so should only be used when the
|
|
89 |
expected size of the line is quite short.
|
|
90 |
|
|
91 |
:returns: a tuple of two strs: (line, excess)
|
|
92 |
"""
|
|
93 |
newline_pos = -1 |
|
94 |
bytes = '' |
|
95 |
while newline_pos == -1: |
|
96 |
new_bytes = read_bytes_func(1) |
|
97 |
bytes += new_bytes |
|
98 |
if new_bytes == '': |
|
99 |
# Ran out of bytes before receiving a complete line.
|
|
100 |
return bytes, '' |
|
101 |
newline_pos = bytes.find('\n') |
|
102 |
line = bytes[:newline_pos+1] |
|
103 |
excess = bytes[newline_pos+1:] |
|
104 |
return line, excess |
|
105 |
||
106 |
||
|
3565.1.1
by Andrew Bennetts
Read no more then 64k at a time in the smart protocol code. |
107 |
class SmartMedium(object): |
108 |
"""Base class for smart protocol media, both client- and server-side.""" |
|
109 |
||
110 |
def __init__(self): |
|
111 |
self._push_back_buffer = None |
|
112 |
||
113 |
def _push_back(self, bytes): |
|
114 |
"""Return unused bytes to the medium, because they belong to the next |
|
115 |
request(s).
|
|
116 |
||
117 |
This sets the _push_back_buffer to the given bytes.
|
|
118 |
"""
|
|
119 |
if self._push_back_buffer is not None: |
|
120 |
raise AssertionError( |
|
121 |
"_push_back called when self._push_back_buffer is %r" |
|
122 |
% (self._push_back_buffer,)) |
|
123 |
if bytes == '': |
|
124 |
return
|
|
125 |
self._push_back_buffer = bytes |
|
126 |
||
127 |
def _get_push_back_buffer(self): |
|
128 |
if self._push_back_buffer == '': |
|
129 |
raise AssertionError( |
|
130 |
'%s._push_back_buffer should never be the empty string, ' |
|
131 |
'which can be confused with EOF' % (self,)) |
|
132 |
bytes = self._push_back_buffer |
|
133 |
self._push_back_buffer = None |
|
134 |
return bytes |
|
135 |
||
136 |
def read_bytes(self, desired_count): |
|
|
3565.1.2
by Andrew Bennetts
Delete some more code, fix some bugs, add more comments. |
137 |
"""Read some bytes from this medium. |
138 |
||
139 |
:returns: some bytes, possibly more or less than the number requested
|
|
140 |
in 'desired_count' depending on the medium.
|
|
141 |
"""
|
|
142 |
if self._push_back_buffer is not None: |
|
143 |
return self._get_push_back_buffer() |
|
|
3565.1.3
by Andrew Bennetts
Define a _MAX_READ_SIZE constant as suggested by John's review. |
144 |
bytes_to_read = min(desired_count, _MAX_READ_SIZE) |
|
3565.1.1
by Andrew Bennetts
Read no more then 64k at a time in the smart protocol code. |
145 |
return self._read_bytes(bytes_to_read) |
146 |
||
147 |
def _read_bytes(self, count): |
|
148 |
raise NotImplementedError(self._read_bytes) |
|
149 |
||
150 |
def _get_line(self): |
|
151 |
"""Read bytes from this request's response until a newline byte. |
|
152 |
|
|
153 |
This isn't particularly efficient, so should only be used when the
|
|
154 |
expected size of the line is quite short.
|
|
155 |
||
156 |
:returns: a string of bytes ending in a newline (byte 0x0A).
|
|
157 |
"""
|
|
|
3606.4.1
by Andrew Bennetts
Fix NotImplementedError when probing for smart protocol via HTTP. |
158 |
line, excess = _get_line(self.read_bytes) |
159 |
self._push_back(excess) |
|
|
3565.1.1
by Andrew Bennetts
Read no more then 64k at a time in the smart protocol code. |
160 |
return line |
161 |
||
162 |
||
163 |
class SmartServerStreamMedium(SmartMedium): |
|
|
2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
164 |
"""Handles smart commands coming over a stream. |
165 |
||
166 |
The stream may be a pipe connected to sshd, or a tcp socket, or an
|
|
167 |
in-process fifo for testing.
|
|
168 |
||
169 |
One instance is created for each connected client; it can serve multiple
|
|
170 |
requests in the lifetime of the connection.
|
|
171 |
||
172 |
The server passes requests through to an underlying backing transport,
|
|
173 |
which will typically be a LocalTransport looking at the server's filesystem.
|
|
|
3236.3.4
by Andrew Bennetts
Rename 'push_back' attribute to '_push_back_buffer', add some docstrings, and remove a little bit of redundant code from SmartServerSocketStreamMedium._serve_one_request_unguarded. |
174 |
|
175 |
:ivar _push_back_buffer: a str of bytes that have been read from the stream
|
|
176 |
but not used yet, or None if there are no buffered bytes. Subclasses
|
|
177 |
should make sure to exhaust this buffer before reading more bytes from
|
|
178 |
the stream. See also the _push_back method.
|
|
|
2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
179 |
"""
|
180 |
||
|
2692.1.11
by Andrew Bennetts
Improve test coverage by making SmartTCPServer_for_testing by default create a server that does not serve the backing transport's root at its own root. This mirrors the way most HTTP smart servers are configured. |
181 |
def __init__(self, backing_transport, root_client_path='/'): |
|
2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
182 |
"""Construct new server. |
183 |
||
184 |
:param backing_transport: Transport for the directory served.
|
|
185 |
"""
|
|
186 |
# backing_transport could be passed to serve instead of __init__
|
|
187 |
self.backing_transport = backing_transport |
|
|
2692.1.11
by Andrew Bennetts
Improve test coverage by making SmartTCPServer_for_testing by default create a server that does not serve the backing transport's root at its own root. This mirrors the way most HTTP smart servers are configured. |
188 |
self.root_client_path = root_client_path |
|
2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
189 |
self.finished = False |
|
3565.1.1
by Andrew Bennetts
Read no more then 64k at a time in the smart protocol code. |
190 |
SmartMedium.__init__(self) |
|
3236.3.5
by Andrew Bennetts
Add _get_push_back_buffer helper. |
191 |
|
|
2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
192 |
def serve(self): |
193 |
"""Serve requests until the client disconnects.""" |
|
194 |
# Keep a reference to stderr because the sys module's globals get set to
|
|
195 |
# None during interpreter shutdown.
|
|
196 |
from sys import stderr |
|
197 |
try: |
|
198 |
while not self.finished: |
|
|
2432.2.3
by Andrew Bennetts
Merge from bzr.dev. |
199 |
server_protocol = self._build_protocol() |
|
2018.5.14
by Andrew Bennetts
Move SmartTCPServer to smart/server.py, and SmartServerRequestHandler to smart/request.py. |
200 |
self._serve_one_request(server_protocol) |
|
2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
201 |
except Exception, e: |
202 |
stderr.write("%s terminating on exception %s\n" % (self, e)) |
|
203 |
raise
|
|
204 |
||
|
2432.2.2
by Andrew Bennetts
Smart server mediums now detect which protocol version a request is and dispatch accordingly. |
205 |
def _build_protocol(self): |
|
2432.2.8
by Andrew Bennetts
NEWS entry, greatly improved docstring in bzrlib.smart. |
206 |
"""Identifies the version of the incoming request, and returns an |
207 |
a protocol object that can interpret it.
|
|
208 |
||
209 |
If more bytes than the version prefix of the request are read, they will
|
|
210 |
be fed into the protocol before it is returned.
|
|
211 |
||
212 |
:returns: a SmartServerRequestProtocol.
|
|
213 |
"""
|
|
|
2432.2.7
by Andrew Bennetts
Use less confusing version strings, and define REQUEST_VERSION_TWO/RESPONSE_VERSION_TWO constants for them. |
214 |
bytes = self._get_line() |
|
3245.4.16
by Andrew Bennetts
Remove duplication of request version identification logic in wsgi.py |
215 |
protocol_factory, unused_bytes = _get_protocol_factory_for_bytes(bytes) |
|
3245.4.14
by Andrew Bennetts
Merge from bzr.dev (via loom thread). |
216 |
protocol = protocol_factory( |
|
2692.1.11
by Andrew Bennetts
Improve test coverage by making SmartTCPServer_for_testing by default create a server that does not serve the backing transport's root at its own root. This mirrors the way most HTTP smart servers are configured. |
217 |
self.backing_transport, self._write_out, self.root_client_path) |
|
3245.4.16
by Andrew Bennetts
Remove duplication of request version identification logic in wsgi.py |
218 |
protocol.accept_bytes(unused_bytes) |
|
2432.2.2
by Andrew Bennetts
Smart server mediums now detect which protocol version a request is and dispatch accordingly. |
219 |
return protocol |
220 |
||
|
2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
221 |
def _serve_one_request(self, protocol): |
222 |
"""Read one request from input, process, send back a response. |
|
223 |
|
|
224 |
:param protocol: a SmartServerRequestProtocol.
|
|
225 |
"""
|
|
226 |
try: |
|
227 |
self._serve_one_request_unguarded(protocol) |
|
228 |
except KeyboardInterrupt: |
|
229 |
raise
|
|
230 |
except Exception, e: |
|
231 |
self.terminate_due_to_error() |
|
232 |
||
233 |
def terminate_due_to_error(self): |
|
234 |
"""Called when an unhandled exception from the protocol occurs.""" |
|
235 |
raise NotImplementedError(self.terminate_due_to_error) |
|
236 |
||
|
3565.1.2
by Andrew Bennetts
Delete some more code, fix some bugs, add more comments. |
237 |
def _read_bytes(self, desired_count): |
|
2432.2.2
by Andrew Bennetts
Smart server mediums now detect which protocol version a request is and dispatch accordingly. |
238 |
"""Get some bytes from the medium. |
239 |
||
240 |
:param desired_count: number of bytes we want to read.
|
|
241 |
"""
|
|
|
3565.1.2
by Andrew Bennetts
Delete some more code, fix some bugs, add more comments. |
242 |
raise NotImplementedError(self._read_bytes) |
|
2432.2.2
by Andrew Bennetts
Smart server mediums now detect which protocol version a request is and dispatch accordingly. |
243 |
|
|
2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
244 |
|
245 |
class SmartServerSocketStreamMedium(SmartServerStreamMedium): |
|
246 |
||
|
2692.1.11
by Andrew Bennetts
Improve test coverage by making SmartTCPServer_for_testing by default create a server that does not serve the backing transport's root at its own root. This mirrors the way most HTTP smart servers are configured. |
247 |
def __init__(self, sock, backing_transport, root_client_path='/'): |
|
2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
248 |
"""Constructor. |
249 |
||
250 |
:param sock: the socket the server will read from. It will be put
|
|
251 |
into blocking mode.
|
|
252 |
"""
|
|
|
2692.1.11
by Andrew Bennetts
Improve test coverage by making SmartTCPServer_for_testing by default create a server that does not serve the backing transport's root at its own root. This mirrors the way most HTTP smart servers are configured. |
253 |
SmartServerStreamMedium.__init__( |
254 |
self, backing_transport, root_client_path=root_client_path) |
|
|
2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
255 |
sock.setblocking(True) |
256 |
self.socket = sock |
|
257 |
||
258 |
def _serve_one_request_unguarded(self, protocol): |
|
259 |
while protocol.next_read_size(): |
|
|
3565.1.3
by Andrew Bennetts
Define a _MAX_READ_SIZE constant as suggested by John's review. |
260 |
# We can safely try to read large chunks. If there is less data
|
261 |
# than _MAX_READ_SIZE ready, the socket wil just return a short
|
|
262 |
# read immediately rather than block.
|
|
263 |
bytes = self.read_bytes(_MAX_READ_SIZE) |
|
|
3236.3.4
by Andrew Bennetts
Rename 'push_back' attribute to '_push_back_buffer', add some docstrings, and remove a little bit of redundant code from SmartServerSocketStreamMedium._serve_one_request_unguarded. |
264 |
if bytes == '': |
265 |
self.finished = True |
|
266 |
return
|
|
267 |
protocol.accept_bytes(bytes) |
|
|
2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
268 |
|
|
3245.4.21
by Andrew Bennetts
Remove 'excess_buffer' attribute and another crufty comment. |
269 |
self._push_back(protocol.unused_data) |
|
3195.3.18
by Andrew Bennetts
call_with_body_bytes now works with v3 (e.g. test_copy_content_remote_to_local passes). Lots of debugging cruft, though. |
270 |
|
|
3565.1.2
by Andrew Bennetts
Delete some more code, fix some bugs, add more comments. |
271 |
def _read_bytes(self, desired_count): |
|
2432.2.2
by Andrew Bennetts
Smart server mediums now detect which protocol version a request is and dispatch accordingly. |
272 |
# We ignore the desired_count because on sockets it's more efficient to
|
|
3565.1.3
by Andrew Bennetts
Define a _MAX_READ_SIZE constant as suggested by John's review. |
273 |
# read large chunks (of _MAX_READ_SIZE bytes) at a time.
|
274 |
return self.socket.recv(_MAX_READ_SIZE) |
|
|
3565.1.1
by Andrew Bennetts
Read no more then 64k at a time in the smart protocol code. |
275 |
|
|
2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
276 |
def terminate_due_to_error(self): |
|
3245.4.59
by Andrew Bennetts
Various tweaks in response to Martin's review. |
277 |
# TODO: This should log to a server log file, but no such thing
|
278 |
# exists yet. Andrew Bennetts 2006-09-29.
|
|
|
2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
279 |
self.socket.close() |
280 |
self.finished = True |
|
281 |
||
282 |
def _write_out(self, bytes): |
|
|
3118.2.1
by Andrew Bennetts
(andrew) Fix #115781 by passing no more than 64k at a time to socket.sendall. |
283 |
osutils.send_all(self.socket, bytes) |
|
2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
284 |
|
285 |
||
286 |
class SmartServerPipeStreamMedium(SmartServerStreamMedium): |
|
287 |
||
288 |
def __init__(self, in_file, out_file, backing_transport): |
|
289 |
"""Construct new server. |
|
290 |
||
291 |
:param in_file: Python file from which requests can be read.
|
|
292 |
:param out_file: Python file to write responses.
|
|
293 |
:param backing_transport: Transport for the directory served.
|
|
294 |
"""
|
|
295 |
SmartServerStreamMedium.__init__(self, backing_transport) |
|
|
2018.5.161
by Andrew Bennetts
Reinstate forcing binary mode on windows in SmartServerStreamMedium. |
296 |
if sys.platform == 'win32': |
297 |
# force binary mode for files
|
|
298 |
import msvcrt |
|
299 |
for f in (in_file, out_file): |
|
300 |
fileno = getattr(f, 'fileno', None) |
|
301 |
if fileno: |
|
302 |
msvcrt.setmode(fileno(), os.O_BINARY) |
|
|
2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
303 |
self._in = in_file |
304 |
self._out = out_file |
|
305 |
||
306 |
def _serve_one_request_unguarded(self, protocol): |
|
307 |
while True: |
|
|
3565.1.2
by Andrew Bennetts
Delete some more code, fix some bugs, add more comments. |
308 |
# We need to be careful not to read past the end of the current
|
309 |
# request, or else the read from the pipe will block, so we use
|
|
310 |
# protocol.next_read_size().
|
|
|
2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
311 |
bytes_to_read = protocol.next_read_size() |
312 |
if bytes_to_read == 0: |
|
313 |
# Finished serving this request.
|
|
314 |
self._out.flush() |
|
315 |
return
|
|
|
3565.1.2
by Andrew Bennetts
Delete some more code, fix some bugs, add more comments. |
316 |
bytes = self.read_bytes(bytes_to_read) |
|
2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
317 |
if bytes == '': |
318 |
# Connection has been closed.
|
|
319 |
self.finished = True |
|
320 |
self._out.flush() |
|
321 |
return
|
|
322 |
protocol.accept_bytes(bytes) |
|
323 |
||
|
3565.1.2
by Andrew Bennetts
Delete some more code, fix some bugs, add more comments. |
324 |
def _read_bytes(self, desired_count): |
|
2432.2.2
by Andrew Bennetts
Smart server mediums now detect which protocol version a request is and dispatch accordingly. |
325 |
return self._in.read(desired_count) |
326 |
||
|
2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
327 |
def terminate_due_to_error(self): |
328 |
# TODO: This should log to a server log file, but no such thing
|
|
329 |
# exists yet. Andrew Bennetts 2006-09-29.
|
|
330 |
self._out.close() |
|
331 |
self.finished = True |
|
332 |
||
333 |
def _write_out(self, bytes): |
|
334 |
self._out.write(bytes) |
|
335 |
||
336 |
||
|
3565.1.2
by Andrew Bennetts
Delete some more code, fix some bugs, add more comments. |
337 |
class SmartClientMediumRequest(object): |
|
2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
338 |
"""A request on a SmartClientMedium. |
339 |
||
340 |
Each request allows bytes to be provided to it via accept_bytes, and then
|
|
341 |
the response bytes to be read via read_bytes.
|
|
342 |
||
343 |
For instance:
|
|
344 |
request.accept_bytes('123')
|
|
345 |
request.finished_writing()
|
|
346 |
result = request.read_bytes(3)
|
|
347 |
request.finished_reading()
|
|
348 |
||
349 |
It is up to the individual SmartClientMedium whether multiple concurrent
|
|
350 |
requests can exist. See SmartClientMedium.get_request to obtain instances
|
|
351 |
of SmartClientMediumRequest, and the concrete Medium you are using for
|
|
352 |
details on concurrency and pipelining.
|
|
353 |
"""
|
|
354 |
||
355 |
def __init__(self, medium): |
|
356 |
"""Construct a SmartClientMediumRequest for the medium medium.""" |
|
357 |
self._medium = medium |
|
358 |
# we track state by constants - we may want to use the same
|
|
359 |
# pattern as BodyReader if it gets more complex.
|
|
360 |
# valid states are: "writing", "reading", "done"
|
|
361 |
self._state = "writing" |
|
362 |
||
363 |
def accept_bytes(self, bytes): |
|
364 |
"""Accept bytes for inclusion in this request. |
|
365 |
||
366 |
This method may not be be called after finished_writing() has been
|
|
367 |
called. It depends upon the Medium whether or not the bytes will be
|
|
368 |
immediately transmitted. Message based Mediums will tend to buffer the
|
|
369 |
bytes until finished_writing() is called.
|
|
370 |
||
371 |
:param bytes: A bytestring.
|
|
372 |
"""
|
|
373 |
if self._state != "writing": |
|
374 |
raise errors.WritingCompleted(self) |
|
375 |
self._accept_bytes(bytes) |
|
376 |
||
377 |
def _accept_bytes(self, bytes): |
|
378 |
"""Helper for accept_bytes. |
|
379 |
||
380 |
Accept_bytes checks the state of the request to determing if bytes
|
|
381 |
should be accepted. After that it hands off to _accept_bytes to do the
|
|
382 |
actual acceptance.
|
|
383 |
"""
|
|
384 |
raise NotImplementedError(self._accept_bytes) |
|
385 |
||
386 |
def finished_reading(self): |
|
387 |
"""Inform the request that all desired data has been read. |
|
388 |
||
389 |
This will remove the request from the pipeline for its medium (if the
|
|
390 |
medium supports pipelining) and any further calls to methods on the
|
|
391 |
request will raise ReadingCompleted.
|
|
392 |
"""
|
|
393 |
if self._state == "writing": |
|
394 |
raise errors.WritingNotComplete(self) |
|
395 |
if self._state != "reading": |
|
396 |
raise errors.ReadingCompleted(self) |
|
397 |
self._state = "done" |
|
398 |
self._finished_reading() |
|
399 |
||
400 |
def _finished_reading(self): |
|
401 |
"""Helper for finished_reading. |
|
402 |
||
403 |
finished_reading checks the state of the request to determine if
|
|
404 |
finished_reading is allowed, and if it is hands off to _finished_reading
|
|
405 |
to perform the action.
|
|
406 |
"""
|
|
407 |
raise NotImplementedError(self._finished_reading) |
|
408 |
||
409 |
def finished_writing(self): |
|
410 |
"""Finish the writing phase of this request. |
|
411 |
||
412 |
This will flush all pending data for this request along the medium.
|
|
413 |
After calling finished_writing, you may not call accept_bytes anymore.
|
|
414 |
"""
|
|
415 |
if self._state != "writing": |
|
416 |
raise errors.WritingCompleted(self) |
|
417 |
self._state = "reading" |
|
418 |
self._finished_writing() |
|
419 |
||
420 |
def _finished_writing(self): |
|
421 |
"""Helper for finished_writing. |
|
422 |
||
423 |
finished_writing checks the state of the request to determine if
|
|
424 |
finished_writing is allowed, and if it is hands off to _finished_writing
|
|
425 |
to perform the action.
|
|
426 |
"""
|
|
427 |
raise NotImplementedError(self._finished_writing) |
|
428 |
||
429 |
def read_bytes(self, count): |
|
430 |
"""Read bytes from this requests response. |
|
431 |
||
432 |
This method will block and wait for count bytes to be read. It may not
|
|
433 |
be invoked until finished_writing() has been called - this is to ensure
|
|
|
2432.2.7
by Andrew Bennetts
Use less confusing version strings, and define REQUEST_VERSION_TWO/RESPONSE_VERSION_TWO constants for them. |
434 |
a message-based approach to requests, for compatibility with message
|
|
2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
435 |
based mediums like HTTP.
|
436 |
"""
|
|
437 |
if self._state == "writing": |
|
438 |
raise errors.WritingNotComplete(self) |
|
439 |
if self._state != "reading": |
|
440 |
raise errors.ReadingCompleted(self) |
|
441 |
return self._read_bytes(count) |
|
442 |
||
443 |
def _read_bytes(self, count): |
|
|
3565.1.2
by Andrew Bennetts
Delete some more code, fix some bugs, add more comments. |
444 |
"""Helper for SmartClientMediumRequest.read_bytes. |
445 |
||
446 |
read_bytes checks the state of the request to determing if bytes
|
|
447 |
should be read. After that it hands off to _read_bytes to do the
|
|
448 |
actual read.
|
|
|
3565.1.1
by Andrew Bennetts
Read no more then 64k at a time in the smart protocol code. |
449 |
|
|
3565.1.2
by Andrew Bennetts
Delete some more code, fix some bugs, add more comments. |
450 |
By default this forwards to self._medium.read_bytes because we are
|
451 |
operating on the medium's stream.
|
|
|
2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
452 |
"""
|
|
3565.1.2
by Andrew Bennetts
Delete some more code, fix some bugs, add more comments. |
453 |
return self._medium.read_bytes(count) |
|
2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
454 |
|
|
2432.2.7
by Andrew Bennetts
Use less confusing version strings, and define REQUEST_VERSION_TWO/RESPONSE_VERSION_TWO constants for them. |
455 |
def read_line(self): |
|
3606.4.1
by Andrew Bennetts
Fix NotImplementedError when probing for smart protocol via HTTP. |
456 |
line = self._read_line() |
|
3565.1.1
by Andrew Bennetts
Read no more then 64k at a time in the smart protocol code. |
457 |
if not line.endswith('\n'): |
458 |
# end of file encountered reading from server
|
|
459 |
raise errors.ConnectionReset( |
|
460 |
"please check connectivity and permissions", |
|
461 |
"(and try -Dhpss if further diagnosis is required)") |
|
|
2432.2.7
by Andrew Bennetts
Use less confusing version strings, and define REQUEST_VERSION_TWO/RESPONSE_VERSION_TWO constants for them. |
462 |
return line |
463 |
||
|
3606.4.1
by Andrew Bennetts
Fix NotImplementedError when probing for smart protocol via HTTP. |
464 |
def _read_line(self): |
465 |
"""Helper for SmartClientMediumRequest.read_line. |
|
466 |
|
|
467 |
By default this forwards to self._medium._get_line because we are
|
|
468 |
operating on the medium's stream.
|
|
469 |
"""
|
|
470 |
return self._medium._get_line() |
|
471 |
||
|
2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
472 |
|
|
3731.2.1
by Andrew Bennetts
Show total HPSS calls (if any) on stderr when -Dhpss is active. |
473 |
_atexit_registered = False |
474 |
||
475 |
||
476 |
class _HPSSCallCounter(object): |
|
477 |
"""An object that counts the HPSS calls made to a particular medium. |
|
478 |
||
479 |
When the medium is garbage-collected, or failing that when atexit functions
|
|
480 |
are run, the total number of calls made on that medium are reported to
|
|
481 |
stderr.
|
|
482 |
||
483 |
It only keeps a weakref to the medium, so shouldn't affect the medium's
|
|
484 |
lifetime.
|
|
485 |
"""
|
|
486 |
||
487 |
def __init__(self, medium): |
|
488 |
self.count = 0 |
|
489 |
self._done = False |
|
490 |
self.medium_ref = weakref.ref(medium, self.done) |
|
491 |
self.medium_repr = repr(medium) |
|
492 |
# Register an atexit handler if one hasn't already been registered.
|
|
493 |
# Usually the weakref callback is enough, but quite often if the
|
|
494 |
# program aborts with an exception the medium doesn't get
|
|
495 |
# garbage-collected, presumably because one of the traceback frames
|
|
496 |
# still references it.
|
|
497 |
global _atexit_registered, _atexit_counters |
|
498 |
if not _atexit_registered: |
|
499 |
import atexit |
|
500 |
_atexit_counters = [] |
|
501 |
def finish_counters(): |
|
|
3731.2.2
by Andrew Bennetts
Iterate over a copy of _atexit_counters, because it is mutated during iteration. |
502 |
for counter in list(_atexit_counters): |
|
3731.2.1
by Andrew Bennetts
Show total HPSS calls (if any) on stderr when -Dhpss is active. |
503 |
counter.done(None) |
504 |
atexit.register(finish_counters) |
|
505 |
_atexit_registered = True |
|
506 |
_atexit_counters.append(self) |
|
507 |
||
508 |
def increment(self, params): |
|
509 |
"""Install this method as a _SmartClient.hooks['call'] hook.""" |
|
510 |
if params.medium is self.medium_ref(): |
|
511 |
self.count += 1 |
|
512 |
||
513 |
def done(self, ref): |
|
514 |
"""This method is called when the medium is done.""" |
|
515 |
# TODO: remove hook
|
|
516 |
if self._done: |
|
517 |
return
|
|
518 |
self._done = True |
|
519 |
if self.count != 0: |
|
520 |
print >> sys.stderr, 'HPSS calls: %d (%s)' % ( |
|
521 |
self.count, self.medium_repr) |
|
522 |
_atexit_counters.remove(self) |
|
523 |
||
524 |
||
|
3565.1.1
by Andrew Bennetts
Read no more then 64k at a time in the smart protocol code. |
525 |
class SmartClientMedium(SmartMedium): |
|
2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
526 |
"""Smart client is a medium for sending smart protocol requests over.""" |
527 |
||
|
3431.3.3
by Andrew Bennetts
Set 'base' in SmartClientMedium base class. |
528 |
def __init__(self, base): |
|
3241.1.1
by Andrew Bennetts
Shift protocol version querying from RemoteBzrDirFormat into SmartClientMedium. |
529 |
super(SmartClientMedium, self).__init__() |
|
3431.3.3
by Andrew Bennetts
Set 'base' in SmartClientMedium base class. |
530 |
self.base = base |
|
3241.1.4
by Andrew Bennetts
Use get_smart_medium as suggested by Robert, and deal with the fallout. |
531 |
self._protocol_version_error = None |
|
3241.1.1
by Andrew Bennetts
Shift protocol version querying from RemoteBzrDirFormat into SmartClientMedium. |
532 |
self._protocol_version = None |
|
3245.4.47
by Andrew Bennetts
Don't automatically send 'hello' requests from RemoteBzrDirFormat.probe_transport unless we have to (i.e. the transport is HTTP). |
533 |
self._done_hello = False |
|
3435.1.1
by Andrew Bennetts
Define _remote_is_at_least_1_2 on SmartClientMedium base class, rather than just SmartClientStreamMedium. |
534 |
# Be optimistic: we assume the remote end can accept new remote
|
|
3453.4.1
by Andrew Bennetts
Better infrastructure on SmartClientMedium for tracking the remote version. |
535 |
# requests until we get an error saying otherwise.
|
|
3453.4.10
by Andrew Bennetts
Change _is_remote_at_least to _is_remote_before. |
536 |
# _remote_version_is_before tracks the bzr version the remote side
|
|
3453.4.1
by Andrew Bennetts
Better infrastructure on SmartClientMedium for tracking the remote version. |
537 |
# can be based on what we've seen so far.
|
|
3453.4.10
by Andrew Bennetts
Change _is_remote_at_least to _is_remote_before. |
538 |
self._remote_version_is_before = None |
|
3731.2.1
by Andrew Bennetts
Show total HPSS calls (if any) on stderr when -Dhpss is active. |
539 |
if 'hpss' in debug.debug_flags: |
540 |
counter = _HPSSCallCounter(self) |
|
541 |
client._SmartClient.hooks.install_named_hook('call', |
|
542 |
counter.increment, 'hpss debug') |
|
|
3453.4.1
by Andrew Bennetts
Better infrastructure on SmartClientMedium for tracking the remote version. |
543 |
|
|
3453.4.10
by Andrew Bennetts
Change _is_remote_at_least to _is_remote_before. |
544 |
def _is_remote_before(self, version_tuple): |
|
3502.1.1
by Matt Nordhoff
Fix a docstring typo, and a two-expression ``raise`` statement |
545 |
"""Is it possible the remote side supports RPCs for a given version? |
|
3453.4.1
by Andrew Bennetts
Better infrastructure on SmartClientMedium for tracking the remote version. |
546 |
|
547 |
Typical use::
|
|
548 |
||
549 |
needed_version = (1, 2)
|
|
|
3453.4.10
by Andrew Bennetts
Change _is_remote_at_least to _is_remote_before. |
550 |
if medium._is_remote_before(needed_version):
|
|
3453.4.1
by Andrew Bennetts
Better infrastructure on SmartClientMedium for tracking the remote version. |
551 |
fallback_to_pre_1_2_rpc()
|
552 |
else:
|
|
553 |
try:
|
|
554 |
do_1_2_rpc()
|
|
555 |
except UnknownSmartMethod:
|
|
|
3453.4.9
by Andrew Bennetts
Rename _remote_is_not to _remember_remote_is_before. |
556 |
medium._remember_remote_is_before(needed_version)
|
|
3453.4.1
by Andrew Bennetts
Better infrastructure on SmartClientMedium for tracking the remote version. |
557 |
fallback_to_pre_1_2_rpc()
|
558 |
||
|
3453.4.9
by Andrew Bennetts
Rename _remote_is_not to _remember_remote_is_before. |
559 |
:seealso: _remember_remote_is_before
|
|
3453.4.1
by Andrew Bennetts
Better infrastructure on SmartClientMedium for tracking the remote version. |
560 |
"""
|
|
3453.4.10
by Andrew Bennetts
Change _is_remote_at_least to _is_remote_before. |
561 |
if self._remote_version_is_before is None: |
|
3453.4.1
by Andrew Bennetts
Better infrastructure on SmartClientMedium for tracking the remote version. |
562 |
# So far, the remote side seems to support everything
|
|
3453.4.10
by Andrew Bennetts
Change _is_remote_at_least to _is_remote_before. |
563 |
return False |
564 |
return version_tuple >= self._remote_version_is_before |
|
|
3453.4.1
by Andrew Bennetts
Better infrastructure on SmartClientMedium for tracking the remote version. |
565 |
|
|
3453.4.9
by Andrew Bennetts
Rename _remote_is_not to _remember_remote_is_before. |
566 |
def _remember_remote_is_before(self, version_tuple): |
|
3453.4.1
by Andrew Bennetts
Better infrastructure on SmartClientMedium for tracking the remote version. |
567 |
"""Tell this medium that the remote side is older the given version. |
568 |
||
|
3453.4.10
by Andrew Bennetts
Change _is_remote_at_least to _is_remote_before. |
569 |
:seealso: _is_remote_before
|
|
3453.4.1
by Andrew Bennetts
Better infrastructure on SmartClientMedium for tracking the remote version. |
570 |
"""
|
|
3453.4.10
by Andrew Bennetts
Change _is_remote_at_least to _is_remote_before. |
571 |
if (self._remote_version_is_before is not None and |
572 |
version_tuple > self._remote_version_is_before): |
|
|
3502.1.1
by Matt Nordhoff
Fix a docstring typo, and a two-expression ``raise`` statement |
573 |
raise AssertionError( |
|
3453.4.9
by Andrew Bennetts
Rename _remote_is_not to _remember_remote_is_before. |
574 |
"_remember_remote_is_before(%r) called, but " |
575 |
"_remember_remote_is_before(%r) was called previously." |
|
|
3453.4.10
by Andrew Bennetts
Change _is_remote_at_least to _is_remote_before. |
576 |
% (version_tuple, self._remote_version_is_before)) |
577 |
self._remote_version_is_before = version_tuple |
|
|
3241.1.1
by Andrew Bennetts
Shift protocol version querying from RemoteBzrDirFormat into SmartClientMedium. |
578 |
|
579 |
def protocol_version(self): |
|
|
3245.4.47
by Andrew Bennetts
Don't automatically send 'hello' requests from RemoteBzrDirFormat.probe_transport unless we have to (i.e. the transport is HTTP). |
580 |
"""Find out if 'hello' smart request works.""" |
|
3241.1.4
by Andrew Bennetts
Use get_smart_medium as suggested by Robert, and deal with the fallout. |
581 |
if self._protocol_version_error is not None: |
582 |
raise self._protocol_version_error |
|
|
3245.4.47
by Andrew Bennetts
Don't automatically send 'hello' requests from RemoteBzrDirFormat.probe_transport unless we have to (i.e. the transport is HTTP). |
583 |
if not self._done_hello: |
|
3241.1.4
by Andrew Bennetts
Use get_smart_medium as suggested by Robert, and deal with the fallout. |
584 |
try: |
585 |
medium_request = self.get_request() |
|
586 |
# Send a 'hello' request in protocol version one, for maximum
|
|
587 |
# backwards compatibility.
|
|
|
3530.1.2
by John Arbash Meinel
missed one of the imports |
588 |
client_protocol = protocol.SmartClientRequestProtocolOne(medium_request) |
|
3245.4.47
by Andrew Bennetts
Don't automatically send 'hello' requests from RemoteBzrDirFormat.probe_transport unless we have to (i.e. the transport is HTTP). |
589 |
client_protocol.query_version() |
590 |
self._done_hello = True |
|
|
3241.1.4
by Andrew Bennetts
Use get_smart_medium as suggested by Robert, and deal with the fallout. |
591 |
except errors.SmartProtocolError, e: |
592 |
# Cache the error, just like we would cache a successful
|
|
593 |
# result.
|
|
594 |
self._protocol_version_error = e |
|
595 |
raise
|
|
|
3245.4.47
by Andrew Bennetts
Don't automatically send 'hello' requests from RemoteBzrDirFormat.probe_transport unless we have to (i.e. the transport is HTTP). |
596 |
return '2' |
597 |
||
598 |
def should_probe(self): |
|
599 |
"""Should RemoteBzrDirFormat.probe_transport send a smart request on |
|
600 |
this medium?
|
|
601 |
||
602 |
Some transports are unambiguously smart-only; there's no need to check
|
|
603 |
if the transport is able to carry smart requests, because that's all
|
|
604 |
it is for. In those cases, this method should return False.
|
|
605 |
||
606 |
But some HTTP transports can sometimes fail to carry smart requests,
|
|
607 |
but still be usuable for accessing remote bzrdirs via plain file
|
|
608 |
accesses. So for those transports, their media should return True here
|
|
609 |
so that RemoteBzrDirFormat can determine if it is appropriate for that
|
|
610 |
transport.
|
|
611 |
"""
|
|
612 |
return False |
|
|
3241.1.1
by Andrew Bennetts
Shift protocol version querying from RemoteBzrDirFormat into SmartClientMedium. |
613 |
|
|
2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
614 |
def disconnect(self): |
615 |
"""If this medium maintains a persistent connection, close it. |
|
616 |
|
|
617 |
The default implementation does nothing.
|
|
618 |
"""
|
|
619 |
||
|
3431.3.11
by Andrew Bennetts
Push remote_path_from_transport logic into SmartClientMedium, removing special-casing of bzr+http from _SmartClient. |
620 |
def remote_path_from_transport(self, transport): |
621 |
"""Convert transport into a path suitable for using in a request. |
|
622 |
|
|
623 |
Note that the resulting remote path doesn't encode the host name or
|
|
624 |
anything but path, so it is only safe to use it in requests sent over
|
|
625 |
the medium from the matching transport.
|
|
626 |
"""
|
|
627 |
medium_base = urlutils.join(self.base, '/') |
|
628 |
rel_url = urlutils.relative_url(medium_base, transport.base) |
|
629 |
return urllib.unquote(rel_url) |
|
630 |
||
|
2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
631 |
|
632 |
class SmartClientStreamMedium(SmartClientMedium): |
|
633 |
"""Stream based medium common class. |
|
634 |
||
635 |
SmartClientStreamMediums operate on a stream. All subclasses use a common
|
|
636 |
SmartClientStreamMediumRequest for their requests, and should implement
|
|
637 |
_accept_bytes and _read_bytes to allow the request objects to send and
|
|
638 |
receive bytes.
|
|
639 |
"""
|
|
640 |
||
|
3431.3.3
by Andrew Bennetts
Set 'base' in SmartClientMedium base class. |
641 |
def __init__(self, base): |
642 |
SmartClientMedium.__init__(self, base) |
|
|
2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
643 |
self._current_request = None |
644 |
||
645 |
def accept_bytes(self, bytes): |
|
646 |
self._accept_bytes(bytes) |
|
647 |
||
648 |
def __del__(self): |
|
649 |
"""The SmartClientStreamMedium knows how to close the stream when it is |
|
650 |
finished with it.
|
|
651 |
"""
|
|
652 |
self.disconnect() |
|
653 |
||
654 |
def _flush(self): |
|
655 |
"""Flush the output stream. |
|
656 |
|
|
657 |
This method is used by the SmartClientStreamMediumRequest to ensure that
|
|
658 |
all data for a request is sent, to avoid long timeouts or deadlocks.
|
|
659 |
"""
|
|
660 |
raise NotImplementedError(self._flush) |
|
661 |
||
662 |
def get_request(self): |
|
663 |
"""See SmartClientMedium.get_request(). |
|
664 |
||
665 |
SmartClientStreamMedium always returns a SmartClientStreamMediumRequest
|
|
666 |
for get_request.
|
|
667 |
"""
|
|
668 |
return SmartClientStreamMediumRequest(self) |
|
669 |
||
670 |
||
671 |
class SmartSimplePipesClientMedium(SmartClientStreamMedium): |
|
672 |
"""A client medium using simple pipes. |
|
673 |
|
|
674 |
This client does not manage the pipes: it assumes they will always be open.
|
|
675 |
"""
|
|
676 |
||
|
3431.3.1
by Andrew Bennetts
First rough cut of a fix for bug #230550, by adding .base to SmartClientMedia rather than relying on other objects to track this accurately while reusing client media. |
677 |
def __init__(self, readable_pipe, writeable_pipe, base): |
|
3431.3.3
by Andrew Bennetts
Set 'base' in SmartClientMedium base class. |
678 |
SmartClientStreamMedium.__init__(self, base) |
|
2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
679 |
self._readable_pipe = readable_pipe |
680 |
self._writeable_pipe = writeable_pipe |
|
681 |
||
682 |
def _accept_bytes(self, bytes): |
|
683 |
"""See SmartClientStreamMedium.accept_bytes.""" |
|
684 |
self._writeable_pipe.write(bytes) |
|
685 |
||
686 |
def _flush(self): |
|
687 |
"""See SmartClientStreamMedium._flush().""" |
|
688 |
self._writeable_pipe.flush() |
|
689 |
||
690 |
def _read_bytes(self, count): |
|
691 |
"""See SmartClientStreamMedium._read_bytes.""" |
|
692 |
return self._readable_pipe.read(count) |
|
693 |
||
694 |
||
695 |
class SmartSSHClientMedium(SmartClientStreamMedium): |
|
696 |
"""A client medium using SSH.""" |
|
697 |
||
698 |
def __init__(self, host, port=None, username=None, password=None, |
|
|
3431.3.1
by Andrew Bennetts
First rough cut of a fix for bug #230550, by adding .base to SmartClientMedia rather than relying on other objects to track this accurately while reusing client media. |
699 |
base=None, vendor=None, bzr_remote_path=None): |
|
2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
700 |
"""Creates a client that will connect on the first use. |
701 |
|
|
702 |
:param vendor: An optional override for the ssh vendor to use. See
|
|
703 |
bzrlib.transport.ssh for details on ssh vendors.
|
|
704 |
"""
|
|
|
3431.3.3
by Andrew Bennetts
Set 'base' in SmartClientMedium base class. |
705 |
SmartClientStreamMedium.__init__(self, base) |
|
2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
706 |
self._connected = False |
707 |
self._host = host |
|
708 |
self._password = password |
|
709 |
self._port = port |
|
710 |
self._username = username |
|
711 |
self._read_from = None |
|
712 |
self._ssh_connection = None |
|
713 |
self._vendor = vendor |
|
714 |
self._write_to = None |
|
|
1551.18.17
by Aaron Bentley
Introduce bzr_remote_path configuration variable |
715 |
self._bzr_remote_path = bzr_remote_path |
716 |
if self._bzr_remote_path is None: |
|
717 |
symbol_versioning.warn( |
|
718 |
'bzr_remote_path is required as of bzr 0.92', |
|
719 |
DeprecationWarning, stacklevel=2) |
|
720 |
self._bzr_remote_path = os.environ.get('BZR_REMOTE_PATH', 'bzr') |
|
|
2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
721 |
|
722 |
def _accept_bytes(self, bytes): |
|
723 |
"""See SmartClientStreamMedium.accept_bytes.""" |
|
724 |
self._ensure_connection() |
|
725 |
self._write_to.write(bytes) |
|
726 |
||
727 |
def disconnect(self): |
|
728 |
"""See SmartClientMedium.disconnect().""" |
|
729 |
if not self._connected: |
|
730 |
return
|
|
731 |
self._read_from.close() |
|
732 |
self._write_to.close() |
|
733 |
self._ssh_connection.close() |
|
734 |
self._connected = False |
|
|
3731.2.1
by Andrew Bennetts
Show total HPSS calls (if any) on stderr when -Dhpss is active. |
735 |
super(SmartSSHClientMedium, self).disconnect() |
|
2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
736 |
|
737 |
def _ensure_connection(self): |
|
738 |
"""Connect this medium if not already connected.""" |
|
739 |
if self._connected: |
|
740 |
return
|
|
741 |
if self._vendor is None: |
|
742 |
vendor = ssh._get_ssh_vendor() |
|
743 |
else: |
|
744 |
vendor = self._vendor |
|
745 |
self._ssh_connection = vendor.connect_ssh(self._username, |
|
746 |
self._password, self._host, self._port, |
|
|
1551.18.17
by Aaron Bentley
Introduce bzr_remote_path configuration variable |
747 |
command=[self._bzr_remote_path, 'serve', '--inet', |
748 |
'--directory=/', '--allow-writes']) |
|
|
2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
749 |
self._read_from, self._write_to = \ |
750 |
self._ssh_connection.get_filelike_channels() |
|
751 |
self._connected = True |
|
752 |
||
753 |
def _flush(self): |
|
754 |
"""See SmartClientStreamMedium._flush().""" |
|
755 |
self._write_to.flush() |
|
756 |
||
757 |
def _read_bytes(self, count): |
|
758 |
"""See SmartClientStreamMedium.read_bytes.""" |
|
759 |
if not self._connected: |
|
760 |
raise errors.MediumNotConnected(self) |
|
|
3565.1.3
by Andrew Bennetts
Define a _MAX_READ_SIZE constant as suggested by John's review. |
761 |
bytes_to_read = min(count, _MAX_READ_SIZE) |
|
3565.1.1
by Andrew Bennetts
Read no more then 64k at a time in the smart protocol code. |
762 |
return self._read_from.read(bytes_to_read) |
|
2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
763 |
|
764 |
||
|
3004.2.1
by Vincent Ladeuil
Fix 150860 by leaving port as user specified it. |
765 |
# Port 4155 is the default port for bzr://, registered with IANA.
|
|
3665.4.1
by Jelmer Vernooij
Support IPv6 in the smart server. |
766 |
BZR_DEFAULT_INTERFACE = None |
|
3004.2.1
by Vincent Ladeuil
Fix 150860 by leaving port as user specified it. |
767 |
BZR_DEFAULT_PORT = 4155 |
768 |
||
769 |
||
|
2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
770 |
class SmartTCPClientMedium(SmartClientStreamMedium): |
771 |
"""A client medium using TCP.""" |
|
772 |
||
|
3431.3.1
by Andrew Bennetts
First rough cut of a fix for bug #230550, by adding .base to SmartClientMedia rather than relying on other objects to track this accurately while reusing client media. |
773 |
def __init__(self, host, port, base): |
|
2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
774 |
"""Creates a client that will connect on the first use.""" |
|
3431.3.3
by Andrew Bennetts
Set 'base' in SmartClientMedium base class. |
775 |
SmartClientStreamMedium.__init__(self, base) |
|
2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
776 |
self._connected = False |
777 |
self._host = host |
|
778 |
self._port = port |
|
779 |
self._socket = None |
|
780 |
||
781 |
def _accept_bytes(self, bytes): |
|
782 |
"""See SmartClientMedium.accept_bytes.""" |
|
783 |
self._ensure_connection() |
|
|
3118.2.1
by Andrew Bennetts
(andrew) Fix #115781 by passing no more than 64k at a time to socket.sendall. |
784 |
osutils.send_all(self._socket, bytes) |
|
2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
785 |
|
786 |
def disconnect(self): |
|
787 |
"""See SmartClientMedium.disconnect().""" |
|
788 |
if not self._connected: |
|
789 |
return
|
|
790 |
self._socket.close() |
|
791 |
self._socket = None |
|
792 |
self._connected = False |
|
|
3731.2.1
by Andrew Bennetts
Show total HPSS calls (if any) on stderr when -Dhpss is active. |
793 |
super(SmartTCPClientMedium, self).disconnect() |
|
2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
794 |
|
795 |
def _ensure_connection(self): |
|
796 |
"""Connect this medium if not already connected.""" |
|
797 |
if self._connected: |
|
798 |
return
|
|
|
3004.2.1
by Vincent Ladeuil
Fix 150860 by leaving port as user specified it. |
799 |
if self._port is None: |
800 |
port = BZR_DEFAULT_PORT |
|
801 |
else: |
|
802 |
port = int(self._port) |
|
|
3711.2.2
by Jelmer Vernooij
Avoid using AI_ADDRCONFIG since it's not portable. |
803 |
try: |
804 |
sockaddrs = socket.getaddrinfo(self._host, port, socket.AF_UNSPEC, |
|
805 |
socket.SOCK_STREAM, 0, 0) |
|
806 |
except socket.gaierror, (err_num, err_msg): |
|
807 |
raise errors.ConnectionError("failed to lookup %s:%d: %s" % |
|
808 |
(self._host, port, err_msg)) |
|
|
3711.2.3
by Jelmer Vernooij
Add comment. |
809 |
# Initialize err in case there are no addresses returned:
|
|
3665.4.2
by Jelmer Vernooij
Fall through to next available address if previous fails. |
810 |
err = socket.error("no address found for %s" % self._host) |
|
3665.4.1
by Jelmer Vernooij
Support IPv6 in the smart server. |
811 |
for (family, socktype, proto, canonname, sockaddr) in sockaddrs: |
812 |
try: |
|
|
3665.4.2
by Jelmer Vernooij
Fall through to next available address if previous fails. |
813 |
self._socket = socket.socket(family, socktype, proto) |
814 |
self._socket.setsockopt(socket.IPPROTO_TCP, |
|
815 |
socket.TCP_NODELAY, 1) |
|
|
3665.4.1
by Jelmer Vernooij
Support IPv6 in the smart server. |
816 |
self._socket.connect(sockaddr) |
817 |
except socket.error, err: |
|
|
3665.4.2
by Jelmer Vernooij
Fall through to next available address if previous fails. |
818 |
if self._socket is not None: |
819 |
self._socket.close() |
|
820 |
self._socket = None |
|
821 |
continue
|
|
822 |
break
|
|
823 |
if self._socket is None: |
|
824 |
# socket errors either have a (string) or (errno, string) as their
|
|
825 |
# args.
|
|
826 |
if type(err.args) is str: |
|
827 |
err_msg = err.args |
|
828 |
else: |
|
829 |
err_msg = err.args[1] |
|
830 |
raise errors.ConnectionError("failed to connect to %s:%d: %s" % |
|
831 |
(self._host, port, err_msg)) |
|
|
2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
832 |
self._connected = True |
833 |
||
834 |
def _flush(self): |
|
835 |
"""See SmartClientStreamMedium._flush(). |
|
836 |
|
|
837 |
For TCP we do no flushing. We may want to turn off TCP_NODELAY and
|
|
838 |
add a means to do a flush, but that can be done in the future.
|
|
839 |
"""
|
|
840 |
||
841 |
def _read_bytes(self, count): |
|
842 |
"""See SmartClientMedium.read_bytes.""" |
|
843 |
if not self._connected: |
|
844 |
raise errors.MediumNotConnected(self) |
|
|
3565.1.3
by Andrew Bennetts
Define a _MAX_READ_SIZE constant as suggested by John's review. |
845 |
# We ignore the desired_count because on sockets it's more efficient to
|
846 |
# read large chunks (of _MAX_READ_SIZE bytes) at a time.
|
|
847 |
return self._socket.recv(_MAX_READ_SIZE) |
|
|
2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
848 |
|
849 |
||
850 |
class SmartClientStreamMediumRequest(SmartClientMediumRequest): |
|
851 |
"""A SmartClientMediumRequest that works with an SmartClientStreamMedium.""" |
|
852 |
||
853 |
def __init__(self, medium): |
|
854 |
SmartClientMediumRequest.__init__(self, medium) |
|
855 |
# check that we are safe concurrency wise. If some streams start
|
|
856 |
# allowing concurrent requests - i.e. via multiplexing - then this
|
|
857 |
# assert should be moved to SmartClientStreamMedium.get_request,
|
|
858 |
# and the setting/unsetting of _current_request likewise moved into
|
|
859 |
# that class : but its unneeded overhead for now. RBC 20060922
|
|
860 |
if self._medium._current_request is not None: |
|
861 |
raise errors.TooManyConcurrentRequests(self._medium) |
|
862 |
self._medium._current_request = self |
|
863 |
||
864 |
def _accept_bytes(self, bytes): |
|
865 |
"""See SmartClientMediumRequest._accept_bytes. |
|
866 |
|
|
867 |
This forwards to self._medium._accept_bytes because we are operating
|
|
868 |
on the mediums stream.
|
|
869 |
"""
|
|
870 |
self._medium._accept_bytes(bytes) |
|
871 |
||
872 |
def _finished_reading(self): |
|
873 |
"""See SmartClientMediumRequest._finished_reading. |
|
874 |
||
875 |
This clears the _current_request on self._medium to allow a new
|
|
876 |
request to be created.
|
|
877 |
"""
|
|
|
3376.2.4
by Martin Pool
Remove every assert statement from bzrlib! |
878 |
if self._medium._current_request is not self: |
879 |
raise AssertionError() |
|
|
2018.5.2
by Andrew Bennetts
Start splitting bzrlib/transport/smart.py into a package. |
880 |
self._medium._current_request = None |
881 |
||
882 |
def _finished_writing(self): |
|
883 |
"""See SmartClientMediumRequest._finished_writing. |
|
884 |
||
885 |
This invokes self._medium._flush to ensure all bytes are transmitted.
|
|
886 |
"""
|
|
887 |
self._medium._flush() |
|
888 |