222 lines
7.9 KiB
Python
222 lines
7.9 KiB
Python
from __future__ import annotations
|
|
|
|
import ctypes
|
|
import hashlib
|
|
import json
|
|
import os
|
|
import shutil
|
|
import stat
|
|
import sys
|
|
import time
|
|
from pathlib import Path
|
|
|
|
|
|
ROOT = Path(r"D:\择校")
|
|
REPORT = ROOT / r"审计报告\20260930_global\files\safe_cache_cleanup_execute_20261003.json"
|
|
FORBIDDEN_PARTS = {
|
|
"原始来源",
|
|
"冲突",
|
|
"数据库",
|
|
"staging",
|
|
"raw",
|
|
"raw_html",
|
|
"conflict",
|
|
"conflicts",
|
|
}
|
|
CACHE_NAMES = {"__pycache__", ".pytest_cache"}
|
|
TEMP_PREFIXES = ("_tmp", "_scratch")
|
|
|
|
|
|
def win_lock_check(path: Path) -> dict:
|
|
"""Try an exclusive Windows handle; failure is retained as a lock signal."""
|
|
if os.name != "nt":
|
|
return {"status": "not_windows"}
|
|
kernel32 = ctypes.WinDLL("kernel32", use_last_error=True)
|
|
kernel32.CreateFileW.argtypes = [
|
|
ctypes.c_wchar_p,
|
|
ctypes.c_uint32,
|
|
ctypes.c_uint32,
|
|
ctypes.c_void_p,
|
|
ctypes.c_uint32,
|
|
ctypes.c_uint32,
|
|
ctypes.c_void_p,
|
|
]
|
|
kernel32.CreateFileW.restype = ctypes.c_void_p
|
|
kernel32.CloseHandle.argtypes = [ctypes.c_void_p]
|
|
handle = kernel32.CreateFileW(
|
|
str(path),
|
|
0xC0000000, # GENERIC_READ | GENERIC_WRITE
|
|
0, # no sharing
|
|
None,
|
|
3, # OPEN_EXISTING
|
|
0x02000000, # FILE_FLAG_BACKUP_SEMANTICS
|
|
None,
|
|
)
|
|
invalid = ctypes.c_void_p(-1).value
|
|
if handle in (None, invalid):
|
|
return {"status": "locked_or_unopenable", "winerror": ctypes.get_last_error()}
|
|
kernel32.CloseHandle(handle)
|
|
return {"status": "exclusive_open_ok"}
|
|
|
|
|
|
def sha256(path: Path) -> str:
|
|
h = hashlib.sha256()
|
|
with path.open("rb") as f:
|
|
for block in iter(lambda: f.read(1024 * 1024), b""):
|
|
h.update(block)
|
|
return h.hexdigest().upper()
|
|
|
|
|
|
def rel(path: Path) -> str:
|
|
return str(path.relative_to(ROOT))
|
|
|
|
|
|
def forbidden(path: Path) -> bool:
|
|
return any(part.casefold() in {p.casefold() for p in FORBIDDEN_PARTS} for part in path.relative_to(ROOT).parts)
|
|
|
|
|
|
def walk_candidates() -> tuple[list[Path], list[Path], list[dict]]:
|
|
cache_dirs: list[Path] = []
|
|
temp_dirs: list[Path] = []
|
|
skipped: list[dict] = []
|
|
for dirpath, dirnames, filenames in os.walk(ROOT, topdown=True, followlinks=False):
|
|
current = Path(dirpath)
|
|
# Prune protected trees before descending, including junction/reparse points.
|
|
keep: list[str] = []
|
|
for name in dirnames:
|
|
p = current / name
|
|
try:
|
|
is_reparse = bool(p.stat(follow_symlinks=False).st_file_attributes & 0x400)
|
|
except Exception as e:
|
|
skipped.append({"path": str(p), "reason": "stat_failed", "error": str(e)})
|
|
continue
|
|
if name.casefold() in {x.casefold() for x in FORBIDDEN_PARTS} or forbidden(p) or is_reparse:
|
|
skipped.append({"path": str(p), "reason": "protected_or_reparse_pruned"})
|
|
continue
|
|
if name in CACHE_NAMES:
|
|
cache_dirs.append(p)
|
|
elif name.startswith(TEMP_PREFIXES):
|
|
try:
|
|
# Only an actually empty temporary directory is eligible.
|
|
if not any(p.iterdir()):
|
|
temp_dirs.append(p)
|
|
else:
|
|
skipped.append({"path": str(p), "reason": "non_empty_temp_dir_not_eligible"})
|
|
except Exception as e:
|
|
skipped.append({"path": str(p), "reason": "temp_enumeration_failed", "error": str(e)})
|
|
keep.append(name)
|
|
dirnames[:] = keep
|
|
# Remove nested cache dirs from the deletion roots to avoid double accounting.
|
|
cache_dirs.sort(key=lambda p: len(p.parts))
|
|
roots: list[Path] = []
|
|
for p in cache_dirs:
|
|
if not any(parent in roots for parent in p.parents):
|
|
roots.append(p)
|
|
return roots, temp_dirs, skipped
|
|
|
|
|
|
def inventory_file(path: Path) -> dict:
|
|
item = {"path": str(path), "relative_path": rel(path), "exists_before": path.exists()}
|
|
try:
|
|
st = path.stat()
|
|
item["size_bytes"] = st.st_size
|
|
item["size_G"] = round(st.st_size / 1_000_000_000, 9)
|
|
item["sha256"] = sha256(path)
|
|
item["lock_check"] = win_lock_check(path)
|
|
except Exception as e:
|
|
item["error"] = str(e)
|
|
return item
|
|
|
|
|
|
def collect_files(roots: list[Path]) -> list[Path]:
|
|
result: list[Path] = []
|
|
for root in roots:
|
|
if not root.exists():
|
|
continue
|
|
for dirpath, _, filenames in os.walk(root, topdown=True, followlinks=False):
|
|
for name in filenames:
|
|
p = Path(dirpath) / name
|
|
if not forbidden(p):
|
|
result.append(p)
|
|
return result
|
|
|
|
|
|
def remove_tree(path: Path) -> dict:
|
|
record = {"path": str(path), "relative_path": rel(path), "removed": False, "error": None}
|
|
try:
|
|
if path.exists():
|
|
shutil.rmtree(path)
|
|
record["removed"] = not path.exists()
|
|
except Exception as e:
|
|
record["error"] = str(e)
|
|
return record
|
|
|
|
|
|
def main() -> int:
|
|
started = time.strftime("%Y-%m-%dT%H:%M:%S%z")
|
|
cache_roots, temp_dirs, skipped = walk_candidates()
|
|
roots = cache_roots + [p for p in temp_dirs if p not in cache_roots]
|
|
before_files = collect_files(cache_roots)
|
|
before = [inventory_file(p) for p in before_files]
|
|
before_bytes = sum(x.get("size_bytes", 0) for x in before)
|
|
locked = [x for x in before if x.get("lock_check", {}).get("status") != "exclusive_open_ok"]
|
|
dry_run = {
|
|
"cache_roots": [str(p) for p in cache_roots],
|
|
"empty_temp_dirs": [str(p) for p in temp_dirs],
|
|
"eligible_root_count": len(roots),
|
|
"file_count": len(before),
|
|
"bytes": before_bytes,
|
|
"G": round(before_bytes / 1_000_000_000, 9),
|
|
"locked_or_unopenable_file_count": len(locked),
|
|
"locked_or_unopenable_files": locked[:100],
|
|
}
|
|
removals = []
|
|
for p in roots:
|
|
if p.exists():
|
|
removals.append(remove_tree(p))
|
|
residual = [str(p) for p in roots if p.exists()]
|
|
deleted_files = sum(1 for x in before if not Path(x["path"]).exists())
|
|
deleted_bytes = sum(x.get("size_bytes", 0) for x in before if not Path(x["path"]).exists())
|
|
report = {
|
|
"schema": "safe_cache_cleanup_execute/v1",
|
|
"started_at": started,
|
|
"finished_at": time.strftime("%Y-%m-%dT%H:%M:%S%z"),
|
|
"root": str(ROOT),
|
|
"protected_top_or_nested_parts": sorted(FORBIDDEN_PARTS),
|
|
"scope": "__pycache__, .pytest_cache, empty _tmp* and _scratch* directories; protected trees pruned",
|
|
"dry_run": dry_run,
|
|
"before_files": before,
|
|
"skipped_or_pruned": skipped,
|
|
"removals": removals,
|
|
"postcheck": {
|
|
"residual_eligible_roots": residual,
|
|
"residual_count": len(residual),
|
|
"deleted_file_count": deleted_files,
|
|
"deleted_bytes": deleted_bytes,
|
|
"deleted_G": round(deleted_bytes / 1_000_000_000, 9),
|
|
},
|
|
"production_database_modified": False,
|
|
"verdict": "PASS" if not residual and all(x.get("removed") for x in removals) else "PARTIAL",
|
|
}
|
|
REPORT.parent.mkdir(parents=True, exist_ok=True)
|
|
REPORT.write_text(json.dumps(report, ensure_ascii=False, indent=2), encoding="utf-8")
|
|
digest = hashlib.sha256(REPORT.read_bytes()).hexdigest().upper()
|
|
REPORT.with_suffix(REPORT.suffix + ".sha256").write_text(f"{digest} {REPORT.name}\n", encoding="ascii")
|
|
print(json.dumps({
|
|
"report": str(REPORT),
|
|
"verdict": report["verdict"],
|
|
"cache_roots": len(cache_roots),
|
|
"empty_temp_dirs": len(temp_dirs),
|
|
"before_files": len(before),
|
|
"before_G": dry_run["G"],
|
|
"locked_or_unopenable": len(locked),
|
|
"deleted_files": deleted_files,
|
|
"deleted_G": report["postcheck"]["deleted_G"],
|
|
"residual": residual,
|
|
}, ensure_ascii=False))
|
|
return 0 if report["verdict"] == "PASS" else 2
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|