/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
3374.1.1 by Martin Pool
Set SO_REUSEADDR on server sockets (#164288)
1
# Copyright (C) 2006, 2007, 2008 Canonical Ltd
2018.5.14 by Andrew Bennetts
Move SmartTCPServer to smart/server.py, and SmartServerRequestHandler to smart/request.py.
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
4183.7.1 by Sabin Iacob
update FSF mailing address
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
2018.5.14 by Andrew Bennetts
Move SmartTCPServer to smart/server.py, and SmartServerRequestHandler to smart/request.py.
16
2018.5.19 by Andrew Bennetts
Add docstrings to all the new modules, and a few other places.
17
"""Server for smart-server protocol."""
18
2400.1.7 by Andrew Bennetts
Merge from bzr.dev.
19
import errno
2018.5.14 by Andrew Bennetts
Move SmartTCPServer to smart/server.py, and SmartServerRequestHandler to smart/request.py.
20
import socket
3374.1.1 by Martin Pool
Set SO_REUSEADDR on server sockets (#164288)
21
import sys
2018.5.14 by Andrew Bennetts
Move SmartTCPServer to smart/server.py, and SmartServerRequestHandler to smart/request.py.
22
import threading
23
4119.3.2 by Robert Collins
Migrate existing hooks over to the new HookPoint infrastructure.
24
from bzrlib.hooks import HookPoint, Hooks
2018.5.14 by Andrew Bennetts
Move SmartTCPServer to smart/server.py, and SmartServerRequestHandler to smart/request.py.
25
from bzrlib import (
2634.1.1 by Robert Collins
(robertc) Reinstate the accidentally backed out external_url patch.
26
    errors,
2018.5.15 by Andrew Bennetts
Tidy some imports, and bugs introduced when adding server.py
27
    trace,
2018.5.14 by Andrew Bennetts
Move SmartTCPServer to smart/server.py, and SmartServerRequestHandler to smart/request.py.
28
    transport,
29
)
3224.5.5 by Andrew Bennetts
Don't import bzrlib.smart.medium from bzrlib.smart.server until it's needed. This helps the bzr-dbus plugin import faster.
30
from bzrlib.lazy_import import lazy_import
31
lazy_import(globals(), """
32
from bzrlib.smart import medium
33
""")
2018.5.14 by Andrew Bennetts
Move SmartTCPServer to smart/server.py, and SmartServerRequestHandler to smart/request.py.
34
35
36
class SmartTCPServer(object):
37
    """Listens on a TCP socket and accepts connections from smart clients.
2018.5.139 by Andrew Bennetts
Merge from bzr.dev, resolving conflicts.
38
2018.5.14 by Andrew Bennetts
Move SmartTCPServer to smart/server.py, and SmartServerRequestHandler to smart/request.py.
39
    Each connection will be served by a SmartServerSocketStreamMedium running in
2018.5.139 by Andrew Bennetts
Merge from bzr.dev, resolving conflicts.
40
    a thread.
2400.1.7 by Andrew Bennetts
Merge from bzr.dev.
41
42
    hooks: An instance of SmartServerHooks.
2018.5.14 by Andrew Bennetts
Move SmartTCPServer to smart/server.py, and SmartServerRequestHandler to smart/request.py.
43
    """
44
2692.1.14 by Andrew Bennetts
All WSGI tests passing, and manual testing works too.
45
    def __init__(self, backing_transport, host='127.0.0.1', port=0,
46
                 root_client_path='/'):
2018.5.14 by Andrew Bennetts
Move SmartTCPServer to smart/server.py, and SmartServerRequestHandler to smart/request.py.
47
        """Construct a new server.
48
49
        To actually start it running, call either start_background_thread or
50
        serve.
51
2692.1.14 by Andrew Bennetts
All WSGI tests passing, and manual testing works too.
52
        :param backing_transport: The transport to serve.
2018.5.14 by Andrew Bennetts
Move SmartTCPServer to smart/server.py, and SmartServerRequestHandler to smart/request.py.
53
        :param host: Name of the interface to listen on.
54
        :param port: TCP port to listen on, or 0 to allocate a transient port.
2692.1.14 by Andrew Bennetts
All WSGI tests passing, and manual testing works too.
55
        :param root_client_path: The client path that will correspond to root
56
            of backing_transport.
2018.5.14 by Andrew Bennetts
Move SmartTCPServer to smart/server.py, and SmartServerRequestHandler to smart/request.py.
57
        """
2400.1.7 by Andrew Bennetts
Merge from bzr.dev.
58
        # let connections timeout so that we get a chance to terminate
59
        # Keep a reference to the exceptions we want to catch because the socket
60
        # module's globals get set to None during interpreter shutdown.
61
        from socket import timeout as socket_timeout
62
        from socket import error as socket_error
63
        self._socket_error = socket_error
64
        self._socket_timeout = socket_timeout
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
65
        addrs = socket.getaddrinfo(host, port, socket.AF_UNSPEC,
3711.2.2 by Jelmer Vernooij
Avoid using AI_ADDRCONFIG since it's not portable.
66
            socket.SOCK_STREAM, 0, socket.AI_PASSIVE)[0]
3665.4.1 by Jelmer Vernooij
Support IPv6 in the smart server.
67
68
        (family, socktype, proto, canonname, sockaddr) = addrs
69
70
        self._server_socket = socket.socket(family, socktype, proto)
3374.1.1 by Martin Pool
Set SO_REUSEADDR on server sockets (#164288)
71
        # SO_REUSERADDR has a different meaning on Windows
72
        if sys.platform != 'win32':
73
            self._server_socket.setsockopt(socket.SOL_SOCKET,
74
                socket.SO_REUSEADDR, 1)
3365.1.1 by Andrea Corbellini
Handle errors raised by socket.bind() (bug 200575)
75
        try:
3665.4.1 by Jelmer Vernooij
Support IPv6 in the smart server.
76
            self._server_socket.bind(sockaddr)
3365.1.1 by Andrea Corbellini
Handle errors raised by socket.bind() (bug 200575)
77
        except self._socket_error, message:
78
            raise errors.CannotBindAddress(host, port, message)
2400.1.7 by Andrew Bennetts
Merge from bzr.dev.
79
        self._sockname = self._server_socket.getsockname()
80
        self.port = self._sockname[1]
2018.5.14 by Andrew Bennetts
Move SmartTCPServer to smart/server.py, and SmartServerRequestHandler to smart/request.py.
81
        self._server_socket.listen(1)
82
        self._server_socket.settimeout(1)
83
        self.backing_transport = backing_transport
2400.1.7 by Andrew Bennetts
Merge from bzr.dev.
84
        self._started = threading.Event()
85
        self._stopped = threading.Event()
2692.1.14 by Andrew Bennetts
All WSGI tests passing, and manual testing works too.
86
        self.root_client_path = root_client_path
2018.5.14 by Andrew Bennetts
Move SmartTCPServer to smart/server.py, and SmartServerRequestHandler to smart/request.py.
87
3245.4.28 by Andrew Bennetts
Remove another XXX, and include test ID in smart server thread names.
88
    def serve(self, thread_name_suffix=''):
2018.5.14 by Andrew Bennetts
Move SmartTCPServer to smart/server.py, and SmartServerRequestHandler to smart/request.py.
89
        self._should_terminate = False
2634.1.1 by Robert Collins
(robertc) Reinstate the accidentally backed out external_url patch.
90
        # for hooks we are letting code know that a server has started (and
91
        # later stopped).
92
        # There are three interesting urls:
93
        # The URL the server can be contacted on. (e.g. bzr://host/)
94
        # The URL that a commit done on the same machine as the server will
95
        # have within the servers space. (e.g. file:///home/user/source)
96
        # The URL that will be given to other hooks in the same process -
97
        # the URL of the backing transport itself. (e.g. chroot+:///)
98
        # We need all three because:
99
        #  * other machines see the first
100
        #  * local commits on this machine should be able to be mapped to
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
101
        #    this server
2634.1.1 by Robert Collins
(robertc) Reinstate the accidentally backed out external_url patch.
102
        #  * commits the server does itself need to be mapped across to this
103
        #    server.
104
        # The latter two urls are different aliases to the servers url,
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
105
        # so we group those in a list - as there might be more aliases
2634.1.1 by Robert Collins
(robertc) Reinstate the accidentally backed out external_url patch.
106
        # in the future.
107
        backing_urls = [self.backing_transport.base]
108
        try:
109
            backing_urls.append(self.backing_transport.external_url())
110
        except errors.InProcessTransport:
111
            pass
2400.1.7 by Andrew Bennetts
Merge from bzr.dev.
112
        for hook in SmartTCPServer.hooks['server_started']:
2634.1.1 by Robert Collins
(robertc) Reinstate the accidentally backed out external_url patch.
113
            hook(backing_urls, self.get_url())
4544.1.2 by Andrew Bennetts
Add test that would catch the lack of ChrootServer in cmd_serve.
114
        for hook in SmartTCPServer.hooks['server_started_ex']:
4544.1.3 by Andrew Bennetts
Pass backing_urls to the new server_started_ex hook.
115
            hook(backing_urls, self)
2400.1.7 by Andrew Bennetts
Merge from bzr.dev.
116
        self._started.set()
117
        try:
118
            try:
119
                while not self._should_terminate:
120
                    try:
121
                        conn, client_addr = self._server_socket.accept()
122
                    except self._socket_timeout:
123
                        # just check if we're asked to stop
124
                        pass
125
                    except self._socket_error, e:
126
                        # if the socket is closed by stop_background_thread
127
                        # we might get a EBADF here, any other socket errors
128
                        # should get logged.
129
                        if e.args[0] != errno.EBADF:
130
                            trace.warning("listening socket error: %s", e)
131
                    else:
3245.4.28 by Andrew Bennetts
Remove another XXX, and include test ID in smart server thread names.
132
                        self.serve_conn(conn, thread_name_suffix)
2400.1.7 by Andrew Bennetts
Merge from bzr.dev.
133
            except KeyboardInterrupt:
134
                # dont log when CTRL-C'd.
135
                raise
136
            except Exception, e:
4695.5.5 by Martin Pool
Unhandled smart-server exceptions are reported using generic report_exception
137
                trace.report_exception(sys.exc_info(), sys.stderr)
2400.1.7 by Andrew Bennetts
Merge from bzr.dev.
138
                raise
139
        finally:
140
            self._stopped.set()
141
            try:
142
                # ensure the server socket is closed.
143
                self._server_socket.close()
144
            except self._socket_error:
145
                # ignore errors on close
146
                pass
147
            for hook in SmartTCPServer.hooks['server_stopped']:
2634.1.1 by Robert Collins
(robertc) Reinstate the accidentally backed out external_url patch.
148
                hook(backing_urls, self.get_url())
2018.5.14 by Andrew Bennetts
Move SmartTCPServer to smart/server.py, and SmartServerRequestHandler to smart/request.py.
149
150
    def get_url(self):
151
        """Return the url of the server"""
2400.1.7 by Andrew Bennetts
Merge from bzr.dev.
152
        return "bzr://%s:%d/" % self._sockname
2018.5.14 by Andrew Bennetts
Move SmartTCPServer to smart/server.py, and SmartServerRequestHandler to smart/request.py.
153
3245.4.28 by Andrew Bennetts
Remove another XXX, and include test ID in smart server thread names.
154
    def serve_conn(self, conn, thread_name_suffix):
2018.5.14 by Andrew Bennetts
Move SmartTCPServer to smart/server.py, and SmartServerRequestHandler to smart/request.py.
155
        # For WIN32, where the timeout value from the listening socket
4031.3.1 by Frank Aspell
Fixing various typos
156
        # propagates to the newly accepted socket.
2018.5.14 by Andrew Bennetts
Move SmartTCPServer to smart/server.py, and SmartServerRequestHandler to smart/request.py.
157
        conn.setblocking(True)
158
        conn.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
3224.5.5 by Andrew Bennetts
Don't import bzrlib.smart.medium from bzrlib.smart.server until it's needed. This helps the bzr-dbus plugin import faster.
159
        handler = medium.SmartServerSocketStreamMedium(
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.
160
            conn, self.backing_transport, self.root_client_path)
3245.4.28 by Andrew Bennetts
Remove another XXX, and include test ID in smart server thread names.
161
        thread_name = 'smart-server-child' + thread_name_suffix
162
        connection_thread = threading.Thread(
163
            None, handler.serve, name=thread_name)
2018.5.14 by Andrew Bennetts
Move SmartTCPServer to smart/server.py, and SmartServerRequestHandler to smart/request.py.
164
        connection_thread.setDaemon(True)
165
        connection_thread.start()
166
3245.4.28 by Andrew Bennetts
Remove another XXX, and include test ID in smart server thread names.
167
    def start_background_thread(self, thread_name_suffix=''):
2400.1.7 by Andrew Bennetts
Merge from bzr.dev.
168
        self._started.clear()
2018.5.14 by Andrew Bennetts
Move SmartTCPServer to smart/server.py, and SmartServerRequestHandler to smart/request.py.
169
        self._server_thread = threading.Thread(None,
3245.4.28 by Andrew Bennetts
Remove another XXX, and include test ID in smart server thread names.
170
                self.serve, args=(thread_name_suffix,),
2018.5.14 by Andrew Bennetts
Move SmartTCPServer to smart/server.py, and SmartServerRequestHandler to smart/request.py.
171
                name='server-' + self.get_url())
172
        self._server_thread.setDaemon(True)
173
        self._server_thread.start()
2400.1.7 by Andrew Bennetts
Merge from bzr.dev.
174
        self._started.wait()
2018.5.14 by Andrew Bennetts
Move SmartTCPServer to smart/server.py, and SmartServerRequestHandler to smart/request.py.
175
176
    def stop_background_thread(self):
2400.1.7 by Andrew Bennetts
Merge from bzr.dev.
177
        self._stopped.clear()
178
        # tell the main loop to quit on the next iteration.
2018.5.14 by Andrew Bennetts
Move SmartTCPServer to smart/server.py, and SmartServerRequestHandler to smart/request.py.
179
        self._should_terminate = True
2400.1.7 by Andrew Bennetts
Merge from bzr.dev.
180
        # close the socket - gives error to connections from here on in,
181
        # rather than a connection reset error to connections made during
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
182
        # the period between setting _should_terminate = True and
2400.1.7 by Andrew Bennetts
Merge from bzr.dev.
183
        # the current request completing/aborting. It may also break out the
184
        # main loop if it was currently in accept() (on some platforms).
185
        try:
186
            self._server_socket.close()
187
        except self._socket_error:
188
            # ignore errors on close
189
            pass
190
        if not self._stopped.isSet():
191
            # server has not stopped (though it may be stopping)
192
            # its likely in accept(), so give it a connection
193
            temp_socket = socket.socket()
194
            temp_socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
195
            if not temp_socket.connect_ex(self._sockname):
196
                # and close it immediately: we dont choose to send any requests.
197
                temp_socket.close()
198
        self._stopped.wait()
199
        self._server_thread.join()
200
201
202
class SmartServerHooks(Hooks):
203
    """Hooks for the smart server."""
204
205
    def __init__(self):
206
        """Create the default hooks.
207
208
        These are all empty initially, because by default nothing should get
209
        notified.
210
        """
211
        Hooks.__init__(self)
4119.3.2 by Robert Collins
Migrate existing hooks over to the new HookPoint infrastructure.
212
        self.create_hook(HookPoint('server_started',
213
            "Called by the bzr server when it starts serving a directory. "
214
            "server_started is called with (backing urls, public url), "
215
            "where backing_url is a list of URLs giving the "
216
            "server-specific directory locations, and public_url is the "
217
            "public URL for the directory being served.", (0, 16), None))
4544.1.2 by Andrew Bennetts
Add test that would catch the lack of ChrootServer in cmd_serve.
218
        self.create_hook(HookPoint('server_started_ex',
219
            "Called by the bzr server when it starts serving a directory. "
4544.1.3 by Andrew Bennetts
Pass backing_urls to the new server_started_ex hook.
220
            "server_started is called with (backing_urls, server_obj).",
221
            (1, 17), None))
4119.3.2 by Robert Collins
Migrate existing hooks over to the new HookPoint infrastructure.
222
        self.create_hook(HookPoint('server_stopped',
223
            "Called by the bzr server when it stops serving a directory. "
224
            "server_stopped is called with the same parameters as the "
225
            "server_started hook: (backing_urls, public_url).", (0, 16), None))
2400.1.7 by Andrew Bennetts
Merge from bzr.dev.
226
227
SmartTCPServer.hooks = SmartServerHooks()
2018.5.14 by Andrew Bennetts
Move SmartTCPServer to smart/server.py, and SmartServerRequestHandler to smart/request.py.
228
2400.1.2 by Andrew Bennetts
Move SmartTCPServer classes into bzrlib/smart/server.py
229
230
class SmartTCPServer_for_testing(SmartTCPServer):
2018.5.14 by Andrew Bennetts
Move SmartTCPServer to smart/server.py, and SmartServerRequestHandler to smart/request.py.
231
    """Server suitable for use by transport tests.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
232
2400.1.2 by Andrew Bennetts
Move SmartTCPServer classes into bzrlib/smart/server.py
233
    This server is backed by the process's cwd.
2018.5.14 by Andrew Bennetts
Move SmartTCPServer to smart/server.py, and SmartServerRequestHandler to smart/request.py.
234
    """
235
3245.4.28 by Andrew Bennetts
Remove another XXX, and include test ID in smart server thread names.
236
    def __init__(self, thread_name_suffix=''):
2018.5.42 by Robert Collins
Various hopefully improvements, but wsgi is broken, handing over to spiv :).
237
        SmartTCPServer.__init__(self, None)
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.
238
        self.client_path_extra = None
3245.4.28 by Andrew Bennetts
Remove another XXX, and include test ID in smart server thread names.
239
        self.thread_name_suffix = thread_name_suffix
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
240
2400.1.2 by Andrew Bennetts
Move SmartTCPServer classes into bzrlib/smart/server.py
241
    def get_backing_transport(self, backing_transport_server):
242
        """Get a backing transport from a server we are decorating."""
2018.5.104 by Andrew Bennetts
Completely rework chrooted transports.
243
        return transport.get_transport(backing_transport_server.get_url())
2400.1.2 by Andrew Bennetts
Move SmartTCPServer classes into bzrlib/smart/server.py
244
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.
245
    def setUp(self, backing_transport_server=None,
246
              client_path_extra='/extra/'):
247
        """Set up server for testing.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
248
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.
249
        :param backing_transport_server: backing server to use.  If not
250
            specified, a LocalURLServer at the current working directory will
251
            be used.
252
        :param client_path_extra: a path segment starting with '/' to append to
253
            the root URL for this server.  For instance, a value of '/foo/bar/'
254
            will mean the root of the backing transport will be published at a
255
            URL like `bzr://127.0.0.1:nnnn/foo/bar/`, rather than
256
            `bzr://127.0.0.1:nnnn/`.  Default value is `extra`, so that tests
257
            by default will fail unless they do the necessary path translation.
258
        """
3376.2.4 by Martin Pool
Remove every assert statement from bzrlib!
259
        if not client_path_extra.startswith('/'):
260
            raise ValueError(client_path_extra)
2018.5.104 by Andrew Bennetts
Completely rework chrooted transports.
261
        from bzrlib.transport.chroot import ChrootServer
2018.5.47 by Andrew Bennetts
Make SmartTCPServer_for_Testing.setUp's backing_transport_server argument optional.
262
        if backing_transport_server is None:
263
            from bzrlib.transport.local import LocalURLServer
264
            backing_transport_server = LocalURLServer()
2018.5.104 by Andrew Bennetts
Completely rework chrooted transports.
265
        self.chroot_server = ChrootServer(
266
            self.get_backing_transport(backing_transport_server))
267
        self.chroot_server.setUp()
268
        self.backing_transport = transport.get_transport(
269
            self.chroot_server.get_url())
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.
270
        self.root_client_path = self.client_path_extra = client_path_extra
3245.4.28 by Andrew Bennetts
Remove another XXX, and include test ID in smart server thread names.
271
        self.start_background_thread(self.thread_name_suffix)
2018.5.14 by Andrew Bennetts
Move SmartTCPServer to smart/server.py, and SmartServerRequestHandler to smart/request.py.
272
273
    def tearDown(self):
274
        self.stop_background_thread()
2018.5.104 by Andrew Bennetts
Completely rework chrooted transports.
275
        self.chroot_server.tearDown()
2018.5.14 by Andrew Bennetts
Move SmartTCPServer to smart/server.py, and SmartServerRequestHandler to smart/request.py.
276
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.
277
    def get_url(self):
278
        url = super(SmartTCPServer_for_testing, self).get_url()
279
        return url[:-1] + self.client_path_extra
280
2018.5.14 by Andrew Bennetts
Move SmartTCPServer to smart/server.py, and SmartServerRequestHandler to smart/request.py.
281
    def get_bogus_url(self):
282
        """Return a URL which will fail to connect"""
283
        return 'bzr://127.0.0.1:1/'
284
285
2018.5.42 by Robert Collins
Various hopefully improvements, but wsgi is broken, handing over to spiv :).
286
class ReadonlySmartTCPServer_for_testing(SmartTCPServer_for_testing):
287
    """Get a readonly server for testing."""
288
289
    def get_backing_transport(self, backing_transport_server):
290
        """Get a backing transport from a server we are decorating."""
2018.5.104 by Andrew Bennetts
Completely rework chrooted transports.
291
        url = 'readonly+' + backing_transport_server.get_url()
2018.5.42 by Robert Collins
Various hopefully improvements, but wsgi is broken, handing over to spiv :).
292
        return transport.get_transport(url)
