aboutsummaryrefslogtreecommitdiffstats
path: root/python/servo/bootstrap_commands.py
diff options
context:
space:
mode:
authorAneesh Agrawal <aneeshusa@gmail.com>2017-01-13 21:55:33 -0500
committerAneesh Agrawal <aneeshusa@gmail.com>2017-01-15 15:21:35 -0500
commit02b054ec9e799705296d5545a744bbf766ddbae6 (patch)
tree9aa1c1319a1dc4033304850ecc6578eb6716eb8e /python/servo/bootstrap_commands.py
parent5b8d783f4cb622fa68c5270b27ebad1e838ee7cb (diff)
downloadservo-02b054ec9e799705296d5545a744bbf766ddbae6.tar.gz
servo-02b054ec9e799705296d5545a744bbf766ddbae6.zip
Create a util.py Python module for common functions
Extracting these functions helps avoid circular dependencies, and make them easier to find/reuse.
Diffstat (limited to 'python/servo/bootstrap_commands.py')
-rw-r--r--python/servo/bootstrap_commands.py93
1 files changed, 2 insertions, 91 deletions
diff --git a/python/servo/bootstrap_commands.py b/python/servo/bootstrap_commands.py
index 49dd40648b1..9128c1dccff 100644
--- a/python/servo/bootstrap_commands.py
+++ b/python/servo/bootstrap_commands.py
@@ -8,7 +8,6 @@
# except according to those terms.
from __future__ import print_function, unicode_literals
-from socket import error as socket_error
import base64
import json
@@ -17,9 +16,6 @@ import os.path as path
import re
import shutil
import sys
-import StringIO
-import tarfile
-import zipfile
import urllib2
from mach.decorators import (
@@ -28,93 +24,8 @@ from mach.decorators import (
Command,
)
-from servo.command_base import CommandBase, host_triple, BIN_SUFFIX
-
-
-def download(desc, src, writer, start_byte=0):
- if start_byte:
- print("Resuming download of %s..." % desc)
- else:
- print("Downloading %s..." % desc)
- dumb = (os.environ.get("TERM") == "dumb") or (not sys.stdout.isatty())
-
- try:
- req = urllib2.Request(src)
- if start_byte:
- req = urllib2.Request(src, headers={'Range': 'bytes={}-'.format(start_byte)})
- resp = urllib2.urlopen(req)
-
- fsize = None
- if resp.info().getheader('Content-Length'):
- fsize = int(resp.info().getheader('Content-Length').strip()) + start_byte
-
- recved = start_byte
- chunk_size = 8192
-
- while True:
- chunk = resp.read(chunk_size)
- if not chunk:
- break
- recved += len(chunk)
- if not dumb:
- if fsize is not None:
- pct = recved * 100.0 / fsize
- print("\rDownloading %s: %5.1f%%" % (desc, pct), end="")
-
- sys.stdout.flush()
- writer.write(chunk)
-
- if not dumb:
- print()
- except urllib2.HTTPError, e:
- print("Download failed (%d): %s - %s" % (e.code, e.reason, src))
- if e.code == 403:
- print("No Rust compiler binary available for this platform. "
- "Please see https://github.com/servo/servo/#prerequisites")
- sys.exit(1)
- except urllib2.URLError, e:
- print("Error downloading Rust compiler: %s. The failing URL was: %s" % (e.reason, src))
- sys.exit(1)
- except socket_error, e:
- print("Looks like there's a connectivity issue, check your Internet connection. %s" % (e))
- sys.exit(1)
- except KeyboardInterrupt:
- writer.flush()
- raise
-
-
-def download_file(desc, src, dst):
- tmp_path = dst + ".part"
- try:
- start_byte = os.path.getsize(tmp_path)
- with open(tmp_path, 'ab') as fd:
- download(desc, src, fd, start_byte=start_byte)
- except os.error:
- with open(tmp_path, 'wb') as fd:
- download(desc, src, fd)
- os.rename(tmp_path, dst)
-
-
-def download_bytes(desc, src):
- content_writer = StringIO.StringIO()
- download(desc, src, content_writer)
- return content_writer.getvalue()
-
-
-def extract(src, dst, movedir=None):
- if src.endswith(".zip"):
- zipfile.ZipFile(src).extractall(dst)
- else:
- tarfile.open(src).extractall(dst)
-
- if movedir:
- for f in os.listdir(movedir):
- frm = path.join(movedir, f)
- to = path.join(dst, f)
- os.rename(frm, to)
- os.rmdir(movedir)
-
- os.remove(src)
+from servo.command_base import CommandBase, BIN_SUFFIX
+from servo.util import download_bytes, download_file, extract, host_triple
@CommandProvider