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