Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: utils.py

Issue 29350236: Issue 4373 - Made jshydra compatible with Python 3 (Closed)
Patch Set: Use urllib2, removed redundant future import, added option to test without caching Created Aug. 30, 2016, 11:33 a.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « tox.ini ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 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.
+ import contextlib
+
+ def urlopen(*args, **kwargs):
+ return contextlib.closing(urllib2.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()
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
« no previous file with comments | « tox.ini ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld