add in download retry, and download progress bar
import sys
import urllib.request
+import urllib.error
import tempfile
import pathlib
import shutil
@@ -7,21 +8,75 @@ import subprocess
import textwrap
import traceback
+import tqdm
+
PYTHON_ENVIRONMENT_POINTER = "https://bootstrap.pymedphys.com/python-urls/win-amd64"
GET_PYMEDPHYS_URL = "https://bootstrap.pymedphys.com/get-pymedphys.py"
HELP_URL = "https://bootstrap.pymedphys.com/help"
+class DownloadProgressBar(tqdm.tqdm):
+ def update_to(self, b=1, bsize=1, tsize=None):
+ if tsize is not None:
+ self.total = tsize
+ self.update(b * bsize - self.n)
+
+
+import time
+from functools import wraps
+
+
+def retry(ExceptionToCheck, tries=4, delay=3, backoff=2, logger=None):
+ """Retry calling the decorated function using an exponential backoff."""
+
+ def deco_retry(f):
+ @wraps(f)
+ def f_retry(*args, **kwargs):
+ mtries, mdelay = tries, delay
+ while mtries > 1:
+ try:
+ return f(*args, **kwargs)
+ except ExceptionToCheck as e:
+ msg = "%s, Retrying in %d seconds..." % (str(e), mdelay)
+ if logger:
+ logger.warning(msg)
+ else:
+ print(msg)
+ time.sleep(mdelay)
+ mtries -= 1
+ mdelay *= backoff
+ return f(*args, **kwargs)
+
+ return f_retry # true decorator
+
+ return deco_retry
+
+
+@retry((urllib.error.HTTPError, ConnectionResetError))
+def download_with_progress(url, filepath, description):
+ with DownloadProgressBar(
+ unit="B", unit_scale=True, miniters=1, desc=description
+ ) as t:
+ urllib.request.urlretrieve(url, filepath, reporthook=t.update_to)
+
+
def main():
+ downloading_prompt = "Downloading installation files..."
+ print(downloading_prompt)
+
with tempfile.TemporaryDirectory() as temp_dir:
get_pymedphys_filepath = pathlib.Path(temp_dir).joinpath("get-pymedphys.py")
- urllib.request.urlretrieve(GET_PYMEDPHYS_URL, get_pymedphys_filepath)
+ download_with_progress(
+ GET_PYMEDPHYS_URL, get_pymedphys_filepath, "get-pymedphys.py"
+ )
python_environment_url = (
urllib.request.urlopen(PYTHON_ENVIRONMENT_POINTER).read().decode()
)
python_environment_zip = pathlib.Path(temp_dir).joinpath("python.zip")
- urllib.request.urlretrieve(python_environment_url, python_environment_zip)
+ download_with_progress(
+ python_environment_url, python_environment_zip, "python-embed.zip"
+ )
python_environment_directory = pathlib.Path(temp_dir).joinpath("python")
shutil.unpack_archive(python_environment_zip, python_environment_directory)
