~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/smart/server.py

  • Committer: Jonathan Riddell
  • Date: 2011-09-16 15:37:58 UTC
  • mto: This revision was merged to the branch mainline in revision 6146.
  • Revision ID: jriddell@canonical.com-20110916153758-y936k3hysjc1tphy
update tests

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006-2010 Canonical Ltd
 
1
# Copyright (C) 2006-2011 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
18
18
 
19
19
import errno
20
20
import os.path
21
 
import select
22
21
import socket
23
22
import sys
24
23
import threading
25
24
 
26
 
from bzrlib.hooks import HookPoint, Hooks
 
25
from bzrlib.hooks import Hooks
27
26
from bzrlib import (
28
27
    errors,
29
28
    trace,
30
 
    transport,
 
29
    transport as _mod_transport,
31
30
)
 
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 (
36
36
    chroot,
37
 
    get_transport,
38
37
    pathfilter,
39
38
    )
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
178
177
 
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])
182
181
 
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
242
241
        notified.
243
242
        """
244
 
        Hooks.__init__(self)
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).",
254
 
            (1, 17), None))
255
 
        self.create_hook(HookPoint('server_stopped',
 
253
            (1, 17))
 
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))
259
263
 
260
264
SmartTCPServer.hooks = SmartServerHooks()
261
265
 
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
339
343
 
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
353
357
 
354
358
    def _change_globals(self):
375
379
        for cleanup in reversed(self.cleanups):
376
380
            cleanup()
377
381
 
378
 
 
379
382
def serve_bzr(transport, host=None, port=None, inet=False):
380
383
    """This is the default implementation of 'bzr serve'.
381
384
    
387
390
    try:
388
391
        bzr_server.set_up(transport, host, port, inet)
389
392
        bzr_server.smart_server.serve()
 
393
    except:
 
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:
 
398
            raise
390
399
    finally:
391
400
        bzr_server.tear_down()
392