fix(packaging): keep distribution minimal

This commit is contained in:
Your Name committed 2026-06-18 11:14:04 +08:00
1 parent c97e4edc74
commit b74baef555
18 files changed
+492 -49

No files matched your search

+27
View File
@@ -286,3 +286,30 @@ def test_startup_update_fail_closed_blocks_old_client(tmp_path, monkeypatch):
)
assert exc.value.code == 3
def test_early_bootstrap_fail_closed_blocks_if_updater_crashes(monkeypatch):
from douyin.updater import bootstrap
from douyin.updater.force_update import UpdateSettings
monkeypatch.setattr(
bootstrap,
"load_update_settings",
lambda: UpdateSettings(
enabled=True,
manifest_url="https://example.test/update.json",
download_dir=Path("updates"),
fail_closed=True,
),
)
def crash(*, app_name: str):
raise RuntimeError("broken updater import")
with pytest.raises(SystemExit) as exc:
bootstrap.run_startup_update_check_early(
app_name="test_app",
update_check=crash,
)
assert exc.value.code == 3
+43 -6
View File
@@ -11,11 +11,16 @@ def test_distribution_manifest_blocks_source_and_debug_artifacts():
"DouyinGuajiGUI.exe",
"_internal/python310.dll",
"assets/fudai_task_count_v2_one_task.png",
"README.md",
"douyin/core/douyin_fudai.py",
"artifacts/debug.png",
"config/config.json",
"logs/unified.log",
"pic/debug_captures/a.png",
"license.dat",
"assets/README.txt",
"webui/debug.html",
"webui/js/README.md",
"DouyinGuajiGUI/_internal/base_library.zip",
"_internal/douyin/core/douyin_fudai.py",
"_internal/cv2/__init__.py",
@@ -28,10 +33,15 @@ def test_distribution_manifest_blocks_source_and_debug_artifacts():
assert "config/config.json" in blocked
assert "logs/unified.log" in blocked
assert "pic/debug_captures/a.png" in blocked
assert "license.dat" in blocked
assert "README.md" in blocked
assert "assets/README.txt" in blocked
assert "assets/fudai_task_count_v2_one_task.png" in blocked
assert "webui/debug.html" in blocked
assert "webui/js/README.md" in blocked
assert "DouyinGuajiGUI/_internal/base_library.zip" in blocked
assert "_internal/douyin/core/douyin_fudai.py" in blocked
assert "_internal/cv2/__init__.py" not in blocked
assert "assets/fudai_task_count_v2_one_task.png" not in blocked
def test_distribution_manifest_allows_expected_runtime_shape():
@@ -43,7 +53,31 @@ def test_distribution_manifest_allows_expected_runtime_shape():
"run_cli.exe",
"_internal/base_library.zip",
"_internal/python312.dll",
"assets/fudai_task_count_v2_one_task.png",
"assets/fudai.png",
"assets/fudai_task_status_dacheng_shape.png",
"assets/fudai_icon_red_left_v20260618.png",
"webui/index.html",
"webui/css/base.css",
"webui/js/api_client.js",
"config/README.md",
"logs/.keep",
"pic/.keep",
"state/.keep",
]
assert find_distribution_issues(entries) == []
def test_distribution_manifest_can_allow_license_for_local_test_package():
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",
@@ -52,7 +86,8 @@ def test_distribution_manifest_allows_expected_runtime_shape():
"license.dat",
]
assert find_distribution_issues(entries) == []
assert "license.dat" in find_distribution_issues(entries)
assert find_distribution_issues(entries, allow_license=True) == []
def test_distribution_manifest_requires_python_runtime_dll():
@@ -63,7 +98,7 @@ def test_distribution_manifest_requires_python_runtime_dll():
"web_gui.exe",
"run_cli.exe",
"_internal/base_library.zip",
"assets/fudai_task_count_v2_one_task.png",
"assets/fudai.png",
"webui/index.html",
"config/README.md",
"logs/.keep",
@@ -84,13 +119,15 @@ def test_distribution_manifest_cli_passes_clean_tree(tmp_path):
"run_cli.exe",
"_internal/base_library.zip",
"_internal/python312.dll",
"assets/fudai_task_count_v2_one_task.png",
"assets/fudai.png",
"assets/fudai_task_status_dacheng_shape.png",
"webui/css/base.css",
"webui/js/api_client.js",
"webui/index.html",
"config/README.md",
"logs/.keep",
"pic/.keep",
"state/.keep",
"license.dat",
]:
path = dist / rel
path.parent.mkdir(parents=True, exist_ok=True)
+1
View File
@@ -142,6 +142,7 @@ def test_windows_packaging_script_exposes_pack_exe_switch():
assert "[switch]$PackExe" in script
assert "[switch]$PackExeForce" in script
assert "[switch]$IncludeLicense" in script
assert "[string]$UpxDir" in script
assert "tools.exe_packing" in script
assert "--smoke-only" in script
+30 -2
View File
@@ -17,11 +17,14 @@ def _make_minimal_project(root: Path) -> None:
_write(root / "douyin" / "updater" / "__init__.py")
_write(root / "douyin" / "updater" / "force_update.py")
_write(root / "gui" / "gui_app.py")
_write(root / "gui" / "gui_app_entry.py")
_write(root / "gui" / "web_gui.py")
_write(root / "gui" / "web_gui_entry.py")
_write(root / "gui" / "components" / "left_config_area.py")
_write(root / "operations" / "__init__.py")
_write(root / "operations" / "ocr_operations.py")
_write(root / "bin" / "run_cli.py")
_write(root / "bin" / "run_cli_entry.py")
_write(root / "tools" / "license_dev.py", "PRIVATE_KEY = 'must-not-ship'\n")
@@ -40,9 +43,12 @@ def test_create_pyarmor_staging_uses_only_runtime_sources(tmp_path, monkeypatch)
assert check is True
_write(staging / "douyin" / "__init__.py", "# obfuscated\n")
_write(staging / "gui" / "gui_app.py", "# obfuscated\n")
_write(staging / "gui" / "gui_app_entry.py", "# obfuscated\n")
_write(staging / "gui" / "web_gui.py", "# obfuscated\n")
_write(staging / "gui" / "web_gui_entry.py", "# obfuscated\n")
_write(staging / "operations" / "__init__.py", "# obfuscated\n")
_write(staging / "bin" / "run_cli.py", "# obfuscated\n")
_write(staging / "bin" / "run_cli_entry.py", "# obfuscated\n")
_write(staging / "pyarmor_runtime_000000" / "__init__.py", "# runtime\n")
return subprocess.CompletedProcess(cmd, 0)
@@ -56,6 +62,7 @@ def test_create_pyarmor_staging_uses_only_runtime_sources(tmp_path, monkeypatch)
assert "gui" in command_text
assert "operations" in command_text
assert "bin/run_cli.py" in command_text
assert "bin/run_cli_entry.py" in command_text
assert "tools" not in command_text
assert "license_dev.py" not in command_text
@@ -66,9 +73,12 @@ def test_validate_pyarmor_staging_requires_runtime_package(tmp_path):
staging = tmp_path / "stage"
_write(staging / "douyin" / "__init__.py")
_write(staging / "gui" / "gui_app.py")
_write(staging / "gui" / "gui_app_entry.py")
_write(staging / "gui" / "web_gui.py")
_write(staging / "gui" / "web_gui_entry.py")
_write(staging / "operations" / "__init__.py")
_write(staging / "bin" / "run_cli.py")
_write(staging / "bin" / "run_cli_entry.py")
with pytest.raises(RuntimeError, match="pyarmor_runtime"):
validate_pyarmor_staging(staging)
@@ -80,9 +90,12 @@ def test_validate_pyarmor_staging_rejects_sensitive_tools(tmp_path):
staging = tmp_path / "stage"
_write(staging / "douyin" / "__init__.py")
_write(staging / "gui" / "gui_app.py")
_write(staging / "gui" / "gui_app_entry.py")
_write(staging / "gui" / "web_gui.py")
_write(staging / "gui" / "web_gui_entry.py")
_write(staging / "operations" / "__init__.py")
_write(staging / "bin" / "run_cli.py")
_write(staging / "bin" / "run_cli_entry.py")
_write(staging / "pyarmor_runtime_000000" / "__init__.py")
_write(staging / "tools" / "license_dev.py", "PRIVATE_KEY = 'leak'\n")
@@ -101,16 +114,21 @@ def test_create_pyarmor_staging_moves_flat_entry_script_back_to_bin(tmp_path, mo
def fake_runner(cmd, *, cwd, check):
_write(staging / "douyin" / "__init__.py", "# obfuscated\n")
_write(staging / "gui" / "gui_app.py", "# obfuscated\n")
_write(staging / "gui" / "gui_app_entry.py", "# obfuscated\n")
_write(staging / "gui" / "web_gui.py", "# obfuscated\n")
_write(staging / "gui" / "web_gui_entry.py", "# obfuscated\n")
_write(staging / "operations" / "__init__.py", "# obfuscated\n")
_write(staging / "run_cli.py", "# flat obfuscated entry\n")
_write(staging / "run_cli_entry.py", "# flat obfuscated entry wrapper\n")
_write(staging / "pyarmor_runtime_000000" / "__init__.py", "# runtime\n")
return subprocess.CompletedProcess(cmd, 0)
create_pyarmor_staging(root, staging, runner=fake_runner)
assert (staging / "bin" / "run_cli.py").read_text(encoding="utf-8") == "# flat obfuscated entry\n"
assert (staging / "bin" / "run_cli_entry.py").read_text(encoding="utf-8") == "# flat obfuscated entry wrapper\n"
assert not (staging / "run_cli.py").exists()
assert not (staging / "run_cli_entry.py").exists()
def test_collect_runtime_hidden_imports_uses_original_sources(tmp_path):
@@ -133,6 +151,7 @@ def test_collect_runtime_hidden_imports_uses_original_sources(tmp_path):
_write(root / "gui" / "web_gui.py", "import secrets\nfrom http.server import ThreadingHTTPServer\n")
_write(root / "operations" / "__init__.py")
_write(root / "bin" / "run_cli.py", "from douyin.license.license_manager import check_license\n")
_write(root / "bin" / "run_cli_entry.py", "from bin.run_cli import main\n")
hidden_imports = collect_runtime_hidden_imports(root)
@@ -157,6 +176,7 @@ def test_collect_runtime_hidden_imports_skips_optional_heavy_modules(tmp_path):
_write(root / "douyin" / "__init__.py")
_write(root / "gui" / "gui_app.py")
_write(root / "bin" / "run_cli.py")
_write(root / "bin" / "run_cli_entry.py")
hidden_imports = collect_runtime_hidden_imports(root)
@@ -188,6 +208,14 @@ def test_windows_packaging_script_exposes_obfuscate_switch():
assert "tools.exe_packing $DistRoot --smoke-only" in script
assert "Executable smoke check failed" in script
assert "Post-smoke distribution manifest check failed" in script
assert "[switch]$IncludeLicense" in script
assert '"--add-data", "assets;assets"' not in script
assert '"--add-data", "webui;webui"' not in script
assert "$RuntimeAssets = @(" in script
assert 'assets\$name' in script
assert "$IncludeLicense -and" in script
assert '$ManifestArgs += "--allow-license"' in script
assert "Get-ChildItem -LiteralPath $srcInternal -Force | Copy-Item" in script
assert "Join-Path $BuildSourceRoot \"gui\\gui_app.py\"" in script
assert "Join-Path $BuildSourceRoot \"bin\\run_cli.py\"" in script
assert "Join-Path $BuildSourceRoot \"gui\\gui_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
+56 -7
View File
@@ -1,5 +1,7 @@
from __future__ import annotations
import subprocess
import sys
from pathlib import Path
@@ -15,25 +17,72 @@ def _assert_bootstrap_before(path: str, *late_markers: str) -> None:
assert call_index < text.index(marker)
def _assert_packaging_uses_wrapper(wrapper_path: str, target_script: str) -> None:
script = Path("packaging/pyinstaller/build_windows.ps1").read_text(encoding="utf-8")
assert f'Join-Path $BuildSourceRoot "{wrapper_path}"' in script
assert f'Join-Path $BuildSourceRoot "{target_script}"' not in script
def _assert_wrapper_imports_target_after_bootstrap(path: str, target_import: str) -> None:
text = Path(path).read_text(encoding="utf-8")
assert "run_startup_update_check_early(" in text
assert target_import in text
assert text.index("run_startup_update_check_early(") < text.index(target_import)
def _assert_source_entry_bootstraps_under_main_guard(path: str, late_marker: str) -> None:
text = Path(path).read_text(encoding="utf-8")
guard_marker = 'if __name__ == "__main__":'
call_marker = "run_startup_update_check_early("
assert guard_marker in text
assert call_marker in text
assert late_marker in text
assert text.index(guard_marker) < text.index(call_marker) < text.index(late_marker)
def test_run_cli_runs_update_bootstrap_before_heavy_imports():
_assert_bootstrap_before(
"bin/run_cli_entry.py",
"from bin.run_cli import main",
)
_assert_wrapper_imports_target_after_bootstrap("bin/run_cli_entry.py", "from bin.run_cli import main")
_assert_source_entry_bootstraps_under_main_guard(
"bin/run_cli.py",
"from douyin.core.douyin_fudai import fudai_analyse",
"from douyin.license.license_manager import require_license_or_exit",
)
_assert_packaging_uses_wrapper("bin\\run_cli_entry.py", "bin\\run_cli.py")
def test_gui_app_runs_update_bootstrap_before_gui_imports():
_assert_bootstrap_before(
"gui/gui_app.py",
"from gui.gui_core import",
"from douyin.license.license_manager import require_license_or_exit",
"gui/gui_app_entry.py",
"from gui.gui_app import main",
)
_assert_wrapper_imports_target_after_bootstrap("gui/gui_app_entry.py", "from gui.gui_app import main")
_assert_source_entry_bootstraps_under_main_guard("gui/gui_app.py", "from gui.gui_core import")
_assert_packaging_uses_wrapper("gui\\gui_app_entry.py", "gui\\gui_app.py")
def test_web_gui_runs_update_bootstrap_before_gui_imports():
_assert_bootstrap_before(
"gui/web_gui.py",
"from douyin.core import path_config",
"from douyin.license.license_manager import require_license_or_exit",
"gui/web_gui_entry.py",
"from gui.web_gui import main",
)
_assert_wrapper_imports_target_after_bootstrap("gui/web_gui_entry.py", "from gui.web_gui import main")
_assert_source_entry_bootstraps_under_main_guard("gui/web_gui.py", "from douyin.core import path_config")
_assert_packaging_uses_wrapper("gui\\web_gui_entry.py", "gui\\web_gui.py")
def test_run_cli_import_does_not_trigger_startup_update(monkeypatch):
monkeypatch.setenv("DOUYIN_AUTO_UPDATE_ENABLED", "1")
monkeypatch.setenv("DOUYIN_UPDATE_MANIFEST_URL", "http://example.test/update.json")
proc = subprocess.run(
[sys.executable, "-c", "import bin.run_cli; print('import-ok')"],
capture_output=True,
text=True,
timeout=20,
)
assert proc.returncode == 0
assert "import-ok" in proc.stdout