"""项目内置 ADB 的路径、完整性检查和 adbutils 环境配置。""" import os from pathlib import Path from typing import Tuple from .db import app_dir ADB_RELATIVE_DIRECTORY = Path("vendor/android-platform-tools/windows") REQUIRED_ADB_FILES: Tuple[str, ...] = ( "adb.exe", "AdbWinApi.dll", "AdbWinUsbApi.dll", ) class BundledAdbError(RuntimeError): """项目内置 ADB 缺失或不完整。""" def bundled_adb_directory() -> Path: """返回项目内置 ADB 目录,不创建目录。""" return app_dir() / ADB_RELATIVE_DIRECTORY def validate_bundled_adb() -> Path: """检查 ADB 和两个配套 DLL,完整时返回 adb.exe 绝对路径。""" directory = bundled_adb_directory() missing_files = [ name for name in REQUIRED_ADB_FILES if not (directory / name).is_file() ] if missing_files: names = "、".join(missing_files) raise BundledAdbError( f"内置 ADB 文件缺失:{names}。请重新安装完整的软件包。" ) return (directory / "adb.exe").resolve() def bundled_adb_path() -> Path: """返回预期的项目内 adb.exe 路径;不访问系统 PATH。""" return (bundled_adb_directory() / "adb.exe").resolve() def configure_bundled_adb_environment() -> Path: """校验内置 ADB,并强制 adbutils/uiautomator2 使用同一文件。""" adb_path = validate_bundled_adb() os.environ["ADBUTILS_ADB_PATH"] = str(adb_path) return adb_path