~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',
262.2.3 by Martin Pool
serve --http handles the --port option
40
            help='Run an http (Loggerhead) server to browse code, by default on port 9876.')]
262.2.2 by Martin Pool
Merge in 'serve --http' based on code from mwh
41
42
    def run(self, *args, **kw):
43
        if 'http' in kw:
44
            # hack around loggerhead expecting to be loaded from the module
262.2.3 by Martin Pool
serve --http handles the --port option
45
            # "loggerhead" - this may be wrong in some cases?
262.2.2 by Martin Pool
Merge in 'serve --http' based on code from mwh
46
            import os.path, sys
47
            sys.path.append(os.path.dirname(__file__))
262.2.3 by Martin Pool
serve --http handles the --port option
48
262.2.2 by Martin Pool
Merge in 'serve --http' based on code from mwh
49
            from loggerhead.apps.filesystem import BranchesFromFileSystemRoot
50
            from paste.httpexceptions import HTTPExceptionHandler
51
            from paste.httpserver import serve
52
            a = HTTPExceptionHandler(BranchesFromFileSystemRoot('.'))
262.2.3 by Martin Pool
serve --http handles the --port option
53
            port = kw.get('port', '9876')
54
            if ':' in port:
55
                host, port = port.split(':')
56
            else:
57
                host = '0.0.0.0'
58
            serve(a, host=host, port=port)
262.2.2 by Martin Pool
Merge in 'serve --http' based on code from mwh
59
        else:
60
            super(cmd_serve, self).run(*args, **kw)
61
62
register_command(cmd_serve)