stdlib: Add 'quiet' option to obtain_resource func

Change-Id: I15d3be959ba7ab8af328fc6ec2912a8151941a1e
This commit is contained in:
Bobby R. Bruce
2023-09-13 16:36:01 -07:00
parent 46be2d2339
commit b12f28af96
2 changed files with 26 additions and 15 deletions

View File

@@ -204,6 +204,7 @@ def get_resource(
resource_version: Optional[str] = None,
clients: Optional[List] = None,
gem5_version: Optional[str] = core.gem5Version,
quiet: bool = False,
) -> None:
"""
Obtains a gem5 resource and stored it to a specified location. If the
@@ -236,6 +237,9 @@ def get_resource(
By default, the version of gem5 being used is used. This is used primarily
for testing purposes.
:param quiet: If true, no output will be printed to the console (baring
exceptions). False by default.
:raises Exception: An exception is thrown if a file is already present at
`to_path` but it does not have the correct md5 sum. An exception will also
be thrown is a directory is present at `to_path`
@@ -326,37 +330,41 @@ def get_resource(
)
shutil.copy(file_uri_path, download_dest)
else:
# TODO: Might be nice to have some kind of download status bar here.
# TODO: There might be a case where this should be silenced.
print(
"Resource '{}' was not found locally. Downloading to '{}'...".format(
resource_name, download_dest
# TODO: Might be nice to have some kind of download status bar here..
if not quiet:
print(
f"Resource '{resource_name}' was not found locally. "
f"Downloading to '{download_dest}'..."
)
)
# Get the URL.
url = resource_json["url"]
_download(url=url, download_to=download_dest)
print(f"Finished downloading resource '{resource_name}'.")
if not quiet:
print(f"Finished downloading resource '{resource_name}'.")
if run_unzip:
print(
f"Decompressing resource '{resource_name}' ('{download_dest}')..."
)
if not quiet:
print(
f"Decompressing resource '{resource_name}' "
f"('{download_dest}')..."
)
unzip_to = download_dest[: -len(zip_extension)]
with gzip.open(download_dest, "rb") as f:
with open(unzip_to, "wb") as o:
shutil.copyfileobj(f, o)
os.remove(download_dest)
download_dest = unzip_to
print(f"Finished decompressing resource '{resource_name}'.")
if not quiet:
print(f"Finished decompressing resource '{resource_name}'.")
if run_tar_extract:
print(
f"Unpacking the the resource '{resource_name}' "
f"('{download_dest}')"
)
if not quiet:
print(
f"Unpacking the the resource '{resource_name}' "
f"('{download_dest}')"
)
unpack_to = download_dest[: -len(tar_extension)]
with tarfile.open(download_dest) as f:

View File

@@ -621,6 +621,7 @@ def obtain_resource(
resource_version: Optional[str] = None,
clients: Optional[List] = None,
gem5_version=core.gem5Version,
quiet: bool = False,
) -> AbstractResource:
"""
This function primarily serves as a factory for resources. It will return
@@ -644,6 +645,7 @@ def obtain_resource(
:param gem5_version: The gem5 version to use to filter incompatible
resource versions. By default set to the current gem5 version. If None,
this filtering is not performed.
:param quiet: If True, suppress output. False by default.
"""
# Obtain the resource object entry for this resource
@@ -695,6 +697,7 @@ def obtain_resource(
resource_version=resource_version,
clients=clients,
gem5_version=gem5_version,
quiet=quiet,
)
# Obtain the type from the JSON. From this we will determine what subclass