"""打包脚本:把项目代码打成 zip 压缩包,排除运行时产物。 用法:python scripts/pack.py 输出:项目根目录下 auto_control.zip """ import os import sys import zipfile ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) OUT = os.path.join(os.path.dirname(ROOT), "auto_control.zip") SKIP_DIRS = {"__pycache__", ".git", ".idea", "venv", ".venv", "node_modules"} SKIP_EXTS = {".pyc", ".pyo"} # 路径片段包含这些的跳过 SKIP_PARTS = {"__pycache__"} # 文件名完全匹配这些的跳过 SKIP_FILES = {} # 文件名后缀匹配这些的跳过 SKIP_SUFFIX = (".migrated",) # 特定目录下特定后缀跳过 # logs/ 下的 .log # data/apks/ 下的 .apk def should_skip(path, name): full = os.path.join(path, name) rel = os.path.relpath(full, ROOT) norm = rel.replace("\\", "/") if "__pycache__" in norm: return True if name in SKIP_FILES: return True if name.endswith(SKIP_SUFFIX): return True ext = os.path.splitext(name)[1] if ext in SKIP_EXTS: return True if name.endswith(".log") and "/logs/" in norm: return True if name.endswith(".apk") and "/data/apks/" in norm: return True return False def main(): if os.path.exists(OUT): os.remove(OUT) count = 0 with zipfile.ZipFile(OUT, "w", zipfile.ZIP_DEFLATED) as z: for dp, dn, fn in os.walk(ROOT): # 原地修改 dn 跳过目录 dn[:] = [d for d in dn if d not in SKIP_DIRS] for f in fn: if should_skip(dp, f): continue full = os.path.join(dp, f) arc = os.path.relpath(full, os.path.dirname(ROOT)) z.write(full, arc) count += 1 size_kb = os.path.getsize(OUT) // 1024 print(f"打包完成: {OUT}") print(f"文件数: {count}, 大小: {size_kb} KB ({size_kb/1024:.1f} MB)") if __name__ == "__main__": main()