python/build/verify: add verify_file_digest()
This commit is contained in:
parent
3dde62befe
commit
4c650e87fa
@ -1,4 +1,4 @@
|
|||||||
from build.verify import file_md5
|
from build.verify import verify_file_digest
|
||||||
import os
|
import os
|
||||||
import urllib.request
|
import urllib.request
|
||||||
|
|
||||||
@ -9,8 +9,7 @@ def download_and_verify(url, md5, parent_path):
|
|||||||
path = os.path.join(parent_path, os.path.basename(url))
|
path = os.path.join(parent_path, os.path.basename(url))
|
||||||
|
|
||||||
try:
|
try:
|
||||||
calculated_md5 = file_md5(path)
|
if verify_file_digest(path, md5): return path
|
||||||
if md5 == calculated_md5: return path
|
|
||||||
os.unlink(path)
|
os.unlink(path)
|
||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
pass
|
pass
|
||||||
@ -19,8 +18,7 @@ def download_and_verify(url, md5, parent_path):
|
|||||||
|
|
||||||
print("download", url)
|
print("download", url)
|
||||||
urllib.request.urlretrieve(url, tmp_path)
|
urllib.request.urlretrieve(url, tmp_path)
|
||||||
calculated_md5 = file_md5(tmp_path)
|
if not verify_file_digest(tmp_path, md5):
|
||||||
if calculated_md5 != md5:
|
|
||||||
os.unlink(tmp_path)
|
os.unlink(tmp_path)
|
||||||
raise RuntimeError("MD5 mismatch")
|
raise RuntimeError("MD5 mismatch")
|
||||||
|
|
||||||
|
@ -27,3 +27,8 @@ def file_md5(path):
|
|||||||
"""Calculate the MD5 checksum of a file and return it in hexadecimal notation."""
|
"""Calculate the MD5 checksum of a file and return it in hexadecimal notation."""
|
||||||
|
|
||||||
return file_digest(hashlib.md5, path)
|
return file_digest(hashlib.md5, path)
|
||||||
|
|
||||||
|
def verify_file_digest(path, expected_digest):
|
||||||
|
"""Verify the digest of a file, and return True if the digest matches with the given expected digest."""
|
||||||
|
|
||||||
|
return file_md5(path) == expected_digest
|
||||||
|
Loading…
Reference in New Issue
Block a user