summaryrefslogtreecommitdiff
path: root/mdf/util
diff options
context:
space:
mode:
Diffstat (limited to 'mdf/util')
-rw-r--r--mdf/util/__init__.py0
-rw-r--r--mdf/util/util.py44
2 files changed, 44 insertions, 0 deletions
diff --git a/mdf/util/__init__.py b/mdf/util/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/mdf/util/__init__.py
diff --git a/mdf/util/util.py b/mdf/util/util.py
new file mode 100644
index 0000000..b233d88
--- /dev/null
+++ b/mdf/util/util.py
@@ -0,0 +1,44 @@
+import subprocess
+import urllib.request
+import urllib.error
+
+from time import sleep
+from threading import Lock
+import http.client
+
+
+_print_lock = Lock()
+
+
+def safe_print(*args, **kwargs):
+ with _print_lock:
+ print(*args, **kwargs)
+
+
+def run(args: list, **kwargs):
+ p = subprocess.run(args, **kwargs)
+ if p.returncode != 0:
+ raise OSError(f'convert returned {p.returncode} ('+' '.join(args)+')')
+
+
+def download_file(url, output, handle_http_errors=True) -> bool:
+ tries_left = 3
+ ok = False
+ while tries_left > 0:
+ try:
+ urllib.request.urlretrieve(url, output)
+ ok = True
+ break
+ except http.client.RemoteDisconnected:
+ ok = False
+ print(' caught an exception, sleeping for 2 seconds and retrying...')
+ sleep(2)
+ tries_left -= 1
+ except urllib.error.HTTPError as e:
+ if not handle_http_errors:
+ raise e
+ else:
+ print(f' failed to download {url}: {str(e)}')
+ return False
+ return ok
+