diff options
Diffstat (limited to 'python')
-rw-r--r-- | python/build/download.py | 40 | ||||
-rw-r--r-- | python/build/tar.py | 11 |
2 files changed, 51 insertions, 0 deletions
diff --git a/python/build/download.py b/python/build/download.py new file mode 100644 index 000000000..62a844435 --- /dev/null +++ b/python/build/download.py @@ -0,0 +1,40 @@ +import os +import hashlib +import urllib.request + +def file_md5(path): + """Calculate the MD5 checksum of a file and return it in hexadecimal notation.""" + + with open(path, 'rb') as f: + m = hashlib.md5() + while True: + data = f.read(65536) + if len(data) == 0: + # end of file + return m.hexdigest() + m.update(data) + +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: + calculated_md5 = file_md5(path) + if md5 == calculated_md5: return path + os.unlink(path) + except FileNotFoundError: + pass + + tmp_path = path + '.tmp' + + print("download", url) + urllib.request.urlretrieve(url, tmp_path) + calculated_md5 = file_md5(tmp_path) + if calculated_md5 != md5: + os.unlink(tmp_path) + raise "MD5 mismatch" + + os.rename(tmp_path, path) + return path diff --git a/python/build/tar.py b/python/build/tar.py new file mode 100644 index 000000000..15bbfca6b --- /dev/null +++ b/python/build/tar.py @@ -0,0 +1,11 @@ +import os, shutil, subprocess + +def untar(tarball_path, parent_path, base): + path = os.path.join(parent_path, base) + try: + shutil.rmtree(path) + except FileNotFoundError: + pass + os.makedirs(parent_path, exist_ok=True) + subprocess.check_call(['/bin/tar', 'xfC', tarball_path, parent_path]) + return path |