Mercurial > mozilla > hg > dogfood
annotate dogdish/dispatcher.py @ 11:5babc2ae6c27
more stubbing
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Wed, 17 Oct 2012 10:18:28 -0700 |
parents | f517d80bafe0 |
children | 234c2427e52b |
rev | line source |
---|---|
1 | 1 #!/usr/bin/env python |
2 | |
0 | 3 """ |
1 | 4 dogdish |
7
65d0a1153864
a stub that sorta does something
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
5 https://bugzilla.mozilla.org/show_bug.cgi?id=800118 |
0 | 6 """ |
7 | |
8 | 8 import os |
4 | 9 import sys |
0 | 10 from urlparse import urlparse |
4 | 11 from webob import Request |
0 | 12 from webob import Response, exc |
11 | 13 from ConfigParser import RawConfigParser as ConfigParser |
0 | 14 |
8 | 15 here = os.path.dirname(os.path.abspath(__file__)) |
16 | |
11 | 17 |
18 class Application(object): | |
19 """class for storing application.ini data""" | |
20 | |
21 def __init__(self, filename): | |
22 """ | |
23 - filename : path to an application.ini file | |
24 """ | |
25 | |
26 ### request handlers | |
27 | |
0 | 28 class Handler(object): |
9 | 29 """abstract handler object for a request""" |
1 | 30 |
0 | 31 def __init__(self, request): |
32 self.request = request | |
33 self.application_path = urlparse(request.application_url)[2] | |
34 | |
35 def link(self, path=(), permanant=False): | |
36 if isinstance(path, basestring): | |
37 path = [ path ] | |
38 path = [ i.strip('/') for i in path ] | |
39 if permanant: | |
40 application_url = [ self.request.application_url ] | |
41 else: | |
42 application_url = [ self.application_path ] | |
43 path = application_url + path | |
44 return '/'.join(path) | |
45 | |
46 def redirect(self, location): | |
47 raise exc.HTTPSeeOther(location=location) | |
48 | |
49 class Get(Handler): | |
9 | 50 """handle GET requests""" |
0 | 51 |
9 | 52 # template for response body |
0 | 53 body = """<?xml version="1.0"?> |
54 <updates> | |
55 <update type="minor" appVersion="19.0a1" version="19.0a1" extensionVersion="19.0a1" buildID="20121010114416" licenseURL="http://www.mozilla.com/test/sample-eula.html" detailsURL="http://www.mozilla.com/test/sample-details.html"> | |
9 | 56 <patch type="complete" URL="http://update.boot2gecko.org/nightly/b2g_update_2012-10-10_114416.mar%(query)s" hashFunction="SHA512" hashValue="84edb1f53891cf983bc0f6066d31492f43e2d063aaceb05e1c51876f4fde81635afeb7ce3203dee6f65dd59be0cae5c73c49093b625c99acd4118000ad72dda8" size="42924805"/> |
0 | 57 </update> |
58 </updates>""" | |
59 | |
9 | 60 ### methods for request handler |
61 | |
0 | 62 @classmethod |
63 def match(cls, request): | |
64 return request.method == 'GET' | |
65 | |
66 def __call__(self): | |
7
65d0a1153864
a stub that sorta does something
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
67 body = self.body |
65d0a1153864
a stub that sorta does something
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
68 query = {} |
65d0a1153864
a stub that sorta does something
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
69 dogfood_id = self.request.GET.get('dogfood_id') |
65d0a1153864
a stub that sorta does something
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
70 if dogfood_id: |
65d0a1153864
a stub that sorta does something
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
71 query['dogfood_id'] = dogfood_id |
65d0a1153864
a stub that sorta does something
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
72 |
65d0a1153864
a stub that sorta does something
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
73 # build query string |
65d0a1153864
a stub that sorta does something
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
74 if query: |
65d0a1153864
a stub that sorta does something
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
75 query = '?' + '&'.join(['%s=%s' % (key, value) for key, value in query.items()]) |
65d0a1153864
a stub that sorta does something
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
76 else: |
65d0a1153864
a stub that sorta does something
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
77 query = '' |
65d0a1153864
a stub that sorta does something
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
78 |
1 | 79 return Response(content_type='text/xml', |
7
65d0a1153864
a stub that sorta does something
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
80 body=body % query) |
0 | 81 |
82 class Dispatcher(object): | |
9 | 83 """web application""" |
84 | |
0 | 85 |
86 ### class level variables | |
8 | 87 defaults = {'directory': here} |
0 | 88 |
89 def __init__(self, **kw): | |
11 | 90 |
91 # set defaults | |
0 | 92 for key in self.defaults: |
93 setattr(self, key, kw.get(key, self.defaults[key])) | |
94 self.handlers = [ Get ] | |
11 | 95 self.updates = {} |
96 self.current_update = None | |
0 | 97 |
11 | 98 # scan directory |
99 self.scan() | |
100 | |
0 | 101 def __call__(self, environ, start_response): |
11 | 102 """WSGI application""" |
103 | |
0 | 104 request = Request(environ) |
105 for h in self.handlers: | |
106 if h.match(request): | |
107 handler = h(request) | |
108 break | |
109 else: | |
110 handler = exc.HTTPNotFound | |
111 res = handler() | |
112 return res(environ, start_response) | |
113 | |
11 | 114 def scan(self): |
115 """scan the directory for updates""" | |
116 contents = os.listdir(self.directory) | |
117 | |
118 | |
0 | 119 def main(args=sys.argv[1:]): |
120 """CLI entry point""" | |
121 | |
11 | 122 # imports for CLI |
0 | 123 import optparse |
124 from wsgiref import simple_server | |
125 | |
11 | 126 # parse CLI options |
0 | 127 parser = optparse.OptionParser() |
128 parser.add_option('-p', '--port', dest='port', | |
129 default=8080, type='int', | |
130 help="port to serve on") | |
11 | 131 parser.add_option('-d', '--directory', dest='directory', |
132 default=os.getcwd(), | |
133 help="directory of update files") | |
0 | 134 options, args = parser.parse_args() |
135 | |
11 | 136 # create the app |
137 app = Dispatcher(directory=options.directory) | |
0 | 138 |
11 | 139 # serve the app |
7
65d0a1153864
a stub that sorta does something
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
140 print "http://localhost:%s/" % options.port |
0 | 141 server = simple_server.make_server(host='0.0.0.0', port=options.port, app=app) |
142 server.serve_forever() | |
143 | |
144 if __name__ == '__main__': | |
145 main() |