Skip to content

Commit

Permalink
Replace detect_is_ab_device by field in the config
Browse files Browse the repository at this point in the history
  • Loading branch information
tsterbak committed Mar 27, 2023
1 parent 916386e commit 2df4a66
Show file tree
Hide file tree
Showing 9 changed files with 9 additions and 90 deletions.
1 change: 0 additions & 1 deletion openandroidinstaller/app_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ def __init__(
self.config = None
self.image_path = None
self.recovery_path = None
self.is_ab = None

# store views
self.default_views: List = []
Expand Down
3 changes: 2 additions & 1 deletion openandroidinstaller/assets/configs/sargo.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
metadata:
maintainer: Tobias Sterbak (tsterbak)
device_name: Pixel 3a
device_code: sargo
is_ab_device: true
device_code: sargo
supported_device_codes:
- sargo
requirements:
Expand Down
2 changes: 2 additions & 0 deletions openandroidinstaller/installer_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ def __init__(
self.metadata = metadata
self.requirements = requirements
self.device_code = metadata.get("device_code")
self.is_ab = metadata.get("is_ab_device", False)
self.supported_device_codes = metadata.get("supported_device_codes")
self.twrp_link = metadata.get("twrp-link")

Expand Down Expand Up @@ -161,6 +162,7 @@ def validate_config(config: str) -> bool:
"metadata": {
"maintainer": str,
"device_name": str,
"is_ab_device": bool,
"device_code": str,
"supported_device_codes": [str],
schema.Optional("twrp-link"): str,
Expand Down
40 changes: 0 additions & 40 deletions openandroidinstaller/tooling.py
Original file line number Diff line number Diff line change
Expand Up @@ -468,43 +468,3 @@ def search_device(platform: str, bin_path: Path) -> Optional[str]:
except CalledProcessError:
logger.error("Failed to detect a device.")
return None


def check_ab_partition(platform: str, bin_path: Path) -> Optional[bool]:
"""Figure out, if its an a/b-partitioned device."""
logger.info(f"Run on {platform} with {bin_path}...")
try:
# check if ab device
if platform in ("linux", "darwin"):
output = check_output(
[
str(bin_path.joinpath(Path("adb"))),
"shell",
"getprop",
"|",
"grep",
"ro.boot.slot_suffix",
],
stderr=STDOUT,
).decode()
elif platform in ("windows", "win32"):
output = check_output(
[
str(bin_path.joinpath(Path("adb.exe"))),
"shell",
"getprop",
"|",
"findstr",
"ro.boot.slot_suffix",
],
stderr=STDOUT,
shell=True,
).decode()
else:
raise Exception(f"Unknown platform {platform}.")
logger.info(output)
logger.info("This is an a/b-partitioned device.")
return True
except CalledProcessError:
logger.info("This is not an a/b-partitioned device.")
return False
2 changes: 1 addition & 1 deletion openandroidinstaller/views/install_addons_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ def run_install_addons(self, e):
for line in adb_twrp_install_addons(
addons=self.state.addon_paths,
bin_path=self.state.bin_path,
is_ab=self.state.is_ab,
is_ab=self.state.config.is_ab,
):
# write the line to advanced output terminal
self.terminal_box.write_line(line)
Expand Down
2 changes: 1 addition & 1 deletion openandroidinstaller/views/install_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ def run_install(self, e):
config_path=self.state.config_path,
bin_path=self.state.bin_path,
install_addons=self.state.install_addons,
is_ab=self.state.is_ab,
is_ab=self.state.config.is_ab,
recovery=self.state.recovery_path,
):
# write the line to advanced output terminal
Expand Down
4 changes: 1 addition & 3 deletions openandroidinstaller/views/start_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ def search_devices(self, e):
# search the device
if self.state.test:
# this only happens for testing
device_code, is_ab = self.state.test_config, True
device_code = self.state.test_config
logger.info(
f"Running search in development mode and loading config {device_code}.yaml."
)
Expand All @@ -239,8 +239,6 @@ def search_devices(self, e):
self.device_name.value = device_code
# load config from file
self.state.load_config(device_code)
# write ab-info to state
self.state.is_ab = is_ab
if self.state.config:
device_name = self.state.config.metadata.get(
"device_name", "No device name in config."
Expand Down
2 changes: 1 addition & 1 deletion openandroidinstaller/views/step_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ def call_to_phone(self, e, command: str):
"fastboot_boot_recovery": partial(
fastboot_boot_recovery,
recovery=self.state.recovery_path,
is_ab=self.state.is_ab,
is_ab=self.state.config.is_ab,
),
"fastboot_flash_boot": partial(
fastboot_flash_boot,
Expand Down
43 changes: 1 addition & 42 deletions tests/test_tooling.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
from openandroidinstaller.tooling import (
adb_reboot,
search_device,
check_ab_partition,
)


Expand Down Expand Up @@ -95,44 +94,4 @@ def patched_check_output(*args, **kwargs):
platform="linux", bin_path=Path("openandroidinstaller/bin/")
)

assert device_code == None


def test_check_ab_device_is_ab(mocker):
"""Test if checking for ab device works fine."""
mocker.patch(
"openandroidinstaller.tooling.check_output",
return_value=b"[ro.boot.slot_suffix]: [_b]",
)

# test linux
is_ab = check_ab_partition(
platform="linux", bin_path=Path("openandroidinstaller/bin/")
)

assert is_ab

# test windows
is_ab = check_ab_partition(
platform="windows", bin_path=Path("openandroidinstaller/bin/")
)

assert is_ab


def test_check_ab_device_not_ab(mocker):
"""Test if checking for ab device returns False if it fails."""

def patched_check_output(*args, **kwargs):
raise CalledProcessError(returncode=1, cmd="output is None")

mocker.patch(
"openandroidinstaller.tooling.check_output",
patched_check_output,
)

is_ab = check_ab_partition(
platform="linux", bin_path=Path("openandroidinstaller/bin/")
)

assert not is_ab
assert device_code == None

0 comments on commit 2df4a66

Please sign in to comment.