3453.5.1 by Andrew Bennetts
Add {bzrdir,repository,branch}_implementations tests for Remote objects using protocol v2 and pre-1.6 RPCs.
293
294
295
class SmartTCPServer_for_testing_v2_only(SmartTCPServer_for_testing):
296
    """A variation of SmartTCPServer_for_testing that limits the client to
297
    using RPCs in protocol v2 (i.e. bzr <= 1.5).
298
    """
299
300
    def get_url(self):
301
        url = super(SmartTCPServer_for_testing_v2_only, self).get_url()
302
        url = 'bzr-v2://' + url[len('bzr://'):]
303
        return url
304
305
306
class ReadonlySmartTCPServer_for_testing_v2_only(SmartTCPServer_for_testing_v2_only):
307
    """Get a readonly server for testing."""
308
309
    def get_backing_transport(self, backing_transport_server):
310
        """Get a backing transport from a server we are decorating."""
311
        url = 'readonly+' + backing_transport_server.get_url()
312
        return transport.get_transport(url)
313
314
4370.4.6 by Jelmer Vernooij
Move server protocol registry to bzrlib.transport.
315
def serve_bzr(transport, host=None, port=None, inet=False):
316
    from bzrlib import lockdir, ui
317
    from bzrlib.transport import get_transport
