Mercurial > mozilla > hg > dogfood
annotate dogdish/dispatcher.py @ 21:ce97a26e4c03 default tip
remove legacy code
author | Jeff Hammel |
---|---|
date | Mon, 12 Nov 2012 23:40:06 +0000 |
parents | 71c36a7842d1 |
children |
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 | |
12
234c2427e52b
beginnings of a scan function
Jeff Hammel <jhammel@mozilla.com>
parents:
11
diff
changeset
|
8 import fnmatch |
14 | 9 import hashlib |
8 | 10 import os |
4 | 11 import sys |
0 | 12 from urlparse import urlparse |
4 | 13 from webob import Request |
0 | 14 from webob import Response, exc |
11 | 15 from ConfigParser import RawConfigParser as ConfigParser |
0 | 16 |
8 | 17 here = os.path.dirname(os.path.abspath(__file__)) |
18 | |
15
fc5b842de4e9
be a little less aggressive when caching
Jeff Hammel <jhammel@mozilla.com>
parents:
14
diff
changeset
|
19 ### models |
11 | 20 |
21 class Application(object): | |
22 """class for storing application.ini data""" | |
23 | |
24 def __init__(self, filename): | |
25 """ | |
26 - filename : path to an application.ini file | |
27 """ | |
14 | 28 config = ConfigParser() |
29 config.read(filename) | |
30 self.build_id = config.get('App', 'BuildID') | |
31 self.version = config.get('App', 'Version') | |
11 | 32 |
15
fc5b842de4e9
be a little less aggressive when caching
Jeff Hammel <jhammel@mozilla.com>
parents:
14
diff
changeset
|
33 |
fc5b842de4e9
be a little less aggressive when caching
Jeff Hammel <jhammel@mozilla.com>
parents:
14
diff
changeset
|
34 class Update(object): |
fc5b842de4e9
be a little less aggressive when caching
Jeff Hammel <jhammel@mozilla.com>
parents:
14
diff
changeset
|
35 """class representing a .mar update file""" |
fc5b842de4e9
be a little less aggressive when caching
Jeff Hammel <jhammel@mozilla.com>
parents:
14
diff
changeset
|
36 |
fc5b842de4e9
be a little less aggressive when caching
Jeff Hammel <jhammel@mozilla.com>
parents:
14
diff
changeset
|
37 prefix = 'b2g_update_' |
fc5b842de4e9
be a little less aggressive when caching
Jeff Hammel <jhammel@mozilla.com>
parents:
14
diff
changeset
|
38 suffix = '.mar' |
fc5b842de4e9
be a little less aggressive when caching
Jeff Hammel <jhammel@mozilla.com>
parents:
14
diff
changeset
|
39 |
fc5b842de4e9
be a little less aggressive when caching
Jeff Hammel <jhammel@mozilla.com>
parents:
14
diff
changeset
|
40 @classmethod |
fc5b842de4e9
be a little less aggressive when caching
Jeff Hammel <jhammel@mozilla.com>
parents:
14
diff
changeset
|
41 def updates(cls, directory): |
fc5b842de4e9
be a little less aggressive when caching
Jeff Hammel <jhammel@mozilla.com>
parents:
14
diff
changeset
|
42 """returns the updates in a directory""" |
fc5b842de4e9
be a little less aggressive when caching
Jeff Hammel <jhammel@mozilla.com>
parents:
14
diff
changeset
|
43 |
fc5b842de4e9
be a little less aggressive when caching
Jeff Hammel <jhammel@mozilla.com>
parents:
14
diff
changeset
|
44 contents = [i for i in os.listdir(directory) |
fc5b842de4e9
be a little less aggressive when caching
Jeff Hammel <jhammel@mozilla.com>
parents:
14
diff
changeset
|
45 if i.startswith(cls.prefix) and i.endswith(cls.suffix)] |
fc5b842de4e9
be a little less aggressive when caching
Jeff Hammel <jhammel@mozilla.com>
parents:
14
diff
changeset
|
46 contents = set(contents) |
fc5b842de4e9
be a little less aggressive when caching
Jeff Hammel <jhammel@mozilla.com>
parents:
14
diff
changeset
|
47 return contents |
fc5b842de4e9
be a little less aggressive when caching
Jeff Hammel <jhammel@mozilla.com>
parents:
14
diff
changeset
|
48 |
fc5b842de4e9
be a little less aggressive when caching
Jeff Hammel <jhammel@mozilla.com>
parents:
14
diff
changeset
|
49 def __init__(self, directory, filename): |
fc5b842de4e9
be a little less aggressive when caching
Jeff Hammel <jhammel@mozilla.com>
parents:
14
diff
changeset
|
50 self.directory = directory |
fc5b842de4e9
be a little less aggressive when caching
Jeff Hammel <jhammel@mozilla.com>
parents:
14
diff
changeset
|
51 self.filename = filename |
fc5b842de4e9
be a little less aggressive when caching
Jeff Hammel <jhammel@mozilla.com>
parents:
14
diff
changeset
|
52 self.path = os.path.join(directory, filename) |
fc5b842de4e9
be a little less aggressive when caching
Jeff Hammel <jhammel@mozilla.com>
parents:
14
diff
changeset
|
53 self.stamp = filename[len(self.prefix):-len(self.suffix)] |
20
71c36a7842d1
Use mtime to determine most recent update; don't cache updates since timestamps can change.
Jonathan Griffin <jgriffin@mozilla.com>
parents:
19
diff
changeset
|
54 self.modifiedTime = os.path.getmtime(self.path) |
15
fc5b842de4e9
be a little less aggressive when caching
Jeff Hammel <jhammel@mozilla.com>
parents:
14
diff
changeset
|
55 self.size = os.path.getsize(self.path) |
fc5b842de4e9
be a little less aggressive when caching
Jeff Hammel <jhammel@mozilla.com>
parents:
14
diff
changeset
|
56 |
fc5b842de4e9
be a little less aggressive when caching
Jeff Hammel <jhammel@mozilla.com>
parents:
14
diff
changeset
|
57 # cached properties |
fc5b842de4e9
be a little less aggressive when caching
Jeff Hammel <jhammel@mozilla.com>
parents:
14
diff
changeset
|
58 self._application = None |
fc5b842de4e9
be a little less aggressive when caching
Jeff Hammel <jhammel@mozilla.com>
parents:
14
diff
changeset
|
59 self._hash = None |
fc5b842de4e9
be a little less aggressive when caching
Jeff Hammel <jhammel@mozilla.com>
parents:
14
diff
changeset
|
60 |
fc5b842de4e9
be a little less aggressive when caching
Jeff Hammel <jhammel@mozilla.com>
parents:
14
diff
changeset
|
61 def application(self): |
fc5b842de4e9
be a little less aggressive when caching
Jeff Hammel <jhammel@mozilla.com>
parents:
14
diff
changeset
|
62 """ |
fc5b842de4e9
be a little less aggressive when caching
Jeff Hammel <jhammel@mozilla.com>
parents:
14
diff
changeset
|
63 returns the path to the application.ini |
fc5b842de4e9
be a little less aggressive when caching
Jeff Hammel <jhammel@mozilla.com>
parents:
14
diff
changeset
|
64 associated with this update |
fc5b842de4e9
be a little less aggressive when caching
Jeff Hammel <jhammel@mozilla.com>
parents:
14
diff
changeset
|
65 """ |
fc5b842de4e9
be a little less aggressive when caching
Jeff Hammel <jhammel@mozilla.com>
parents:
14
diff
changeset
|
66 |
fc5b842de4e9
be a little less aggressive when caching
Jeff Hammel <jhammel@mozilla.com>
parents:
14
diff
changeset
|
67 if not self._application: |
fc5b842de4e9
be a little less aggressive when caching
Jeff Hammel <jhammel@mozilla.com>
parents:
14
diff
changeset
|
68 application_ini = 'application_%s.ini' % self.stamp |
fc5b842de4e9
be a little less aggressive when caching
Jeff Hammel <jhammel@mozilla.com>
parents:
14
diff
changeset
|
69 application_ini = os.path.join(self.directory, application_ini) |
fc5b842de4e9
be a little less aggressive when caching
Jeff Hammel <jhammel@mozilla.com>
parents:
14
diff
changeset
|
70 assert os.path.exists(application_ini) |
fc5b842de4e9
be a little less aggressive when caching
Jeff Hammel <jhammel@mozilla.com>
parents:
14
diff
changeset
|
71 self._application = Application(application_ini) |
fc5b842de4e9
be a little less aggressive when caching
Jeff Hammel <jhammel@mozilla.com>
parents:
14
diff
changeset
|
72 return self._application |
fc5b842de4e9
be a little less aggressive when caching
Jeff Hammel <jhammel@mozilla.com>
parents:
14
diff
changeset
|
73 |
fc5b842de4e9
be a little less aggressive when caching
Jeff Hammel <jhammel@mozilla.com>
parents:
14
diff
changeset
|
74 def hash(self): |
fc5b842de4e9
be a little less aggressive when caching
Jeff Hammel <jhammel@mozilla.com>
parents:
14
diff
changeset
|
75 if not self._hash: |
fc5b842de4e9
be a little less aggressive when caching
Jeff Hammel <jhammel@mozilla.com>
parents:
14
diff
changeset
|
76 # compute the hash |
fc5b842de4e9
be a little less aggressive when caching
Jeff Hammel <jhammel@mozilla.com>
parents:
14
diff
changeset
|
77 with file(self.path) as f: |
fc5b842de4e9
be a little less aggressive when caching
Jeff Hammel <jhammel@mozilla.com>
parents:
14
diff
changeset
|
78 self._hash = hashlib.sha512(f.read()).hexdigest() |
fc5b842de4e9
be a little less aggressive when caching
Jeff Hammel <jhammel@mozilla.com>
parents:
14
diff
changeset
|
79 return self._hash |
fc5b842de4e9
be a little less aggressive when caching
Jeff Hammel <jhammel@mozilla.com>
parents:
14
diff
changeset
|
80 |
17 | 81 class UpdateStable(Update): |
82 prefix = 'b2g_stable_update_' | |
83 | |
84 | |
11 | 85 ### request handlers |
86 | |
0 | 87 class Handler(object): |
9 | 88 """abstract handler object for a request""" |
1 | 89 |
14 | 90 def __init__(self, app, request): |
91 self.app = app | |
0 | 92 self.request = request |
93 self.application_path = urlparse(request.application_url)[2] | |
94 | |
95 def link(self, path=(), permanant=False): | |
96 if isinstance(path, basestring): | |
97 path = [ path ] | |
98 path = [ i.strip('/') for i in path ] | |
99 if permanant: | |
100 application_url = [ self.request.application_url ] | |
101 else: | |
102 application_url = [ self.application_path ] | |
103 path = application_url + path | |
104 return '/'.join(path) | |
105 | |
106 def redirect(self, location): | |
107 raise exc.HTTPSeeOther(location=location) | |
108 | |
109 class Get(Handler): | |
9 | 110 """handle GET requests""" |
0 | 111 |
9 | 112 # template for response body |
0 | 113 body = """<?xml version="1.0"?> |
114 <updates> | |
19 | 115 <update type="minor" appVersion="%(version)s" version="%(version)s" extensionVersion="%(version)s" buildID="%(build_id)s" licenseURL="http://www.mozilla.com/test/sample-eula.html" detailsURL="http://www.mozilla.com/test/sample-details.html"> |
18 | 116 <patch type="complete" URL="http://update.boot2gecko.org/%(path)s/%(update)s%(query)s" hashFunction="SHA512" hashValue="%(hash)s" size="%(size)s"/> |
0 | 117 </update> |
118 </updates>""" | |
119 | |
9 | 120 ### methods for request handler |
121 | |
0 | 122 @classmethod |
123 def match(cls, request): | |
124 return request.method == 'GET' | |
125 | |
126 def __call__(self): | |
14 | 127 |
7
65d0a1153864
a stub that sorta does something
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
128 body = self.body |
65d0a1153864
a stub that sorta does something
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
129 query = {} |
65d0a1153864
a stub that sorta does something
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
130 dogfood_id = self.request.GET.get('dogfood_id') |
65d0a1153864
a stub that sorta does something
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
131 if dogfood_id: |
12
234c2427e52b
beginnings of a scan function
Jeff Hammel <jhammel@mozilla.com>
parents:
11
diff
changeset
|
132 query['dogfooding_prerelease_id'] = dogfood_id |
15
fc5b842de4e9
be a little less aggressive when caching
Jeff Hammel <jhammel@mozilla.com>
parents:
14
diff
changeset
|
133 current_update = self.app.current_update |
fc5b842de4e9
be a little less aggressive when caching
Jeff Hammel <jhammel@mozilla.com>
parents:
14
diff
changeset
|
134 application = current_update.application() |
14 | 135 query['build_id'] = application.build_id |
136 query['version'] = application.version | |
7
65d0a1153864
a stub that sorta does something
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
137 |
65d0a1153864
a stub that sorta does something
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
138 # build query string |
65d0a1153864
a stub that sorta does something
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
139 if query: |
14 | 140 query = '?' + '&'.join(['%s=%s' % (key, value) for key, value in query.items()]) |
7
65d0a1153864
a stub that sorta does something
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
141 else: |
65d0a1153864
a stub that sorta does something
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
142 query = '' |
65d0a1153864
a stub that sorta does something
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
143 |
14 | 144 |
12
234c2427e52b
beginnings of a scan function
Jeff Hammel <jhammel@mozilla.com>
parents:
11
diff
changeset
|
145 # template variables |
15
fc5b842de4e9
be a little less aggressive when caching
Jeff Hammel <jhammel@mozilla.com>
parents:
14
diff
changeset
|
146 variables = dict(update=current_update.filename, |
fc5b842de4e9
be a little less aggressive when caching
Jeff Hammel <jhammel@mozilla.com>
parents:
14
diff
changeset
|
147 size=current_update.size, |
19 | 148 version=application.version, |
15
fc5b842de4e9
be a little less aggressive when caching
Jeff Hammel <jhammel@mozilla.com>
parents:
14
diff
changeset
|
149 hash=current_update.hash(), |
18 | 150 path=self.app.path, |
19 | 151 build_id=application.build_id, |
14 | 152 query=query) |
12
234c2427e52b
beginnings of a scan function
Jeff Hammel <jhammel@mozilla.com>
parents:
11
diff
changeset
|
153 |
1 | 154 return Response(content_type='text/xml', |
12
234c2427e52b
beginnings of a scan function
Jeff Hammel <jhammel@mozilla.com>
parents:
11
diff
changeset
|
155 body=body % variables) |
0 | 156 |
157 class Dispatcher(object): | |
9 | 158 """web application""" |
159 | |
0 | 160 |
161 ### class level variables | |
18 | 162 defaults = {'directory': here, |
163 'path': None, | |
17 | 164 'update_class': Update} |
0 | 165 |
166 def __init__(self, **kw): | |
11 | 167 |
168 # set defaults | |
0 | 169 for key in self.defaults: |
170 setattr(self, key, kw.get(key, self.defaults[key])) | |
171 self.handlers = [ Get ] | |
14 | 172 |
18 | 173 # path |
174 if not self.path: | |
19 | 175 self.path = os.path.split(self.directory.strip(os.path.sep))[-1] |
18 | 176 |
14 | 177 # cache |
11 | 178 self.updates = {} |
179 self.current_update = None | |
0 | 180 |
11 | 181 # scan directory |
182 self.scan() | |
16 | 183 assert self.current_update, "No updates found in %s" % self.directory |
11 | 184 |
0 | 185 def __call__(self, environ, start_response): |
11 | 186 """WSGI application""" |
187 | |
0 | 188 request = Request(environ) |
21 | 189 self.scan() |
0 | 190 for h in self.handlers: |
191 if h.match(request): | |
14 | 192 handler = h(self, request) |
193 res = handler() | |
0 | 194 break |
195 else: | |
14 | 196 res = exc.HTTPNotFound() |
197 | |
0 | 198 return res(environ, start_response) |
199 | |
11 | 200 def scan(self): |
14 | 201 """ |
202 scan the directory for updates | |
203 returns True if new updates are found, False otherwise | |
204 """ | |
15
fc5b842de4e9
be a little less aggressive when caching
Jeff Hammel <jhammel@mozilla.com>
parents:
14
diff
changeset
|
205 |
fc5b842de4e9
be a little less aggressive when caching
Jeff Hammel <jhammel@mozilla.com>
parents:
14
diff
changeset
|
206 # check for new updates |
17 | 207 contents = self.update_class.updates(self.directory) |
20
71c36a7842d1
Use mtime to determine most recent update; don't cache updates since timestamps can change.
Jonathan Griffin <jgriffin@mozilla.com>
parents:
19
diff
changeset
|
208 for update in contents: |
17 | 209 self.updates[update] = self.update_class(self.directory, update) |
15
fc5b842de4e9
be a little less aggressive when caching
Jeff Hammel <jhammel@mozilla.com>
parents:
14
diff
changeset
|
210 if self.current_update: |
20
71c36a7842d1
Use mtime to determine most recent update; don't cache updates since timestamps can change.
Jonathan Griffin <jgriffin@mozilla.com>
parents:
19
diff
changeset
|
211 if self.updates[update].modifiedTime > self.current_update.modifiedTime: |
15
fc5b842de4e9
be a little less aggressive when caching
Jeff Hammel <jhammel@mozilla.com>
parents:
14
diff
changeset
|
212 self.current_update = self.updates[update] |
fc5b842de4e9
be a little less aggressive when caching
Jeff Hammel <jhammel@mozilla.com>
parents:
14
diff
changeset
|
213 else: |
fc5b842de4e9
be a little less aggressive when caching
Jeff Hammel <jhammel@mozilla.com>
parents:
14
diff
changeset
|
214 self.current_update = self.updates[update] |
13
71f9f68986b5
directory scanning + caching
Jeff Hammel <jhammel@mozilla.com>
parents:
12
diff
changeset
|
215 |
14 | 216 # TODO: could remove old files from the cache if not found in contents |
217 | |
218 return True | |
219 | |
0 | 220 def main(args=sys.argv[1:]): |
221 """CLI entry point""" | |
222 | |
11 | 223 # imports for CLI |
0 | 224 import optparse |
225 from wsgiref import simple_server | |
226 | |
11 | 227 # parse CLI options |
0 | 228 parser = optparse.OptionParser() |
229 parser.add_option('-p', '--port', dest='port', | |
230 default=8080, type='int', | |
231 help="port to serve on") | |
11 | 232 parser.add_option('-d', '--directory', dest='directory', |
233 default=os.getcwd(), | |
234 help="directory of update files") | |
0 | 235 options, args = parser.parse_args() |
236 | |
11 | 237 # create the app |
238 app = Dispatcher(directory=options.directory) | |
0 | 239 |
11 | 240 # serve the app |
7
65d0a1153864
a stub that sorta does something
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
241 print "http://localhost:%s/" % options.port |
0 | 242 server = simple_server.make_server(host='0.0.0.0', port=options.port, app=app) |
243 server.serve_forever() | |
244 | |
245 if __name__ == '__main__': | |
246 main() |