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.
This commit is contained in:
Cyryl Płotnicki-Chudyk 2016-04-30 08:09:53 +02:00
parent 9b63263d0d
commit f5884f6824
1 changed files with 23 additions and 5 deletions

View File

@ -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", "")