feat: 内置并统一使用项目 ADB (#149)
This commit is contained in:
@@ -4,8 +4,15 @@ from dataclasses import dataclass
|
||||
import ipaddress
|
||||
import subprocess
|
||||
import time
|
||||
from pathlib import Path
|
||||
from typing import Callable, List, Optional, Sequence
|
||||
|
||||
from .adb_runtime import (
|
||||
BundledAdbError,
|
||||
bundled_adb_path,
|
||||
validate_bundled_adb,
|
||||
)
|
||||
|
||||
|
||||
DEFAULT_ADB_TIMEOUT_SECONDS = 5.0
|
||||
DEFAULT_DEVICE_READY_CHECKS = 8
|
||||
@@ -78,19 +85,24 @@ class AndroidDeviceService:
|
||||
command_runner: CommandRunner = run_adb_command,
|
||||
timeout_seconds: float = DEFAULT_ADB_TIMEOUT_SECONDS,
|
||||
sleeper: Sleeper = time.sleep,
|
||||
adb_executable: Optional[Path] = None,
|
||||
):
|
||||
if timeout_seconds <= 0:
|
||||
raise ValueError("ADB 超时时间必须大于 0")
|
||||
self._run_command = command_runner
|
||||
self._timeout_seconds = timeout_seconds
|
||||
self._sleep = sleeper
|
||||
self._uses_bundled_adb = adb_executable is None
|
||||
self._adb_executable = str(adb_executable or bundled_adb_path())
|
||||
|
||||
def search(
|
||||
self, is_cancelled: Optional[Callable[[], bool]] = None
|
||||
) -> List[AndroidDevice]:
|
||||
"""返回当前 ADB 设备;取消后停止补充设备属性。"""
|
||||
|
||||
result = self._run(["adb", "devices", "-l"], "搜索设备")
|
||||
result = self._run(
|
||||
[self._adb_executable, "devices", "-l"], "搜索设备"
|
||||
)
|
||||
devices = self.parse_devices(result.stdout or "")
|
||||
|
||||
enriched = []
|
||||
@@ -158,7 +170,15 @@ class AndroidDeviceService:
|
||||
raise AndroidDeviceSearchError("应用包名格式不正确")
|
||||
|
||||
result = self._run(
|
||||
["adb", "-s", serial, "shell", "pm", "path", package_name],
|
||||
[
|
||||
self._adb_executable,
|
||||
"-s",
|
||||
serial,
|
||||
"shell",
|
||||
"pm",
|
||||
"path",
|
||||
package_name,
|
||||
],
|
||||
"检测 PDD 应用",
|
||||
)
|
||||
return any(
|
||||
@@ -193,7 +213,7 @@ class AndroidDeviceService:
|
||||
self._check_cancelled(is_cancelled)
|
||||
self._report(on_progress, "正在读取勾选设备的 Wi-Fi 地址…")
|
||||
route = self._run(
|
||||
["adb", "-s", usb_serial, "shell", "ip", "route"],
|
||||
[self._adb_executable, "-s", usb_serial, "shell", "ip", "route"],
|
||||
"读取 Wi-Fi 地址",
|
||||
)
|
||||
ip_address = self.parse_wifi_ipv4(route.stdout or "")
|
||||
@@ -202,7 +222,7 @@ class AndroidDeviceService:
|
||||
self._check_cancelled(is_cancelled)
|
||||
self._report(on_progress, "正在开启 ADB 5555 端口…")
|
||||
self._run(
|
||||
["adb", "-s", usb_serial, "tcpip", "5555"],
|
||||
[self._adb_executable, "-s", usb_serial, "tcpip", "5555"],
|
||||
"开启 Wi-Fi 调试",
|
||||
)
|
||||
|
||||
@@ -312,7 +332,14 @@ class AndroidDeviceService:
|
||||
|
||||
try:
|
||||
result = self._run(
|
||||
["adb", "-s", serial, "shell", "getprop", property_name],
|
||||
[
|
||||
self._adb_executable,
|
||||
"-s",
|
||||
serial,
|
||||
"shell",
|
||||
"getprop",
|
||||
property_name,
|
||||
],
|
||||
"读取设备信息",
|
||||
)
|
||||
except AndroidDeviceSearchError:
|
||||
@@ -343,12 +370,14 @@ class AndroidDeviceService:
|
||||
self._sleep(DEFAULT_DEVICE_READY_INTERVAL_SECONDS)
|
||||
|
||||
def _list_devices(self) -> List[AndroidDevice]:
|
||||
result = self._run(["adb", "devices", "-l"], "读取设备列表")
|
||||
result = self._run(
|
||||
[self._adb_executable, "devices", "-l"], "读取设备列表"
|
||||
)
|
||||
return self.parse_devices(result.stdout or "")
|
||||
|
||||
def _connect_wifi(self, wifi_serial: str) -> None:
|
||||
result = self._run(
|
||||
["adb", "connect", wifi_serial],
|
||||
[self._adb_executable, "connect", wifi_serial],
|
||||
"连接 Wi-Fi 设备",
|
||||
)
|
||||
detail = " ".join(
|
||||
@@ -401,11 +430,16 @@ class AndroidDeviceService:
|
||||
callback(message)
|
||||
|
||||
def _run(self, command: Sequence[str], action: str):
|
||||
if self._uses_bundled_adb:
|
||||
try:
|
||||
validate_bundled_adb()
|
||||
except BundledAdbError as exc:
|
||||
raise AndroidDeviceSearchError(str(exc)) from exc
|
||||
try:
|
||||
result = self._run_command(command, self._timeout_seconds)
|
||||
except FileNotFoundError as exc:
|
||||
raise AndroidDeviceSearchError(
|
||||
"未找到 adb,请安装 Android platform-tools 并把 adb 加入 PATH 后重试"
|
||||
"内置 adb.exe 无法启动,请重新安装完整的软件包"
|
||||
) from exc
|
||||
except subprocess.TimeoutExpired as exc:
|
||||
raise AndroidDeviceSearchError(
|
||||
@@ -413,7 +447,7 @@ class AndroidDeviceService:
|
||||
) from exc
|
||||
except OSError as exc:
|
||||
raise AndroidDeviceSearchError(
|
||||
f"无法启动 adb:{str(exc) or '系统拒绝执行'}"
|
||||
f"无法启动内置 adb:{str(exc) or '系统拒绝执行'}"
|
||||
) from exc
|
||||
|
||||
if result.returncode != 0:
|
||||
|
||||
Reference in New Issue
Block a user