diff --git a/douyin/core/path_config.py b/douyin/core/path_config.py index b8b3fd6..e37e690 100644 --- a/douyin/core/path_config.py +++ b/douyin/core/path_config.py @@ -265,6 +265,30 @@ def get_run_cli_command() -> list[str]: return [sys.executable, get_run_cli_path()] +def get_bundled_platform_tools_dir() -> str: + """返回随包 Android platform-tools 目录。""" + return os.path.join(PROJECT_ROOT, "platform-tools") + + +def get_bundled_adb_path() -> str: + """返回随包 adb 路径。""" + exe_name = "adb.exe" if sys.platform.startswith("win") else "adb" + return os.path.join(get_bundled_platform_tools_dir(), exe_name) + + +def find_bundled_adb() -> Optional[str]: + """若分发包内自带 adb,返回其绝对路径。""" + candidates = [get_bundled_adb_path()] + meipass = getattr(sys, "_MEIPASS", None) + if meipass: + exe_name = "adb.exe" if sys.platform.startswith("win") else "adb" + candidates.append(os.path.join(str(meipass), "platform-tools", exe_name)) + for path in candidates: + if os.path.exists(path): + return os.path.abspath(path) + return None + + def ensure_dir(path: str) -> None: """确保目录存在,不存在则创建""" dir_path = os.path.dirname(path) if os.path.isfile(path) else path diff --git a/douyin/core/underlying_operations.py b/douyin/core/underlying_operations.py index 4c89978..7fecf2f 100644 --- a/douyin/core/underlying_operations.py +++ b/douyin/core/underlying_operations.py @@ -178,7 +178,7 @@ class underlying_operations: ) @staticmethod def _adb() -> str: - adb = str(os.environ.get("ADB_EXECUTABLE", "adb") or "adb") + adb = _impl_adb_path() # 始终加引号,避免路径带空格 / 版本混用 if adb.startswith('"') and adb.endswith('"'): return adb diff --git a/douyin/core/underlying_operations_pkg/adb_helpers.py b/douyin/core/underlying_operations_pkg/adb_helpers.py index 1c07b1b..033676e 100644 --- a/douyin/core/underlying_operations_pkg/adb_helpers.py +++ b/douyin/core/underlying_operations_pkg/adb_helpers.py @@ -8,12 +8,18 @@ from __future__ import annotations import os from typing import List +from douyin.core import path_config + def _adb_path() -> str: """返回可用于 subprocess(list_args) 的 adb 路径(不带外层引号)。""" adb = str(os.environ.get("ADB_EXECUTABLE", "adb") or "adb") if adb.startswith('"') and adb.endswith('"'): adb = adb[1:-1] + if adb == "adb": + bundled_adb = path_config.find_bundled_adb() + if bundled_adb: + return bundled_adb return adb diff --git a/gui/gui_core_pkg/adb.py b/gui/gui_core_pkg/adb.py index 99ba565..29aea47 100644 --- a/gui/gui_core_pkg/adb.py +++ b/gui/gui_core_pkg/adb.py @@ -11,14 +11,30 @@ import shutil import subprocess from typing import Optional +from douyin.core import path_config + + +def _normalize_adb_path(adb: str | None) -> str: + value = str(adb or "").strip() + if value.startswith('"') and value.endswith('"'): + value = value[1:-1] + return value def find_adb_executable() -> str: - """返回可用的 adb 可执行文件路径(优先 PATH,其次默认安装目录)。""" + """返回可用的 adb 可执行文件路径。""" + env_adb = _normalize_adb_path(os.environ.get("ADB_EXECUTABLE")) + if env_adb and os.path.exists(env_adb): + return os.path.abspath(env_adb) + + bundled_adb = path_config.find_bundled_adb() + if bundled_adb: + return bundled_adb + adb = shutil.which("adb") if adb: return adb - # 默认安装目录(我们之前下载的 platform-tools) + # 默认安装目录(本机开发/打包机常用 platform-tools 位置) localapp = os.environ.get("LOCALAPPDATA") or "" candidates = [] if localapp: @@ -33,7 +49,7 @@ def find_adb_executable() -> str: for p in candidates: if os.path.exists(p): - return p + return os.path.abspath(p) return "" diff --git a/packaging/README.md b/packaging/README.md index a982cb9..2fe7f98 100644 --- a/packaging/README.md +++ b/packaging/README.md @@ -69,6 +69,7 @@ dist\DouyinGuaji-\ - `web_gui.exe`:Web GUI 服务入口 - `run_cli.exe`:后台运行入口,桌面 GUI / Web GUI 会调用它 - `assets/`、`webui/`、`config/`:运行所需资源 +- `platform-tools/`:随包 ADB 运行组件,至少包含 `adb.exe`、`AdbWinApi.dll`、`AdbWinUsbApi.dll` - `logs/`、`pic/`、`state/`:运行时输出目录 - `license.dat`:默认不随包分发;仅本地测试包需要时才使用 `-IncludeLicense` 显式复制 @@ -95,8 +96,8 @@ dist\installer\DouyinGuaji-3.3.3-Setup.exe ## 发给别人前检查 - 打包脚本会自动运行 `python -m tools.distribution_manifest dist\DouyinGuaji-`,防止源码、调试产物、个人配置和旧日志混入分发目录。 -- 最终分发目录只应保留根目录三个 exe、一个 `_internal/`、运行时白名单 `assets/`、运行时白名单 `webui/`、`config/README.md`、`logs/.keep`、`pic/.keep`、`state/.keep`。正式分发不要包含 `license.dat`、`README.md`、`artifacts/`、`tests/`、源码目录、调试截图、临时模板或 debug/README 页面。 -- 目标电脑需要能使用 ADB,或者把 Android platform-tools 加入 PATH。 +- 最终分发目录只应保留根目录三个 exe、一个 `_internal/`、运行时白名单 `assets/`、运行时白名单 `webui/`、`platform-tools/` 中的 ADB 白名单文件、`config/README.md`、`logs/.keep`、`pic/.keep`、`state/.keep`。正式分发不要包含 `license.dat`、`README.md`、`artifacts/`、`tests/`、源码目录、调试截图、临时模板或 debug/README 页面。 +- 正式包会自带 ADB,不要求目标电脑额外安装 Android platform-tools 或配置 PATH。新电脑仍需要在手机上开启开发者选项/USB 调试并允许本机 RSA 授权;部分品牌手机首次 USB 识别还需要 Windows 能正确安装对应 USB 驱动。 - 首次启动 PaddleOCR 相关组件可能较慢,杀毒软件也可能扫描 exe。 - `config/config.json` 是本地运行配置,默认不随安装包带个人配置;让使用者首次打开后在 GUI 中保存配置。 - 默认 Cython 编译后仍要以 `python -m tools.distribution_manifest dist\DouyinGuaji-` 的结果为准;PASS 才能发给别人。使用 `-NoCythonForDevOnly` 生成的明文包禁止分发。 diff --git a/packaging/pyinstaller/build_windows.ps1 b/packaging/pyinstaller/build_windows.ps1 index 0525e3b..b1ff2c9 100644 --- a/packaging/pyinstaller/build_windows.ps1 +++ b/packaging/pyinstaller/build_windows.ps1 @@ -8,6 +8,7 @@ param( [switch]$PackExeForce, [switch]$IncludeLicense, [switch]$ReuseCache, + [string]$PlatformToolsDir = "", [string]$UpxDir = "" ) @@ -185,6 +186,62 @@ foreach ($dir in @("logs", "pic", "state")) { New-Item -ItemType File -Force (Join-Path $runtimeDir ".keep") | Out-Null } +$PlatformToolCandidates = @() +if ($PlatformToolsDir) { + $PlatformToolCandidates += $PlatformToolsDir +} +foreach ($envName in @("ANDROID_PLATFORM_TOOLS", "ANDROID_HOME", "ANDROID_SDK_ROOT")) { + $envValue = [Environment]::GetEnvironmentVariable($envName) + if (-not $envValue) { + continue + } + if ($envName -eq "ANDROID_PLATFORM_TOOLS") { + $PlatformToolCandidates += $envValue + } else { + $PlatformToolCandidates += (Join-Path $envValue "platform-tools") + } +} +if ($env:LOCALAPPDATA) { + $PlatformToolCandidates += (Join-Path $env:LOCALAPPDATA "platform-tools") +} +$PlatformToolCandidates += (Join-Path $Root "platform-tools") + +$ResolvedPlatformTools = "" +foreach ($candidate in $PlatformToolCandidates) { + if (-not $candidate) { + continue + } + $adbCandidate = Join-Path $candidate "adb.exe" + if (Test-Path $adbCandidate) { + $ResolvedPlatformTools = (Resolve-Path $candidate).Path + break + } +} +if (-not $ResolvedPlatformTools) { + throw "Missing Android platform-tools. Provide -PlatformToolsDir or install it under LOCALAPPDATA\platform-tools." +} +$PlatformToolsTarget = Join-Path $DistRoot "platform-tools" +if (Test-Path $PlatformToolsTarget) { + Remove-Item -LiteralPath $PlatformToolsTarget -Recurse -Force +} +New-Item -ItemType Directory -Force $PlatformToolsTarget | Out-Null +$PlatformToolFiles = @( + "adb.exe", + "AdbWinApi.dll", + "AdbWinUsbApi.dll", + "NOTICE.txt", + "source.properties" +) +foreach ($name in $PlatformToolFiles) { + $src = Join-Path $ResolvedPlatformTools $name + if ($name -in @("adb.exe", "AdbWinApi.dll", "AdbWinUsbApi.dll") -and -not (Test-Path $src)) { + throw "Missing required platform-tools file: $name" + } + if (Test-Path $src) { + Copy-Item -LiteralPath $src -Destination (Join-Path $PlatformToolsTarget $name) -Force + } +} + $licensePath = Join-Path $Root "license.dat" if ($IncludeLicense -and (Test-Path $licensePath)) { Copy-Item -LiteralPath $licensePath -Destination (Join-Path $DistRoot "license.dat") -Force diff --git a/tests/test_distribution_manifest.py b/tests/test_distribution_manifest.py index ad94253..1493086 100644 --- a/tests/test_distribution_manifest.py +++ b/tests/test_distribution_manifest.py @@ -60,6 +60,9 @@ def test_distribution_manifest_blocks_project_native_binaries_outside_core_allow "_internal/douyin/fudai/core/compiled.cp312-win_amd64.pyd", "_internal/numpy/core/runtime.pyd", "webui/index.html", + "platform-tools/adb.exe", + "platform-tools/AdbWinApi.dll", + "platform-tools/AdbWinUsbApi.dll", "config/README.md", "logs/.keep", "pic/.keep", @@ -89,6 +92,11 @@ def test_distribution_manifest_allows_expected_runtime_shape(): "webui/index.html", "webui/css/base.css", "webui/js/api_client.js", + "platform-tools/adb.exe", + "platform-tools/AdbWinApi.dll", + "platform-tools/AdbWinUsbApi.dll", + "platform-tools/NOTICE.txt", + "platform-tools/source.properties", "config/README.md", "logs/.keep", "pic/.keep", @@ -98,6 +106,22 @@ def test_distribution_manifest_allows_expected_runtime_shape(): assert find_distribution_issues(entries) == [] +def test_distribution_manifest_blocks_unexpected_platform_tools_files(): + from tools.distribution_manifest import find_forbidden_distribution_entries + + entries = [ + "platform-tools/adb.exe", + "platform-tools/fastboot.exe", + "platform-tools/mke2fs.conf", + ] + + blocked = find_forbidden_distribution_entries(entries) + + assert "platform-tools/adb.exe" not in blocked + assert "platform-tools/fastboot.exe" in blocked + assert "platform-tools/mke2fs.conf" in blocked + + def test_distribution_manifest_can_allow_license_for_local_test_package(): from tools.distribution_manifest import find_distribution_issues @@ -109,6 +133,9 @@ def test_distribution_manifest_can_allow_license_for_local_test_package(): "_internal/python312.dll", "assets/fudai.png", "webui/index.html", + "platform-tools/adb.exe", + "platform-tools/AdbWinApi.dll", + "platform-tools/AdbWinUsbApi.dll", "config/README.md", "logs/.keep", "pic/.keep", @@ -130,6 +157,9 @@ def test_distribution_manifest_requires_python_runtime_dll(): "_internal/base_library.zip", "assets/fudai.png", "webui/index.html", + "platform-tools/adb.exe", + "platform-tools/AdbWinApi.dll", + "platform-tools/AdbWinUsbApi.dll", "config/README.md", "logs/.keep", "pic/.keep", @@ -141,6 +171,30 @@ def test_distribution_manifest_requires_python_runtime_dll(): assert "_internal/python*.dll" in issues +def test_distribution_manifest_requires_bundled_adb_runtime(): + from tools.distribution_manifest import find_distribution_issues + + entries = [ + "DouyinGuajiGUI.exe", + "web_gui.exe", + "run_cli.exe", + "_internal/base_library.zip", + "_internal/python312.dll", + "assets/fudai.png", + "webui/index.html", + "config/README.md", + "logs/.keep", + "pic/.keep", + "state/.keep", + ] + + issues = find_distribution_issues(entries) + + assert "platform-tools/adb.exe" in issues + assert "platform-tools/AdbWinApi.dll" in issues + assert "platform-tools/AdbWinUsbApi.dll" in issues + + def test_distribution_manifest_cli_passes_clean_tree(tmp_path): dist = tmp_path / "dist" for rel in [ @@ -154,6 +208,9 @@ def test_distribution_manifest_cli_passes_clean_tree(tmp_path): "webui/css/base.css", "webui/js/api_client.js", "webui/index.html", + "platform-tools/adb.exe", + "platform-tools/AdbWinApi.dll", + "platform-tools/AdbWinUsbApi.dll", "config/README.md", "logs/.keep", "pic/.keep", diff --git a/tests/test_obfuscation_staging.py b/tests/test_obfuscation_staging.py index ad0bcb3..53f642f 100644 --- a/tests/test_obfuscation_staging.py +++ b/tests/test_obfuscation_staging.py @@ -267,3 +267,9 @@ def test_windows_packaging_script_protects_core_modules_by_default(): assert "Join-Path $BuildSourceRoot \"gui\\gui_dashboard_app_entry.py\"" in script assert "Join-Path $BuildSourceRoot \"gui\\web_gui_entry.py\"" in script assert "Join-Path $BuildSourceRoot \"bin\\run_cli_entry.py\"" in script + assert '[string]$PlatformToolsDir = ""' in script + assert '"ANDROID_PLATFORM_TOOLS", "ANDROID_HOME", "ANDROID_SDK_ROOT"' in script + assert '$PlatformToolFiles = @(' in script + assert '"adb.exe"' in script + assert '"AdbWinApi.dll"' in script + assert '"AdbWinUsbApi.dll"' in script diff --git a/tests/test_path_config.py b/tests/test_path_config.py index b2a8066..7288359 100644 --- a/tests/test_path_config.py +++ b/tests/test_path_config.py @@ -119,6 +119,11 @@ class TestSpecificPaths: os.path.join(PROJECT_ROOT, "run_cli.exe") ] + def test_bundled_adb_path_uses_project_root(self): + assert path_config.get_bundled_adb_path() == os.path.join( + PROJECT_ROOT, "platform-tools", "adb.exe" + ) + # ============================================================ # 路径间不应互相重复 diff --git a/tests/test_underlying_operations_pkg.py b/tests/test_underlying_operations_pkg.py index dab374e..c95d483 100644 --- a/tests/test_underlying_operations_pkg.py +++ b/tests/test_underlying_operations_pkg.py @@ -27,6 +27,10 @@ class TestAdbPath: def test_default_returns_adb(self, monkeypatch): # 默认走 ADB_EXECUTABLE 环境变量;如果未设,应返回 'adb' monkeypatch.delenv("ADB_EXECUTABLE", raising=False) + monkeypatch.setattr( + "douyin.core.underlying_operations_pkg.adb_helpers.path_config.find_bundled_adb", + lambda: None, + ) assert _adb_path() == "adb" def test_env_override(self, monkeypatch): @@ -38,6 +42,14 @@ class TestAdbPath: # 包了引号的应当去引号返回 raw 路径 assert _adb_path() == "C:\\Program Files\\adb.exe" + def test_uses_bundled_adb_when_env_missing(self, monkeypatch): + monkeypatch.delenv("ADB_EXECUTABLE", raising=False) + monkeypatch.setattr( + "douyin.core.underlying_operations_pkg.adb_helpers.path_config.find_bundled_adb", + lambda: r"C:\App\platform-tools\adb.exe", + ) + assert _adb_path() == r"C:\App\platform-tools\adb.exe" + class TestAdbArgs: def test_basic(self): diff --git a/tools/distribution_manifest.py b/tools/distribution_manifest.py index 3da261d..5fb5db1 100644 --- a/tools/distribution_manifest.py +++ b/tools/distribution_manifest.py @@ -76,6 +76,14 @@ ALLOWED_WEBUI_SUFFIXES = { ".js", } +ALLOWED_PLATFORM_TOOLS_FILES = { + "platform-tools/adb.exe", + "platform-tools/adbwinapi.dll", + "platform-tools/adbwinusbapi.dll", + "platform-tools/notice.txt", + "platform-tools/source.properties", +} + FORBIDDEN_SUFFIXES = ( ".py", ".pyc", @@ -150,6 +158,8 @@ def is_forbidden_distribution_entry(entry: str, *, allow_license: bool = False) if not any(lower.startswith(prefix) for prefix in ALLOWED_WEBUI_DIRS): return True return Path(lower).suffix not in ALLOWED_WEBUI_SUFFIXES + if lower.startswith("platform-tools/"): + return lower not in ALLOWED_PLATFORM_TOOLS_FILES if any(lower.startswith(prefix.lower()) for prefix in FORBIDDEN_PREFIXES): return True return any(lower.endswith(suffix) for suffix in FORBIDDEN_SUFFIXES) @@ -173,6 +183,13 @@ def find_missing_required_runtime_entries(entries: list[str]) -> list[str]: for required in ("DouyinGuajiGUI.exe", "web_gui.exe", "run_cli.exe"): if required.lower() not in normalized: missing.append(required) + for required in ( + "platform-tools/adb.exe", + "platform-tools/AdbWinApi.dll", + "platform-tools/AdbWinUsbApi.dll", + ): + if required.lower() not in normalized: + missing.append(required) if not any(entry.startswith("_internal/") and entry.startswith("_internal/python") and entry.endswith(".dll") for entry in normalized): missing.append("_internal/python*.dll") if "_internal/base_library.zip" not in normalized: