From f5884f6824291cda15096d84af9e346e13a41578 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cyryl=20P=C5=82otnicki-Chudyk?= Date: Sat, 30 Apr 2016 08:09:53 +0200 Subject: [PATCH] Get a file during bootstrap to a temp location. When downloading a file in the bootstrap phase - get it to a temp location first. Verify it there and only if downloaded properly move it to the `cache` directory. This should prevent `make` being stuck if the download was interrupted or otherwise corrupted. The temporary files are deleted in case of an exception. --- src/bootstrap/bootstrap.py | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/src/bootstrap/bootstrap.py b/src/bootstrap/bootstrap.py index a05700c1af49..6efaf16a9743 100644 --- a/src/bootstrap/bootstrap.py +++ b/src/bootstrap/bootstrap.py @@ -16,12 +16,26 @@ import shutil import subprocess import sys import tarfile +import tempfile + def get(url, path, verbose=False): - print("downloading " + url) sha_url = url + ".sha256" - sha_path = path + ".sha256" - for _url, _path in ((url, path), (sha_url, sha_path)): + temp_file = tempfile.NamedTemporaryFile(delete=False) + temp_path = temp_file.name + sha_file = tempfile.NamedTemporaryFile(suffix=".sha256", delete=True) + sha_path = sha_file.name + download(sha_path, sha_url, temp_path, url, verbose) + verify(sha_path, temp_path, verbose) + sha_file.close() + print("moving " + temp_path + " to " + path) + shutil.move(temp_path, path) + temp_file.close() + + +def download(sha_path, sha_url, temp_path, url, verbose): + for _url, _path in ((url, temp_path), (sha_url, sha_path)): + print("downloading " + _url + " to " + _path) # see http://serverfault.com/questions/301128/how-to-download if sys.platform == 'win32': run(["PowerShell.exe", "/nologo", "-Command", @@ -30,8 +44,11 @@ def get(url, path, verbose=False): verbose=verbose) else: run(["curl", "-o", _path, _url], verbose=verbose) - print("verifying " + path) - with open(path, "rb") as f: + + +def verify(sha_path, temp_path, verbose): + print("verifying " + temp_path) + with open(temp_path, "rb") as f: found = hashlib.sha256(f.read()).hexdigest() with open(sha_path, "r") as f: expected, _ = f.readline().split() @@ -43,6 +60,7 @@ def get(url, path, verbose=False): raise RuntimeError(err) sys.exit(err) + def unpack(tarball, dst, verbose=False, match=None): print("extracting " + tarball) fname = os.path.basename(tarball).replace(".tar.gz", "")