2016-12-29 21:40:54 +01:00
|
|
|
from build.verify import verify_file_digest
|
2015-11-20 22:10:22 +01:00
|
|
|
import os
|
|
|
|
import urllib.request
|
|
|
|
|
|
|
|
def download_and_verify(url, md5, parent_path):
|
|
|
|
"""Download a file, verify its MD5 checksum and return the local path."""
|
|
|
|
|
|
|
|
os.makedirs(parent_path, exist_ok=True)
|
|
|
|
path = os.path.join(parent_path, os.path.basename(url))
|
|
|
|
|
|
|
|
try:
|
2016-12-29 21:40:54 +01:00
|
|
|
if verify_file_digest(path, md5): return path
|
2015-11-20 22:10:22 +01:00
|
|
|
os.unlink(path)
|
|
|
|
except FileNotFoundError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
tmp_path = path + '.tmp'
|
|
|
|
|
|
|
|
print("download", url)
|
|
|
|
urllib.request.urlretrieve(url, tmp_path)
|
2016-12-29 21:40:54 +01:00
|
|
|
if not verify_file_digest(tmp_path, md5):
|
2015-11-20 22:10:22 +01:00
|
|
|
os.unlink(tmp_path)
|
2016-12-29 21:43:47 +01:00
|
|
|
raise RuntimeError("Digest mismatch")
|
2015-11-20 22:10:22 +01:00
|
|
|
|
|
|
|
os.rename(tmp_path, path)
|
|
|
|
return path
|