~loggerhead-team/loggerhead/trunk-rich

262.2.1 by Martin Pool
Stub code to allow loggerhead to load as a plugin
1
# Copyright 2009 Canonical Ltd
2
3
# This file allows loggerhead to be treated as a plugin for bzr.
4
#
5
# XXX: Because loggerhead already contains a loggerhead directory, much of the code
6
# is going to live in bzrlib.plugins.loggerhead.loggerhead.  But moving it can
262.2.2 by Martin Pool
Merge in 'serve --http' based on code from mwh
7
# wait.  When we do move it, we may need to guard the plugin code by __name__
8
# so it can be used as a library from other places.  -- mbp 20090123
262.2.1 by Martin Pool
Stub code to allow loggerhead to load as a plugin
9
10
"""Loggerhead web viewer for Bazaar branches."""
11
12
import bzrlib
13
from bzrlib.api import require_api
14
262.2.2 by Martin Pool
Merge in 'serve --http' based on code from mwh
15
version_info = (1, 11, 0)
16
262.2.1 by Martin Pool
Stub code to allow loggerhead to load as a plugin
17
require_api(bzrlib, (1, 11, 0))
262.2.2 by Martin Pool
Merge in 'serve --http' based on code from mwh
18
19
20
# TODO: All the following should be in a lazily-loaded module.
21
#
22
# TODO: This should provide a new type of server that can be used by bzr
23
# serve, maybe through a registry, rather than overriding the command.  Though
24
# maybe we should keep the wrapper to work with older bzr releases, at least
25
# for a bit.
26
#
27
# TODO: If a --port option is given, use that.
28
29
import bzrlib.builtins
30
from bzrlib.commands import get_cmd_object, register_command
31
from bzrlib.option import Option
32
33
_original_command = get_cmd_object('serve')
34
35
class cmd_serve(bzrlib.builtins.cmd_serve):
36
    __doc__ = _original_command.__doc__
37
38
    takes_options = _original_command.takes_options + [
39
        Option('http',
40
            help='Run an http (Loggerhead) server to browse code.')]
41
42
    def run(self, *args, **kw):
43
        if 'http' in kw:
44
            # hack around loggerhead expecting to be loaded from the module
45
            # "loggerhead"
46
            import os.path, sys
47
            sys.path.append(os.path.dirname(__file__))
48
            from loggerhead.apps.filesystem import BranchesFromFileSystemRoot
49
            from paste.httpexceptions import HTTPExceptionHandler
50
            from paste.httpserver import serve
51
            a = HTTPExceptionHandler(BranchesFromFileSystemRoot('.'))
52
            serve(a, host='0.0.0.0', port='9876')
53
        else:
54
            super(cmd_serve, self).run(*args, **kw)
55
56
register_command(cmd_serve)