data-gen-bpu/scripts/release_files.py

57 lines
2.9 KiB
Python

"""One source allowlist for packaging, sync and drift checks; stdlib only."""
from __future__ import annotations
import hashlib
import os
from pathlib import Path
SERVER_GENERATED = {"registry/models.csv", "registry/models_源清单.xlsx"}
DIRECTORIES = {'bin', 'harness', 'agent', 'export', 'registry', 'scripts', 'docs', 'docker'}
FILES = {'Dockerfile', '.dockerignore', 'requirements.txt', 'constraints.txt',
'config.yaml', 'README.md', 'CONTRACT.md', 'HANDBOOK.md', 'CONTINUE.md'}
SUFFIXES = {'.py', '.sh', '.ts', '.js', '.mjs', '.tmpl', '.json', '.yaml', '.yml', '.csv', '.md', '.html', '.txt'}
BLOCKED = {'.git', '.ssh', '.aws', '.azure', '.config', '__pycache__', 'node_modules',
'state', 'runs', 'data', 'cache', 'datasets', 'reports', 'outputs', 'work', 'venv', '.venv'}
def allowed(relative: Path) -> bool:
for part in relative.parts:
low = part.lower()
if ((part.startswith('.') and not (len(relative.parts) == 1 and part == '.dockerignore'))
or low in {'auth.json', 'auth.toml', 'known_hosts'} or low in BLOCKED or low.startswith(('.env', 'secret', 'credential', 'id_rsa', 'id_ed25519'))
or low.endswith(('.pem', '.key', '.p12', '.pfx', '.sqlite', '.db'))):
return False
if len(relative.parts) == 1:
return relative.name in FILES
return relative.parts[0] in DIRECTORIES and (relative.suffix in SUFFIXES or str(relative) in {'bin/bench', 'agent/VERSION'})
def source_files(root: Path, for_sync: bool = False):
"""Walk only source folders, pruning excluded dirs before touching their contents."""
for name in sorted(FILES):
p = root / name
if p.is_file() and not p.is_symlink():
yield p
for name in sorted(DIRECTORIES):
top = root / name
if top.is_symlink() or not top.is_dir():
continue
for parent, dirs, names in os.walk(top, followlinks=False):
dirs[:] = sorted(d for d in dirs if not d.startswith('.') and d.lower() not in BLOCKED
and not d.lower().startswith(('.env', 'secret', 'credential'))
and not (Path(parent) / d).is_symlink())
for filename in sorted(names):
p = Path(parent) / filename
if (not p.is_symlink() and p.is_file() and allowed(p.relative_to(root))
and not (for_sync and p.relative_to(root).as_posix() in SERVER_GENERATED)):
yield p
def snapshot(root: Path, for_sync: bool = False) -> dict:
root = root.resolve(strict=True)
if not root.is_dir() or root == Path('/'):
raise ValueError('expected a specific existing source directory')
return {'root': str(root), 'files': {
p.relative_to(root).as_posix(): {
'sha256': hashlib.sha256(p.read_bytes()).hexdigest(),
'mode': 0o755 if p.stat().st_mode & 0o111 else 0o644}
for p in sorted(source_files(root, for_sync=for_sync))}}