|
15 | 15 |
|
16 | 16 | import sys |
17 | 17 | from pathlib import Path |
18 | | -from subprocess import STDOUT, CalledProcessError, call, check_output |
19 | | -from typing import Optional |
| 18 | +from subprocess import STDOUT, CalledProcessError, call, check_output, PIPE, run, CompletedProcess |
| 19 | +from typing import Optional, List |
20 | 20 |
|
21 | 21 | import regex as re |
22 | 22 | from loguru import logger |
|
25 | 25 | PLATFORM = sys.platform |
26 | 26 |
|
27 | 27 |
|
| 28 | +def run_command(tool: str, command: List[str], bin_path: Path) -> CompletedProcess: |
| 29 | + """Run a command with a tool (adb, fastboot, heimdall).""" |
| 30 | + if tool not in ["adb", "fastboot", "heimdall"]: |
| 31 | + raise Exception(f"Unknown tool {tool}. Use adb, fastboot or heimdall.") |
| 32 | + if PLATFORM == "win32": |
| 33 | + full_command = [str(bin_path + f"{tool}.exe")] + command |
| 34 | + else: |
| 35 | + full_command = [str(bin_path + f"{tool}")] + command |
| 36 | + |
| 37 | + logger.info(f"Run command: {full_command}") |
| 38 | + result = run(full_command, stdout=PIPE, stderr=PIPE, universal_newlines=True) |
| 39 | + # result contains result.returncode, result.stdout, result.stderr |
| 40 | + return result |
| 41 | + |
| 42 | + |
| 43 | +def adb_reboot(bin_path: Path) -> bool: |
| 44 | + """Run adb reboot on the device and return success.""" |
| 45 | + logger.info("Rebooting device with adb.") |
| 46 | + result = run_command("adb", ["reboot"], bin_path) |
| 47 | + if result.returncode != 0: |
| 48 | + logger.info("Reboot failed.") |
| 49 | + return False |
| 50 | + return True |
| 51 | + |
| 52 | + |
| 53 | +def adb_reboot_bootloader(bin_path: Path) -> bool: |
| 54 | + """Reboot the device into bootloader and return success.""" |
| 55 | + logger.info("Rebooting device into bootloader with adb.") |
| 56 | + result = run_command("adb", ["reboot", "bootloader"], bin_path) |
| 57 | + if result.returncode != 0: |
| 58 | + logger.info("Reboot into bootloader failed.") |
| 59 | + return False |
| 60 | + # check if in fastboot mode |
| 61 | + result = run_command("fastboot", ["devices"], bin_path) |
| 62 | + if result.returncode != 0: |
| 63 | + logger.info("Reboot into bootloader failed.") |
| 64 | + logger.info(result.returncode) |
| 65 | + logger.info(result.stdout) |
| 66 | + logger.info(result.stderr) |
| 67 | + return False |
| 68 | + return True |
| 69 | + |
| 70 | + |
| 71 | +def adb_reboot_download(bin_path: Path) -> bool: |
| 72 | + """Reboot the device into download mode of samsung devices and return success.""" |
| 73 | + logger.info("Rebooting device into download mode with adb.") |
| 74 | + result = run_command("adb", ["reboot", "download"], bin_path) |
| 75 | + if result.returncode != 0: |
| 76 | + logger.info("Reboot into download mode failed.") |
| 77 | + return False |
| 78 | + # check if in download mode with heimdall? |
| 79 | + return True |
| 80 | + |
| 81 | + |
| 82 | + |
| 83 | +def adb_sideload(bin_path: Path, target: str) -> bool: |
| 84 | + """Sideload the target to device and return success.""" |
| 85 | + logger.info("Rebooting device into bootloader with adb.") |
| 86 | + result = run_command("adb", ["sideload", target], bin_path) |
| 87 | + if result.returncode != 0: |
| 88 | + logger.info(f"Sideloading {target} failed.") |
| 89 | + return False |
| 90 | + return True |
| 91 | + |
| 92 | + |
28 | 93 | def call_tool_with_command(command: str, bin_path: Path) -> bool: |
29 | 94 | """Call an executable with a specific command.""" |
30 | 95 | if PLATFORM == "win32": |
|
0 commit comments