From 4e3c70b20edc9be4ae7e563957e9b6aa87394469 Mon Sep 17 00:00:00 2001 From: Tobias Sterbak Date: Wed, 7 Feb 2024 10:54:19 +0000 Subject: [PATCH 1/2] Try to find the right grep and display a proper message if it is not found --- openandroidinstaller/tooling.py | 40 +++++++++++++++++++++--- openandroidinstaller/utils.py | 1 + openandroidinstaller/views/start_view.py | 32 ++++++++++--------- 3 files changed, 54 insertions(+), 19 deletions(-) diff --git a/openandroidinstaller/tooling.py b/openandroidinstaller/tooling.py index 9fb01d2b..8991a4bb 100644 --- a/openandroidinstaller/tooling.py +++ b/openandroidinstaller/tooling.py @@ -1,4 +1,5 @@ """This module contains functions to deal with tools like adb, fastboot and heimdall.""" + # This file is part of OpenAndroidInstaller. # OpenAndroidInstaller is free software: you can redistribute it and/or modify it under the terms of # the GNU General Public License as published by the Free Software Foundation, @@ -10,6 +11,7 @@ # If not, see .""" # Author: Tobias Sterbak import shlex +from dataclasses import dataclass import subprocess import sys from pathlib import Path @@ -574,24 +576,49 @@ def heimdall_flash_recovery(bin_path: Path, recovery: str) -> TerminalResponse: yield line -def search_device(platform: str, bin_path: Path) -> Optional[str]: +@dataclass(frozen=True) +class SearchResult: + """Result of the device search. + + Attributes: + device_code: The device code of the connected device. + msg: Message describing the result. + """ + + device_code: str = None + msg: str = None + + +def search_device(platform: str, bin_path: Path) -> SearchResult: """Search for a connected device.""" logger.info(f"Search devices on {platform} with {bin_path}...") try: # read device code if platform in ("linux", "darwin"): + # check if grep is installed and find the right path + try: + grep_command = check_output(["which", "grep"]).decode().strip() + except CalledProcessError: + logger.error( + "Failed to detect a device. Please make sure `grep` it is installed." + ) + return SearchResult( + msg="Failed to detect a device. Please make sure `grep` it is installed." + ) + # run the command to get the device code output = check_output( [ str(bin_path.joinpath(Path("adb"))), "shell", "getprop", "|", - "grep", + grep_command, "ro.product.device", ], stderr=STDOUT, ).decode() elif platform in ("windows", "win32"): + # run the command to get the device code on windows output = check_output( [ str(bin_path.joinpath(Path("adb.exe"))), @@ -608,7 +635,12 @@ def search_device(platform: str, bin_path: Path) -> Optional[str]: raise Exception(f"Unknown platform {platform}.") device_code = output.split("[")[-1].strip()[:-1].strip() logger.info(f"Found device code '{device_code}'") - return device_code + return SearchResult( + device_code=device_code, + msg=f"Found device with device code '{device_code}'.", + ) except CalledProcessError: logger.error("Failed to detect a device.") - return None + return SearchResult( + msg="Failed to detect a device. Connect to USB and try again." + ) diff --git a/openandroidinstaller/utils.py b/openandroidinstaller/utils.py index 3db391d7..a15b9d2a 100644 --- a/openandroidinstaller/utils.py +++ b/openandroidinstaller/utils.py @@ -1,4 +1,5 @@ """This file contains some utility functions.""" + # This file is part of OpenAndroidInstaller. # OpenAndroidInstaller is free software: you can redistribute it and/or modify it under the terms of # the GNU General Public License as published by the Free Software Foundation, diff --git a/openandroidinstaller/views/start_view.py b/openandroidinstaller/views/start_view.py index 2057ad39..a3a88b7d 100644 --- a/openandroidinstaller/views/start_view.py +++ b/openandroidinstaller/views/start_view.py @@ -1,4 +1,5 @@ """Contains the start view.""" + # This file is part of OpenAndroidInstaller. # OpenAndroidInstaller is free software: you can redistribute it and/or modify it under the terms of # the GNU General Public License as published by the Free Software Foundation, @@ -30,7 +31,7 @@ from flet_core.buttons import CountinuosRectangleBorder from loguru import logger from styles import Markdown, Text -from tooling import search_device +from tooling import search_device, SearchResult from views import BaseView from widgets import get_title @@ -219,29 +220,30 @@ def search_devices_clicked(self, e): # search the device if self.state.test: # this only happens for testing - device_code = self.state.test_config + result = SearchResult( + device_code=self.state.test_config, + msg=f"Found device with device code '{self.state.test_config}'.", + ) logger.info( - f"Running search in development mode and loading config {device_code}.yaml." + f"Running search in development mode and loading config {result.device_code}.yaml." ) else: - device_code = search_device( + result = search_device( platform=self.state.platform, bin_path=self.state.bin_path ) - if device_code: - self.device_name.value = device_code + if result.device_code: + self.device_name.value = result.device_code self.device_name.color = colors.BLACK else: logger.info("No device detected! Connect to USB and try again.") - self.device_name.value = ( - "No device detected! Connect to USB and try again." - ) + self.device_name.value = result.msg self.device_name.color = colors.RED # load the config, if a device is detected - if device_code: - self.device_name.value = device_code + if result.device_code: + self.device_name.value = result.device_code # load config from file - self.state.load_config(device_code) + self.state.load_config(result.device_code) if self.state.config: device_name = self.state.config.metadata.get( "device_name", "No device name in config." @@ -265,13 +267,13 @@ def search_devices_clicked(self, e): else: # failed to load config or device is not supported logger.error( - f"Device with code '{device_code}' is not supported or the config is corrupted. Please check the logs for more information." + f"Device with code '{result.device_code}' is not supported or the config is corrupted. Please check the logs for more information." ) self.device_name.value = ( - f"Device with code '{device_code}' is not supported yet." + f"Device with code '{result.device_code}' is not supported yet." ) # add request support for device button - request_url = f"https://github.com/openandroidinstaller-dev/openandroidinstaller/issues/new?labels=device&template=device-support-request.yaml&title=Add support for `{device_code}`" + request_url = f"https://github.com/openandroidinstaller-dev/openandroidinstaller/issues/new?labels=device&template=device-support-request.yaml&title=Add support for `{result.device_code}`" self.device_request_row.controls.append( ElevatedButton( "Request support for this device", From 67ea0f1907fbdfc4d1641b1c9aaeae9960de62c8 Mon Sep 17 00:00:00 2001 From: Tobias Sterbak Date: Wed, 7 Feb 2024 17:43:08 +0000 Subject: [PATCH 2/2] Fix error message --- openandroidinstaller/tooling.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/openandroidinstaller/tooling.py b/openandroidinstaller/tooling.py index 8991a4bb..0403264b 100644 --- a/openandroidinstaller/tooling.py +++ b/openandroidinstaller/tooling.py @@ -600,10 +600,10 @@ def search_device(platform: str, bin_path: Path) -> SearchResult: grep_command = check_output(["which", "grep"]).decode().strip() except CalledProcessError: logger.error( - "Failed to detect a device. Please make sure `grep` it is installed." + "Failed to detect a device. Please make sure `grep` is installed." ) return SearchResult( - msg="Failed to detect a device. Please make sure `grep` it is installed." + msg="Failed to detect a device. Please make sure `grep` is installed." ) # run the command to get the device code output = check_output(