76 lines
2.5 KiB
Python
76 lines
2.5 KiB
Python
#!/usr/bin/env python3
|
|
"""Create a source-and-context ZIP for a ChatGPT project handover."""
|
|
|
|
from datetime import datetime
|
|
import os
|
|
from pathlib import Path
|
|
from zipfile import ZIP_DEFLATED, ZipFile
|
|
|
|
|
|
ROOT = Path(__file__).resolve().parent.parent
|
|
OUTPUT = ROOT / "docs" / "snapshots"
|
|
ROOT_ENTRIES = ("AGENTS.md", ".gitignore", "README.md", "api", "react", "docs", "scripts")
|
|
EXCLUDED_DIRS = {
|
|
".git", ".worktrees", ".venv", "venv", "node_modules", "share",
|
|
"instance", "__pycache__", ".pytest_cache", ".mypy_cache", ".ruff_cache",
|
|
"dist", "dist-ssr", "build", "coverage", "snapshots", "logs",
|
|
}
|
|
EXCLUDED_NAMES = {".env", ".flaskenv", ".DS_Store"}
|
|
EXCLUDED_SUFFIXES = (".pyc", ".pyo", ".log", ".zip", ".db", ".sqlite", ".sqlite3", ".pem", ".key", ".p12", ".pfx")
|
|
|
|
|
|
def safe_file(path: Path) -> bool:
|
|
name = path.name.lower()
|
|
if path.is_symlink() or not path.is_file():
|
|
return False
|
|
if name.startswith(".") and name != ".gitignore":
|
|
return False
|
|
if name in EXCLUDED_NAMES or name.startswith(".env."):
|
|
return False
|
|
if name.startswith(("credentials", "id_rsa", "id_ed25519")):
|
|
return False
|
|
return not name.endswith(EXCLUDED_SUFFIXES)
|
|
|
|
|
|
def source_files():
|
|
for root_name in ROOT_ENTRIES:
|
|
entry = ROOT / root_name
|
|
if entry.is_file():
|
|
if safe_file(entry):
|
|
yield entry
|
|
continue
|
|
if not entry.is_dir() or entry.is_symlink():
|
|
continue
|
|
for directory, dirs, files in os.walk(entry, followlinks=False):
|
|
dirs[:] = sorted(
|
|
name for name in dirs
|
|
if name not in EXCLUDED_DIRS
|
|
and not name.startswith(".")
|
|
and not (Path(directory) / name).is_symlink()
|
|
)
|
|
for name in sorted(files):
|
|
path = Path(directory) / name
|
|
if safe_file(path):
|
|
yield path
|
|
|
|
|
|
def main():
|
|
OUTPUT.mkdir(exist_ok=True)
|
|
stamp = datetime.now().strftime("%Y%m%d-%H%M%S-%f")
|
|
archive = OUTPUT / f"foldershare-{stamp}.zip"
|
|
temporary = OUTPUT / f".foldershare-{stamp}.tmp"
|
|
count = 0
|
|
try:
|
|
with ZipFile(temporary, "w", compression=ZIP_DEFLATED) as zip_file:
|
|
for path in source_files():
|
|
zip_file.write(path, path.relative_to(ROOT).as_posix())
|
|
count += 1
|
|
temporary.replace(archive)
|
|
finally:
|
|
temporary.unlink(missing_ok=True)
|
|
print(f"Created {archive} ({count} files)")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|