Skip to content

Commit

Permalink
Handle zip related exceptions (#417)
Browse files Browse the repository at this point in the history
Related to #416, but don't explain it.
This PR just handle a system image as invalid instead of crashing when :
- File is not a zip file (so like #416 )
- File is a zip file but does not contain metadata file

For now, only `IMG-e-1.18-t-20231207360613-stable-FP5.zip` is known to
be in the second case, and it's a very strange installation package, OAI
cannot handle that for now (related to #288 ?), so the file is shown as
invalid.
Maybe we can explain it with more details to the user?
  • Loading branch information
tsterbak committed Jan 15, 2024
2 parents bf01053 + b0110dd commit 40c9838
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions openandroidinstaller/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ def retrieve_image_metadata(image_path: str) -> dict:
].decode("utf-8")
logger.info(f"Metadata retrieved from image {image_path.split('/')[-1]}.")
return metadata_dict
except zipfile.BadZipFile as e:
raise e
except (FileNotFoundError, KeyError):
logger.error(
f"Metadata file {metapath} not found in {image_path.split('/')[-1]}."
Expand All @@ -107,12 +109,12 @@ def image_sdk_level(image_path: str) -> int:
Returns:
Android version as integer.
"""
metadata = retrieve_image_metadata(image_path)
try:
metadata = retrieve_image_metadata(image_path)
sdk_level = metadata["post-sdk-level"]
logger.info(f"Android version of {image_path}: {sdk_level}")
return int(sdk_level)
except (ValueError, TypeError, KeyError) as e:
except (ValueError, TypeError, KeyError, zipfile.BadZipFile) as e:
logger.error(f"Could not determine Android version of {image_path}. Error: {e}")
return -1

Expand All @@ -129,8 +131,8 @@ def image_works_with_device(
Returns:
CheckResult object containing the compatibility status and a message.
"""
metadata = retrieve_image_metadata(image_path)
try:
metadata = retrieve_image_metadata(image_path)
supported_devices = metadata["pre-device"].split(",")
logger.info(f"Image works with the following device(s): {supported_devices}")
if any(code in supported_devices for code in supported_device_codes):
Expand All @@ -145,6 +147,12 @@ def image_works_with_device(
CompatibilityStatus.INCOMPATIBLE,
f"Image file {image_path.split('/')[-1]} is not supported by device code.",
)
except zipfile.BadZipFile:
logger.error("Selected image is not a zip file.")
return CheckResult(
CompatibilityStatus.INCOMPATIBLE,
f"Selected image {image_path.split('/')[-1]} is not a zip file.",
)
except KeyError:
logger.error(
f"Could not determine supported devices for {image_path.split('/')[-1]}."
Expand Down

0 comments on commit 40c9838

Please sign in to comment.