~loggerhead-team/loggerhead/trunk-rich

« back to all changes in this revision

Viewing changes to __init__.py

  • Committer: Matt Nordhoff
  • Date: 2009-06-26 09:42:43 UTC
  • Revision ID: mnordhoff@mattnordhoff.com-20090626094243-8ia9poyixoj1en6l
Pass around and use argv correctly

Show diffs side-by-side

added added

removed removed

Lines of Context:
36
36
    import bzrlib
37
37
    from bzrlib.api import require_any_api
38
38
 
39
 
    require_any_api(bzrlib, [(1, 11, 0), (1, 13, 0), (1, 15, 0)])
 
39
    require_any_api(bzrlib, [(1, 13, 0), (1, 15, 0), (1, 16, 0), (1, 17, 0)])
40
40
 
41
41
    # NB: Normally plugins should lazily load almost everything, but this
42
42
    # seems reasonable to have in-line here: bzrlib.commands and options are
43
43
    # normally loaded, and the rest of loggerhead won't be loaded until serve
44
44
    # --http is run.
45
 
        
 
45
 
46
46
    # transport_server_registry was added in bzr 1.16. When we drop support for
47
47
    # older releases, we can remove the code to override cmd_serve.
48
48
 
53
53
 
54
54
    DEFAULT_HOST = '0.0.0.0'
55
55
    DEFAULT_PORT = 8080
56
 
    HELP = ('Loggerhead web-based code viewer and server. (default port: %d)' %
 
56
    HELP = ('Loggerhead, a web-based code viewer and server. (default port: %d)' %
57
57
            (DEFAULT_PORT,))
58
58
 
59
59
    def serve_http(transport, host=None, port=None, inet=None):
60
60
        # loggerhead internal code will try to 'import loggerhead', so
61
 
        # let's put it on the path
62
 
        import os.path, sys
63
 
        sys.path.append(os.path.dirname(__file__))
 
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__))
64
67
 
65
68
        from loggerhead.apps.transport import BranchesFromTransportRoot
66
69
        from loggerhead.config import LoggerheadConfig
70
73
            host = DEFAULT_HOST
71
74
        if port is None:
72
75
            port = DEFAULT_PORT
73
 
        argv = ['--host', host, '--port', str(port), transport.base]
 
76
        argv = ['--host', host, '--port', str(port), '--', transport.base]
 
77
        if not transport.is_readonly():
 
78
            argv.insert(0, '--allow-writes')
74
79
        config = LoggerheadConfig(argv)
75
80
        app = BranchesFromTransportRoot(transport, config)
76
81
        app = HTTPExceptionHandler(app)
94
99
            def run(self, *args, **kw):
95
100
                if 'http' in kw:
96
101
                    from bzrlib.transport import get_transport
 
102
                    allow_writes = kw.get('allow_writes', False)
97
103
                    path = kw.get('directory', '.')
98
104
                    port = kw.get('port', DEFAULT_PORT)
99
105
                    # port might be an int already...
101
107
                        host, port = port.split(':')
102
108
                    else:
103
109
                        host = DEFAULT_HOST
104
 
                    transport = get_transport(path)
 
110
                    if allow_writes:
 
111
                        transport = get_transport(path)
 
112
                    else:
 
113
                        transport = get_transport('readonly+' + path)
105
114
                    serve_http(transport, host, port)
106
115
                else:
107
116
                    super(cmd_serve, self).run(*args, **kw)