From 5c81a342aff8cf995babed8011661bd16122265a Mon Sep 17 00:00:00 2001 From: Raphael Luciano Date: Fri, 10 Nov 2023 14:51:36 -0500 Subject: [PATCH] Removing deprecated `urlretrieve` to fix `download_file` issue (#2234) --- setup.py | 1 + src/ansys/fluent/core/examples/downloads.py | 21 ++++++++++++++------- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/setup.py b/setup.py index e9ee666193a..123b7e24b38 100644 --- a/setup.py +++ b/setup.py @@ -33,6 +33,7 @@ "pyyaml>=6.0", "docker>=6.1.3", "psutil>=5.9.5", + "requests>=2.31.0", ] extras_require = { diff --git a/src/ansys/fluent/core/examples/downloads.py b/src/ansys/fluent/core/examples/downloads.py index c496974a8b5..9e09865e4ee 100644 --- a/src/ansys/fluent/core/examples/downloads.py +++ b/src/ansys/fluent/core/examples/downloads.py @@ -1,14 +1,18 @@ """Functions to download sample datasets from the Ansys example data repository.""" +import logging import os from pathlib import Path import re import shutil from typing import Optional -import urllib.request import zipfile +import requests + import ansys.fluent.core as pyfluent +logger = logging.getLogger("pyfluent.networking") + def delete_downloads(): """Delete all downloaded examples from the default examples folder to free space or @@ -55,30 +59,33 @@ def _retrieve_file( local_path_no_zip = re.sub(".zip$", "", local_path) file_name_no_zip = re.sub(".zip$", "", file_name) # First check if file has already been downloaded - print("Checking if specified file already exists...") + logger.info(f"Checking if {local_path_no_zip} already exists...") if os.path.isfile(local_path_no_zip) or os.path.isdir(local_path_no_zip): print(f"File already exists. File path:\n{local_path_no_zip}") + logger.info("File already exists.") if return_without_path: return file_name_no_zip else: return local_path_no_zip - print("File does not exist. Downloading specified file...") + logger.info("File does not exist. Downloading specified file...") # Check if save path exists if not os.path.exists(save_path): os.makedirs(save_path) - # grab the correct url retriever - urlretrieve = urllib.request.urlretrieve + # Download file + logger.info(f'Downloading URL: "{url}"') + content = requests.get(url).content + with open(local_path, "wb") as f: + f.write(content) - # Perform download - urlretrieve(url, filename=local_path) if local_path.endswith(".zip"): _decompress(local_path) local_path = local_path_no_zip file_name = file_name_no_zip print(f"Download successful. File path:\n{local_path}") + logger.info("Download successful.") if return_without_path: return file_name else: