OLD | NEW |
1 import re | 1 import re |
2 import os | 2 import os |
3 import sys | 3 import sys |
4 import json | 4 import json |
5 import urllib2 | 5 import urllib2 |
6 import errno | 6 import errno |
7 import logging | 7 import logging |
8 from xml.dom import minidom | 8 from xml.dom import minidom |
9 | 9 |
10 from jinja2 import contextfunction | 10 from jinja2 import contextfunction |
11 | 11 |
12 BROWSERS = {} | 12 BROWSERS = {} |
13 | 13 |
14 CHROME_UPDATE_XML = '''\ | 14 CHROME_UPDATE_XML = '''\ |
15 <?xml version="1.0" encoding="UTF-8"?> | 15 <?xml version="1.0" encoding="UTF-8"?> |
16 <request protocol="3.0" ismachine="0"> | 16 <request protocol="3.0" ismachine="0"> |
17 <os platform="win" version="99" arch="x64"/> | 17 <os platform="win" version="99" arch="x64"/> |
18 <app appid="{4DC8B4CA-1BDA-483E-B5FA-D3C12E15B62D}"> | 18 <app appid="{4DC8B4CA-1BDA-483E-B5FA-D3C12E15B62D}"> |
19 <updatecheck/> | 19 <updatecheck/> |
20 </app> | 20 </app> |
21 <app appid="{4DC8B4CA-1BDA-483E-B5FA-D3C12E15B62D}" ap="x64-beta-multi-chrome"
> | 21 <app appid="{4DC8B4CA-1BDA-483E-B5FA-D3C12E15B62D}" ap="x64-beta-multi-chrome"
> |
22 <updatecheck/> | 22 <updatecheck/> |
23 </app> | 23 </app> |
24 <app appid="{4DC8B4CA-1BDA-483E-B5FA-D3C12E15B62D}" ap="x64-dev-multi-chrome"> | 24 <app appid="{4DC8B4CA-1BDA-483E-B5FA-D3C12E15B62D}" ap="x64-dev-multi-chrome"> |
25 <updatecheck/> | 25 <updatecheck/> |
26 </app> | 26 </app> |
27 </request>''' | 27 </request>''' |
28 | 28 |
| 29 cache = {} |
| 30 |
29 def get_mozilla_version(product, origin_version, channel, | 31 def get_mozilla_version(product, origin_version, channel, |
30 minor=False, subdomain='aus4', origin_build='-', | 32 minor=False, subdomain='aus4', origin_build='-', |
31 attribute='appVersion', platform='WINNT_x86-msvc'): | 33 attribute='appVersion', platform='WINNT_x86-msvc'): |
32 response = urllib2.urlopen('https://%s.mozilla.org/update/3/%s/%s/%s/%s/en-US/
%s/-/default/default/update.xml?force=1' % ( | 34 response = urllib2.urlopen('https://%s.mozilla.org/update/3/%s/%s/%s/%s/en-US/
%s/-/default/default/update.xml?force=1' % ( |
33 subdomain, | 35 subdomain, |
34 product, | 36 product, |
35 origin_version, | 37 origin_version, |
36 origin_build, | 38 origin_build, |
37 platform, | 39 platform, |
38 channel | 40 channel |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
150 fd = os.open(filename, flags) | 152 fd = os.open(filename, flags) |
151 except OSError as e: | 153 except OSError as e: |
152 if e.errno != errno.ENOENT: | 154 if e.errno != errno.ENOENT: |
153 raise | 155 raise |
154 os.makedirs(os.path.dirname(filename)) | 156 os.makedirs(os.path.dirname(filename)) |
155 fd = os.open(filename, flags) | 157 fd = os.open(filename, flags) |
156 return os.fdopen(fd, 'w+') | 158 return os.fdopen(fd, 'w+') |
157 | 159 |
158 @contextfunction | 160 @contextfunction |
159 def get_browser_versions(context, browser): | 161 def get_browser_versions(context, browser): |
| 162 versions = cache.get(browser) |
| 163 if versions: |
| 164 return versions |
| 165 |
160 func = BROWSERS[browser] | 166 func = BROWSERS[browser] |
161 exc_info = None | 167 exc_info = None |
162 try: | 168 try: |
163 versions = func() | 169 versions = func() |
164 except Exception: | 170 except Exception: |
165 exc_info = sys.exc_info() | 171 exc_info = sys.exc_info() |
166 | 172 |
167 filename = os.path.join(context['source'].get_cache_dir(), 'browsers.json') | 173 filename = os.path.join(context['source'].get_cache_dir(), 'browsers.json') |
168 with open_cache_file(filename) as file: | 174 with open_cache_file(filename) as file: |
169 try: | 175 try: |
170 cache = json.load(file) | 176 persistent_cache = json.load(file) |
171 except ValueError: | 177 except ValueError: |
172 if file.tell() > 0: | 178 if file.tell() > 0: |
173 raise | 179 raise |
174 cache = {} | 180 persistent_cache = {} |
175 | 181 |
176 cached_versions = cache.get(browser) | 182 cached_versions = persistent_cache.get(browser) |
177 if exc_info: | 183 if exc_info: |
178 if not cached_versions: | 184 if not cached_versions: |
179 raise exc_info[0], exc_info[1], exc_info[2] | 185 raise exc_info[0], exc_info[1], exc_info[2] |
180 | 186 |
181 versions = cached_versions | 187 versions = cached_versions |
182 logging.warning('Failed to get %s versions, falling back to ' | 188 logging.warning('Failed to get %s versions, falling back to ' |
183 'cached versions', browser, exc_info=exc_info) | 189 'cached versions', browser, exc_info=exc_info) |
184 else: | 190 else: |
185 # Determine previous version: If we recorded the version before and it | 191 # Determine previous version: If we recorded the version before and it |
186 # changed since then, the old current version becomes the new previous | 192 # changed since then, the old current version becomes the new previous |
187 # version. If the version didn't change, use the cached previous version. | 193 # version. If the version didn't change, use the cached previous version. |
188 current = versions['current'] | 194 current = versions['current'] |
189 previous = None | 195 previous = None |
190 if cached_versions: | 196 if cached_versions: |
191 cached_current = cached_versions['current'] | 197 cached_current = cached_versions['current'] |
192 if cached_current != current: | 198 if cached_current != current: |
193 previous = cached_current | 199 previous = cached_current |
194 else: | 200 else: |
195 previous = cached_versions['previous'] | 201 previous = cached_versions['previous'] |
196 versions['previous'] = previous | 202 versions['previous'] = previous |
197 | 203 |
198 # Remove duplicates from unreleased versions. Occasionally, | 204 # Remove duplicates from unreleased versions. Occasionally, |
199 # different channels are on the same version, but we want | 205 # different channels are on the same version, but we want |
200 # to list each version only once. | 206 # to list each version only once. |
201 versions['unreleased'] = sorted( | 207 versions['unreleased'] = sorted( |
202 set(versions['unreleased']) - {current, previous}, | 208 set(versions['unreleased']) - {current, previous}, |
203 key=lambda ver: map(int, ver.split('.')) | 209 key=lambda ver: map(int, ver.split('.')) |
204 ) | 210 ) |
205 | 211 |
206 cache[browser] = versions | 212 persistent_cache[browser] = versions |
207 file.seek(0) | 213 file.seek(0) |
208 json.dump(cache, file) | 214 json.dump(persistent_cache, file) |
209 file.truncate() | 215 file.truncate() |
210 | 216 |
211 if not versions['previous']: | 217 if not versions['previous']: |
212 logging.warning("Couldn't determine previous browser version, " | 218 logging.warning("Couldn't determine previous browser version, " |
213 'please set %s.previous in %s', browser, filename) | 219 'please set %s.previous in %s', browser, filename) |
214 | 220 |
| 221 cache[browser] = versions |
215 return versions | 222 return versions |
OLD | NEW |