Skip to content

Commit 307e732

Browse files
committed
Improve logging and docstrings
1 parent 32841e1 commit 307e732

File tree

4 files changed

+99
-16
lines changed

4 files changed

+99
-16
lines changed

openandroidinstaller/openandroidinstaller.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@
5757

5858

5959
PLATFORM = sys.platform
60-
logger.info(f"Running OpenAndroidInstaller on {PLATFORM}")
6160
# Define asset paths
6261
CONFIG_PATH = Path(__file__).parent.joinpath(Path("assets/configs")).resolve()
6362
IMAGE_PATH = Path(__file__).parent.joinpath(Path("assets/imgs")).resolve()
@@ -228,6 +227,7 @@ def search_devices(self, e):
228227
# select a new path and load again
229228
pass
230229
except CalledProcessError:
230+
logger.info(f"Did not detect a device.")
231231
if DEVELOPMENT:
232232
path = CONFIG_PATH.joinpath(Path(f"{DEVELOPMENT_CONFIG}.yaml"))
233233
load_config_success = self.load_config(path)
@@ -344,7 +344,7 @@ def build(
344344

345345

346346
class MainView(UserControl):
347-
def __init__(self, page: Page):
347+
def __init__(self):
348348
super().__init__()
349349
self.config = None
350350
# initialize the progress bar indicator
@@ -427,22 +427,27 @@ def load_config(self, path: str):
427427
try:
428428
self.config = InstallerConfig.from_file(path)
429429
self.num_total_steps = len(self.config.steps)
430+
logger.info(f"Loaded device config from {path}.")
431+
logger.info(f"Config metadata: {self.config.metadata}.")
430432
return self.config.metadata.get("devicename", "No device name in config.")
431433
except FileNotFoundError:
434+
logger.info(f"No device config found for {path}.")
432435
return False
433436

434437
def pick_image_result(self, e: FilePickerResultEvent):
435438
self.selected_image.value = (
436439
", ".join(map(lambda f: f.name, e.files)) if e.files else "Cancelled!"
437440
)
438441
self.image_path = e.files[0].path
442+
logger.info(f"Selected image from {self.image_path}")
439443
self.selected_image.update()
440444

441445
def pick_recovery_result(self, e: FilePickerResultEvent):
442446
self.selected_recovery.value = (
443447
", ".join(map(lambda f: f.name, e.files)) if e.files else "Cancelled!"
444448
)
445449
self.recovery_path = e.files[0].path
450+
logger.info(f"Selected recovery from {self.recovery_path}")
446451
self.selected_recovery.update()
447452

448453

@@ -500,7 +505,11 @@ def build(self):
500505
return self.view
501506

502507
def call_to_phone(self, e, command: str):
503-
# TODO: use proper windows paths
508+
"""
509+
Run the command given on the phone.
510+
511+
Some parts of the command are changed by placeholders.
512+
"""
504513
command = command.replace("adb", str(BIN_PATH.joinpath(Path("adb"))))
505514
command = command.replace("fastboot", str(BIN_PATH.joinpath(Path("fastboot"))))
506515
command = command.replace("heimdall", str(BIN_PATH.joinpath(Path("heimdall"))))
@@ -518,6 +527,7 @@ def call_to_phone(self, e, command: str):
518527
logger.info(f"Run command: {command}")
519528
res = call(f"{command}", shell=True)
520529
if res != 0:
530+
logger.info(f"Command {command} failed.")
521531
self.right_view.controls.pop()
522532
self.right_view.controls.append(Text("Command {command} failed!"))
523533
else:
@@ -526,10 +536,12 @@ def call_to_phone(self, e, command: str):
526536
self.right_view.controls.append(
527537
ElevatedButton("Confirm and continue", on_click=self.on_confirm)
528538
)
539+
logger.info("Success.")
529540
self.view.update()
530541

531542

532543
def main(page: Page):
544+
logger.info(f"Running OpenAndroidInstaller on {PLATFORM}")
533545
# Configure the application base page
534546
page.title = "OpenAndroidInstaller"
535547
page.window_height = 720
@@ -581,11 +593,10 @@ def close_banner(e):
581593
)
582594
# TODO: disable the banner for now
583595
# page.banner.open = True
584-
585-
page.update()
596+
# page.update()
586597

