~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/smart/server.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2009-10-06 20:45:48 UTC
  • mfrom: (4728.1.2 integration)
  • Revision ID: pqm@pqm.ubuntu.com-20091006204548-bjnc3z4k256ppimz
MutableTree.has_changes() does not require a tree parameter anymore

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
"""Server for smart-server protocol."""
18
18
 
19
19
import errno
 
20
import os.path
20
21
import socket
21
22
import sys
22
23
import threading
30
31
from bzrlib.lazy_import import lazy_import
31
32
lazy_import(globals(), """
32
33
from bzrlib.smart import medium
 
34
from bzrlib.transport import (
 
35
    chroot,
 
36
    get_transport,
 
37
    pathfilter,
 
38
    )
 
39
from bzrlib import (
 
40
    urlutils,
 
41
    )
33
42
""")
34
43
 
35
44
 
111
120
            pass
112
121
        for hook in SmartTCPServer.hooks['server_started']:
113
122
            hook(backing_urls, self.get_url())
 
123
        for hook in SmartTCPServer.hooks['server_started_ex']:
 
124
            hook(backing_urls, self)
114
125
        self._started.set()
115
126
        try:
116
127
            try:
132
143
                # dont log when CTRL-C'd.
133
144
                raise
134
145
            except Exception, e:
135
 
                trace.error("Unhandled smart server error.")
136
 
                trace.log_exception_quietly()
 
146
                trace.report_exception(sys.exc_info(), sys.stderr)
137
147
                raise
138
148
        finally:
139
149
            self._stopped.set()
152
162
 
153
163
    def serve_conn(self, conn, thread_name_suffix):
154
164
        # For WIN32, where the timeout value from the listening socket
155
 
        # propogates to the newly accepted socket.
 
165
        # propagates to the newly accepted socket.
156
166
        conn.setblocking(True)
157
167
        conn.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
158
168
        handler = medium.SmartServerSocketStreamMedium(
214
224
            "where backing_url is a list of URLs giving the "
215
225
            "server-specific directory locations, and public_url is the "
216
226
            "public URL for the directory being served.", (0, 16), None))
 
227
        self.create_hook(HookPoint('server_started_ex',
 
228
            "Called by the bzr server when it starts serving a directory. "
 
229
            "server_started is called with (backing_urls, server_obj).",
 
230
            (1, 17), None))
