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 CACHE_FILENAME = os.path.join(os.path.dirname(os.path.dirname(__file__)), 'cache
', 'browsers.json') |
| 10 |
| 11 CHROME_UPDATE_XML = '''\ |
| 12 <?xml version="1.0" encoding="UTF-8"?> |
| 13 <request protocol="3.0" ismachine="0"> |
| 14 <os platform="win" version="99" arch="x64"/> |
| 15 <app appid="{4DC8B4CA-1BDA-483E-B5FA-D3C12E15B62D}"> |
| 16 <updatecheck/> |
| 17 </app> |
| 18 <app appid="{4DC8B4CA-1BDA-483E-B5FA-D3C12E15B62D}" ap="x64-beta-multi-chrome"
> |
| 19 <updatecheck/> |
| 20 </app> |
| 21 <app appid="{4DC8B4CA-1BDA-483E-B5FA-D3C12E15B62D}" ap="x64-dev-multi-chrome"> |
| 22 <updatecheck/> |
| 23 </app> |
| 24 </request>''' |
| 25 |
| 26 def get_mozilla_update(subdomain, product, version, build, 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)) |
| 28 try: |
| 29 doc = minidom.parse(response) |
| 30 finally: |
| 31 response.close() |
| 32 |
| 33 return doc.getElementsByTagName('update')[0] |
| 34 |
| 35 def get_mozilla_version(product, version, channel): |
| 36 update = get_mozilla_update('aus4', product, version, '-', channel) |
| 37 return update.getAttribute('appVersion').split('.')[0] |
| 38 |
| 39 def get_mozilla_versions(product, version): |
| 40 return { |
| 41 'current': get_mozilla_version(product, version, 'release'), |
| 42 'unreleased': [ |
| 43 get_mozilla_version(product, version, 'beta'), |
| 44 get_mozilla_version(product, version, 'aurora'), |
| 45 get_mozilla_version(product, version, 'nightly'), |
| 46 ] |
| 47 } |
| 48 |
| 49 def get_seamonkey_version(channel, build): |
| 50 update = get_mozilla_update('aus2-community', 'SeaMonkey', '2.32', build, chan
nel) |
| 51 return re.search(r'^^\d+\.\d+', update.getAttribute('version')).group(0) |
| 52 |
| 53 def get_chrome_version(manifest): |
| 54 return manifest.getAttribute('version').split('.')[0] |
| 55 |
| 56 def get_opera_version(channel): |
| 57 response = urllib2.urlopen('https://autoupdate.geo.opera.com/netinstaller/' +
channel) |
| 58 try: |
| 59 spec = json.load(response) |
| 60 finally: |
| 61 response.close() |
| 62 |
| 63 return re.search(r'\d+', spec['installer_filename']).group(0) |
| 64 |
| 65 def get_yandex_version(suffix): |
| 66 response = urllib2.urlopen('https://api.browser.yandex.ru/update-info/browser/
yandex%s/win-yandex.xml' % suffix) |
| 67 try: |
| 68 doc = minidom.parse(response) |
| 69 finally: |
| 70 response.close() |
| 71 |
| 72 item = doc.getElementsByTagName('item')[0] |
| 73 description = item.getElementsByTagName('description')[0] |
| 74 return re.search(r'\d+\.\d+', description.firstChild.nodeValue).group(0) |
| 75 |
| 76 def open_cache_file(): |
| 77 flags = os.O_RDWR | os.O_CREAT |
| 78 try: |
| 79 fd = os.open(CACHE_FILENAME, flags) |
| 80 except OSError as e: |
| 81 if e.errno != errno.ENOENT: |
| 82 raise |
| 83 os.makedirs(os.path.dirname(CACHE_FILENAME)) |
| 84 fd = os.open(CACHE_FILENAME, flags) |
| 85 return os.fdopen(fd, 'w+') |
| 86 |
| 87 class BrowserVersions: |
| 88 def get_firefox_versions(self): |
| 89 return get_mozilla_versions('Firefox', '37.0') |
| 90 |
| 91 def get_thunderbird_versions(self): |
| 92 return get_mozilla_versions('Thunderbird', '31.0') |
| 93 |
| 94 def get_seamonkey_versions(self): |
| 95 return { |
| 96 'current': get_seamonkey_version('release', '20150112201917'), |
| 97 'unreleased': [get_seamonkey_version('beta', '20150101215737')] |
| 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: |
| 103 doc = minidom.parse(response) |
| 104 finally: |
| 105 response.close() |
| 106 |
| 107 manifests = doc.getElementsByTagName('manifest') |
| 108 return { |
| 109 'current': get_chrome_version(manifests[0]), |
| 110 'unreleased': map(get_chrome_version, manifests[1:]) |
| 111 } |
| 112 |
| 113 def get_opera_versions(self): |
| 114 return { |
| 115 'current': get_opera_version('Stable'), |
| 116 'unreleased': [ |
| 117 get_opera_version('Beta'), |
| 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 |
| 153 # changed since then, the old current version becomes the new previous |
| 154 # version. If the version didn't change, use the cached previous version. |
| 155 current = versions['current'] |
| 156 if cached_versions: |
| 157 cached_current = cached_versions['current'] |
| 158 if cached_current != current: |
| 159 versions['previous'] = cached_current |
| 160 else: |
| 161 cached_previous = cached_versions.get('previous') |
| 162 if cached_previous: |
| 163 versions['previous'] = cached_previous |
| 164 |
| 165 # Remove duplicated from unreleased versions. Occasionally, |
| 166 # different channels are on the same version, but we want |
| 167 # to list each version only once. |
| 168 unreleased = versions['unreleased'] |
| 169 previous = versions.get('previous') |
| 170 for i, version in list(enumerate(unreleased))[::-1]: |
| 171 if version == current or previous and version == previous: |
| 172 del unreleased[i] |
| 173 |
| 174 cache[browser] = versions |
| 175 file.seek(0) |
| 176 json.dump(cache, file) |
| 177 |
| 178 return versions |
| 179 |
| 180 get_browser_versions = BrowserVersions().get_versions |
OLD | NEW |