Skip to content

Commit 4e3c70b

Browse files
committed
Try to find the right grep and display a proper message if it is not found
1 parent 367c71b commit 4e3c70b

File tree

3 files changed

+54
-19
lines changed

3 files changed

+54
-19
lines changed

openandroidinstaller/tooling.py

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
"""This module contains functions to deal with tools like adb, fastboot and heimdall."""
2+
23
# This file is part of OpenAndroidInstaller.
34
# OpenAndroidInstaller is free software: you can redistribute it and/or modify it under the terms of
45
# the GNU General Public License as published by the Free Software Foundation,
@@ -10,6 +11,7 @@
1011
# If not, see <https://www.gnu.org/licenses/>."""
1112
# Author: Tobias Sterbak
1213
import shlex
14+
from dataclasses import dataclass
1315
import subprocess
1416
import sys
1517
from pathlib import Path
@@ -574,24 +576,49 @@ def heimdall_flash_recovery(bin_path: Path, recovery: str) -> TerminalResponse:
574576
yield line
575577

576578

577-
def search_device(platform: str, bin_path: Path) -> Optional[str]:
579+
@dataclass(frozen=True)
580+
class SearchResult:
581+
"""Result of the device search.
582+
583+
Attributes:
584+
device_code: The device code of the connected device.
585+
msg: Message describing the result.
586+
"""
587+
588+
device_code: str = None
589+
msg: str = None
590+
591+
592+
def search_device(platform: str, bin_path: Path) -> SearchResult:
578593
"""Search for a connected device."""
579594
logger.info(f"Search devices on {platform} with {bin_path}...")
580595
try:
581596
# read device code
582597
if platform in ("linux", "darwin"):
598+
# check if grep is installed and find the right path
599+
try:
600+
grep_command = check_output(["which", "grep"]).decode().strip()
601+
except CalledProcessError:
602+
logger.error(
603+
"Failed to detect a device. Please make sure `grep` it is installed."
604+
)
605+
return SearchResult(
606+
msg="Failed to detect a device. Please make sure `grep` it is installed."
607+
)
608+
# run the command to get the device code
583609
output = check_output(
584610
[
585611
str(bin_path.joinpath(Path("adb"))),
586612
"shell",
587613
"getprop",
588614
"|",
589-
"grep",
615+
grep_command,
590616
"ro.product.device",
591617
],
592618
stderr=STDOUT,
593619
).decode()
594620
elif platform in ("windows", "win32"):
621+
# run the command to get the device code on windows
595622
output = check_output(
596623
[
597624
str(bin_path.joinpath(Path("adb.exe"))),
@@ -608,7 +635,12 @@ def search_device(platform: str, bin_path: Path) -> Optional[str]:
608635
raise Exception(f"Unknown platform {platform}.")
609636
device_code = output.split("[")[-1].strip()[:-1].strip()
610637
logger.info(f"Found device code '{device_code}'")
611-
return device_code
638+
return SearchResult(
639+
device_code=device_code,
640+
msg=f"Found device with device code '{device_code}'.",
641+
)
612642
except CalledProcessError:
613643
logger.error("Failed to detect a device.")
614-
return None
644+
return SearchResult(
645+
msg="Failed to detect a device. Connect to USB and try again."
646+
)

openandroidinstaller/utils.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
"""This file contains some utility functions."""
2+
23
# This file is part of OpenAndroidInstaller.
34
# OpenAndroidInstaller is free software: you can redistribute it and/or modify it under the terms of
45
# the GNU General Public License as published by the Free Software Foundation,

openandroidinstaller/views/start_view.py

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
"""Contains the start view."""
2+
23
# This file is part of OpenAndroidInstaller.
34
# OpenAndroidInstaller is free software: you can redistribute it and/or modify it under the terms of
45
# the GNU General Public License as published by the Free Software Foundation,
@@ -30,7 +31,7 @@
3031
from flet_core.buttons import CountinuosRectangleBorder
3132
from loguru import logger
3233
from styles import Markdown, Text
33-
from tooling import search_device
34+
from tooling import search_device, SearchResult
3435
from views import BaseView
3536
from widgets import get_title
3637

@@ -219,29 +220,30 @@ def search_devices_clicked(self, e):
219220
# search the device
220221
if self.state.test:
221222
# this only happens for testing
222-
device_code = self.state.test_config
223+
result = SearchResult(
224+
device_code=self.state.test_config,
225+
msg=f"Found device with device code '{self.state.test_config}'.",
226+
)
223227
logger.info(
224-
f"Running search in development mode and loading config {device_code}.yaml."
228+
f"Running search in development mode and loading config {result.device_code}.yaml."
225229
)
226230
else:
227-
device_code = search_device(
231+
result = search_device(
228232
platform=self.state.platform, bin_path=self.state.bin_path
229233
)
230-
if device_code:
231-
self.device_name.value = device_code
234+
if result.device_code:
235+
self.device_name.value = result.device_code
232236
self.device_name.color = colors.BLACK
233237
else:
234238
logger.info("No device detected! Connect to USB and try again.")
235-
self.device_name.value = (
236-
"No device detected! Connect to USB and try again."
237-
)
239+
self.device_name.value = result.msg
238240
self.device_name.color = colors.RED
239241

240242
# load the config, if a device is detected
241-
if device_code:
242-
self.device_name.value = device_code
243+
if result.device_code:
244+
self.device_name.value = result.device_code
243245
# load config from file
244-
self.state.load_config(device_code)
246+
self.state.load_config(result.device_code)
245247
if self.state.config:
246248
device_name = self.state.config.metadata.get(
247249
"device_name", "No device name in config."
@@ -265,13 +267,13 @@ def search_devices_clicked(self, e):
265267
else:
266268
# failed to load config or device is not supported
267269
logger.error(
268-
f"Device with code '{device_code}' is not supported or the config is corrupted. Please check the logs for more information."
270+
f"Device with code '{result.device_code}' is not supported or the config is corrupted. Please check the logs for more information."
269271
)
270272
self.device_name.value = (
271-
f"Device with code '{device_code}' is not supported yet."
273+
f"Device with code '{result.device_code}' is not supported yet."
272274
)
273275
# add request support for device button
274-
request_url = f"https://github.com/openandroidinstaller-dev/openandroidinstaller/issues/new?labels=device&template=device-support-request.yaml&title=Add support for `{device_code}`"
276+
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}`"
275277
self.device_request_row.controls.append(
276278
ElevatedButton(
277279
"Request support for this device",

0 commit comments

Comments
 (0)