217
231
        self.create_hook(HookPoint('server_stopped',
218
232
            "Called by the bzr server when it stops serving a directory. "
219
233
            "server_stopped is called with the same parameters as the "
307
321
        return transport.get_transport(url)
308
322
 
309
323
 
 
324
def _local_path_for_transport(transport):
 
325
    """Return a local path for transport, if reasonably possible.
 
326
    
 
327
    This function works even if transport's url has a "readonly+" prefix,
 
328
    unlike local_path_from_url.
 
329
    
 
330
    This essentially recovers the --directory argument the user passed to "bzr
 
331
    serve" from the transport passed to serve_bzr.
 
332
    """
 
333
    try:
 
334
        base_url = transport.external_url()
 
335
    except (errors.InProcessTransport, NotImplementedError):
 
336
        return None
 
337
    else:
 
338
        # Strip readonly prefix
 
339
        if base_url.startswith('readonly+'):
 
340
            base_url = base_url[len('readonly+'):]
 
341
        try:
 
342
            return urlutils.local_path_from_url(base_url)
 
343
        except errors.InvalidURL:
 
344
            return None
 
345
 
 
346
 
 
347
class BzrServerFactory(object):
 
348
    """Helper class for serve_bzr."""
 
349
 
 
350
    def __init__(self, userdir_expander=None, get_base_path=None):
 
351
        self.cleanups = []
 
352
        self.base_path = None
 
353
        self.backing_transport = None
 
354
        if userdir_expander is None:
 
355
            userdir_expander = os.path.expanduser
 
356
        self.userdir_expander = userdir_expander
 
357
        if get_base_path is None:
 
358
            get_base_path = _local_path_for_transport
 
359
        self.get_base_path = get_base_path
 
360
 
 
361
    def _expand_userdirs(self, path):
 
362
        """Translate /~/ or /~user/ to e.g. /home/foo, using
 
363
        self.userdir_expander (os.path.expanduser by default).
 
364
 
 
365
        If the translated path would fall outside base_path, or the path does
 
366
        not start with ~, then no translation is applied.
 
367
 
 
368
        If the path is inside, it is adjusted to be relative to the base path.
 
369
 
 
370
        e.g. if base_path is /home, and the expanded path is /home/joe, then
 
371
        the translated path is joe.
 
372
        """
 
373
        result = path
 
374
        if path.startswith('~'):
 
375
            expanded = self.userdir_expander(path)
 
376
            if not expanded.endswith('/'):
 
377
                expanded += '/'
 
378
            if expanded.startswith(self.base_path):
 
379
                result = expanded[len(self.base_path):]
 
380
        return result
 
381
 
 
382
    def _make_expand_userdirs_filter(self, transport):
 
383
        return pathfilter.PathFilteringServer(transport, self._expand_userdirs)
 
384
 
 
385
    def _make_backing_transport(self, transport):
 
386
        """Chroot transport, and decorate with userdir expander."""
 
387
        self.base_path = self.get_base_path(transport)
 
388
        chroot_server = chroot.ChrootServer(transport)
 
389
        chroot_server.setUp()
 
390
        self.cleanups.append(chroot_server.tearDown)
 
391
        transport = get_transport(chroot_server.get_url())
 
392
        if self.base_path is not None:
 
393
            # Decorate the server's backing transport with a filter that can
 
394
            # expand homedirs.
 
395
            expand_userdirs = self._make_expand_userdirs_filter(transport)
 
396
            expand_userdirs.setUp()
 
397
            self.cleanups.append(expand_userdirs.tearDown)
 
398
            transport = get_transport(expand_userdirs.get_url())
 
399
        self.transport = transport
 
400
 
 
401
    def _make_smart_server(self, host, port, inet):
 
402
        if inet:
 
403
            smart_server = medium.SmartServerPipeStreamMedium(
 
404
                sys.stdin, sys.stdout, self.transport)
 
405
        else:
 
406
            if host is None:
 
407
                host = medium.BZR_DEFAULT_INTERFACE
 
408
            if port is None:
 
409
                port = medium.BZR_DEFAULT_PORT
 
410
            smart_server = SmartTCPServer(self.transport, host=host, port=port)
 
411
            trace.note('listening on port: %s' % smart_server.port)
 
412
        self.smart_server = smart_server
 
413
 
 
414
    def _change_globals(self):
 
415
        from bzrlib import lockdir, ui
 
416
        # For the duration of this server, no UI output is permitted. note
 
417
        # that this may cause problems with blackbox tests. This should be
 
418
        # changed with care though, as we dont want to use bandwidth sending
 
419
        # progress over stderr to smart server clients!
 
420
        old_factory = ui.ui_factory
 
421
        old_lockdir_timeout = lockdir._DEFAULT_TIMEOUT_SECONDS
 
422
        def restore_default_ui_factory_and_lockdir_timeout():
 
423
            ui.ui_factory = old_factory
 
424
            lockdir._DEFAULT_TIMEOUT_SECONDS = old_lockdir_timeout
 
425
        self.cleanups.append(restore_default_ui_factory_and_lockdir_timeout)
 
426
        ui.ui_factory = ui.SilentUIFactory()
 
427
        lockdir._DEFAULT_TIMEOUT_SECONDS = 0
 
428
 
 
429
    def set_up(self, transport, host, port, inet):
 
430
        self._make_backing_transport(transport)
 
431
        self._make_smart_server(host, port, inet)
 
432
        self._change_globals()
 
433
 
 
434
    def tear_down(self):
 
435
        for cleanup in reversed(self.cleanups):
 
436
            cleanup()
 
437
 
 
438
 
 
439
def serve_bzr(transport, host=None, port=None, inet=False):
 
440
    """This is the default implementation of 'bzr serve'.
 
441
    
 
442
    It creates a TCP or pipe smart server on 'transport, and runs it.  The
 
443
    transport will be decorated with a chroot and pathfilter (using
 
444
    os.path.expanduser).
 
445
    """
 
446
    bzr_server = BzrServerFactory()
 
447
    try:
 
448
        bzr_server.set_up(transport, host, port, inet)
 
449
        bzr_server.smart_server.serve()
 
450
    finally:
 
451
        bzr_server.tear_down()
 
452