diff --git a/python/build/download.py b/python/build/download.py index 56af5c7bd..4e4efdac8 100644 --- a/python/build/download.py +++ b/python/build/download.py @@ -1,4 +1,4 @@ -from build.verify import file_md5 +from build.verify import verify_file_digest import os 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)) try: - calculated_md5 = file_md5(path) - if md5 == calculated_md5: return path + if verify_file_digest(path, md5): return path os.unlink(path) except FileNotFoundError: pass @@ -19,8 +18,7 @@ def download_and_verify(url, md5, parent_path): print("download", url) urllib.request.urlretrieve(url, tmp_path) - calculated_md5 = file_md5(tmp_path) - if calculated_md5 != md5: + if not verify_file_digest(tmp_path, md5): os.unlink(tmp_path) raise RuntimeError("MD5 mismatch") diff --git a/python/build/verify.py b/python/build/verify.py index 47edb6cb0..450b2aece 100644 --- a/python/build/verify.py +++ b/python/build/verify.py @@ -27,3 +27,8 @@ def file_md5(path): """Calculate the MD5 checksum of a file and return it in hexadecimal notation.""" 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