From 9b4b63314b65a4bc27fef094b95e49d3ba0673ec Mon Sep 17 00:00:00 2001 From: rudu <40572253+anon1892@users.noreply.github.com> Date: Sun, 14 Jan 2024 10:22:18 +0100 Subject: [PATCH 1/3] Little fix --- openandroidinstaller/utils.py | 37 +++++++++++++++++++++-------------- 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/openandroidinstaller/utils.py b/openandroidinstaller/utils.py index a8511611..ab57f4a7 100644 --- a/openandroidinstaller/utils.py +++ b/openandroidinstaller/utils.py @@ -51,21 +51,28 @@ def image_works_with_device(supported_device_codes: List[str], image_path: str) True if the image works with the device, False otherwise. """ with zipfile.ZipFile(image_path) as image_zip: - with image_zip.open( - "META-INF/com/android/metadata", mode="r" - ) as image_metadata: - metadata = image_metadata.readlines() - supported_devices = str(metadata[-1]).split("=")[-1][:-3].split(",") - logger.info(f"Image works with device: {supported_devices}") - - if any(code in supported_devices for code in supported_device_codes): - logger.success("Device supported by the selected image.") - return True - else: - logger.error( - f"Image file {image_path.split('/')[-1]} is not supported." - ) - return False + try: + with image_zip.open( + "META-INF/com/android/metadata", mode="r" + ) as image_metadata: + metadata = image_metadata.readlines() + supported_devices = str(metadata[-1]).split("=")[-1][:-3].split(",") + logger.info(f"Image works with device: {supported_devices}") + + if any(code in supported_devices for code in supported_device_codes): + logger.success("Device supported by the selected image.") + return True + else: + logger.error( + f"Image file {image_path.split('/')[-1]} is not supported." + ) + return False + except KeyError: + logger.error("Selected image does not contains a metadata file. Can't check compatibility with the device") + return False + except zipfile.BadZipFile: + logger.error("Selected image is not a zip file.") + return False def image_sdk_level(image_path: str) -> int: From 6da6055e5b3133aea80be8344a4b26f5de25bb03 Mon Sep 17 00:00:00 2001 From: Tobias Sterbak Date: Sun, 14 Jan 2024 20:56:31 +0000 Subject: [PATCH 2/3] Move exception handling of zipfile exception up on function call --- openandroidinstaller/utils.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/openandroidinstaller/utils.py b/openandroidinstaller/utils.py index b6ecbd9a..8291a18e 100644 --- a/openandroidinstaller/utils.py +++ b/openandroidinstaller/utils.py @@ -84,9 +84,9 @@ 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: + except zipfile.BadZipFile as e: logger.error("Selected image is not a zip file.") - return dict + raise e except (FileNotFoundError, KeyError): logger.error( f"Metadata file {metapath} not found in {image_path.split('/')[-1]}." @@ -132,8 +132,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): @@ -148,6 +148,11 @@ def image_works_with_device( CompatibilityStatus.INCOMPATIBLE, f"Image file {image_path.split('/')[-1]} is not supported by device code.", ) + except zipfile.BadZipFile: + 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]}." From b0110dd71740492e25f6651d5adc56a8a362d0bd Mon Sep 17 00:00:00 2001 From: Tobias Sterbak Date: Sun, 14 Jan 2024 20:59:46 +0000 Subject: [PATCH 3/3] Fix errorhandling for image_sdk_level --- openandroidinstaller/utils.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/openandroidinstaller/utils.py b/openandroidinstaller/utils.py index 8291a18e..eb503f9f 100644 --- a/openandroidinstaller/utils.py +++ b/openandroidinstaller/utils.py @@ -85,7 +85,6 @@ def retrieve_image_metadata(image_path: str) -> dict: logger.info(f"Metadata retrieved from image {image_path.split('/')[-1]}.") return metadata_dict except zipfile.BadZipFile as e: - logger.error("Selected image is not a zip file.") raise e except (FileNotFoundError, KeyError): logger.error( @@ -110,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 @@ -149,6 +148,7 @@ def image_works_with_device( 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.",