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>")
40
parser.add_option("--user-dirs", action="store_true",
41
help="Serve user directories as ~user.")
42
parser.add_option("--trunk-dir", metavar="DIR",
43
help="The directory that contains the trunk branches.")
44
parser.add_option("--port", dest="user_port",
45
help=("Port Loggerhead should listen on "
46
"(defaults to 8080)."))
47
parser.add_option("--host", dest="user_host",
48
help="Host Loggerhead should listen on.")
49
parser.add_option("--protocol", dest="protocol",
50
help=("Protocol to use: http, scgi, fcgi, ajp"
51
"(defaults to http)."))
52
parser.add_option("--log-level", default=None, action='callback',
53
callback=_optparse_level_to_int_level,
55
help="Set the verbosity of logging. Can either"
56
" be set to a numeric or string"
57
" (eg, 10=debug, 30=warning)")
58
parser.add_option("--memory-profile", action="store_true",
59
help="Profile the memory usage using Dozer.")
60
parser.add_option("--prefix", dest="user_prefix",
61
help="Specify host prefix.")
62
parser.add_option("--profile", action="store_true",
63
help="Generate callgrind profile data to "
64
"%d-stats.callgrind on each request.")
65
parser.add_option("--reload", action="store_true",
66
help="Restarts the application when changing python"
67
" files. Only used for development purposes.")
68
parser.add_option("--log-folder",
69
help="The directory to place log files in.")
70
parser.add_option("--version", action="store_true", dest="show_version",
71
help="Print the software version and exit")
72
parser.add_option("--use-cdn", action="store_true", dest="use_cdn",
73
help="Serve YUI from Yahoo!'s CDN")
74
parser.add_option("--cache-dir", dest="sql_dir",
75
help="The directory to place the SQL cache in")
76
parser.add_option("--allow-writes", action="store_true",
77
help="Allow writing to the Bazaar server.")
89
def _optparse_level_to_int_level(option, opt_str, value, parser):
90
parser.values.log_level = _level_to_int_level(value)
93
def _level_to_int_level(value):
94
"""Convert a string level to an integer value."""
101
return _log_levels[value.lower()]
104
class LoggerheadConfig(object):
105
"""A configuration object."""
107
def __init__(self, argv=None):
110
self._parser = command_line_parser()
111
self._options, self._args = self._parser.parse_args(argv)
113
sql_dir = self.get_option('sql_dir')
115
sql_dir = _get_temporary_sql_dir()
116
self.SQL_DIR = sql_dir
118
def get_option(self, option):
119
"""Get the value for the config option, either
120
from ~/.bazaar/bazaar.conf or from the command line.
121
All loggerhead-specific settings start with 'http_'
123
global_config = config.GlobalConfig().get_user_option('http_'+option)
124
cmd_config = getattr(self._options, option)
125
if global_config is not None and (
126
cmd_config is None or cmd_config is False):
131
def get_log_level(self):
132
opt = self.get_option('log_level')
133
return _level_to_int_level(opt)
135
def get_arg(self, index):
136
"""Get an arg from the arg list."""
137
return self._args[index]
139
def print_help(self):
140
"""Wrapper around OptionParser.print_help."""
141
return self._parser.print_help()
145
"""Return the number of args from the option parser."""
146
return len(self._args)