from __future__ import annotations import argparse import os from pathlib import Path FORBIDDEN_PREFIXES = ( ".git/", ".github/", ".pytest_cache/", ".venv/", ".vscode/", "__pycache__/", "artifacts/", "bin/", "build/", "deleted_tests_backup/", "docs/", "douyin/", "fudai_detail_modules/", "gui/", "MagicMock/", "operations/", "packaging/", "scripts/", "tests/", "tools/", "typings/", "DouyinGuajiGUI/", "run_cli/", "web_gui/", ) FORBIDDEN_EXACT = { "config/config.json", "douyin_guaji.code-workspace", "pyproject.toml", "pyrightconfig.json", "repo-backup.bundle", "requirements.txt", "requirements-minimal.txt", } FORBIDDEN_SUFFIXES = ( ".py", ".pyc", ".pyo", ".spec", ".log", ".jsonl", ".sqlite", ".sqlite3", ".db", ".bundle", ) ALLOWED_RUNTIME_PLACEHOLDERS = { "logs/.keep", "pic/.keep", "state/.keep", } def normalize_entry(entry: str) -> str: return entry.replace("\\", "/").lstrip("./") def is_forbidden_distribution_entry(entry: str) -> bool: norm = normalize_entry(entry) lower = norm.lower() if lower.startswith("_internal/"): project_bits = { "_internal/douyin/", "_internal/gui/", "_internal/operations/", "_internal/bin/", "_internal/tools/", "_internal/tests/", "_internal/artifacts/", } return any(lower.startswith(prefix) for prefix in project_bits) if lower in ALLOWED_RUNTIME_PLACEHOLDERS: return False if lower in FORBIDDEN_EXACT: return True if lower.startswith("logs/") or lower.startswith("state/"): return lower not in ALLOWED_RUNTIME_PLACEHOLDERS if lower.startswith("pic/"): return lower not in ALLOWED_RUNTIME_PLACEHOLDERS if any(lower.startswith(prefix.lower()) for prefix in FORBIDDEN_PREFIXES): return True return any(lower.endswith(suffix) for suffix in FORBIDDEN_SUFFIXES) def find_forbidden_distribution_entries(entries: list[str]) -> list[str]: return [entry for entry in entries if is_forbidden_distribution_entry(entry)] def find_missing_required_runtime_entries(entries: list[str]) -> list[str]: normalized = [normalize_entry(entry).lower() for entry in entries] missing: list[str] = [] for required in ("DouyinGuajiGUI.exe", "web_gui.exe", "run_cli.exe"): 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: missing.append("_internal/base_library.zip") return missing def find_distribution_issues(entries: list[str]) -> list[str]: return [ *find_forbidden_distribution_entries(entries), *find_missing_required_runtime_entries(entries), ] def iter_distribution_entries(root: str | os.PathLike[str]) -> list[str]: base = Path(root) entries: list[str] = [] for path in base.rglob("*"): if path.is_file(): entries.append(path.relative_to(base).as_posix()) return sorted(entries) def main(argv: list[str] | None = None) -> int: parser = argparse.ArgumentParser(description="检查分发目录是否混入源码/调试产物") parser.add_argument("dist_dir", help="例如 dist/DouyinGuaji-3.3.3") args = parser.parse_args(argv) entries = iter_distribution_entries(args.dist_dir) blocked = find_distribution_issues(entries) if blocked: print("[DIST-CHECK] FAIL forbidden entries:") for entry in blocked[:200]: print(f" {entry}") if len(blocked) > 200: print(f" ... and {len(blocked) - 200} more") return 2 print(f"[DIST-CHECK] PASS checked_files={len(entries)}") return 0 if __name__ == "__main__": raise SystemExit(main())