Skip to content

Commit 371101a

Browse files
committed
VMImage Params Caching Request
Introduced Caching Method Before Network Requests. A few changes to Error Handling Reference: #6167 Signed-off-by: Harvey Lynden <[email protected]>
1 parent 199475a commit 371101a

File tree

2 files changed

+281
-74
lines changed

2 files changed

+281
-74
lines changed

avocado/utils/asset.py

Lines changed: 61 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,9 @@ def fetch(self, timeout=None):
443443

444444
def find_asset_file(self, create_metadata=False):
445445
"""
446-
Search for the asset file in each one of the cache locations
446+
Search for the asset file in each one of the cache locations.
447+
448+
It handles both normal asset names and direct file paths.
447449
448450
:param bool create_metadata: Should this method create the
449451
metadata in case asset file found
@@ -454,27 +456,50 @@ def find_asset_file(self, create_metadata=False):
454456
:raises: OSError
455457
"""
456458

457-
for cache_dir in self.cache_dirs:
458-
cache_dir = os.path.expanduser(cache_dir)
459-
asset_file = os.path.join(cache_dir, self.relative_dir)
460-
461-
# Ignore non-files
462-
if not os.path.isfile(asset_file):
463-
continue
464-
465-
# Ignore expired asset files
466-
if self._is_expired(asset_file, self.expire):
467-
continue
468-
469-
# Ignore mismatch hash
470-
if not self._has_valid_hash(asset_file, self.asset_hash, self.algorithm):
471-
continue
459+
# Try 1: Check if self.name is already a direct file path
460+
if self.name and os.path.isfile(self.name):
461+
# For direct paths, expired/invalid files are errors, not skips
462+
if self._is_expired(self.name, self.expire):
463+
raise OSError(f"File {self.asset_name} has expired.")
464+
if not self._has_valid_hash(self.name, self.asset_hash, self.algorithm):
465+
raise OSError(f"File {self.asset_name} hash mismatch.")
472466

473467
if create_metadata:
474-
self._create_metadata_file(asset_file)
475-
476-
LOG.info("Asset already exists in cache.")
477-
return asset_file
468+
self._create_metadata_file(self.name)
469+
LOG.info("Asset found as direct file path.")
470+
return self.name
471+
472+
# Try 2 & 3: Search in cache directories
473+
search_paths = [
474+
# Try relative_dir approach first
475+
*[
476+
(
477+
os.path.join(os.path.expanduser(cache_dir), self.relative_dir),
478+
"Asset already exists in cache.",
479+
)
480+
for cache_dir in self.cache_dirs
481+
],
482+
# Then try by name
483+
*[
484+
(
485+
os.path.join(os.path.expanduser(cache_dir), self.name),
486+
"Asset found in cache by name.",
487+
)
488+
for cache_dir in self.cache_dirs
489+
],
490+
]
491+
492+
for asset_file, log_msg in search_paths:
493+
if (
494+
os.path.isfile(asset_file)
495+
and not self._is_expired(asset_file, self.expire)
496+
and self._has_valid_hash(asset_file, self.asset_hash, self.algorithm)
497+
):
498+
499+
if create_metadata:
500+
self._create_metadata_file(asset_file)
501+
LOG.info(log_msg)
502+
return asset_file
478503

479504
raise OSError(f"File {self.asset_name} not found in the cache.")
480505

@@ -484,18 +509,29 @@ def get_metadata(self):
484509
485510
:return: metadata
486511
:rtype: dict or None
512+
:raises: OSError if asset file doesn't exist
487513
"""
488514
try:
489515
asset_file = self.find_asset_file()
490-
except OSError as exc:
491-
raise OSError("Metadata not available.") from exc
516+
except OSError as e:
517+
# Re-raise OSError if asset file doesn't exist
518+
LOG.debug("Asset file not found, metadata not available for %s", self.name)
519+
raise OSError("Metadata not available.") from e
492520

521+
# Look for metadata file
493522
basename = os.path.splitext(asset_file)[0]
494523
metadata_file = f"{basename}_metadata.json"
524+
495525
if os.path.isfile(metadata_file):
496-
with open(metadata_file, "r", encoding="utf-8") as f:
497-
metadata = json.load(f)
498-
return metadata
526+
try:
527+
with open(metadata_file, "r", encoding="utf-8") as f:
528+
metadata = json.load(f)
529+
return metadata
530+
except json.JSONDecodeError as e:
531+
LOG.warning("Failed to parse metadata file %s: %s", metadata_file, e)
532+
except OSError as e:
533+
LOG.warning("Failed to read metadata file %s: %s", metadata_file, e)
534+
499535
return None
500536

501537
@property

0 commit comments

Comments
 (0)