Left: | ||
Right: |
OLD | NEW |
---|---|
1 # coding: utf-8 | |
2 | |
3 # This Source Code is subject to the terms of the Mozilla Public License | 1 # This Source Code is subject to the terms of the Mozilla Public License |
4 # version 2.0 (the "License"). You can obtain a copy of the License at | 2 # version 2.0 (the "License"). You can obtain a copy of the License at |
5 # http://mozilla.org/MPL/2.0/. | 3 # http://mozilla.org/MPL/2.0/. |
6 | 4 |
7 from contextlib import closing | |
8 import os | 5 import os |
9 import platform | 6 import platform |
10 from StringIO import StringIO | 7 import io |
11 import sys | |
12 import urllib | |
13 import zipfile | 8 import zipfile |
14 | 9 |
15 JSSHELL_DIR = "mozilla-esr31" | 10 try: |
16 JSSHELL_URL = ("https://ftp.mozilla.org/pub/mozilla.org/firefox/nightly" | 11 from urllib.request import urlopen |
17 "/2015/02/2015-02-25-00-22-19-%s/jsshell-%%s.zip" % JSSHELL_DIR) | 12 except ImportError: |
13 import urllib2 | |
Sebastian Noack
2016/08/30 11:44:39
It seems more resonable to fallback to urllib2 (ra
Vasily Kuznetsov
2016/08/30 13:16:30
Acknowledged.
| |
14 import contextlib | |
15 | |
16 def urlopen(*args, **kwargs): | |
17 return contextlib.closing(urllib2.urlopen(*args, **kwargs)) | |
18 | |
19 JSSHELL_DIR = 'mozilla-esr31' | |
20 JSSHELL_URL = ('https://ftp.mozilla.org/pub/mozilla.org/firefox/nightly' | |
21 '/2015/02/2015-02-25-00-22-19-{}' | |
22 '/jsshell-{{}}.zip'.format(JSSHELL_DIR)) | |
18 | 23 |
19 JSSHELL_SUPPORTED_PLATFORMS = { | 24 JSSHELL_SUPPORTED_PLATFORMS = { |
20 "win32": "win32", | 25 'Windows': 'win32', |
21 "linux2": { | 26 'Linux': { |
22 "i686": "linux-i686", | 27 'i686': 'linux-i686', |
23 "x86_64": "linux-x86_64" | 28 'x86_64': 'linux-x86_64' |
24 }, | 29 }, |
25 "darwin": "mac" | 30 'Darwin': 'mac' |
26 } | 31 } |
27 | 32 |
28 | 33 |
29 def ensureJSShell(): | 34 def ensureJSShell(): |
30 path = os.environ.get('SPIDERMONKEY_BINARY') | 35 path = os.environ.get('SPIDERMONKEY_BINARY') |
31 if path and os.path.isfile(path): | 36 if path and os.path.isfile(path): |
32 return path | 37 return path |
33 | 38 |
34 baseDir = os.path.dirname(__file__) | 39 baseDir = os.path.dirname(__file__) |
40 system = platform.system() | |
35 | 41 |
36 try: | 42 try: |
37 build = JSSHELL_SUPPORTED_PLATFORMS[sys.platform] | 43 build = JSSHELL_SUPPORTED_PLATFORMS[system] |
38 if isinstance(build, dict): | 44 if isinstance(build, dict): |
39 build = build[platform.machine()] | 45 build = build[platform.machine()] |
40 except KeyError: | 46 except KeyError: |
41 raise Exception('Platform %s (%s) not supported by JS shell' % ( | 47 raise Exception('Platform {} ({}) not supported by JS shell'.format( |
42 sys.platform, platform.machine() | 48 system, platform.machine() |
43 )) | 49 )) |
44 | 50 |
45 shell_dir = os.path.join(baseDir, JSSHELL_DIR + "-" + build) | 51 shell_dir = os.path.join(baseDir, JSSHELL_DIR + "-" + build) |
46 if not os.path.exists(shell_dir): | 52 if not os.path.exists(shell_dir): |
47 os.makedirs(shell_dir) | 53 os.makedirs(shell_dir) |
48 if sys.platform == 'win32': | 54 if system == 'Windows': |
49 path = os.path.join(shell_dir, 'js.exe') | 55 path = os.path.join(shell_dir, 'js.exe') |
50 else: | 56 else: |
51 path = os.path.join(shell_dir, 'js') | 57 path = os.path.join(shell_dir, 'js') |
52 | 58 |
53 if os.path.exists(path): | 59 if os.path.exists(path): |
54 return path | 60 return path |
55 | 61 |
56 with closing(urllib.urlopen(JSSHELL_URL % build)) as response: | 62 with urlopen(JSSHELL_URL.format(build)) as response: |
57 data = response.read() | 63 data = response.read() |
58 | 64 |
59 with zipfile.ZipFile(StringIO(data)) as zip: | 65 with zipfile.ZipFile(io.BytesIO(data)) as archive: |
60 zip.extractall(shell_dir) | 66 archive.extractall(shell_dir) |
61 | 67 |
62 if not os.path.exists(path): | 68 if not os.path.exists(path): |
63 raise Exception('Downloaded package didn\'t contain JS shell executable' ) | 69 raise Exception('Downloaded package didn\'t contain JS shell executable' ) |
64 | 70 |
65 try: | 71 try: |
66 os.chmod(path, 0700) | 72 os.chmod(path, 0o700) |
67 except: | 73 except: |
68 pass | 74 pass |
69 | 75 |
70 return path | 76 return path |
OLD | NEW |