diff options
author | Evgeny Zinoviev <me@ch1p.io> | 2023-12-30 15:37:08 +0300 |
---|---|---|
committer | Evgeny Zinoviev <me@ch1p.io> | 2023-12-30 15:37:08 +0300 |
commit | abd1975def213891afdf0d87adbf79c2c7dbc0cb (patch) | |
tree | 4c0feb5ecc7f8dcf5c17e74a81a2bae9ba6c649f /mdf/util/util.py | |
parent | 38474234431fcf1dae50c00cdf649b4102c0faf9 (diff) |
Diffstat (limited to 'mdf/util/util.py')
-rw-r--r-- | mdf/util/util.py | 44 |
1 files changed, 44 insertions, 0 deletions
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 + |