|
17 | 17 |
|
18 | 18 | """Command-line script that runs a server over roundup.cgi.client. |
19 | 19 |
|
20 | | -$Id: roundup_server.py,v 1.71 2004-10-30 08:43:06 a1s Exp $ |
| 20 | +$Id: roundup_server.py,v 1.72 2004-11-02 09:07:08 a1s Exp $ |
21 | 21 | """ |
22 | 22 | __docformat__ = 'restructuredtext' |
23 | 23 |
|
|
52 | 52 | DEFAULT_PORT = 8080 |
53 | 53 |
|
54 | 54 | # See what types of multiprocess server are available |
55 | | -MULTIPROCESS_TYPES = ["none"] |
| 55 | +# Note: the order is important. Preferred multiprocess type |
| 56 | +# is the last element of this list. |
| 57 | +# "debug" means "none" + no tracker/template cache |
| 58 | +MULTIPROCESS_TYPES = ["debug", "none"] |
56 | 59 | try: |
57 | 60 | import thread |
58 | 61 | except ImportError: |
|
65 | 68 |
|
66 | 69 | class RoundupRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler): |
67 | 70 | TRACKER_HOMES = {} |
| 71 | + TRACKERS = None |
68 | 72 | LOG_IPADDRESS = 1 |
69 | 73 |
|
| 74 | + def get_tracker(self, name): |
| 75 | + """Return a tracker instance for given tracker name""" |
| 76 | + # Note: try/except KeyError works faster that has_key() check |
| 77 | + # if the key is usually found in the dictionary |
| 78 | + # |
| 79 | + # Return cached tracker instance if we have a tracker cache |
| 80 | + if self.TRACKERS: |
| 81 | + try: |
| 82 | + return self.TRACKERS[name] |
| 83 | + except KeyError: |
| 84 | + pass |
| 85 | + # No cached tracker. Look for home path. |
| 86 | + try: |
| 87 | + tracker_home = self.TRACKER_HOMES[name] |
| 88 | + except KeyError: |
| 89 | + raise client.NotFound |
| 90 | + # open the instance |
| 91 | + tracker = roundup.instance.open(tracker_home) |
| 92 | + # and cache it if we have a tracker cache |
| 93 | + if self.TRACKERS: |
| 94 | + self.TRACKERS[name] = tracker |
| 95 | + return tracker |
| 96 | + |
70 | 97 | def run_cgi(self): |
71 | 98 | """ Execute the CGI command. Wrap an innner call in an error |
72 | 99 | handler so all errors can be caught. |
@@ -158,12 +185,6 @@ def inner_run_cgi(self): |
158 | 185 | self.wfile.write('Moved Permanently') |
159 | 186 | return |
160 | 187 |
|
161 | | - if self.TRACKER_HOMES.has_key(tracker_name): |
162 | | - tracker_home = self.TRACKER_HOMES[tracker_name] |
163 | | - tracker = roundup.instance.open(tracker_home) |
164 | | - else: |
165 | | - raise client.NotFound |
166 | | - |
167 | 188 | # figure out what the rest of the path is |
168 | 189 | if len(l_path) > 2: |
169 | 190 | rest = '/'.join(l_path[2:]) |
@@ -193,7 +214,8 @@ def inner_run_cgi(self): |
193 | 214 | env['SERVER_PORT'] = str(self.server.server_port) |
194 | 215 | env['HTTP_HOST'] = self.headers['host'] |
195 | 216 |
|
196 | | - # do the roundup thang |
| 217 | + # do the roundup thing |
| 218 | + tracker = self.get_tracker(tracker_name) |
197 | 219 | tracker.Client(tracker, self, env).main() |
198 | 220 |
|
199 | 221 | def address_string(self): |
@@ -376,10 +398,18 @@ def get_server(self): |
376 | 398 | sys.stdout = sys.stderr = open(self["LOGFILE"], 'a', 0) |
377 | 399 | # we don't want the cgi module interpreting the command-line args ;) |
378 | 400 | sys.argv = sys.argv[:1] |
| 401 | + # preload all trackers unless we are in "debug" mode |
| 402 | + tracker_homes = self.trackers() |
| 403 | + if self["MULTIPROCESS"] == "debug": |
| 404 | + trackers = None |
| 405 | + else: |
| 406 | + trackers = dict([(name, roundup.instance.open(home)) |
| 407 | + for (name, home) in tracker_homes]) |
379 | 408 | # build customized request handler class |
380 | 409 | class RequestHandler(RoundupRequestHandler): |
381 | 410 | LOG_IPADDRESS = not self["LOG_HOSTNAMES"] |
382 | | - TRACKER_HOMES = dict(self.trackers()) |
| 411 | + TRACKER_HOMES = dict(tracker_homes) |
| 412 | + TRACKERS = trackers |
383 | 413 | # obtain request server class |
384 | 414 | if self["MULTIPROCESS"] not in MULTIPROCESS_TYPES: |
385 | 415 | print _("Multiprocess mode \"%s\" is not available, " |
|
0 commit comments