26
from bzrlib.hooks import HookPoint, Hooks
25
from bzrlib.hooks import Hooks
27
26
from bzrlib import (
29
transport as _mod_transport,
31
from bzrlib.i18n import gettext
32
32
from bzrlib.lazy_import import lazy_import
33
33
lazy_import(globals(), """
34
34
from bzrlib.smart import medium
35
35
from bzrlib.transport import (
40
39
from bzrlib import (
105
104
# The URL that a commit done on the same machine as the server will
106
105
# have within the servers space. (e.g. file:///home/user/source)
107
106
# The URL that will be given to other hooks in the same process -
108
# the URL of the backing transport itself. (e.g. chroot+:///)
107
# the URL of the backing transport itself. (e.g. filtered-36195:///)
109
108
# We need all three because:
110
109
# * other machines see the first
111
110
# * local commits on this machine should be able to be mapped to
179
178
def get_url(self):
180
179
"""Return the url of the server"""
181
return "bzr://%s:%d/" % self._sockname
180
return "bzr://%s:%s/" % (self._sockname[0], self._sockname[1])
183
182
def serve_conn(self, conn, thread_name_suffix):
184
183
# For WIN32, where the timeout value from the listening socket
241
240
These are all empty initially, because by default nothing should get
245
self.create_hook(HookPoint('server_started',
243
Hooks.__init__(self, "bzrlib.smart.server", "SmartTCPServer.hooks")
244
self.add_hook('server_started',
246
245
"Called by the bzr server when it starts serving a directory. "
247
246
"server_started is called with (backing urls, public url), "
248
247
"where backing_url is a list of URLs giving the "
249
248
"server-specific directory locations, and public_url is the "
250
"public URL for the directory being served.", (0, 16), None))
251
self.create_hook(HookPoint('server_started_ex',
249
"public URL for the directory being served.", (0, 16))
250
self.add_hook('server_started_ex',
252
251
"Called by the bzr server when it starts serving a directory. "
253
252
"server_started is called with (backing_urls, server_obj).",
255
self.create_hook(HookPoint('server_stopped',
254
self.add_hook('server_stopped',
256
255
"Called by the bzr server when it stops serving a directory. "
257
256
"server_stopped is called with the same parameters as the "
258
"server_started hook: (backing_urls, public_url).", (0, 16), None))
257
"server_started hook: (backing_urls, public_url).", (0, 16))
258
self.add_hook('server_exception',
259
"Called by the bzr server when an exception occurs. "
260
"server_exception is called with the sys.exc_info() tuple "
261
"return true for the hook if the exception has been handled, "
262
"in which case the server will exit normally.", (2, 4))
260
264
SmartTCPServer.hooks = SmartServerHooks()
327
331
chroot_server = chroot.ChrootServer(transport)
328
332
chroot_server.start_server()
329
333
self.cleanups.append(chroot_server.stop_server)
330
transport = get_transport(chroot_server.get_url())
334
transport = _mod_transport.get_transport_from_url(chroot_server.get_url())
331
335
if self.base_path is not None:
332
336
# Decorate the server's backing transport with a filter that can
333
337
# expand homedirs.
334
338
expand_userdirs = self._make_expand_userdirs_filter(transport)
335
339
expand_userdirs.start_server()
336
340
self.cleanups.append(expand_userdirs.stop_server)
337
transport = get_transport(expand_userdirs.get_url())
341
transport = _mod_transport.get_transport_from_url(expand_userdirs.get_url())
338
342
self.transport = transport
340
344
def _make_smart_server(self, host, port, inet):
348
352
port = medium.BZR_DEFAULT_PORT
349
353
smart_server = SmartTCPServer(self.transport)
350
354
smart_server.start_server(host, port)
351
trace.note('listening on port: %s' % smart_server.port)
355
trace.note(gettext('listening on port: %s') % smart_server.port)
352
356
self.smart_server = smart_server
354
358
def _change_globals(self):
388
391
bzr_server.set_up(transport, host, port, inet)
389
392
bzr_server.smart_server.serve()
394
hook_caught_exception = False
395
for hook in SmartTCPServer.hooks['server_exception']:
396
hook_caught_exception = hook(sys.exc_info())
397
if not hook_caught_exception:
391
400
bzr_server.tear_down()