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", dest="user_dirs",
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('--memory-profile', action='store_true',
50
dest='memory_profile',
51
help='Profile the memory usage using Dozer.')
52
parser.add_option("--prefix", dest="user_prefix",
53
help="Specify host prefix.")
54
parser.add_option("--profile", action="store_true", dest="profile",
55
help="Generate callgrind profile data to "
56
"%d-stats.callgrind on each request.")
57
parser.add_option("--reload", action="store_true", dest="reload",
58
help="Restarts the application when changing python"
59
" files. Only used for development purposes.")
60
parser.add_option('--log-folder', dest="log_folder",
61
type=str, help="The directory to place log files in.")
62
parser.add_option("--version", action="store_true", dest="show_version",
63
help="Print the software version and exit")
64
parser.add_option("--use-cdn", action="store_true", dest="use_cdn",
65
help="Serve YUI from Yahoo!'s CDN")
66
parser.add_option("--cache-dir", dest="sql_dir",
67
help="The directory to place the SQL cache in")
68
parser.add_option('--allow-writes', action='store_true',
69
help="Allow writing to the Bazaar server.")
73
class LoggerheadConfig(object):
74
'''A configuration object.'''
76
def __init__(self, argv=None):
79
self._parser = command_line_parser()
80
self._options, self._args = self._parser.parse_args(argv)
82
sql_dir = self.get_option('sql_dir')
84
sql_dir = _get_temporary_sql_dir()
85
self.SQL_DIR = sql_dir
87
def get_option(self, option):
88
"""Get the value for the config option, either
89
from ~/.bazaar/bazaar.conf or from the command line.
90
All loggerhead-specific settings start with 'http_'"""
91
global_config = config.GlobalConfig().get_user_option('http_'+option)
92
cmd_config = getattr(self._options, option)
93
if global_config is not None and (
94
cmd_config is None or cmd_config is False):
99
def get_arg(self, index):
100
"""Get an arg from the arg list."""
101
return self._args[index]
103
def print_help(self):
104
"""Wrapper around OptionParser.print_help."""
105
return self._parser.print_help()
109
"""Return the number of args from the option parser."""
110
return len(self._args)