587598
# create application instance
588-
app = MainView(page)
599+
app = MainView()
589600

590601
# add application's root control to the page
591602
page.add(app)

openandroidinstaller/widgets.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ def get_title(title: str):
3838
def confirm_button(
3939
text: str, confirm_func: Callable, confirm_text: str = "Confirm and continue"
4040
) -> Column:
41+
"""Get a button, that calls a given function when clicked."""
4142
return Column(
4243
[
4344
Text(f"{text}"),
@@ -59,6 +60,7 @@ def confirm_button(
5960
def call_button(
6061
text: str, call_func: Callable, command: str, confirm_text: str = "Confirm and run"
6162
) -> Column:
63+
"""Get a button, that calls a given function with given command when clicked."""
6264
return Column(
6365
[
6466
Text(f"{text}"),

poetry.lock

Lines changed: 62 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

scripts/download-tools.py

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,13 @@
66
from pathlib import Path
77
import zipfile
88
from io import BytesIO
9+
from loguru import logger
10+
import click
911

1012

1113
def download_adb_fastboot(platform: str):
14+
"""Download adb and fastboot executable from dl.google.com, extract the zip and save to file."""
15+
logger.info(f"Download adb and fastboot for {platform}...")
1216
url = f"https://dl.google.com/android/repository/platform-tools-latest-{platform}.zip"
1317
# Downloading the file by sending the request to the URL
1418
response = requests.get(url, allow_redirects=True)
@@ -19,10 +23,12 @@ def download_adb_fastboot(platform: str):
1923
download_path = Path(__file__).parent.joinpath(Path("tools")).resolve()
2024
file = zipfile.ZipFile(BytesIO(response.content))
2125
file.extractall(download_path.name)
22-
return filename
26+
logger.info("DONE.")
2327

2428

2529
def download_heimdall(platform: str):
30+
"""Download heimdall executable from ubuntu.com, extract the zip and save to file."""
31+
logger.info(f"Download heimdall for {platform}...")
2632
url = f"https://people.ubuntu.com/~neothethird/heimdall-{platform}.zip"
2733
# Downloading the file by sending the request to the URL
2834
response = requests.get(url, allow_redirects=True)
@@ -33,12 +39,13 @@ def download_heimdall(platform: str):
3339
download_path = Path(__file__).parent.joinpath(Path("heimdall")).resolve()
3440
file = zipfile.ZipFile(BytesIO(response.content))
3541
file.extractall(download_path.name)
36-
return filename
37-
42+
logger.info("DONE.")
3843

3944

4045
def move_files_to_lib():
46+
"""Move files to the expected path in the openandroidinstaller package."""
4147
target_path = Path("openandroidinstaller/bin", exist_ok=True)
48+
logger.info(f"Move executables to {target_path}...")
4249
target_path.mkdir()
4350
# move adb
4451
adb_path = Path(__file__).parent.joinpath(Path("../tools/platform-tools/adb")).resolve()
@@ -52,17 +59,19 @@ def move_files_to_lib():
5259
hd_path = Path(__file__).parent.joinpath(Path("../heimdall/heimdall")).resolve()
5360
hd_target_path = Path(__file__).parent.joinpath(Path("../openandroidinstaller/bin/heimdall")).resolve()
5461
hd_path.rename(hd_target_path)
62+
logger.info("DONE.")
5563
# make executable
64+
logger.info("Allow the executables to be executed.")
5665
adb_target_path.chmod(0o755)
5766
fb_target_path.chmod(0o755)
5867
hd_target_path.chmod(0o755)
59-
print("Done")
68+
logger.info("DONE.")
6069

61-
62-
def main():
63-
filename = download_adb_fastboot(platform="linux")
64-
print(filename)
65-
filename = download_heimdall(platform="linux")
70+
@click.command()
71+
@click.option("--platform", help="On which platform should the tools work?", default="linux")
72+
def main(platform: str):
73+
download_adb_fastboot(platform=platform)
74+
download_heimdall(platform=platform)
6675
move_files_to_lib()
6776

6877

0 commit comments

Comments
 (0)