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", "license.dat", "pyproject.toml", "pyrightconfig.json", "readme.md", "repo-backup.bundle", "requirements.txt", "requirements-minimal.txt", } ALLOWED_ASSET_FILES = { "assets/coupon.png", "assets/fav.png", "assets/fudai.png", "assets/fudai_icon_countdown_v20260618.png", "assets/fudai_icon_pending_v20260618.png", "assets/fudai_icon_purple_left_v20260618.png", "assets/fudai_icon_red_left_v20260618.png", "assets/fudai_task_status_dacheng_shape.png", "assets/like.png", "assets/x.png", "assets/xin.png", } ALLOWED_WEBUI_ROOT_FILES = { "webui/app.js", "webui/index.html", "webui/styles.css", } ALLOWED_WEBUI_DIRS = ( "webui/css/", "webui/js/", "webui/partials/", ) ALLOWED_WEBUI_SUFFIXES = { ".css", ".html", ".js", } FORBIDDEN_SUFFIXES = ( ".py", ".pyc", ".pyo", ".spec", ".log", ".jsonl", ".sqlite", ".sqlite3", ".db", ".bundle", ) ALLOWED_RUNTIME_PLACEHOLDERS = { "logs/.keep", "pic/.keep", "state/.keep", } INTERNAL_PROJECT_PREFIXES = ( "_internal/douyin/", "_internal/gui/", "_internal/operations/", "_internal/bin/", "_internal/tools/", "_internal/tests/", "_internal/artifacts/", ) ALLOWED_INTERNAL_PROJECT_PYD_PREFIXES = ( "_internal/douyin/detection/", "_internal/douyin/fudai/", "_internal/douyin/reservation/", "_internal/douyin/stage/", ) def normalize_entry(entry: str) -> str: return entry.replace("\\", "/").lstrip("./") def is_forbidden_distribution_entry(entry: str, *, allow_license: bool = False) -> bool: norm = normalize_entry(entry) lower = norm.lower() if lower == "license.dat" and allow_license: return False if lower.startswith("_internal/"): if any(lower.startswith(prefix) for prefix in INTERNAL_PROJECT_PREFIXES): if lower.endswith(".pyd") and any( lower.startswith(prefix) for prefix in ALLOWED_INTERNAL_PROJECT_PYD_PREFIXES ): return False return True if lower.endswith((".pyd", ".dll")): return False return False 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 lower.startswith("assets/"): return lower not in ALLOWED_ASSET_FILES if lower.startswith("webui/"): if lower in ALLOWED_WEBUI_ROOT_FILES: return False if lower.endswith("/readme.md") or lower.endswith("/readme.txt"): return True if not any(lower.startswith(prefix) for prefix in ALLOWED_WEBUI_DIRS): return True return Path(lower).suffix not in ALLOWED_WEBUI_SUFFIXES 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], *, allow_license: bool = False, ) -> list[str]: return [ entry for entry in entries if is_forbidden_distribution_entry(entry, allow_license=allow_license) ] 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], *, allow_license: bool = False, ) -> list[str]: return [ *find_forbidden_distribution_entries(entries, allow_license=allow_license), *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") parser.add_argument( "--allow-license", action="store_true", help="允许测试包包含 license.dat;正式分发不要使用", ) args = parser.parse_args(argv) entries = iter_distribution_entries(args.dist_dir) blocked = find_distribution_issues(entries, allow_license=args.allow_license) 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())