Index: globals/get_browser_versions.py |
=================================================================== |
new file mode 100644 |
--- /dev/null |
+++ b/globals/get_browser_versions.py |
@@ -0,0 +1,187 @@ |
+import re |
+import os |
+import sys |
+import json |
+import urllib2 |
+import errno |
+from xml.dom import minidom |
+ |
+from jinja2 import contextfunction |
+ |
+BROWSERS = {} |
+ |
+CHROME_UPDATE_XML = '''\ |
+<?xml version="1.0" encoding="UTF-8"?> |
+<request protocol="3.0" ismachine="0"> |
+ <os platform="win" version="99" arch="x64"/> |
+ <app appid="{4DC8B4CA-1BDA-483E-B5FA-D3C12E15B62D}"> |
+ <updatecheck/> |
+ </app> |
+ <app appid="{4DC8B4CA-1BDA-483E-B5FA-D3C12E15B62D}" ap="x64-beta-multi-chrome"> |
+ <updatecheck/> |
+ </app> |
+ <app appid="{4DC8B4CA-1BDA-483E-B5FA-D3C12E15B62D}" ap="x64-dev-multi-chrome"> |
+ <updatecheck/> |
+ </app> |
+</request>''' |
+ |
+def get_mozilla_update(subdomain, product, version, build, channel): |
+ 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, version, build, channel)) |
+ try: |
+ doc = minidom.parse(response) |
+ finally: |
+ response.close() |
+ |
+ return doc.getElementsByTagName('update')[0] |
+ |
+def get_mozilla_version(product, version, channel): |
+ update = get_mozilla_update('aus4', product, version, '-', channel) |
+ return update.getAttribute('appVersion').split('.')[0] |
+ |
+def get_mozilla_versions(product, version): |
+ return { |
+ 'current': get_mozilla_version(product, version, 'release'), |
+ 'unreleased': [ |
+ get_mozilla_version(product, version, 'beta'), |
+ get_mozilla_version(product, version, 'aurora'), |
+ get_mozilla_version(product, version, 'nightly'), |
+ ] |
+ } |
+ |
+BROWSERS['firefox'] = lambda: get_mozilla_versions('Firefox', '37.0') |
+BROWSERS['thunderbird'] = lambda: get_mozilla_versions('Thunderbird', '31.0') |
+ |
+def get_seamonkey_version(channel, version, build): |
+ update = get_mozilla_update('aus2-community', 'SeaMonkey', version, build, channel) |
+ return re.search(r'^^\d+\.\d+', update.getAttribute('version')).group(0) |
+ |
+def get_seamonkey_versions(): |
+ return { |
+ 'current': get_seamonkey_version('release', '2.32', '20150112201917'), |
+ 'unreleased': [get_seamonkey_version('beta', '2.32', '20150101215737')] |
+ } |
+ |
+BROWSERS['seamonkey'] = get_seamonkey_versions |
+ |
+def get_chrome_version(manifest): |
+ return manifest.getAttribute('version').split('.')[0] |
+ |
+def get_chrome_versions(): |
+ response = urllib2.urlopen(urllib2.Request('https://tools.google.com/service/update2', CHROME_UPDATE_XML)) |
+ try: |
+ doc = minidom.parse(response) |
+ finally: |
+ response.close() |
+ |
+ manifests = doc.getElementsByTagName('manifest') |
+ return { |
+ 'current': get_chrome_version(manifests[0]), |
+ 'unreleased': map(get_chrome_version, manifests[1:]) |
+ } |
+ |
+BROWSERS['chrome'] = get_chrome_versions |
+ |
+def get_opera_version(channel): |
+ response = urllib2.urlopen('https://autoupdate.geo.opera.com/netinstaller/' + channel) |
+ try: |
+ spec = json.load(response) |
+ finally: |
+ response.close() |
+ |
+ return re.search(r'\d+', spec['installer_filename']).group(0) |
+ |
+def get_opera_versions(): |
+ return { |
+ 'current': get_opera_version('Stable'), |
+ 'unreleased': [ |
+ get_opera_version('Beta'), |
+ get_opera_version('Developer') |
+ ] |
+ } |
+ |
+BROWSERS['opera'] = get_opera_versions |
+ |
+def get_yandex_version(suffix): |
+ response = urllib2.urlopen('https://api.browser.yandex.ru/update-info/browser/yandex%s/win-yandex.xml' % suffix) |
+ try: |
+ doc = minidom.parse(response) |
+ finally: |
+ response.close() |
+ |
+ item = doc.getElementsByTagName('item')[0] |
+ description = item.getElementsByTagName('description')[0] |
+ return re.search(r'\d+\.\d+', description.firstChild.nodeValue).group(0) |
+ |
+def get_yandex_versions(): |
+ return { |
+ 'current': get_yandex_version(''), |
+ 'unreleased': [get_yandex_version('-beta')] |
+ } |
+ |
+BROWSERS['yandex'] = get_yandex_versions |
+ |
+def open_cache_file(source): |
+ filename = os.path.join(source.get_cache_dir(), 'browsers.json') |
+ flags = os.O_RDWR | os.O_CREAT |
+ try: |
+ fd = os.open(filename, flags) |
+ except OSError as e: |
+ if e.errno != errno.ENOENT: |
+ raise |
+ os.makedirs(os.path.dirname(filename)) |
+ fd = os.open(filename, flags) |
+ return os.fdopen(fd, 'w+') |
+ |
+@contextfunction |
+def get_browser_versions(context, browser): |
+ func = BROWSERS[browser] |
+ exception = None |
+ try: |
+ versions = func() |
+ except Exception as e: |
+ exception = e |
+ |
+ with open_cache_file(context['source']) as file: |
+ try: |
+ cache = json.load(file) |
+ except ValueError: |
+ if file.tell() > 0: |
+ raise |
+ cache = {} |
+ |
+ cached_versions = cache.get(browser) |
+ if exception: |
+ if not cached_versions: |
+ raise exception |
+ |
+ print >>sys.stderr, "Warning: Failed to get %s versions, falling back to cached versions" % browser |
+ return cached_versions |
+ |
+ # Determine previous version: If we recorded the version before and it |
+ # changed since then, the old current version becomes the new previous |
+ # version. If the version didn't change, use the cached previous version. |
+ current = versions['current'] |
+ if cached_versions: |
+ cached_current = cached_versions['current'] |
+ if cached_current != current: |
+ versions['previous'] = cached_current |
+ else: |
+ cached_previous = cached_versions.get('previous') |
+ if cached_previous: |
+ versions['previous'] = cached_previous |
+ |
+ # Remove duplicated from unreleased versions. Occasionally, |
+ # different channels are on the same version, but we want |
+ # to list each version only once. |
+ unreleased = versions['unreleased'] |
+ previous = versions.get('previous') |
+ for i, version in list(enumerate(unreleased))[::-1]: |
+ if version == current or previous and version == previous: |
+ del unreleased[i] |
+ |
+ cache[browser] = versions |
+ file.seek(0) |
+ json.dump(cache, file) |
+ file.truncate() |
+ |
+ return versions |