Skip to content

Commit 40c9838

Browse files
authored
Handle zip related exceptions (#417)
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?
2 parents bf01053 + b0110dd commit 40c9838

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

openandroidinstaller/utils.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ def retrieve_image_metadata(image_path: str) -> dict:
8484
].decode("utf-8")
8585
logger.info(f"Metadata retrieved from image {image_path.split('/')[-1]}.")
8686
return metadata_dict
87+
except zipfile.BadZipFile as e:
88+
raise e
8789
except (FileNotFoundError, KeyError):
8890
logger.error(
8991
f"Metadata file {metapath} not found in {image_path.split('/')[-1]}."
@@ -107,12 +109,12 @@ def image_sdk_level(image_path: str) -> int:
107109
Returns:
108110
Android version as integer.
109111
"""
110-
metadata = retrieve_image_metadata(image_path)
111112
try:
113+
metadata = retrieve_image_metadata(image_path)
112114
sdk_level = metadata["post-sdk-level"]
113115
logger.info(f"Android version of {image_path}: {sdk_level}")
114116
return int(sdk_level)
115-
except (ValueError, TypeError, KeyError) as e:
117+
except (ValueError, TypeError, KeyError, zipfile.BadZipFile) as e:
116118
logger.error(f"Could not determine Android version of {image_path}. Error: {e}")
117119
return -1
118120

@@ -129,8 +131,8 @@ def image_works_with_device(
129131
Returns:
130132
CheckResult object containing the compatibility status and a message.
131133
"""
132-
metadata = retrieve_image_metadata(image_path)
133134
try:
135+
metadata = retrieve_image_metadata(image_path)
134136
supported_devices = metadata["pre-device"].split(",")
135137
logger.info(f"Image works with the following device(s): {supported_devices}")
136138
if any(code in supported_devices for code in supported_device_codes):
@@ -145,6 +147,12 @@ def image_works_with_device(
145147
CompatibilityStatus.INCOMPATIBLE,
146148
f"Image file {image_path.split('/')[-1]} is not supported by device code.",
147149
)
150+
except zipfile.BadZipFile:
151+
logger.error("Selected image is not a zip file.")
152+
return CheckResult(
153+
CompatibilityStatus.INCOMPATIBLE,
154+
f"Selected image {image_path.split('/')[-1]} is not a zip file.",
155+
)
148156
except KeyError:
149157
logger.error(
150158
f"Could not determine supported devices for {image_path.split('/')[-1]}."

0 commit comments

Comments
 (0)