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