Index: utils.py |
=================================================================== |
--- a/utils.py |
+++ b/utils.py |
@@ -1,28 +1,33 @@ |
-# coding: utf-8 |
- |
# This Source Code is subject to the terms of the Mozilla Public License |
# version 2.0 (the "License"). You can obtain a copy of the License at |
# http://mozilla.org/MPL/2.0/. |
-from contextlib import closing |
import os |
import platform |
-from StringIO import StringIO |
-import sys |
-import urllib |
+import io |
import zipfile |
-JSSHELL_DIR = "mozilla-esr31" |
-JSSHELL_URL = ("https://ftp.mozilla.org/pub/mozilla.org/firefox/nightly" |
- "/2015/02/2015-02-25-00-22-19-%s/jsshell-%%s.zip" % JSSHELL_DIR) |
+try: |
+ from urllib.request import urlopen |
+except ImportError: |
+ import urllib |
+ import contextlib |
+ |
+ def urlopen(*args, **kwargs): |
+ return contextlib.closing(urllib.urlopen(*args, **kwargs)) |
+ |
+JSSHELL_DIR = 'mozilla-esr31' |
+JSSHELL_URL = ('https://ftp.mozilla.org/pub/mozilla.org/firefox/nightly' |
+ '/2015/02/2015-02-25-00-22-19-{}' |
+ '/jsshell-{{}}.zip'.format(JSSHELL_DIR)) |
JSSHELL_SUPPORTED_PLATFORMS = { |
- "win32": "win32", |
- "linux2": { |
- "i686": "linux-i686", |
- "x86_64": "linux-x86_64" |
+ 'Windows': 'win32', |
+ 'Linux': { |
+ 'i686': 'linux-i686', |
+ 'x86_64': 'linux-x86_64' |
}, |
- "darwin": "mac" |
+ 'Darwin': 'mac' |
} |
@@ -32,20 +37,21 @@ |
return path |
baseDir = os.path.dirname(__file__) |
+ system = platform.system() |
Sebastian Noack
2016/08/28 22:36:14
sys.platform behaves inconsistently since Python 3
Vasily Kuznetsov
2016/08/29 15:29:52
Acknowledged.
|
try: |
- build = JSSHELL_SUPPORTED_PLATFORMS[sys.platform] |
+ build = JSSHELL_SUPPORTED_PLATFORMS[system] |
if isinstance(build, dict): |
build = build[platform.machine()] |
except KeyError: |
- raise Exception('Platform %s (%s) not supported by JS shell' % ( |
- sys.platform, platform.machine() |
+ raise Exception('Platform {} ({}) not supported by JS shell'.format( |
+ system, platform.machine() |
)) |
shell_dir = os.path.join(baseDir, JSSHELL_DIR + "-" + build) |
if not os.path.exists(shell_dir): |
os.makedirs(shell_dir) |
- if sys.platform == 'win32': |
+ if system == 'Windows': |
path = os.path.join(shell_dir, 'js.exe') |
else: |
path = os.path.join(shell_dir, 'js') |
@@ -53,17 +59,17 @@ |
if os.path.exists(path): |
return path |
- with closing(urllib.urlopen(JSSHELL_URL % build)) as response: |
+ with urlopen(JSSHELL_URL.format(build)) as response: |
data = response.read() |
- with zipfile.ZipFile(StringIO(data)) as zip: |
- zip.extractall(shell_dir) |
+ with zipfile.ZipFile(io.BytesIO(data)) as archive: |
+ archive.extractall(shell_dir) |
if not os.path.exists(path): |
raise Exception('Downloaded package didn\'t contain JS shell executable') |
try: |
- os.chmod(path, 0700) |
+ os.chmod(path, 0o700) |
except: |
pass |