Skip to content

Commit

Permalink
Supported device codes for a config are now a list; config is loaded …
Browse files Browse the repository at this point in the history
…based on this
  • Loading branch information
tsterbak committed Mar 23, 2023
1 parent b17020a commit c7706b9
Show file tree
Hide file tree
Showing 17 changed files with 94 additions and 71 deletions.
7 changes: 5 additions & 2 deletions openandroidinstaller/assets/configs/avicii.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
metadata:
maintainer: Tobias Sterbak (tsterbak)
devicename: OnePlus Nord
devicecode: avicii
device_name: OnePlus Nord
device_code: avicii
supported_device_codes:
- avicii
- Nord
requirements:
android: 10
steps:
Expand Down
7 changes: 5 additions & 2 deletions openandroidinstaller/assets/configs/cheeseburger.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
metadata:
maintainer: SirRGB
devicename: OnePlus 5
devicecode: cheeseburger
device_name: OnePlus 5
device_code: cheeseburger
supported_device_codes:
- cheeseburger
- OnePlus5
twrp-link: cheeseburger_dumpling
steps:
unlock_bootloader:
Expand Down
6 changes: 4 additions & 2 deletions openandroidinstaller/assets/configs/d2s.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
metadata:
maintainer: Tobias Sterbak (tsterbak)
devicename: Samsung Galaxy Note 10+
devicecode: d2s
device_name: Samsung Galaxy Note 10+
device_code: d2s
supported_device_codes:
- d2s
requirements:
android: 12
steps:
Expand Down
7 changes: 5 additions & 2 deletions openandroidinstaller/assets/configs/dre.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
metadata:
maintainer: Tobias Sterbak (tsterbak)
devicename: OnePlus Nord N200
devicecode: dre
device_name: OnePlus Nord N200
device_code: dre
supported_device_codes:
- dre
- NordN200
requirements:
android: 11
steps:
Expand Down
7 changes: 5 additions & 2 deletions openandroidinstaller/assets/configs/dumpling.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
metadata:
maintainer: SirRGB
devicename: OnePlus 5T
devicecode: dumpling
device_name: OnePlus 5T
device_code: dumpling
supported_device_codes:
- dumpling
- OnePlus5T
twrp-link: cheeseburger_dumpling
steps:
unlock_bootloader:
Expand Down
7 changes: 5 additions & 2 deletions openandroidinstaller/assets/configs/enchilada.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
metadata:
maintainer: Tobias Sterbak (tsterbak)
devicename: OnePlus 6
devicecode: enchilada
device_name: OnePlus 6
device_code: enchilada
supported_device_codes:
- enchilada
- OnePlus6
requirements:
android: 11
steps:
Expand Down
7 changes: 5 additions & 2 deletions openandroidinstaller/assets/configs/fajita.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
metadata:
maintainer: Tobias Sterbak (tsterbak)
devicename: OnePlus 6T
devicecode: fajita
device_name: OnePlus 6T
device_code: fajita
supported_device_codes:
- fajita
- OnePlus6T
requirements:
android: 11
steps:
Expand Down
7 changes: 5 additions & 2 deletions openandroidinstaller/assets/configs/guacamole.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
metadata:
maintainer: Tobias Sterbak (tsterbak)
devicename: OnePlus 7 Pro
devicecode: guacamole
device_name: OnePlus 7 Pro
device_code: guacamole
supported_device_codes:
- guacamole
- OnePlus7Pro
requirements:
android: 12
steps:
Expand Down
7 changes: 5 additions & 2 deletions openandroidinstaller/assets/configs/guacamoleb.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
metadata:
maintainer: Tobias Sterbak (tsterbak)
devicename: OnePlus 7
devicecode: guacamoleb
device_name: OnePlus 7
device_code: guacamoleb
supported_device_codes:
- guacamoleb
- OnePlus7
requirements:
android: 12
steps:
Expand Down
7 changes: 5 additions & 2 deletions openandroidinstaller/assets/configs/hotdog.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
metadata:
maintainer: Tobias Sterbak (tsterbak)
devicename: OnePlus 7T Pro
devicecode: hotdog
device_name: OnePlus 7T Pro
device_code: hotdog
supported_device_codes:
- hotdog
- OnePlus7TPro
requirements:
android: 12
steps:
Expand Down
7 changes: 5 additions & 2 deletions openandroidinstaller/assets/configs/hotdogb.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
metadata:
maintainer: Tobias Sterbak (tsterbak)
devicename: OnePlus 7T
devicecode: hotdogb
device_name: OnePlus 7T
device_code: hotdogb
supported_device_codes:
- hotdogb
- OnePlus7T
requirements:
android: 12
steps:
Expand Down
6 changes: 4 additions & 2 deletions openandroidinstaller/assets/configs/sargo.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
metadata:
maintainer: Tobias Sterbak (tsterbak)
devicename: Pixel 3a
devicecode: sargo
device_name: Pixel 3a
device_code: sargo
supported_device_codes:
- sargo
requirements:
android: 12.1.0
steps:
Expand Down
7 changes: 5 additions & 2 deletions openandroidinstaller/assets/configs/yuga.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
metadata:
maintainer: Tobias Sterbak (tsterbak)
devicename: Sony Xperia Z
devicecode: yuga
device_name: Sony Xperia Z
device_code: yuga
supported_device_codes:
- yuga
- C6603
steps:
unlock_bootloader:
- type: confirm_button
Expand Down
55 changes: 23 additions & 32 deletions openandroidinstaller/installer_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,23 +50,6 @@ def __init__(

class InstallerConfig:

# map some detected device codes to their real code.
device_code_mapping: Dict[str, str] = {
# Sony issues
"C6603": "yuga",
# OnePlus issues
"OnePlus5": "cheeseburger",
"OnePlus5T": "dumpling",
"OnePlus6": "enchilada",
"OnePlus6T": "fajita",
"OnePlus7": "guacamoleb",
"OnePlus7Pro": "guacamole",
"OnePlus7T": "hotdogb",
"OnePlus7TPro": "hotdog",
"Nord": "avicii",
"NordN200": "dre",
}

def __init__(
self,
unlock_bootloader: List[Step],
Expand All @@ -78,15 +61,10 @@ def __init__(
self.flash_recovery = flash_recovery
self.metadata = metadata
self.requirements = requirements
self.device_code = metadata.get("devicecode")
self.device_code = metadata.get("device_code")
self.supported_device_codes = metadata.get("supported_device_codes")
self.twrp_link = metadata.get("twrp-link")

# manage device codes and alternative device codes/names
inverted_mapping: Dict[str, str] = dict(map(reversed, self.device_code_mapping.items())) # type: ignore
self.alternative_device_code = inverted_mapping.get(
self.device_code, self.device_code # type: ignore
)

@classmethod
def from_file(cls, path):
with open(path, "r", encoding="utf-8") as stream:
Expand Down Expand Up @@ -118,25 +96,37 @@ def from_file(cls, path):
return cls(unlock_bootloader, flash_recovery, metadata, requirements)


def _find_config_file(device_code: str, config_path: Path) -> Optional[Path]:
"""Find the config file which is supported by the given device code."""
for path in config_path.rglob("*.yaml"):
with open(path, "r", encoding="utf-8") as stream:
try:
raw_config = dict(yaml.safe_load(stream))
if device_code in raw_config.get("metadata", dict()).get("supported_device_codes", []):
logger.info(f"Device code '{device_code}' is supported by config '{path}'.")
return path
except:
pass
return None


def _load_config(device_code: str, config_path: Path) -> Optional[InstallerConfig]:
"""
Function to load a function from given path and directory path.
Try to load local file in the same directory as the executable first, then load from assets.
"""
# try loading a custom local file first
mapped_device_code = InstallerConfig.device_code_mapping.get(
device_code, device_code
)
custom_path = Path.cwd().joinpath(Path(f"{mapped_device_code}.yaml"))
custom_path = _find_config_file(device_code, config_path=Path.cwd())
try:
config = InstallerConfig.from_file(custom_path)
logger.info(f"Loaded custom device config from {custom_path}.")
logger.info(f"Config metadata: {config.metadata}.")
return config
except FileNotFoundError:
# if no localfile, then try to load a config file from assets
path = config_path.joinpath(Path(f"{mapped_device_code}.yaml"))
path = _find_config_file(device_code, config_path)

# path = config_path.joinpath(Path(f"{mapped_device_code}.yaml"))
try:
config = InstallerConfig.from_file(path)
logger.info(f"Loaded device config from {path}.")
Expand Down Expand Up @@ -168,8 +158,9 @@ def validate_config(config: str) -> bool:
{
"metadata": {
"maintainer": str,
"devicename": str,
"devicecode": str,
"device_name": str,
"device_code": str,
"supported_device_codes": [str],
schema.Optional("twrp-link"): str,
},
schema.Optional("requirements"): {
Expand Down
8 changes: 3 additions & 5 deletions openandroidinstaller/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
# Author: Tobias Sterbak

import zipfile
from typing import Optional
from typing import Optional, List

import requests
from loguru import logger
Expand All @@ -40,7 +40,7 @@ def get_download_link(devicecode: str) -> Optional[str]:


def image_works_with_device(
device_code: str, alternative_device_code: str, image_path: str
supported_device_codes: List[str], image_path: str
) -> bool:
"""Determine if an image works for the given device."""
with zipfile.ZipFile(image_path) as image_zip:
Expand All @@ -51,9 +51,7 @@ def image_works_with_device(
supported_devices = str(metadata[-1]).split("=")[-1][:-3].split(",")
logger.info(f"Image works with device: {supported_devices}")

if (device_code in supported_devices) or (
alternative_device_code in 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:
Expand Down
9 changes: 3 additions & 6 deletions openandroidinstaller/views/select_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def build(self):

# download link
self.download_link = get_download_link(
self.state.config.metadata.get("devicecode", "ERROR")
self.state.config.metadata.get("device_code", "NOTFOUND")
)

# attach hidden dialogues
Expand Down Expand Up @@ -256,10 +256,8 @@ def pick_image_result(self, e: FilePickerResultEvent):
logger.info("No image selected.")
# check if the image works with the device and show the filename in different colors accordingly
if e.files:
device_code = self.state.config.device_code
if image_works_with_device(
device_code=device_code,
alternative_device_code=self.state.config.alternative_device_code,
supported_device_codes=self.state.config.supported_device_codes,
image_path=self.state.image_path,
):
self.selected_image.color = colors.GREEN
Expand Down Expand Up @@ -300,8 +298,7 @@ def enable_button_if_ready(self, e):
device_code = self.state.config.device_code
if not (
image_works_with_device(
device_code=device_code,
alternative_device_code=self.state.config.alternative_device_code,
supported_device_codes=self.state.config.supported_device_codes,
image_path=self.state.image_path,
)
and recovery_works_with_device(
Expand Down
4 changes: 2 additions & 2 deletions openandroidinstaller/views/start_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ def search_devices(self, e):
self.state.is_ab = is_ab
if self.state.config:
device_name = self.state.config.metadata.get(
"devicename", "No device name in config."
"device_name", "No device name in config."
)
else:
device_name = None
Expand All @@ -254,7 +254,7 @@ def search_devices(self, e):
self.continue_button.disabled = False
self.bootloader_switch.disabled = False
# overwrite the text field with the real name from the config
self.device_name.value = f"{device_name} (code: {InstallerConfig.device_code_mapping.get(device_code, device_code)})"
self.device_name.value = f"{device_name} (code: {self.state.config.device_code})"
self.device_name.color = colors.GREEN
else:
# failed to load config
Expand Down

0 comments on commit c7706b9

Please sign in to comment.