@@ -51,21 +51,28 @@ def image_works_with_device(supported_device_codes: List[str], image_path: str)
5151 True if the image works with the device, False otherwise.
5252 """
5353 with zipfile .ZipFile (image_path ) as image_zip :
54- with image_zip .open (
55- "META-INF/com/android/metadata" , mode = "r"
56- ) as image_metadata :
57- metadata = image_metadata .readlines ()
58- supported_devices = str (metadata [- 1 ]).split ("=" )[- 1 ][:- 3 ].split ("," )
59- logger .info (f"Image works with device: { supported_devices } " )
60-
61- if any (code in supported_devices for code in supported_device_codes ):
62- logger .success ("Device supported by the selected image." )
63- return True
64- else :
65- logger .error (
66- f"Image file { image_path .split ('/' )[- 1 ]} is not supported."
67- )
68- return False
54+ try :
55+ with image_zip .open (
56+ "META-INF/com/android/metadata" , mode = "r"
57+ ) as image_metadata :
58+ metadata = image_metadata .readlines ()
59+ supported_devices = str (metadata [- 1 ]).split ("=" )[- 1 ][:- 3 ].split ("," )
60+ logger .info (f"Image works with device: { supported_devices } " )
61+
62+ if any (code in supported_devices for code in supported_device_codes ):
63+ logger .success ("Device supported by the selected image." )
64+ return True
65+ else :
66+ logger .error (
67+ f"Image file { image_path .split ('/' )[- 1 ]} is not supported."
68+ )
69+ return False
70+ except KeyError :
71+ logger .error ("Selected image does not contains a metadata file. Can't check compatibility with the device" )
72+ return False
73+ except zipfile .BadZipFile :
74+ logger .error ("Selected image is not a zip file." )
75+ return False
6976
7077
7178def image_sdk_level (image_path : str ) -> int :
0 commit comments