Skip to content

Commit f5884f6

Browse files
committed
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.
1 parent 9b63263 commit f5884f6

File tree

1 file changed

+23
-5
lines changed

1 file changed

+23
-5
lines changed

src/bootstrap/bootstrap.py

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,26 @@
1616
import subprocess
1717
import sys
1818
import tarfile
19+
import tempfile
20+
1921

2022
def get(url, path, verbose=False):
21-
print("downloading " + url)
2223
sha_url = url + ".sha256"
23-
sha_path = path + ".sha256"
24-
for _url, _path in ((url, path), (sha_url, sha_path)):
24+
temp_file = tempfile.NamedTemporaryFile(delete=False)
25+
temp_path = temp_file.name
26+
sha_file = tempfile.NamedTemporaryFile(suffix=".sha256", delete=True)
27+
sha_path = sha_file.name
28+
download(sha_path, sha_url, temp_path, url, verbose)
29+
verify(sha_path, temp_path, verbose)
30+
sha_file.close()
31+
print("moving " + temp_path + " to " + path)
32+
shutil.move(temp_path, path)
33+
temp_file.close()
34+
35+
36+
def download(sha_path, sha_url, temp_path, url, verbose):
37+
for _url, _path in ((url, temp_path), (sha_url, sha_path)):
38+
print("downloading " + _url + " to " + _path)
2539
# see http://serverfault.com/questions/301128/how-to-download
2640
if sys.platform == 'win32':
2741
run(["PowerShell.exe", "/nologo", "-Command",
@@ -30,8 +44,11 @@ def get(url, path, verbose=False):
3044
verbose=verbose)
3145
else:
3246
run(["curl", "-o", _path, _url], verbose=verbose)
33-
print("verifying " + path)
34-
with open(path, "rb") as f:
47+
48+
49+
def verify(sha_path, temp_path, verbose):
50+
print("verifying " + temp_path)
51+
with open(temp_path, "rb") as f:
3552
found = hashlib.sha256(f.read()).hexdigest()
3653
with open(sha_path, "r") as f:
3754
expected, _ = f.readline().split()
@@ -43,6 +60,7 @@ def get(url, path, verbose=False):
4360
raise RuntimeError(err)
4461
sys.exit(err)
4562

63+
4664
def unpack(tarball, dst, verbose=False, match=None):
4765
print("extracting " + tarball)
4866
fname = os.path.basename(tarball).replace(".tar.gz", "")

0 commit comments

Comments
 (0)