| LEFT | RIGHT | 
|---|
| 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 |  | 
| 3 # http://mozilla.org/MPL/2.0/. |  | 
| 4 |  | 
| 5 import os |  | 
| 6 import platform |  | 
| 7 import io |  | 
| 8 import zipfile |  | 
| 9 |  | 
| 10 try: |  | 
| 11     from urllib.request import urlopen |  | 
| 12 except ImportError: |  | 
| 13     import urllib |  | 
| 14     import contextlib |  | 
| 15 |  | 
| 16     def urlopen(*args, **kwargs): |  | 
| 17         return contextlib.closing(urllib.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)) |  | 
| 23 |  | 
| 24 JSSHELL_SUPPORTED_PLATFORMS = { |  | 
| 25     'Windows': 'win32', |  | 
| 26     'Linux': { |  | 
| 27         'i686': 'linux-i686', |  | 
| 28         'x86_64': 'linux-x86_64' |  | 
| 29     }, |  | 
| 30     'Darwin': 'mac' |  | 
| 31 } |  | 
| 32 |  | 
| 33 |  | 
| 34 def ensure_jsshell(): |  | 
| 35     path = os.environ.get('SPIDERMONKEY_BINARY') |  | 
| 36     if path and os.path.isfile(path): |  | 
| 37         return path |  | 
| 38 |  | 
| 39     system = platform.system() |  | 
| 40     try: |  | 
| 41         build = JSSHELL_SUPPORTED_PLATFORMS[system] |  | 
| 42         if isinstance(build, dict): |  | 
| 43             build = build[platform.machine()] |  | 
| 44     except KeyError: |  | 
| 45         raise Exception('Platform {} ({}) not supported by JS shell'.format( |  | 
| 46             system, platform.machine() |  | 
| 47         )) |  | 
| 48 |  | 
| 49     shell_dir = os.path.join(os.path.dirname(__file__), |  | 
| 50                              '{}-{}'.format(JSSHELL_DIR, build)) |  | 
| 51     if not os.path.exists(shell_dir): |  | 
| 52         os.makedirs(shell_dir) |  | 
| 53     if system == 'Windows': |  | 
| 54         path = os.path.join(shell_dir, 'js.exe') |  | 
| 55     else: |  | 
| 56         path = os.path.join(shell_dir, 'js') |  | 
| 57 |  | 
| 58     if os.path.exists(path): |  | 
| 59         return path |  | 
| 60 |  | 
| 61     with urlopen(JSSHELL_URL.format(build)) as response: |  | 
| 62         data = response.read() |  | 
| 63 |  | 
| 64     with zipfile.ZipFile(io.BytesIO(data)) as archive: |  | 
| 65         archive.extractall(shell_dir) |  | 
| 66 |  | 
| 67     if not os.path.exists(path): |  | 
| 68         raise Exception("Downloaded package doesn't contain JS shell") |  | 
| 69 |  | 
| 70     try: |  | 
| 71         os.chmod(path, 0o700) |  | 
| 72     except: |  | 
| 73         pass |  | 
| 74 |  | 
| 75     return path |  | 
| LEFT | RIGHT | 
|---|