318
    from bzrlib.transport.chroot import ChrootServer
319
    chroot_server = ChrootServer(transport)
320
    chroot_server.setUp()
4544.1.1 by Andrew Bennetts
Actually use the chroot transport we set up.
321
    transport = get_transport(chroot_server.get_url())
4370.4.6 by Jelmer Vernooij
Move server protocol registry to bzrlib.transport.
322
    if inet:
323
        smart_server = medium.SmartServerPipeStreamMedium(
324
            sys.stdin, sys.stdout, transport)
325
    else:
326
        if host is None:
327
            host = medium.BZR_DEFAULT_INTERFACE
328
        if port is None:
329
            port = medium.BZR_DEFAULT_PORT
4544.1.1 by Andrew Bennetts
Actually use the chroot transport we set up.
330
        smart_server = SmartTCPServer(transport, host=host, port=port)
4370.4.6 by Jelmer Vernooij
Move server protocol registry to bzrlib.transport.
331
        trace.note('listening on port: %s' % smart_server.port)
332
    # For the duration of this server, no UI output is permitted. note
333
    # that this may cause problems with blackbox tests. This should be
334
    # changed with care though, as we dont want to use bandwidth sending
335
    # progress over stderr to smart server clients!
336
    old_factory = ui.ui_factory
337
    old_lockdir_timeout = lockdir._DEFAULT_TIMEOUT_SECONDS
338
    try:
339
        ui.ui_factory = ui.SilentUIFactory()
340
        lockdir._DEFAULT_TIMEOUT_SECONDS = 0
341
        smart_server.serve()
342
    finally:
343
        ui.ui_factory = old_factory
344
        lockdir._DEFAULT_TIMEOUT_SECONDS = old_lockdir_timeout
345