Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle zip related exceptions #417

Merged
merged 4 commits into from
Jan 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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