Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: globals/get_browser_versions.py

Issue 6702768332996608: Issue 2432 - Auto-generate browser versions on requirements page (Closed)
Patch Set: Got rid of CMS_CACHE_DIR variable in favor of Source.get_cache_dir() Created April 30, 2015, 2:33 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« .hgignore ('K') | « .hgignore ('k') | pages/requirements.tmpl » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: globals/get_browser_versions.py
===================================================================
new file mode 100644
--- /dev/null
+++ b/globals/get_browser_versions.py
@@ -0,0 +1,182 @@
+import re
+import os
+import sys
+import json
+import urllib2
+import errno
+from xml.dom import minidom
+
+from jinja2 import contextfunction
+
+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]
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.
+
+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'),
+ ]
+ }
+
+def get_seamonkey_version(channel, build):
+ update = get_mozilla_update('aus2-community', 'SeaMonkey', '2.32', build, channel)
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
+ return re.search(r'^^\d+\.\d+', update.getAttribute('version')).group(0)
+
+def get_chrome_version(manifest):
+ return manifest.getAttribute('version').split('.')[0]
+
+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_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 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+')
+
+class BrowserVersions:
+ def get_firefox_versions(self):
+ return get_mozilla_versions('Firefox', '37.0')
+
+ def get_thunderbird_versions(self):
+ return get_mozilla_versions('Thunderbird', '31.0')
+
+ def get_seamonkey_versions(self):
+ return {
+ 'current': get_seamonkey_version('release', '20150112201917'),
+ 'unreleased': [get_seamonkey_version('beta', '20150101215737')]
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:
+ }
+
+ def get_chrome_versions(self):
+ 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:])
+ }
+
+ def get_opera_versions(self):
+ return {
+ 'current': get_opera_version('Stable'),
+ 'unreleased': [
+ get_opera_version('Beta'),
+ get_opera_version('Developer')
+ ]
+ }
+
+ def get_yandex_versions(self):
+ return {
+ 'current': get_yandex_version(''),
+ 'unreleased': [get_yandex_version('-beta')]
+ }
+
+ @contextfunction
+ def get_versions(self, context, browser):
+ method = getattr(self, 'get_%s_versions' % browser)
+ exception = None
+ try:
+ versions = method()
+ 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 = {}
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
+
+ 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
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
+ 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
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
+
+ # 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]
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
+
+ cache[browser] = versions
+ file.seek(0)
Wladimir Palant 2015/04/30 15:34:47 Truncate file?
Sebastian Noack 2015/04/30 20:26:20 Done.
+ json.dump(cache, file)
+
+ return versions
+
+get_browser_versions = BrowserVersions().get_versions
« .hgignore ('K') | « .hgignore ('k') | pages/requirements.tmpl » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld