| Left: | ||
| Right: |
| 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 from jinja2 import contextfunction | 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] |
|
Wladimir Palant
2015/04/30 15:34:47
Unfortunately, you forgot my comment on code local
Sebastian Noack
2015/04/30 20:26:20
Here you have your code locality. ;P
Wladimir Palant
2015/05/13 13:17:23
Thank you, much better.
| |
| 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) |
|
Wladimir Palant
2015/04/30 15:34:47
Build ID is tied to the version number - if you ar
Sebastian Noack
2015/04/30 20:26:20
Well, I just tried to don't repeat myself. But it
| |
| 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(source): | 134 def get_yandex_versions(): |
| 77 filename = os.path.join(source.get_cache_dir(), 'browsers.json') | 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): | |
| 78 flags = os.O_RDWR | os.O_CREAT | 143 flags = os.O_RDWR | os.O_CREAT |
| 79 try: | 144 try: |
| 80 fd = os.open(filename, flags) | 145 fd = os.open(filename, flags) |
| 81 except OSError as e: | 146 except OSError as e: |
| 82 if e.errno != errno.ENOENT: | 147 if e.errno != errno.ENOENT: |
| 83 raise | 148 raise |
| 84 os.makedirs(os.path.dirname(filename)) | 149 os.makedirs(os.path.dirname(filename)) |
| 85 fd = os.open(filename, flags) | 150 fd = os.open(filename, flags) |
| 86 return os.fdopen(fd, 'w+') | 151 return os.fdopen(fd, 'w+') |
| 87 | 152 |
| 88 class BrowserVersions: | 153 @contextfunction |
| 89 def get_firefox_versions(self): | 154 def get_browser_versions(context, browser): |
| 90 return get_mozilla_versions('Firefox', '37.0') | 155 func = BROWSERS[browser] |
| 91 | 156 exc_info = None |
| 92 def get_thunderbird_versions(self): | 157 try: |
| 93 return get_mozilla_versions('Thunderbird', '31.0') | 158 versions = func() |
| 94 | 159 except Exception: |
| 95 def get_seamonkey_versions(self): | 160 exc_info = sys.exc_info() |
| 96 return { | 161 |
| 97 'current': get_seamonkey_version('release', '20150112201917'), | 162 filename = os.path.join(context['source'].get_cache_dir(), 'browsers.json') |
| 98 'unreleased': [get_seamonkey_version('beta', '20150101215737')] | 163 with open_cache_file(filename) as file: |
|
Wladimir Palant
2015/04/30 15:34:47
What about Aurora and Nightly? Note that these upd
Sebastian Noack
2015/04/30 20:26:20
They do not exist.
Wladimir Palant
2015/05/13 13:17:23
That would be news to me.
https://ftp.mozilla.org
Sebastian Noack
2015/05/13 18:35:11
You seem to be right. But for reference, Aurora bu
Wladimir Palant
2015/05/14 19:53:19
Firefox 37 is the current stable version - that wo
Sebastian Noack
2015/05/15 10:24:54
Nighties are back. Now, we get following versions:
| |
| 99 } | |
| 100 | |
| 101 def get_chrome_versions(self): | |
| 102 response = urllib2.urlopen(urllib2.Request('https://tools.google.com/service /update2', CHROME_UPDATE_XML)) | |
| 103 try: | 164 try: |
| 104 doc = minidom.parse(response) | 165 cache = json.load(file) |
| 105 finally: | 166 except ValueError: |
| 106 response.close() | 167 if file.tell() > 0: |
| 107 | 168 raise |
| 108 manifests = doc.getElementsByTagName('manifest') | 169 cache = {} |
| 109 return { | 170 |
| 110 'current': get_chrome_version(manifests[0]), | 171 cached_versions = cache.get(browser) |
| 111 'unreleased': map(get_chrome_version, manifests[1:]) | 172 if exc_info: |
| 112 } | 173 if not cached_versions: |
| 113 | 174 raise exc_info[0], exc_info[1], exc_info[2] |
| 114 def get_opera_versions(self): | 175 |
| 115 return { | 176 versions = cached_versions |
| 116 'current': get_opera_version('Stable'), | 177 logging.warning('Failed to get %s versions, falling back to ' |
| 117 'unreleased': [ | 178 'cached versions', browser, exc_info=exc_info) |
| 118 get_opera_version('Beta'), | 179 else: |
| 119 get_opera_version('Developer') | |
| 120 ] | |
| 121 } | |
| 122 | |
| 123 def get_yandex_versions(self): | |
| 124 return { | |
| 125 'current': get_yandex_version(''), | |
| 126 'unreleased': [get_yandex_version('-beta')] | |
| 127 } | |
| 128 | |
| 129 @contextfunction | |
| 130 def get_versions(self, context, browser): | |
| 131 method = getattr(self, 'get_%s_versions' % browser) | |
| 132 exception = None | |
| 133 try: | |
| 134 versions = method() | |
| 135 except Exception as e: | |
| 136 exception = e | |
| 137 | |
| 138 with open_cache_file(context['source']) as file: | |
| 139 try: | |
| 140 cache = json.load(file) | |
| 141 except ValueError: | |
| 142 if file.tell() > 0: | |
| 143 raise | |
| 144 cache = {} | |
|
Wladimir Palant
2015/04/30 15:34:47
Honestly, that's an extremely complicated way of d
Sebastian Noack
2015/04/30 20:26:20
Yes your approach is slightly simpler, but IMO not
| |
| 145 | |
| 146 cached_versions = cache.get(browser) | |
| 147 if exception: | |
| 148 if not cached_versions: | |
| 149 raise exception | |
| 150 | |
| 151 print >>sys.stderr, "Warning: Failed to get %s versions, falling back to cached versions" % browser | |
|
Wladimir Palant
2015/04/30 15:34:47
Use logging.warning?
Sebastian Noack
2015/04/30 20:26:20
I almost used the logging module. But then I looke
Sebastian Noack
2015/05/05 13:34:35
Done, after making the cms use the logging module
| |
| 152 return cached_versions | |
| 153 | |
| 154 # Determine previous version: If we recorded the version before and it | 180 # Determine previous version: If we recorded the version before and it |
| 155 # changed since then, the old current version becomes the new previous | 181 # changed since then, the old current version becomes the new previous |
| 156 # 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. |
| 157 current = versions['current'] | 183 current = versions['current'] |
| 184 previous = None | |
| 158 if cached_versions: | 185 if cached_versions: |
| 159 cached_current = cached_versions['current'] | 186 cached_current = cached_versions['current'] |
| 160 if cached_current != current: | 187 if cached_current != current: |
| 161 versions['previous'] = cached_current | 188 previous = cached_current |
| 162 else: | 189 else: |
| 163 cached_previous = cached_versions.get('previous') | 190 previous = cached_versions['previous'] |
| 164 if cached_previous: | 191 versions['previous'] = previous |
| 165 versions['previous'] = cached_previous | 192 |
|
Wladimir Palant
2015/04/30 15:34:47
This algorithm won't work for Thunderbird. Here th
Sebastian Noack
2015/04/30 20:26:20
Alternatively, we could stop listening the previou
Sebastian Noack
2015/04/30 22:39:41
Never mind. I found a way to include the minor ver
Sebastian Noack
2015/05/05 13:34:35
I made this and some other checks obsolete, by alw
| |
| 166 | 193 # Remove duplicates from unreleased versions. Occasionally, |
| 167 # Remove duplicated from unreleased versions. Occasionally, | |
| 168 # different channels are on the same version, but we want | 194 # different channels are on the same version, but we want |
| 169 # to list each version only once. | 195 # to list each version only once. |
| 170 unreleased = versions['unreleased'] | 196 versions['unreleased'] = sorted( |
| 171 previous = versions.get('previous') | 197 set(versions['unreleased']) - {current, previous}, |
| 172 for i, version in list(enumerate(unreleased))[::-1]: | 198 key=lambda ver: map(int, ver.split('.')) |
| 173 if version == current or previous and version == previous: | 199 ) |
| 174 del unreleased[i] | |
|
Wladimir Palant
2015/04/30 15:34:47
I doubt that this will work correctly if you need
Sebastian Noack
2015/04/30 20:26:20
It does work correctly for any number of elements,
Sebastian Noack
2015/05/05 13:34:35
Never mind, I am using a set now. As this will not
| |
| 175 | 200 |
| 176 cache[browser] = versions | 201 cache[browser] = versions |
| 177 file.seek(0) | 202 file.seek(0) |
|
Wladimir Palant
2015/04/30 15:34:47
Truncate file?
Sebastian Noack
2015/04/30 20:26:20
Done.
| |
| 178 json.dump(cache, file) | 203 json.dump(cache, file) |
| 179 | 204 file.truncate() |
| 180 return versions | 205 |
| 181 | 206 if not versions['previous']: |
| 182 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 |