~loggerhead-team/loggerhead/trunk-rich

« back to all changes in this revision

Viewing changes to loggerhead/config.py

[rs=igc] update to lp:loggerhead trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#
 
2
# Copyright (C) 2008, 2009 Canonical Ltd
 
3
#
 
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.
 
8
#
 
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.
 
13
#
 
14
"""Configuration tools for Loggerhead."""
 
15
 
 
16
from optparse import OptionParser
 
17
import sys
 
18
import tempfile
 
19
 
 
20
from bzrlib import config
 
21
 
 
22
_temporary_sql_dir = None
 
23
 
 
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
 
29
 
 
30
def command_line_parser():
 
31
    parser = OptionParser("%prog [options] <path>")
 
32
    parser.set_defaults(
 
33
        user_dirs=False,
 
34
        show_version=False,
 
35
        log_folder=None,
 
36
        use_cdn=False,
 
37
        sql_dir=None,
 
38
        allow_writes=False,
 
39
        )
 
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("--memory-profile", action="store_true",
 
53
                      help="Profile the memory usage using Dozer.")
 
54
    parser.add_option("--prefix", dest="user_prefix",
 
55
                      help="Specify host prefix.")
 
56
    parser.add_option("--profile", action="store_true",
 
57
                      help="Generate callgrind profile data to "
 
58
                        "%d-stats.callgrind on each request.")
 
59
    parser.add_option("--reload", action="store_true",
 
60
                      help="Restarts the application when changing python"
 
61
                           " files. Only used for development purposes.")
 
62
    parser.add_option("--log-folder",
 
63
                      help="The directory to place log files in.")
 
64
    parser.add_option("--version", action="store_true", dest="show_version",
 
65
                      help="Print the software version and exit")
 
66
    parser.add_option("--use-cdn", action="store_true", dest="use_cdn",
 
67
                      help="Serve YUI from Yahoo!'s CDN")
 
68
    parser.add_option("--cache-dir", dest="sql_dir",
 
69
                      help="The directory to place the SQL cache in")
 
70
    parser.add_option("--allow-writes", action="store_true",
 
71
                      help="Allow writing to the Bazaar server.")
 
72
    return parser
 
73
 
 
74
 
 
75
class LoggerheadConfig(object):
 
76
    """A configuration object."""
 
77
 
 
78
    def __init__(self, argv=None):
 
79
        if argv is None:
 
80
            argv = sys.argv[1:]
 
81
        self._parser = command_line_parser()
 
82
        self._options, self._args = self._parser.parse_args(argv)
 
83
 
 
84
        sql_dir = self.get_option('sql_dir')
 
85
        if sql_dir is None:
 
86
            sql_dir = _get_temporary_sql_dir()
 
87
        self.SQL_DIR = sql_dir
 
88
 
 
89
    def get_option(self, option):
 
90
        """Get the value for the config option, either
 
91
        from ~/.bazaar/bazaar.conf or from the command line.
 
92
        All loggerhead-specific settings start with 'http_'
 
93
        """
 
94
        global_config = config.GlobalConfig().get_user_option('http_'+option)
 
95
        cmd_config = getattr(self._options, option)
 
96
        if global_config is not None and (
 
97
            cmd_config is None or cmd_config is False):
 
98
            return global_config
 
99
        else:
 
100
            return cmd_config
 
101
 
 
102
    def get_arg(self, index):
 
103
        """Get an arg from the arg list."""
 
104
        return self._args[index]
 
105
 
 
106
    def print_help(self):
 
107
        """Wrapper around OptionParser.print_help."""
 
108
        return self._parser.print_help()
 
109
 
 
110
    @property
 
111
    def arg_count(self):
 
112
        """Return the number of args from the option parser."""
 
113
        return len(self._args)