LEFT | RIGHT |
1 # 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 |
2 # 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 |
3 # http://mozilla.org/MPL/2.0/. | 3 # http://mozilla.org/MPL/2.0/. |
4 | 4 |
5 from __future__ import print_function | |
6 | |
7 import os | 5 import os |
| 6 import platform |
| 7 import io |
| 8 import zipfile |
8 import subprocess | 9 import subprocess |
9 | 10 |
10 import utils | 11 try: |
| 12 from urllib.request import urlopen |
| 13 except ImportError: |
| 14 import urllib2 |
| 15 import contextlib |
| 16 |
| 17 def urlopen(*args, **kwargs): |
| 18 return contextlib.closing(urllib2.urlopen(*args, **kwargs)) |
| 19 |
| 20 JSSHELL_DIR = 'mozilla-esr31' |
| 21 JSSHELL_URL = ('https://ftp.mozilla.org/pub/mozilla.org/firefox/nightly' |
| 22 '/2015/02/2015-02-25-00-22-19-{}' |
| 23 '/jsshell-{{}}.zip'.format(JSSHELL_DIR)) |
| 24 |
| 25 JSSHELL_SUPPORTED_PLATFORMS = { |
| 26 'Windows': 'win32', |
| 27 'Linux': { |
| 28 'i686': 'linux-i686', |
| 29 'x86_64': 'linux-x86_64' |
| 30 }, |
| 31 'Darwin': 'mac' |
| 32 } |
| 33 |
| 34 |
| 35 def ensure_jsshell(): |
| 36 path = os.environ.get('SPIDERMONKEY_BINARY') |
| 37 if path and os.path.isfile(path): |
| 38 return path |
| 39 |
| 40 system = platform.system() |
| 41 try: |
| 42 build = JSSHELL_SUPPORTED_PLATFORMS[system] |
| 43 if isinstance(build, dict): |
| 44 build = build[platform.machine()] |
| 45 except KeyError: |
| 46 raise Exception('Platform {} ({}) not supported by JS shell'.format( |
| 47 system, platform.machine() |
| 48 )) |
| 49 |
| 50 shell_dir = os.path.join(os.path.dirname(__file__), |
| 51 '{}-{}'.format(JSSHELL_DIR, build)) |
| 52 if not os.path.exists(shell_dir): |
| 53 os.makedirs(shell_dir) |
| 54 if system == 'Windows': |
| 55 path = os.path.join(shell_dir, 'js.exe') |
| 56 else: |
| 57 path = os.path.join(shell_dir, 'js') |
| 58 |
| 59 if os.path.exists(path): |
| 60 return path |
| 61 |
| 62 with urlopen(JSSHELL_URL.format(build)) as response: |
| 63 data = response.read() |
| 64 |
| 65 with zipfile.ZipFile(io.BytesIO(data)) as archive: |
| 66 archive.extractall(shell_dir) |
| 67 |
| 68 if not os.path.exists(path): |
| 69 raise Exception("Downloaded package doesn't contain JS shell") |
| 70 |
| 71 try: |
| 72 os.chmod(path, 0o700) |
| 73 except: |
| 74 pass |
| 75 |
| 76 return path |
11 | 77 |
12 | 78 |
13 def rewrite_js(args, script=None): | 79 def rewrite_js(args, script=None): |
14 jsshell = utils.ensure_jsshell() | 80 jsshell = ensure_jsshell() |
15 env = {'LD_LIBRARY_PATH': os.path.relpath(os.path.dirname(jsshell))} | 81 env = {'LD_LIBRARY_PATH': os.path.relpath(os.path.dirname(jsshell))} |
16 base_dir = os.path.dirname(__file__) | 82 base_dir = os.path.dirname(__file__) |
17 | 83 |
18 if not script: | 84 if not script: |
19 script = os.path.join(base_dir, 'scripts', 'abprewrite.js') | 85 script = os.path.join(base_dir, 'scripts', 'abprewrite.js') |
20 | 86 |
21 command = [jsshell, os.path.join(base_dir, 'jshydra.js'), script] + args | 87 command = [jsshell, os.path.join(base_dir, 'jshydra.js'), script] + args |
22 return subprocess.check_output(command, env=env, universal_newlines=True) | 88 return subprocess.check_output(command, env=env, universal_newlines=True) |
LEFT | RIGHT |