Merge lp:~rockstar/loggerhead/config-cleanup into lp:loggerhead

Proposed by Paul Hummer
Status: Merged
Merged at revision: not available
Proposed branch: lp:~rockstar/loggerhead/config-cleanup
Merge into: lp:loggerhead
Diff against target: None lines
To merge this branch: bzr merge lp:~rockstar/loggerhead/config-cleanup
Reviewer Review Type Date Requested Status
Michael Hudson-Doyle Pending
Review via email: mp+5084@code.launchpad.net
To post a comment you must log in.
Revision history for this message
Paul Hummer (rockstar) wrote :

This branch cleans up (a bit) the configuration stuff for Loggerhead, and
merges command line and options with (some) configuration options. I think
this really needs a lot of cleaning up, but I'm still not sure how to do this
without it being really messy.

 reviewer mwhudson

319. By Paul Hummer

Responded to mwhudson's review

320. By Paul Hummer

Fixed an issue where I seemed to have poo for brains at the time, and created memory profile logs ALL the time.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'loggerhead/apps/filesystem.py'
2--- loggerhead/apps/filesystem.py 2009-02-19 18:04:11 +0000
3+++ loggerhead/apps/filesystem.py 2009-03-31 19:31:45 +0000
4@@ -1,7 +1,6 @@
5 """Serve branches at urls that mimic the file system layout."""
6
7 import os
8-import tempfile
9
10 from bzrlib import branch, errors, lru_cache
11
12@@ -11,10 +10,9 @@
13
14 from loggerhead.apps.branch import BranchWSGIApp
15 from loggerhead.apps import favicon_app, static_app
16+from loggerhead.config import LoggerheadConfig
17 from loggerhead.controllers.directory_ui import DirectoryUI
18
19-sql_dir = tempfile.mkdtemp(prefix='loggerhead-cache-')
20-
21
22 class BranchesFromFileSystemServer(object):
23
24@@ -22,6 +20,7 @@
25 self.path = path
26 self.root = root
27 self.name = name
28+ self._config = LoggerheadConfig()
29
30 def app_for_branch(self, branch):
31 if not self.name:
32@@ -31,8 +30,9 @@
33 name = self.name
34 is_root = False
35 branch_app = BranchWSGIApp(
36- branch, name, {'cachepath': sql_dir}, self.root.graph_cache,
37- is_root=is_root)
38+ branch, name,
39+ {'cachepath': self._config.SQL_DIR},
40+ self.root.graph_cache, is_root=is_root)
41 return branch_app.app
42
43 def app_for_non_branch(self, environ):
44
45=== added file 'loggerhead/config.py'
46--- loggerhead/config.py 1970-01-01 00:00:00 +0000
47+++ loggerhead/config.py 2009-03-31 19:31:45 +0000
48@@ -0,0 +1,66 @@
49+'''Configuration tools for Loggerhead.'''
50+from optparse import OptionParser
51+import sys
52+import tempfile
53+
54+
55+def command_line_parser():
56+ parser = OptionParser("%prog [options] <path>")
57+ parser.set_defaults(
58+ user_dirs=False,
59+ show_version=False,
60+ log_folder=None,
61+ )
62+ parser.add_option("--user-dirs", action="store_true", dest="user_dirs",
63+ help="Serve user directories as ~user.")
64+ parser.add_option("--trunk-dir", metavar="DIR",
65+ help="The directory that contains the trunk branches.")
66+ parser.add_option("--port", dest="user_port",
67+ help=("Port Loggerhead should listen on "
68+ "(defaults to 8080)."))
69+ parser.add_option("--host", dest="user_host",
70+ help="Host Loggerhead should listen on.")
71+ parser.add_option('--memory-profile', action='store_true',
72+ dest='memory_profile',
73+ help='Profile the memory usage using heapy.')
74+ parser.add_option("--prefix", dest="user_prefix",
75+ help="Specify host prefix.")
76+ parser.add_option("--profile", action="store_true", dest="profile",
77+ help="Generate callgrind profile data to "
78+ "%d-stats.callgrind on each request.")
79+ parser.add_option("--reload", action="store_true", dest="reload",
80+ help="Restarts the application when changing python"
81+ " files. Only used for development purposes.")
82+ parser.add_option('--log-folder', dest="log_folder",
83+ type=str, help="The directory to place log files in.")
84+ parser.add_option("--version", action="store_true", dest="show_version",
85+ help="Print the software version and exit")
86+ return parser
87+
88+
89+class LoggerheadConfig:
90+ '''A configuration object.'''
91+
92+ def __init__(self):
93+ self._parser = command_line_parser()
94+ self._options, self._args = self._parser.parse_args(sys.argv[1:])
95+
96+ self.SQL_DIR = tempfile.mkdtemp(prefix='loggerhead-cache-')
97+
98+ def get_option(self, option):
99+ '''Get an option from the options dict.'''
100+ return self._options.__dict__[option]
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)
114+
115
116=== modified file 'serve-branches'
117--- serve-branches 2009-03-30 22:11:06 +0000
118+++ serve-branches 2009-03-31 19:31:45 +0000
119@@ -19,8 +19,6 @@
120 import os
121 import sys
122
123-from optparse import OptionParser
124-
125 from bzrlib.plugin import load_plugins
126
127 from paste import httpserver
128@@ -30,56 +28,22 @@
129 from loggerhead import __version__
130 from loggerhead.apps.filesystem import (
131 BranchesFromFileSystemRoot, UserBranchesFromFileSystemRoot)
132+from loggerhead.config import LoggerheadConfig
133 from loggerhead.util import Reloader
134 from loggerhead.apps.error import ErrorHandlerApp
135
136
137-def command_line_parser():
138- parser = OptionParser("%prog [options] <path>")
139- parser.set_defaults(
140- user_dirs=False,
141- show_version=False,
142- log_folder=None,
143- )
144- parser.add_option("--user-dirs", action="store_true", dest="user_dirs",
145- help="Serve user directories as ~user.")
146- parser.add_option("--trunk-dir", metavar="DIR",
147- help="The directory that contains the trunk branches.")
148- parser.add_option("--port", dest="user_port",
149- help=("Port Loggerhead should listen on "
150- "(defaults to 8080)."))
151- parser.add_option("--host", dest="user_host",
152- help="Host Loggerhead should listen on.")
153- parser.add_option('--memory-profile', action='store_true',
154- dest='memory_profile',
155- help='Profile the memory usage using heapy.')
156- parser.add_option("--prefix", dest="user_prefix",
157- help="Specify host prefix.")
158- parser.add_option("--profile", action="store_true", dest="profile",
159- help="Generate callgrind profile data to "
160- "%d-stats.callgrind on each request.")
161- parser.add_option("--reload", action="store_true", dest="reload",
162- help="Restarts the application when changing python"
163- " files. Only used for development purposes.")
164- parser.add_option('--log-folder', dest="log_folder",
165- type=str, help="The directory to place log files in.")
166- parser.add_option("--version", action="store_true", dest="show_version",
167- help="Print the software version and exit")
168- return parser
169-
170-
171 def main(args):
172- parser = command_line_parser()
173- (options, args) = parser.parse_args(sys.argv[1:])
174+ config = LoggerheadConfig()
175
176- if options.show_version:
177+ if config.get_option('show_version'):
178 print "loggerhead %s" % __version__
179 sys.exit(0)
180
181- if len(args) > 1:
182- parser.print_help()
183+ if config.arg_count > 1:
184+ config.print_help()
185 sys.exit(1)
186- elif len(args) == 1:
187+ elif config.arg_count == 1:
188 [path] = args
189 else:
190 path = '.'
191@@ -88,21 +52,22 @@
192 print "%s is not a directory" % path
193 sys.exit(1)
194
195- if options.trunk_dir and not options.user_dirs:
196+ if config.get_option('trunk_dir') and not config.get_option('user_dirs'):
197 print "--trunk-dir is only valid with --user-dirs"
198 sys.exit(1)
199
200- if options.reload:
201+ if config.get_option('reload'):
202 if Reloader.is_installed():
203 Reloader.install()
204 else:
205 return Reloader.restart_with_reloader()
206
207- if options.user_dirs:
208- if not options.trunk_dir:
209+ if config.get_option('user_dirs'):
210+ if not config.get_option['trunk_dir']:
211 print "You didn't specify a directory for the trunk directories."
212 sys.exit(1)
213- app = UserBranchesFromFileSystemRoot(path, options.trunk_dir)
214+ app = UserBranchesFromFileSystemRoot(
215+ path, config.get_option('trunk_dir'))
216 else:
217 app = BranchesFromFileSystemRoot(path)
218
219@@ -110,8 +75,9 @@
220 logging.basicConfig()
221 logging.getLogger('').setLevel(logging.DEBUG)
222 logger = getattr(app, 'log', logging.getLogger('loggerhead'))
223- if options.log_folder:
224- logfile_path = os.path.join(options.log_folder, 'serve-branches.log')
225+ if config.get_option('log_folder'):
226+ logfile_path = os.path.join(
227+ config.get_option['log_folder'], 'serve-branches.log')
228 else:
229 logfile_path = 'serve-branches.log'
230 logfile = logging.FileHandler(logfile_path, 'a')
231@@ -128,17 +94,17 @@
232 # setup_logging() #end
233
234 app = TransLogger(app, logger=logger)
235- if options.profile:
236+ if config.get_option('profile'):
237 from loggerhead.middleware.profile import LSProfMiddleware
238 app = LSProfMiddleware(app)
239- if options.memory_profile:
240+ if config.get_option('memory_profile'):
241 from loggerhead.middleware.profile import MemoryProfileMiddleware
242 app = MemoryProfileMiddleware(app)
243
244- if not options.user_prefix:
245+ if not config.get_option('user_prefix'):
246 prefix = '/'
247 else:
248- prefix = options.user_prefix
249+ prefix = config.get_option('user_prefix')
250 if not prefix.startswith('/'):
251 prefix = '/' + prefix
252
253@@ -163,15 +129,15 @@
254 app = HTTPExceptionHandler(app)
255 app = ErrorHandlerApp(app)
256
257- if not options.user_port:
258+ if not config.get_option('user_port'):
259 port = '8080'
260 else:
261- port = options.user_port
262+ port = config.get_option('user_port')
263
264- if not options.user_host:
265+ if not config.get_option('user_host'):
266 host = '0.0.0.0'
267 else:
268- host = options.user_host
269+ host = config.get_option('user_host')
270
271 load_plugins()
272

Subscribers

People subscribed via source and target branches