Skip to content

Commit cc7444c

Browse files
committed
Refactor metadata loading from ROM image; improve Makefile
1 parent d3d703f commit cc7444c

File tree

2 files changed

+71
-25
lines changed

2 files changed

+71
-25
lines changed

Makefile

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
.phoney: install export format lint typing test app test-app build-app clean-build
2+
3+
help:
4+
@echo "install - install dependencies"
5+
@echo "export - export dependencies to requirements.txt"
6+
@echo "format - format code with black"
7+
@echo "lint - lint code with ruff"
8+
@echo "typing - type check code with mypy"
9+
@echo "test - run tests"
10+
@echo "app - run app"
11+
@echo "test-app - run app in test mode with test config for sargo"
12+
@echo "build-app - build app"
13+
@echo "clean-build - clean build"
14+
115
poetry:
216
curl -sSL https://install.python-poetry.org | python3 -
317

openandroidinstaller/utils.py

Lines changed: 57 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,34 @@ def get_download_link(devicecode: str) -> Optional[str]:
4040
return None
4141

4242

43+
def retrieve_image_metadata(image_path: str) -> dict:
44+
"""Retrieve metadata from the selected image.
45+
46+
Args:
47+
image_path: Path to the image file.
48+
49+
Returns:
50+
Dictionary containing the metadata.
51+
"""
52+
metapath = "META-INF/com/android/metadata"
53+
try:
54+
with zipfile.ZipFile(image_path) as image_zip:
55+
with image_zip.open(metapath, mode="r") as image_metadata:
56+
metadata = image_metadata.readlines()
57+
metadata_dict = {}
58+
for line in metadata:
59+
metadata_dict[line[: line.find(b"=")].decode("utf-8")] = line[
60+
line.find(b"=") + 1 : -1
61+
].decode("utf-8")
62+
logger.info(f"Metadata retrieved from image {image_path.split('/')[-1]}.")
63+
return metadata_dict
64+
except FileNotFoundError:
65+
logger.error(
66+
f"Metadata file {metapath} not found in {image_path.split('/')[-1]}."
67+
)
68+
return dict()
69+
70+
4371
def image_works_with_device(supported_device_codes: List[str], image_path: str) -> bool:
4472
"""Determine if an image works for the given device.
4573
@@ -50,39 +78,43 @@ def image_works_with_device(supported_device_codes: List[str], image_path: str)
5078
Returns:
5179
True if the image works with the device, False otherwise.
5280
"""
53-
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
81+
metadata = retrieve_image_metadata(image_path)
82+
try:
83+
supported_devices = metadata["pre-device"].split(",")
84+
logger.info(f"Image works with the following device(s): {supported_devices}")
85+
if any(code in supported_devices for code in supported_device_codes):
86+
logger.success("Device supported by the selected image.")
87+
return True
88+
else:
89+
logger.error(f"Image file {image_path.split('/')[-1]} is not supported.")
90+
return False
91+
except KeyError:
92+
logger.error(
93+
f"Could not determine supported devices for {image_path.split('/')[-1]}."
94+
)
95+
return False
6996

7097

7198
def image_sdk_level(image_path: str) -> int:
7299
"""Determine Android version of the selected image.
73100
74101
Example:
75102
Android 13: 33
103+
104+
Args:
105+
image_path: Path to the image file.
106+
107+
Returns:
108+
Android version as integer.
76109
"""
77-
with zipfile.ZipFile(image_path) as image_zip:
78-
with image_zip.open(
79-
"META-INF/com/android/metadata", mode="r"
80-
) as image_metadata:
81-
metadata = image_metadata.readlines()
82-
for line in metadata:
83-
if b"sdk-level" in line:
84-
return int(line[line.find(b"=") + 1 : -1].decode("utf-8"))
85-
return 0
110+
metadata = retrieve_image_metadata(image_path)
111+
try:
112+
sdk_level = metadata["post-sdk-level"]
113+
logger.info(f"Android version of {image_path}: {sdk_level}")
114+
return int(sdk_level)
115+
except (ValueError, TypeError, KeyError) as e:
116+
logger.error(f"Could not determine Android version of {image_path}. Error: {e}")
117+
return 0
86118

87119

88120
def recovery_works_with_device(

0 commit comments

Comments
 (0)