1
'''Configuration tools for Loggerhead.'''
2
from optparse import OptionParser
7
_temporary_sql_dir = None
9
def _get_temporary_sql_dir():
10
global _temporary_sql_dir
11
if _temporary_sql_dir is None:
12
_temporary_sql_dir = tempfile.mkdtemp(prefix='loggerhead-cache-')
13
return _temporary_sql_dir
15
def command_line_parser():
16
parser = OptionParser("%prog [options] <path>")
24
parser.add_option("--user-dirs", action="store_true", dest="user_dirs",
25
help="Serve user directories as ~user.")
26
parser.add_option("--trunk-dir", metavar="DIR",
27
help="The directory that contains the trunk branches.")
28
parser.add_option("--port", dest="user_port",
29
help=("Port Loggerhead should listen on "
30
"(defaults to 8080)."))
31
parser.add_option("--host", dest="user_host",
32
help="Host Loggerhead should listen on.")
33
parser.add_option('--memory-profile', action='store_true',
34
dest='memory_profile',
35
help='Profile the memory usage using heapy.')
36
parser.add_option("--prefix", dest="user_prefix",
37
help="Specify host prefix.")
38
parser.add_option("--profile", action="store_true", dest="profile",
39
help="Generate callgrind profile data to "
40
"%d-stats.callgrind on each request.")
41
parser.add_option("--reload", action="store_true", dest="reload",
42
help="Restarts the application when changing python"
43
" files. Only used for development purposes.")
44
parser.add_option('--log-folder', dest="log_folder",
45
type=str, help="The directory to place log files in.")
46
parser.add_option("--version", action="store_true", dest="show_version",
47
help="Print the software version and exit")
48
parser.add_option('--use-cdn', action='store_true',
49
help="Serve YUI from Yahoo!'s CDN")
50
parser.add_option('--cache-dir', dest='sql_dir',
51
help="The directory to place the SQL cache in")
55
class LoggerheadConfig(object):
56
'''A configuration object.'''
59
self._parser = command_line_parser()
60
self._options, self._args = self._parser.parse_args(sys.argv[1:])
62
sql_dir = self.get_option('sql_dir')
64
sql_dir = _get_temporary_sql_dir()
65
self.SQL_DIR = sql_dir
67
def get_option(self, option):
68
'''Get an option from the options dict.'''
69
return getattr(self._options, option)
71
def get_arg(self, index):
72
'''Get an arg from the arg list.'''
73
return self._args[index]
76
'''Wrapper around OptionParser.print_help.'''
77
return self._parser.print_help()
81
'''Return the number of args from the option parser.'''
82
return len(self._args)