Left: | ||
Right: |
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 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] | |
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 | |
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) | |
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) | |
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(source): | |
77 filename = os.path.join(source.get_cache_dir(), 'browsers.json') | |
78 flags = os.O_RDWR | os.O_CREAT | |
79 try: | |
80 fd = os.open(filename, flags) | |
81 except OSError as e: | |
82 if e.errno != errno.ENOENT: | |
83 raise | |
84 os.makedirs(os.path.dirname(filename)) | |
85 fd = os.open(filename, flags) | |
86 return os.fdopen(fd, 'w+') | |
87 | |
88 class BrowserVersions: | |
89 def get_firefox_versions(self): | |
90 return get_mozilla_versions('Firefox', '37.0') | |
91 | |
92 def get_thunderbird_versions(self): | |
93 return get_mozilla_versions('Thunderbird', '31.0') | |
94 | |
95 def get_seamonkey_versions(self): | |
96 return { | |
97 'current': get_seamonkey_version('release', '20150112201917'), | |
98 '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:
| |
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: | |
104 doc = minidom.parse(response) | |
105 finally: | |
106 response.close() | |
107 | |
108 manifests = doc.getElementsByTagName('manifest') | |
109 return { | |
110 'current': get_chrome_version(manifests[0]), | |
111 'unreleased': map(get_chrome_version, manifests[1:]) | |
112 } | |
113 | |
114 def get_opera_versions(self): | |
115 return { | |
116 'current': get_opera_version('Stable'), | |
117 'unreleased': [ | |
118 get_opera_version('Beta'), | |
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 | |
155 # changed since then, the old current version becomes the new previous | |
156 # version. If the version didn't change, use the cached previous version. | |
157 current = versions['current'] | |
158 if cached_versions: | |
159 cached_current = cached_versions['current'] | |
160 if cached_current != current: | |
161 versions['previous'] = cached_current | |
162 else: | |
163 cached_previous = cached_versions.get('previous') | |
164 if cached_previous: | |
165 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
| |
166 | |
167 # Remove duplicated from unreleased versions. Occasionally, | |
168 # different channels are on the same version, but we want | |
169 # to list each version only once. | |
170 unreleased = versions['unreleased'] | |
171 previous = versions.get('previous') | |
172 for i, version in list(enumerate(unreleased))[::-1]: | |
173 if version == current or previous and version == previous: | |
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 | |
176 cache[browser] = versions | |
177 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) | |
179 | |
180 return versions | |
181 | |
182 get_browser_versions = BrowserVersions().get_versions | |
OLD | NEW |