1
# Copyright 2009, 2010, 2011 Canonical Ltd
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.
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.
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., 51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA
18
# This file allows loggerhead to be treated as a plugin for bzr.
20
# XXX: Because loggerhead already contains a loggerhead directory, much of the
21
# code is going to appear loaded at breezy.plugins.loggerhead.loggerhead.
22
# This seems like the easiest thing, because breezy 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
27
"""Loggerhead web viewer for Bazaar branches.
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.
34
import importlib.metadata as importlib_metadata
36
import importlib_metadata
39
from packaging.version import Version
42
version_info = Version(importlib_metadata.version("loggerhead")).release
43
except importlib_metadata.PackageNotFoundError:
44
# Support running tests from the build tree without installation.
48
from breezy import commands
50
from breezy.transport import transport_server_registry
52
DEFAULT_HOST = '0.0.0.0'
54
HELP = ('Loggerhead, a web-based code viewer and server. (default port: %d)' %
58
def serve_http(transport, host=None, port=None, inet=None, client_timeout=None):
59
# TODO: if we supported inet to pass requests in and respond to them,
60
# then it would be easier to test the full stack, but it probably
61
# means routing around paste.httpserver.serve which probably
62
# isn't testing the full stack
63
from paste.httpexceptions import HTTPExceptionHandler
64
from paste.httpserver import serve
67
from .loggerhead.apps.http_head import HeadMiddleware
68
from .loggerhead.apps.transport import BranchesFromTransportRoot
69
from .loggerhead.config import LoggerheadConfig
70
from .loggerhead.main import setup_logging
72
from loggerhead.apps.http_head import HeadMiddleware
73
from loggerhead.apps.transport import BranchesFromTransportRoot
74
from loggerhead.config import LoggerheadConfig
75
from loggerhead.main import setup_logging
81
argv = ['--host', host, '--port', str(port), '--', transport.base]
82
if not transport.is_readonly():
83
argv.insert(0, '--allow-writes')
84
config = LoggerheadConfig(argv)
85
setup_logging(config, init_logging=False, log_file=sys.stderr)
86
app = BranchesFromTransportRoot(transport.base, config)
87
# Bug #758618, HeadMiddleware seems to break HTTPExceptionHandler from
88
# actually sending appropriate return codes to the client. Since nobody
89
# desperately needs HeadMiddleware right now, just ignoring it.
90
# app = HeadMiddleware(app)
91
app = HTTPExceptionHandler(app)
92
serve(app, host=host, port=port)
94
transport_server_registry.register('http', serve_http, help=HELP)
96
class cmd_load_test_loggerhead(commands.Command):
97
"""Run a load test against a live loggerhead instance.
99
Pass in the name of a script file to run. See loggerhead/load_test.py
100
for a description of the file format.
104
takes_args = ["filename"]
106
def run(self, filename):
108
from .loggerhead.loggerhead import load_test
110
from loggerhead.loggerhead import load_test
111
script = load_test.run_script(filename)
112
for thread_id in sorted(script._threads):
113
worker = script._threads[thread_id][0]
114
for url, success, time in worker.stats:
115
self.outf.write(' %5.3fs %s %s\n'
116
% (time, str(success)[0], url))
118
commands.register_command(cmd_load_test_loggerhead)
120
def load_tests(loader, basic_tests, pattern):
122
from .loggerhead.tests import test_suite
124
from loggerhead.tests import test_suite
125
basic_tests.addTest(test_suite())