~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
262.2.4 by Martin Pool
Act as a plugin only when loaded by bzr; update docs
2
#
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
7
#
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
12
#
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
262.2.1 by Martin Pool
Stub code to allow loggerhead to load as a plugin
17
18
# This file allows loggerhead to be treated as a plugin for bzr.
19
#
262.2.4 by Martin Pool
Act as a plugin only when loaded by bzr; update docs
20
# XXX: Because loggerhead already contains a loggerhead directory, much of the
21
# code is going to appear loaded at bzrlib.plugins.loggerhead.loggerhead.
22
# This seems like the easiest thing, because bzrlib wants the top-level plugin
23
# directory to be the module, but when it's used as a library people expect
24
# the source directory to contain a directory called loggerhead.  -- mbp
25
# 20090123
26
27
"""Loggerhead web viewer for Bazaar branches.
28
29
This provides a new option "--http" to the "bzr serve" command, that 
30
starts a web server to browse the contents of a branch.
31
"""
262.2.1 by Martin Pool
Stub code to allow loggerhead to load as a plugin
32
262.2.2 by Martin Pool
Merge in 'serve --http' based on code from mwh
33
version_info = (1, 11, 0)
34
262.2.4 by Martin Pool
Act as a plugin only when loaded by bzr; update docs
35
if __name__ == 'bzrlib.plugins.loggerhead':
36
    import bzrlib
262.2.5 by Martin Pool
This loggerhead will work with bzr 1.13
37
    from bzrlib.api import require_any_api
262.2.4 by Martin Pool
Act as a plugin only when loaded by bzr; update docs
38
356.3.3 by Martin Albisetti
Update bzrlib API in /__init__.py
39
    require_any_api(bzrlib, [(1, 13, 0), (1, 15, 0)])
262.2.4 by Martin Pool
Act as a plugin only when loaded by bzr; update docs
40
41
    # NB: Normally plugins should lazily load almost everything, but this
42
    # seems reasonable to have in-line here: bzrlib.commands and options are
43
    # normally loaded, and the rest of loggerhead won't be loaded until serve
44
    # --http is run.
360 by Martin Albisetti
Avoid diluting sys.path if loggerhead is installed systemwide, install loggerhead as a plugin (Jelmer Vernooij)
45
352.4.1 by Matt Nordhoff
Use the "bzr serve" protocol registry if it's available.
46
    # transport_server_registry was added in bzr 1.16. When we drop support for
47
    # older releases, we can remove the code to override cmd_serve.
48
49
    try:
50
        from bzrlib.transport import transport_server_registry
51
    except ImportError:
52
        transport_server_registry = None
53
54
    DEFAULT_HOST = '0.0.0.0'
262.2.4 by Martin Pool
Act as a plugin only when loaded by bzr; update docs
55
    DEFAULT_PORT = 8080
360 by Martin Albisetti
Avoid diluting sys.path if loggerhead is installed systemwide, install loggerhead as a plugin (Jelmer Vernooij)
56
    HELP = ('Loggerhead, a web-based code viewer and server. (default port: %d)' %
352.4.3 by Matt Nordhoff
Better line-wrapping
57
            (DEFAULT_PORT,))
352.4.1 by Matt Nordhoff
Use the "bzr serve" protocol registry if it's available.
58
59
    def serve_http(transport, host=None, port=None, inet=None):
60
        # loggerhead internal code will try to 'import loggerhead', so
359.1.2 by Jelmer Vernooij
Avoid diluting sys.path if loggerhead is installed systemwide.
61
        # let's put it on the path if we can't find it in the existing path
62
        try:
63
            import loggerhead
64
        except ImportError:
65
            import os.path, sys
66
            sys.path.append(os.path.dirname(__file__))
352.4.1 by Matt Nordhoff
Use the "bzr serve" protocol registry if it's available.
67
68
        from loggerhead.apps.transport import BranchesFromTransportRoot
69
        from loggerhead.config import LoggerheadConfig
70
        from paste.httpexceptions import HTTPExceptionHandler
71
        from paste.httpserver import serve
72
        if host is None:
73
            host = DEFAULT_HOST
74
        if port is None:
75
            port = DEFAULT_PORT
359.2.2 by Matt Nordhoff
Fix a stupid mistake constructing argv
76
        argv = ['--host', host, '--port', str(port), '--', transport.base]
77
        if not transport.is_readonly():
78
            argv.insert(0, '--allow-writes')
352.4.1 by Matt Nordhoff
Use the "bzr serve" protocol registry if it's available.
79
        config = LoggerheadConfig(argv)
80
        app = BranchesFromTransportRoot(transport, config)
81
        app = HTTPExceptionHandler(app)
82
        serve(app, host=host, port=port)
83
84
    if transport_server_registry is not None:
85
        transport_server_registry.register('http', serve_http, help=HELP)
86
    else:
87
        import bzrlib.builtins
88
        from bzrlib.commands import get_cmd_object, register_command
89
        from bzrlib.option import Option
90
91
        _original_command = get_cmd_object('serve')
92
93
        class cmd_serve(bzrlib.builtins.cmd_serve):
94
            __doc__ = _original_command.__doc__
95
96
            takes_options = _original_command.takes_options + [
97
                Option('http', help=HELP)]
98
99
            def run(self, *args, **kw):
100
                if 'http' in kw:
101
                    from bzrlib.transport import get_transport
359.2.1 by Matt Nordhoff
Add an --allow-writes option to serve-branches and "bzr serve"
102
                    allow_writes = kw.get('allow_writes', False)
352.4.1 by Matt Nordhoff
Use the "bzr serve" protocol registry if it's available.
103
                    path = kw.get('directory', '.')
104
                    port = kw.get('port', DEFAULT_PORT)
105
                    # port might be an int already...
106
                    if isinstance(port, basestring) and ':' in port:
107
                        host, port = port.split(':')
108
                    else:
109
                        host = DEFAULT_HOST
359.2.1 by Matt Nordhoff
Add an --allow-writes option to serve-branches and "bzr serve"
110
                    if allow_writes:
111
                        transport = get_transport(path)
112
                    else:
113
                        transport = get_transport('readonly+' + path)
352.4.1 by Matt Nordhoff
Use the "bzr serve" protocol registry if it's available.
114
                    serve_http(transport, host, port)
262.2.4 by Martin Pool
Act as a plugin only when loaded by bzr; update docs
115
                else:
352.4.1 by Matt Nordhoff
Use the "bzr serve" protocol registry if it's available.
116
                    super(cmd_serve, self).run(*args, **kw)
262.2.2 by Martin Pool
Merge in 'serve --http' based on code from mwh
117
352.4.1 by Matt Nordhoff
Use the "bzr serve" protocol registry if it's available.
118
        register_command(cmd_serve)