stdlib: Fix resource downloader download to cwd upon failure

There are some cases where default downloading to `~/.cache/gem5` will
not work (for example, running gem5 in a Docker container, an error
observed here:
https://gem5-review.googlesource.com/c/public/gem5/+/51950).

To fix this, the `_get_default_resource_dir` has been altered to iterate
through a list of default resource directory targets. This change will
mean if `~/.cache/gem5` is not available then the resource is downloaded
to the current working directory of gem5.

Change-Id: I84e523f3adc182e140959243ff9335510d6b7185
Reviewed-on: https://gem5-review.googlesource.com/c/public/gem5/+/52423
Reviewed-by: Bobby R. Bruce <bbruce@ucdavis.edu>
Maintainer: Bobby R. Bruce <bbruce@ucdavis.edu>
Tested-by: kokoro <noreply+kokoro@google.com>
This commit is contained in:
Bobby R. Bruce
2021-11-04 14:31:54 -07:00
parent 1b0b59f49f
commit 76251f9407

View File

@@ -102,7 +102,7 @@ class Resource(AbstractResource):
:param resource_directory: The location of the directory in which the
resource is to be stored. If this parameter is not set, it will set to
the environment variable `GEM5_RESOURCE_DIR`. If the environment is not
set it will default to `~/.cache/gem5`.
set it will default to `~/.cache/gem5` if available, otherwise the CWD.
:param override: If the resource is present, but does not have the
correct md5 value, the resoruce will be deleted and re-downloaded if
this value is True. Otherwise an exception will be thrown. False by
@@ -137,8 +137,28 @@ class Resource(AbstractResource):
def _get_default_resource_dir(cls) -> str:
"""
Obtain the default gem5 resources directory on the host system.
Obtain the default gem5 resources directory on the host system. This
function will iterate through sensible targets until it finds one that
works on the host system.
:returns: The default gem5 resources directory.
"""
return os.path.join(Path.home(), ".cache", "gem5")
test_list = [
# First try `~/.cache/gem5`.
os.path.join(Path.home(), ".cache", "gem5"),
# Last resort, just put things in the cwd.
os.path.join(Path.cwd(), "resources"),
]
for path in test_list:
if os.path.exists(path): # If the path already exists...
if os.path.isdir(path): # Check to see the path is a directory.
return path # If so, the path is valid and can be used.
else: # If the path does not exist, try to create it.
try:
os.makedirs(path, exist_ok=False)
return path
except OSError:
continue # If the path cannot be created, then try another.
raise Exception("Cannot find a valid location to download resources")