aboutsummaryrefslogtreecommitdiffstats
path: root/python/build/download.py
diff options
context:
space:
mode:
authorMax Kellermann <max@duempel.org>2015-11-20 22:10:22 +0100
committerMax Kellermann <max@duempel.org>2015-11-20 22:14:53 +0100
commit556f9ee39cf1e675b961614f61e784f996af1e98 (patch)
tree01c6e79e4907539cdb2a674dac329e266b2a6729 /python/build/download.py
parent7dad662d690cc1c374009bbcd0df03bccb8f9ad7 (diff)
downloadmpd-556f9ee39cf1e675b961614f61e784f996af1e98.tar.gz
mpd-556f9ee39cf1e675b961614f61e784f996af1e98.tar.xz
mpd-556f9ee39cf1e675b961614f61e784f996af1e98.zip
{android,win32}/build.py: move common code to python/build/
Diffstat (limited to 'python/build/download.py')
-rw-r--r--python/build/download.py40
1 files changed, 40 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