annotate dogdish/dispatcher.py @ 18:6ffbad8cc31f

deal with paths
author jhammel
date Fri, 26 Oct 2012 15:11:06 +0000
parents 427a94428f04
children 608df6b3de80
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1
2cb8b06d64df minor fixes
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
1 #!/usr/bin/env python
2cb8b06d64df minor fixes
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
2
0
b3fd8f98df32 initial stub
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
3 """
1
2cb8b06d64df minor fixes
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
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
b3fd8f98df32 initial stub
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
6 """
b3fd8f98df32 initial stub
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
7
12
234c2427e52b beginnings of a scan function
Jeff Hammel <jhammel@mozilla.com>
parents: 11
diff changeset
8 import fnmatch
14
cffb6f681b59 things seem to work
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
9 import hashlib
8
ebb65758d725 add a default directory
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
10 import os
4
4a5a4582be3b forgot an import
Jeff Hammel <jhammel@mozilla.com>
parents: 3
diff changeset
11 import sys
0
b3fd8f98df32 initial stub
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
12 from urlparse import urlparse
4
4a5a4582be3b forgot an import
Jeff Hammel <jhammel@mozilla.com>
parents: 3
diff changeset
13 from webob import Request
0
b3fd8f98df32 initial stub
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
14 from webob import Response, exc
11
5babc2ae6c27 more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
15 from ConfigParser import RawConfigParser as ConfigParser
0
b3fd8f98df32 initial stub
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
16
8
ebb65758d725 add a default directory
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
17 here = os.path.dirname(os.path.abspath(__file__))
ebb65758d725 add a default directory
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
18
15
fc5b842de4e9 be a little less aggressive when caching
Jeff Hammel <jhammel@mozilla.com>
parents: 14
diff changeset
19 ### models
11
5babc2ae6c27 more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
20
5babc2ae6c27 more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
21 class Application(object):
5babc2ae6c27 more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
22 """class for storing application.ini data"""
5babc2ae6c27 more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
23
5babc2ae6c27 more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
24 def __init__(self, filename):
5babc2ae6c27 more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
25 """
5babc2ae6c27 more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
26 - filename : path to an application.ini file
5babc2ae6c27 more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
27 """
14
cffb6f681b59 things seem to work
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
28 config = ConfigParser()
cffb6f681b59 things seem to work
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
29 config.read(filename)
cffb6f681b59 things seem to work
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
30 self.build_id = config.get('App', 'BuildID')
cffb6f681b59 things seem to work
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
31 self.version = config.get('App', 'Version')
11
5babc2ae6c27 more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
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)]
fc5b842de4e9 be a little less aggressive when caching
Jeff Hammel <jhammel@mozilla.com>
parents: 14
diff changeset
54 self.size = os.path.getsize(self.path)
fc5b842de4e9 be a little less aggressive when caching
Jeff Hammel <jhammel@mozilla.com>
parents: 14
diff changeset
55
fc5b842de4e9 be a little less aggressive when caching
Jeff Hammel <jhammel@mozilla.com>
parents: 14
diff changeset
56 # cached properties
fc5b842de4e9 be a little less aggressive when caching
Jeff Hammel <jhammel@mozilla.com>
parents: 14
diff changeset
57 self._application = None
fc5b842de4e9 be a little less aggressive when caching
Jeff Hammel <jhammel@mozilla.com>
parents: 14
diff changeset
58 self._hash = None
fc5b842de4e9 be a little less aggressive when caching
Jeff Hammel <jhammel@mozilla.com>
parents: 14
diff changeset
59
fc5b842de4e9 be a little less aggressive when caching
Jeff Hammel <jhammel@mozilla.com>
parents: 14
diff changeset
60 def application(self):
fc5b842de4e9 be a little less aggressive when caching
Jeff Hammel <jhammel@mozilla.com>
parents: 14
diff changeset
61 """
fc5b842de4e9 be a little less aggressive when caching
Jeff Hammel <jhammel@mozilla.com>
parents: 14
diff changeset
62 returns the path to the application.ini
fc5b842de4e9 be a little less aggressive when caching
Jeff Hammel <jhammel@mozilla.com>
parents: 14
diff changeset
63 associated with this update
fc5b842de4e9 be a little less aggressive when caching
Jeff Hammel <jhammel@mozilla.com>
parents: 14
diff changeset
64 """
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 if not self._application:
fc5b842de4e9 be a little less aggressive when caching
Jeff Hammel <jhammel@mozilla.com>
parents: 14
diff changeset
67 application_ini = 'application_%s.ini' % self.stamp
fc5b842de4e9 be a little less aggressive when caching
Jeff Hammel <jhammel@mozilla.com>
parents: 14
diff changeset
68 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
69 assert os.path.exists(application_ini)
fc5b842de4e9 be a little less aggressive when caching
Jeff Hammel <jhammel@mozilla.com>
parents: 14
diff changeset
70 self._application = Application(application_ini)
fc5b842de4e9 be a little less aggressive when caching
Jeff Hammel <jhammel@mozilla.com>
parents: 14
diff changeset
71 return self._application
fc5b842de4e9 be a little less aggressive when caching
Jeff Hammel <jhammel@mozilla.com>
parents: 14
diff changeset
72
fc5b842de4e9 be a little less aggressive when caching
Jeff Hammel <jhammel@mozilla.com>
parents: 14
diff changeset
73 def hash(self):
fc5b842de4e9 be a little less aggressive when caching
Jeff Hammel <jhammel@mozilla.com>
parents: 14
diff changeset
74 if not self._hash:
fc5b842de4e9 be a little less aggressive when caching
Jeff Hammel <jhammel@mozilla.com>
parents: 14
diff changeset
75 # compute the hash
fc5b842de4e9 be a little less aggressive when caching
Jeff Hammel <jhammel@mozilla.com>
parents: 14
diff changeset
76 with file(self.path) as f:
fc5b842de4e9 be a little less aggressive when caching
Jeff Hammel <jhammel@mozilla.com>
parents: 14
diff changeset
77 self._hash = hashlib.sha512(f.read()).hexdigest()
fc5b842de4e9 be a little less aggressive when caching
Jeff Hammel <jhammel@mozilla.com>
parents: 14
diff changeset
78 return self._hash
fc5b842de4e9 be a little less aggressive when caching
Jeff Hammel <jhammel@mozilla.com>
parents: 14
diff changeset
79
17
427a94428f04 have a different class for stable updates
jhammel
parents: 16
diff changeset
80 class UpdateStable(Update):
427a94428f04 have a different class for stable updates
jhammel
parents: 16
diff changeset
81 prefix = 'b2g_stable_update_'
427a94428f04 have a different class for stable updates
jhammel
parents: 16
diff changeset
82
427a94428f04 have a different class for stable updates
jhammel
parents: 16
diff changeset
83
11
5babc2ae6c27 more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
84 ### request handlers
5babc2ae6c27 more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
85
0
b3fd8f98df32 initial stub
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
86 class Handler(object):
9
f517d80bafe0 more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 8
diff changeset
87 """abstract handler object for a request"""
1
2cb8b06d64df minor fixes
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
88
14
cffb6f681b59 things seem to work
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
89 def __init__(self, app, request):
cffb6f681b59 things seem to work
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
90 self.app = app
0
b3fd8f98df32 initial stub
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
91 self.request = request
b3fd8f98df32 initial stub
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
92 self.application_path = urlparse(request.application_url)[2]
b3fd8f98df32 initial stub
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
93
b3fd8f98df32 initial stub
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
94 def link(self, path=(), permanant=False):
b3fd8f98df32 initial stub
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
95 if isinstance(path, basestring):
b3fd8f98df32 initial stub
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
96 path = [ path ]
b3fd8f98df32 initial stub
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
97 path = [ i.strip('/') for i in path ]
b3fd8f98df32 initial stub
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
98 if permanant:
b3fd8f98df32 initial stub
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
99 application_url = [ self.request.application_url ]
b3fd8f98df32 initial stub
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
100 else:
b3fd8f98df32 initial stub
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
101 application_url = [ self.application_path ]
b3fd8f98df32 initial stub
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
102 path = application_url + path
b3fd8f98df32 initial stub
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
103 return '/'.join(path)
b3fd8f98df32 initial stub
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
104
b3fd8f98df32 initial stub
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
105 def redirect(self, location):
b3fd8f98df32 initial stub
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
106 raise exc.HTTPSeeOther(location=location)
b3fd8f98df32 initial stub
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
107
b3fd8f98df32 initial stub
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
108 class Get(Handler):
9
f517d80bafe0 more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 8
diff changeset
109 """handle GET requests"""
0
b3fd8f98df32 initial stub
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
110
9
f517d80bafe0 more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 8
diff changeset
111 # template for response body
0
b3fd8f98df32 initial stub
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
112 body = """<?xml version="1.0"?>
b3fd8f98df32 initial stub
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
113 <updates>
b3fd8f98df32 initial stub
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
114 <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">
18
6ffbad8cc31f deal with paths
jhammel
parents: 17
diff changeset
115 <patch type="complete" URL="http://update.boot2gecko.org/%(path)s/%(update)s%(query)s" hashFunction="SHA512" hashValue="%(hash)s" size="%(size)s"/>
0
b3fd8f98df32 initial stub
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
116 </update>
b3fd8f98df32 initial stub
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
117 </updates>"""
b3fd8f98df32 initial stub
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
118
9
f517d80bafe0 more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 8
diff changeset
119 ### methods for request handler
f517d80bafe0 more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 8
diff changeset
120
0
b3fd8f98df32 initial stub
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
121 @classmethod
b3fd8f98df32 initial stub
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
122 def match(cls, request):
b3fd8f98df32 initial stub
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
123 return request.method == 'GET'
b3fd8f98df32 initial stub
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
124
b3fd8f98df32 initial stub
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
125 def __call__(self):
14
cffb6f681b59 things seem to work
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
126
7
65d0a1153864 a stub that sorta does something
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
127 body = self.body
65d0a1153864 a stub that sorta does something
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
128 query = {}
65d0a1153864 a stub that sorta does something
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
129 dogfood_id = self.request.GET.get('dogfood_id')
65d0a1153864 a stub that sorta does something
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
130 if dogfood_id:
12
234c2427e52b beginnings of a scan function
Jeff Hammel <jhammel@mozilla.com>
parents: 11
diff changeset
131 query['dogfooding_prerelease_id'] = dogfood_id
15
fc5b842de4e9 be a little less aggressive when caching
Jeff Hammel <jhammel@mozilla.com>
parents: 14
diff changeset
132 current_update = self.app.current_update
fc5b842de4e9 be a little less aggressive when caching
Jeff Hammel <jhammel@mozilla.com>
parents: 14
diff changeset
133 application = current_update.application()
14
cffb6f681b59 things seem to work
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
134 query['build_id'] = application.build_id
cffb6f681b59 things seem to work
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
135 query['version'] = application.version
7
65d0a1153864 a stub that sorta does something
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
136
65d0a1153864 a stub that sorta does something
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
137 # build query string
65d0a1153864 a stub that sorta does something
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
138 if query:
14
cffb6f681b59 things seem to work
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
139 query = '?' + '&amp;'.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
140 else:
65d0a1153864 a stub that sorta does something
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
141 query = ''
65d0a1153864 a stub that sorta does something
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
142
14
cffb6f681b59 things seem to work
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
143
12
234c2427e52b beginnings of a scan function
Jeff Hammel <jhammel@mozilla.com>
parents: 11
diff changeset
144 # template variables
15
fc5b842de4e9 be a little less aggressive when caching
Jeff Hammel <jhammel@mozilla.com>
parents: 14
diff changeset
145 variables = dict(update=current_update.filename,
fc5b842de4e9 be a little less aggressive when caching
Jeff Hammel <jhammel@mozilla.com>
parents: 14
diff changeset
146 size=current_update.size,
fc5b842de4e9 be a little less aggressive when caching
Jeff Hammel <jhammel@mozilla.com>
parents: 14
diff changeset
147 hash=current_update.hash(),
18
6ffbad8cc31f deal with paths
jhammel
parents: 17
diff changeset
148 path=self.app.path,
14
cffb6f681b59 things seem to work
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
149 query=query)
12
234c2427e52b beginnings of a scan function
Jeff Hammel <jhammel@mozilla.com>
parents: 11
diff changeset
150
1
2cb8b06d64df minor fixes
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
151 return Response(content_type='text/xml',
12
234c2427e52b beginnings of a scan function
Jeff Hammel <jhammel@mozilla.com>
parents: 11
diff changeset
152 body=body % variables)
0
b3fd8f98df32 initial stub
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
153
b3fd8f98df32 initial stub
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
154 class Dispatcher(object):
9
f517d80bafe0 more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 8
diff changeset
155 """web application"""
f517d80bafe0 more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 8
diff changeset
156
0
b3fd8f98df32 initial stub
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
157
b3fd8f98df32 initial stub
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
158 ### class level variables
18
6ffbad8cc31f deal with paths
jhammel
parents: 17
diff changeset
159 defaults = {'directory': here,
6ffbad8cc31f deal with paths
jhammel
parents: 17
diff changeset
160 'path': None,
17
427a94428f04 have a different class for stable updates
jhammel
parents: 16
diff changeset
161 'update_class': Update}
0
b3fd8f98df32 initial stub
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
162
b3fd8f98df32 initial stub
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
163 def __init__(self, **kw):
11
5babc2ae6c27 more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
164
5babc2ae6c27 more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
165 # set defaults
0
b3fd8f98df32 initial stub
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
166 for key in self.defaults:
b3fd8f98df32 initial stub
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
167 setattr(self, key, kw.get(key, self.defaults[key]))
b3fd8f98df32 initial stub
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
168 self.handlers = [ Get ]
14
cffb6f681b59 things seem to work
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
169
18
6ffbad8cc31f deal with paths
jhammel
parents: 17
diff changeset
170 # path
6ffbad8cc31f deal with paths
jhammel
parents: 17
diff changeset
171 if not self.path:
6ffbad8cc31f deal with paths
jhammel
parents: 17
diff changeset
172 self.path = os.path.split(self.directory.strip(os.path.sep))
6ffbad8cc31f deal with paths
jhammel
parents: 17
diff changeset
173
14
cffb6f681b59 things seem to work
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
174 # cache
11
5babc2ae6c27 more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
175 self.updates = {}
5babc2ae6c27 more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
176 self.current_update = None
0
b3fd8f98df32 initial stub
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
177
11
5babc2ae6c27 more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
178 # scan directory
5babc2ae6c27 more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
179 self.scan()
16
31898fb3b7d9 better error message
jhammel
parents: 15
diff changeset
180 assert self.current_update, "No updates found in %s" % self.directory
11
5babc2ae6c27 more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
181
0
b3fd8f98df32 initial stub
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
182 def __call__(self, environ, start_response):
11
5babc2ae6c27 more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
183 """WSGI application"""
5babc2ae6c27 more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
184
0
b3fd8f98df32 initial stub
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
185 request = Request(environ)
14
cffb6f681b59 things seem to work
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
186 new = self.scan()
0
b3fd8f98df32 initial stub
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
187 for h in self.handlers:
b3fd8f98df32 initial stub
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
188 if h.match(request):
14
cffb6f681b59 things seem to work
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
189 handler = h(self, request)
cffb6f681b59 things seem to work
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
190 res = handler()
0
b3fd8f98df32 initial stub
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
191 break
b3fd8f98df32 initial stub
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
192 else:
14
cffb6f681b59 things seem to work
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
193 res = exc.HTTPNotFound()
cffb6f681b59 things seem to work
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
194
0
b3fd8f98df32 initial stub
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
195 return res(environ, start_response)
b3fd8f98df32 initial stub
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
196
11
5babc2ae6c27 more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
197 def scan(self):
14
cffb6f681b59 things seem to work
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
198 """
cffb6f681b59 things seem to work
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
199 scan the directory for updates
cffb6f681b59 things seem to work
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
200 returns True if new updates are found, False otherwise
cffb6f681b59 things seem to work
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
201 """
15
fc5b842de4e9 be a little less aggressive when caching
Jeff Hammel <jhammel@mozilla.com>
parents: 14
diff changeset
202
fc5b842de4e9 be a little less aggressive when caching
Jeff Hammel <jhammel@mozilla.com>
parents: 14
diff changeset
203 # check for new updates
17
427a94428f04 have a different class for stable updates
jhammel
parents: 16
diff changeset
204 contents = self.update_class.updates(self.directory)
12
234c2427e52b beginnings of a scan function
Jeff Hammel <jhammel@mozilla.com>
parents: 11
diff changeset
205 new = contents.difference(self.updates.keys())
234c2427e52b beginnings of a scan function
Jeff Hammel <jhammel@mozilla.com>
parents: 11
diff changeset
206 if not new:
234c2427e52b beginnings of a scan function
Jeff Hammel <jhammel@mozilla.com>
parents: 11
diff changeset
207 # directory contents unchanged from cached values
14
cffb6f681b59 things seem to work
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
208 return False
11
5babc2ae6c27 more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
209
13
71f9f68986b5 directory scanning + caching
Jeff Hammel <jhammel@mozilla.com>
parents: 12
diff changeset
210 for update in new:
17
427a94428f04 have a different class for stable updates
jhammel
parents: 16
diff changeset
211 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
212 if self.current_update:
fc5b842de4e9 be a little less aggressive when caching
Jeff Hammel <jhammel@mozilla.com>
parents: 14
diff changeset
213 if self.updates[update].stamp > self.current_update.stamp:
fc5b842de4e9 be a little less aggressive when caching
Jeff Hammel <jhammel@mozilla.com>
parents: 14
diff changeset
214 self.current_update = self.updates[update]
fc5b842de4e9 be a little less aggressive when caching
Jeff Hammel <jhammel@mozilla.com>
parents: 14
diff changeset
215 else:
fc5b842de4e9 be a little less aggressive when caching
Jeff Hammel <jhammel@mozilla.com>
parents: 14
diff changeset
216 self.current_update = self.updates[update]
13
71f9f68986b5 directory scanning + caching
Jeff Hammel <jhammel@mozilla.com>
parents: 12
diff changeset
217
14
cffb6f681b59 things seem to work
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
218 # TODO: could remove old files from the cache if not found in contents
cffb6f681b59 things seem to work
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
219
cffb6f681b59 things seem to work
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
220 return True
cffb6f681b59 things seem to work
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
221
0
b3fd8f98df32 initial stub
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
222 def main(args=sys.argv[1:]):
b3fd8f98df32 initial stub
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
223 """CLI entry point"""
b3fd8f98df32 initial stub
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
224
11
5babc2ae6c27 more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
225 # imports for CLI
0
b3fd8f98df32 initial stub
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
226 import optparse
b3fd8f98df32 initial stub
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
227 from wsgiref import simple_server
b3fd8f98df32 initial stub
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
228
11
5babc2ae6c27 more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
229 # parse CLI options
0
b3fd8f98df32 initial stub
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
230 parser = optparse.OptionParser()
b3fd8f98df32 initial stub
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
231 parser.add_option('-p', '--port', dest='port',
b3fd8f98df32 initial stub
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
232 default=8080, type='int',
b3fd8f98df32 initial stub
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
233 help="port to serve on")
11
5babc2ae6c27 more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
234 parser.add_option('-d', '--directory', dest='directory',
5babc2ae6c27 more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
235 default=os.getcwd(),
5babc2ae6c27 more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
236 help="directory of update files")
0
b3fd8f98df32 initial stub
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
237 options, args = parser.parse_args()
b3fd8f98df32 initial stub
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
238
11
5babc2ae6c27 more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
239 # create the app
5babc2ae6c27 more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
240 app = Dispatcher(directory=options.directory)
0
b3fd8f98df32 initial stub
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
241
11
5babc2ae6c27 more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
242 # serve the app
7
65d0a1153864 a stub that sorta does something
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
243 print "http://localhost:%s/" % options.port
0
b3fd8f98df32 initial stub
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
244 server = simple_server.make_server(host='0.0.0.0', port=options.port, app=app)
b3fd8f98df32 initial stub
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
245 server.serve_forever()
b3fd8f98df32 initial stub
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
246
b3fd8f98df32 initial stub
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
247 if __name__ == '__main__':
b3fd8f98df32 initial stub
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
248 main()