100 lines
3.8 KiB
Python
100 lines
3.8 KiB
Python
from __future__ import annotations
|
|
|
|
import hashlib
|
|
import json
|
|
from datetime import datetime, timezone
|
|
from pathlib import Path
|
|
|
|
|
|
CANONICAL = Path(r"D:\择校")
|
|
SCAN_ROOTS = [CANONICAL / "脚本"]
|
|
OUTPUT = CANONICAL / "审计报告" / "20260930_global" / "files" / "final_no_mapping_path_audit_20261003.json"
|
|
CODE_SUFFIXES = {".py", ".ps1", ".psm1", ".cmd", ".bat", ".sh", ".js", ".mjs", ".ts", ".sql"}
|
|
OLD_TOKENS = (
|
|
r"C:\Users\12742\Desktop\考研择校",
|
|
r"D:\zexiao_run",
|
|
r"D:\zexiao",
|
|
)
|
|
ALLOWLIST = {
|
|
"final_no_mapping_path_audit_20261003.py",
|
|
"flatten_single_physical_root_20261003.ps1",
|
|
"single_root_consolidation_postcheck_20261003.ps1",
|
|
"single_root_no_mapping_path_rewrite_audit_20261003.ps1",
|
|
}
|
|
|
|
|
|
def sha256(path: Path) -> str:
|
|
digest = hashlib.sha256()
|
|
with path.open("rb") as handle:
|
|
for block in iter(lambda: handle.read(1024 * 1024), b""):
|
|
digest.update(block)
|
|
return digest.hexdigest()
|
|
|
|
|
|
def main() -> None:
|
|
matches = []
|
|
scanned = 0
|
|
read_errors = []
|
|
for root in SCAN_ROOTS:
|
|
if not root.exists():
|
|
read_errors.append({"path": str(root), "error": "root_missing"})
|
|
continue
|
|
for path in root.rglob("*"):
|
|
if not path.is_file() or path.suffix.lower() not in CODE_SUFFIXES:
|
|
continue
|
|
if any(part.lower() in {"__pycache__", "缓存", "临时产物"} for part in path.parts):
|
|
continue
|
|
scanned += 1
|
|
try:
|
|
text = path.read_text(encoding="utf-8", errors="replace")
|
|
except OSError as exc:
|
|
read_errors.append({"path": str(path), "error": str(exc)})
|
|
continue
|
|
for token in OLD_TOKENS:
|
|
line_hits = []
|
|
for line_no, line in enumerate(text.splitlines(), 1):
|
|
if token in line:
|
|
line_hits.append({"line": line_no, "text": line[:500]})
|
|
if line_hits:
|
|
matches.append(
|
|
{
|
|
"path": str(path),
|
|
"file": path.name,
|
|
"sha256": sha256(path),
|
|
"token": token,
|
|
"allowlisted_validator": path.name in ALLOWLIST,
|
|
"line_matches": line_hits[:20],
|
|
}
|
|
)
|
|
|
|
operational = [row for row in matches if not row["allowlisted_validator"]]
|
|
report = {
|
|
"schema_version": "final_no_mapping_path_audit.v1",
|
|
"generated_at_utc": datetime.now(timezone.utc).isoformat(),
|
|
"mode": "read_only",
|
|
"canonical_root": str(CANONICAL),
|
|
"scanned_roots": [str(root) for root in SCAN_ROOTS],
|
|
"scanned_code_file_count": scanned,
|
|
"match_count": len(matches),
|
|
"operational_match_count": len(operational),
|
|
"allowlisted_validator_match_count": len(matches) - len(operational),
|
|
"matches": matches,
|
|
"read_errors": read_errors,
|
|
"production_database_write": False,
|
|
"move_performed": False,
|
|
"delete_performed": False,
|
|
"verdict": "PASS" if not operational and not read_errors else "PARTIAL",
|
|
"note": "旧路径只允许出现在用于验证旧根不存在的收口审计脚本中;活动业务脚本不得继续依赖旧根或 C 盘工作区。",
|
|
}
|
|
OUTPUT.parent.mkdir(parents=True, exist_ok=True)
|
|
OUTPUT.write_text(json.dumps(report, ensure_ascii=False, indent=2), encoding="utf-8")
|
|
report_hash = sha256(OUTPUT)
|
|
OUTPUT.with_suffix(OUTPUT.suffix + ".sha256").write_text(
|
|
f"{report_hash} {OUTPUT.name}\n", encoding="ascii"
|
|
)
|
|
print(json.dumps({key: report[key] for key in ("scanned_code_file_count", "match_count", "operational_match_count", "verdict")}, ensure_ascii=False))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|