LEFT | RIGHT |
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 from xml.dom import minidom | 8 from xml.dom import minidom |
8 | 9 |
9 CACHE_FILENAME = os.path.join(CMS_CACHE_DIR, 'browsers.json') | 10 from jinja2 import contextfunction |
| 11 |
| 12 BROWSERS = {} |
10 | 13 |
11 CHROME_UPDATE_XML = '''\ | 14 CHROME_UPDATE_XML = '''\ |
12 <?xml version="1.0" encoding="UTF-8"?> | 15 <?xml version="1.0" encoding="UTF-8"?> |
13 <request protocol="3.0" ismachine="0"> | 16 <request protocol="3.0" ismachine="0"> |
14 <os platform="win" version="99" arch="x64"/> | 17 <os platform="win" version="99" arch="x64"/> |
15 <app appid="{4DC8B4CA-1BDA-483E-B5FA-D3C12E15B62D}"> | 18 <app appid="{4DC8B4CA-1BDA-483E-B5FA-D3C12E15B62D}"> |
16 <updatecheck/> | 19 <updatecheck/> |
17 </app> | 20 </app> |
18 <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"
> |
19 <updatecheck/> | 22 <updatecheck/> |
20 </app> | 23 </app> |
21 <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"> |
22 <updatecheck/> | 25 <updatecheck/> |
23 </app> | 26 </app> |
24 </request>''' | 27 </request>''' |
25 | 28 |
26 def get_mozilla_update(subdomain, product, version, build, channel): | 29 def get_mozilla_version(product, origin_version, channel, |
27 response = urllib2.urlopen('https://%s.mozilla.org/update/3/%s/%s/%s/WINNT_x86
-msvc/en-US/%s/-/default/default/update.xml?force=1' % (subdomain, product, vers
ion, build, channel)) | 30 minor=False, subdomain='aus4', origin_build='-', |
| 31 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' % ( |
| 33 subdomain, |
| 34 product, |
| 35 origin_version, |
| 36 origin_build, |
| 37 platform, |
| 38 channel |
| 39 )) |
28 try: | 40 try: |
29 doc = minidom.parse(response) | 41 doc = minidom.parse(response) |
30 finally: | 42 finally: |
31 response.close() | 43 response.close() |
32 | 44 |
33 return doc.getElementsByTagName('update')[0] | 45 update = doc.getElementsByTagName('update')[0] |
34 | 46 full_version = update.getAttribute(attribute) |
35 def get_mozilla_version(product, version, channel): | 47 |
36 update = get_mozilla_update('aus4', product, version, '-', channel) | 48 match = re.search(r'^(\d+)(?:\.\d+)?', full_version) |
37 return update.getAttribute('appVersion').split('.')[0] | 49 if minor: |
38 | 50 return match.group(0) |
39 def get_mozilla_versions(product, version): | 51 return match.group(1) |
40 return { | 52 |
41 'current': get_mozilla_version(product, version, 'release'), | 53 def get_mozilla_versions(product, origin_version, release_minor=False): |
| 54 return { |
| 55 'current': get_mozilla_version(product, origin_version, 'release', release_m
inor), |
42 'unreleased': [ | 56 'unreleased': [ |
43 get_mozilla_version(product, version, 'beta'), | 57 get_mozilla_version(product, origin_version, 'beta'), |
44 get_mozilla_version(product, version, 'aurora'), | 58 get_mozilla_version(product, origin_version, 'aurora'), |
45 get_mozilla_version(product, version, 'nightly'), | 59 get_mozilla_version(product, origin_version, 'nightly'), |
46 ] | 60 ] |
47 } | 61 } |
48 | 62 |
49 def get_seamonkey_version(channel, build): | 63 BROWSERS['firefox'] = lambda: get_mozilla_versions('Firefox', '37.0') |
50 update = get_mozilla_update('aus2-community', 'SeaMonkey', '2.32', build, chan
nel) | 64 BROWSERS['thunderbird'] = lambda: get_mozilla_versions('Thunderbird', '31.0', Tr
ue) |
51 return re.search(r'^^\d+\.\d+', update.getAttribute('version')).group(0) | 65 |
| 66 def get_seamonkey_version(origin_version, origin_build, channel, **kw): |
| 67 return get_mozilla_version('SeaMonkey', origin_version, channel, True, |
| 68 'aus2-community', origin_build, 'version', **kw) |
| 69 |
| 70 def get_seamonkey_versions(): |
| 71 return { |
| 72 'current': get_seamonkey_version('2.32', '20150112201917', 'release'), |
| 73 'unreleased': [ |
| 74 get_seamonkey_version('2.32', '20150101215737', 'beta'), |
| 75 |
| 76 # Aurora and Nightly builds for Windows are currently broken. |
| 77 # https://bugzilla.mozilla.org/show_bug.cgi?id=1086553 |
| 78 get_seamonkey_version('2.32', '-', 'aurora', platform='Linux_x86-gcc3'), |
| 79 get_seamonkey_version('2.32', '-', 'nightly', platform='Linux_x86-gcc3') |
| 80 ] |
| 81 } |
| 82 |
| 83 BROWSERS['seamonkey'] = get_seamonkey_versions |
52 | 84 |
53 def get_chrome_version(manifest): | 85 def get_chrome_version(manifest): |
54 return manifest.getAttribute('version').split('.')[0] | 86 return manifest.getAttribute('version').split('.')[0] |
55 | 87 |
| 88 def get_chrome_versions(): |
| 89 response = urllib2.urlopen(urllib2.Request('https://tools.google.com/service/u
pdate2', CHROME_UPDATE_XML)) |
| 90 try: |
| 91 doc = minidom.parse(response) |
| 92 finally: |
| 93 response.close() |
| 94 |
| 95 manifests = doc.getElementsByTagName('manifest') |
| 96 return { |
| 97 'current': get_chrome_version(manifests[0]), |
| 98 'unreleased': map(get_chrome_version, manifests[1:]) |
| 99 } |
| 100 |
| 101 BROWSERS['chrome'] = get_chrome_versions |
| 102 |
56 def get_opera_version(channel): | 103 def get_opera_version(channel): |
57 response = urllib2.urlopen('https://autoupdate.geo.opera.com/netinstaller/' +
channel) | 104 response = urllib2.urlopen('https://autoupdate.geo.opera.com/netinstaller/' +
channel) |
58 try: | 105 try: |
59 spec = json.load(response) | 106 spec = json.load(response) |
60 finally: | 107 finally: |
61 response.close() | 108 response.close() |
62 | 109 |
63 return re.search(r'\d+', spec['installer_filename']).group(0) | 110 return re.search(r'\d+', spec['installer_filename']).group(0) |
| 111 |
| 112 def get_opera_versions(): |
| 113 return { |
| 114 'current': get_opera_version('Stable'), |
| 115 'unreleased': [ |
| 116 get_opera_version('Beta'), |
| 117 get_opera_version('Developer') |
| 118 ] |
| 119 } |
| 120 |
| 121 BROWSERS['opera'] = get_opera_versions |
64 | 122 |
65 def get_yandex_version(suffix): | 123 def get_yandex_version(suffix): |
66 response = urllib2.urlopen('https://api.browser.yandex.ru/update-info/browser/
yandex%s/win-yandex.xml' % suffix) | 124 response = urllib2.urlopen('https://api.browser.yandex.ru/update-info/browser/
yandex%s/win-yandex.xml' % suffix) |
67 try: | 125 try: |
68 doc = minidom.parse(response) | 126 doc = minidom.parse(response) |
69 finally: | 127 finally: |
70 response.close() | 128 response.close() |
71 | 129 |
72 item = doc.getElementsByTagName('item')[0] | 130 item = doc.getElementsByTagName('item')[0] |
73 description = item.getElementsByTagName('description')[0] | 131 description = item.getElementsByTagName('description')[0] |
74 return re.search(r'\d+\.\d+', description.firstChild.nodeValue).group(0) | 132 return re.search(r'\d+\.\d+', description.firstChild.nodeValue).group(0) |
75 | 133 |
76 def open_cache_file(): | 134 def get_yandex_versions(): |
| 135 return { |
| 136 'current': get_yandex_version(''), |
| 137 'unreleased': [get_yandex_version('-beta')] |
| 138 } |
| 139 |
| 140 BROWSERS['yandex'] = get_yandex_versions |
| 141 |
| 142 def open_cache_file(filename): |
77 flags = os.O_RDWR | os.O_CREAT | 143 flags = os.O_RDWR | os.O_CREAT |
78 try: | 144 try: |
79 fd = os.open(CACHE_FILENAME, flags) | 145 fd = os.open(filename, flags) |
80 except OSError as e: | 146 except OSError as e: |
81 if e.errno != errno.ENOENT: | 147 if e.errno != errno.ENOENT: |
82 raise | 148 raise |
83 os.makedirs(os.path.dirname(CACHE_FILENAME)) | 149 os.makedirs(os.path.dirname(filename)) |
84 fd = os.open(CACHE_FILENAME, flags) | 150 fd = os.open(filename, flags) |
85 return os.fdopen(fd, 'w+') | 151 return os.fdopen(fd, 'w+') |
86 | 152 |
87 class BrowserVersions: | 153 @contextfunction |
88 def get_firefox_versions(self): | 154 def get_browser_versions(context, browser): |
89 return get_mozilla_versions('Firefox', '37.0') | 155 func = BROWSERS[browser] |
90 | 156 exc_info = None |
91 def get_thunderbird_versions(self): | 157 try: |
92 return get_mozilla_versions('Thunderbird', '31.0') | 158 versions = func() |
93 | 159 except Exception: |
94 def get_seamonkey_versions(self): | 160 exc_info = sys.exc_info() |
95 return { | 161 |
96 'current': get_seamonkey_version('release', '20150112201917'), | 162 filename = os.path.join(context['source'].get_cache_dir(), 'browsers.json') |
97 'unreleased': [get_seamonkey_version('beta', '20150101215737')] | 163 with open_cache_file(filename) as file: |
98 } | |
99 | |
100 def get_chrome_versions(self): | |
101 response = urllib2.urlopen(urllib2.Request('https://tools.google.com/service
/update2', CHROME_UPDATE_XML)) | |
102 try: | 164 try: |
103 doc = minidom.parse(response) | 165 cache = json.load(file) |
104 finally: | 166 except ValueError: |
105 response.close() | 167 if file.tell() > 0: |
106 | 168 raise |
107 manifests = doc.getElementsByTagName('manifest') | 169 cache = {} |
108 return { | 170 |
109 'current': get_chrome_version(manifests[0]), | 171 cached_versions = cache.get(browser) |
110 'unreleased': map(get_chrome_version, manifests[1:]) | 172 if exc_info: |
111 } | 173 if not cached_versions: |
112 | 174 raise exc_info[0], exc_info[1], exc_info[2] |
113 def get_opera_versions(self): | 175 |
114 return { | 176 versions = cached_versions |
115 'current': get_opera_version('Stable'), | 177 logging.warning('Failed to get %s versions, falling back to ' |
116 'unreleased': [ | 178 'cached versions', browser, exc_info=exc_info) |
117 get_opera_version('Beta'), | 179 else: |
118 get_opera_version('Developer') | |
119 ] | |
120 } | |
121 | |
122 def get_yandex_versions(self): | |
123 return { | |
124 'current': get_yandex_version(''), | |
125 'unreleased': [get_yandex_version('-beta')] | |
126 } | |
127 | |
128 def get_versions(self, browser): | |
129 method = getattr(self, 'get_%s_versions' % browser) | |
130 exception = None | |
131 try: | |
132 versions = method() | |
133 except Exception as e: | |
134 exception = e | |
135 | |
136 with open_cache_file() as file: | |
137 try: | |
138 cache = json.load(file) | |
139 except ValueError: | |
140 if file.tell() > 0: | |
141 raise | |
142 cache = {} | |
143 | |
144 cached_versions = cache.get(browser) | |
145 if exception: | |
146 if not cached_versions: | |
147 raise exception | |
148 | |
149 print >>sys.stderr, "Warning: Failed to get %s versions, falling back to
cached versions" % browser | |
150 return cached_versions | |
151 | |
152 # Determine previous version: If we recorded the version before and it | 180 # Determine previous version: If we recorded the version before and it |
153 # changed since then, the old current version becomes the new previous | 181 # changed since then, the old current version becomes the new previous |
154 # version. If the version didn't change, use the cached previous version. | 182 # version. If the version didn't change, use the cached previous version. |
155 current = versions['current'] | 183 current = versions['current'] |
| 184 previous = None |
156 if cached_versions: | 185 if cached_versions: |
157 cached_current = cached_versions['current'] | 186 cached_current = cached_versions['current'] |
158 if cached_current != current: | 187 if cached_current != current: |
159 versions['previous'] = cached_current | 188 previous = cached_current |
160 else: | 189 else: |
161 cached_previous = cached_versions.get('previous') | 190 previous = cached_versions['previous'] |
162 if cached_previous: | 191 versions['previous'] = previous |
163 versions['previous'] = cached_previous | 192 |
164 | 193 # Remove duplicates from unreleased versions. Occasionally, |
165 # Remove duplicated from unreleased versions. Occasionally, | |
166 # different channels are on the same version, but we want | 194 # different channels are on the same version, but we want |
167 # to list each version only once. | 195 # to list each version only once. |
168 unreleased = versions['unreleased'] | 196 versions['unreleased'] = sorted( |
169 previous = versions.get('previous') | 197 set(versions['unreleased']) - {current, previous}, |
170 for i, version in list(enumerate(unreleased))[::-1]: | 198 key=lambda ver: map(int, ver.split('.')) |
171 if version == current or previous and version == previous: | 199 ) |
172 del unreleased[i] | |
173 | 200 |
174 cache[browser] = versions | 201 cache[browser] = versions |
175 file.seek(0) | 202 file.seek(0) |
176 json.dump(cache, file) | 203 json.dump(cache, file) |
177 | 204 file.truncate() |
178 return versions | 205 |
179 | 206 if not versions['previous']: |
180 get_browser_versions = BrowserVersions().get_versions | 207 logging.warning("Couldn't determine previous browser version, " |
| 208 'please set %s.previous in %s', browser, filename) |
| 209 |
| 210 return versions |
LEFT | RIGHT |