2
# Copyright (C) 2008, 2009 Canonical Ltd
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
# GNU General Public License for more details.
14
"""Configuration tools for Loggerhead."""
16
from optparse import OptionParser
20
from bzrlib import config
22
_temporary_sql_dir = None
24
def _get_temporary_sql_dir():
25
global _temporary_sql_dir
26
if _temporary_sql_dir is None:
27
_temporary_sql_dir = tempfile.mkdtemp(prefix='loggerhead-cache-')
28
return _temporary_sql_dir
30
def command_line_parser():
31
parser = OptionParser("%prog [options] <path>")
41
parser.add_option("--user-dirs", action="store_true",
42
help="Serve user directories as ~user.")
43
parser.add_option("--trunk-dir", metavar="DIR",
44
help="The directory that contains the trunk branches.")
45
parser.add_option("--port", dest="user_port",
46
help=("Port Loggerhead should listen on "
47
"(defaults to 8080)."))
48
parser.add_option("--host", dest="user_host",
49
help="Host Loggerhead should listen on.")
50
parser.add_option("--protocol", dest="protocol",
51
help=("Protocol to use: http, scgi, fcgi, ajp"
52
"(defaults to http)."))
53
parser.add_option("--log-level", default=None, action='callback',
54
callback=_optparse_level_to_int_level,
56
help="Set the verbosity of logging. Can either"
57
" be set to a numeric or string"
58
" (eg, 10=debug, 30=warning)")
59
parser.add_option("--memory-profile", action="store_true",
60
help="Profile the memory usage using Dozer.")
61
parser.add_option("--prefix", dest="user_prefix",
62
help="Specify host prefix.")
63
parser.add_option("--profile", action="store_true",
64
help="Generate callgrind profile data to "
65
"%d-stats.callgrind on each request.")
66
parser.add_option("--reload", action="store_true",
67
help="Restarts the application when changing python"
68
" files. Only used for development purposes.")
69
parser.add_option("--log-folder",
70
help="The directory to place log files in.")
71
parser.add_option("--version", action="store_true", dest="show_version",
72
help="Print the software version and exit")
73
parser.add_option("--use-cdn", action="store_true", dest="use_cdn",
74
help="Serve YUI from Yahoo!'s CDN")
75
parser.add_option("--cache-dir", dest="sql_dir",
76
help="The directory to place the SQL cache in")
77
parser.add_option("--allow-writes", action="store_true",
78
help="Allow writing to the Bazaar server.")
79
parser.add_option("--export-tarballs", action="store_true",
80
help="Allow exporting revisions to tarballs.")
92
def _optparse_level_to_int_level(option, opt_str, value, parser):
93
parser.values.log_level = _level_to_int_level(value)
96
def _level_to_int_level(value):
97
"""Convert a string level to an integer value."""
104
return _log_levels[value.lower()]
107
class LoggerheadConfig(object):
108
"""A configuration object."""
110
def __init__(self, argv=None):
113
self._parser = command_line_parser()
114
self._options, self._args = self._parser.parse_args(argv)
116
sql_dir = self.get_option('sql_dir')
118
sql_dir = _get_temporary_sql_dir()
119
self.SQL_DIR = sql_dir
121
def get_option(self, option):
122
"""Get the value for the config option, either
123
from ~/.bazaar/bazaar.conf or from the command line.
124
All loggerhead-specific settings start with 'http_'
126
global_config = config.GlobalConfig().get_user_option('http_'+option)
127
cmd_config = getattr(self._options, option)
128
if global_config is not None and (
129
cmd_config is None or cmd_config is False):
134
def get_log_level(self):
135
opt = self.get_option('log_level')
136
return _level_to_int_level(opt)
138
def get_arg(self, index):
139
"""Get an arg from the arg list."""
140
return self._args[index]
142
def print_help(self):
143
"""Wrapper around OptionParser.print_help."""
144
return self._parser.print_help()
148
"""Return the number of args from the option parser."""
149
return len(self._args)