#!/usr/bin/env python3 """Read-only structural audit for an AI-ready repository.""" from __future__ import annotations import argparse import json from pathlib import Path import re import sys PROFILES = { "documentation-guide": { "README.md", "AUTHOR.md", "repository.json", "LICENSE", "CHANGELOG.md", "UPDATES.md", "VERSION", "CITATION.cff", "docs", }, "skill-workflow": { "README.md", "AUTHOR.md", "repository.json", "LICENSE", "CHANGELOG.md", "UPDATES.md", "VERSION", "AGENTS.md", "skills.md", "skills", "templates", "examples", }, "software-automation": { "README.md", "AUTHOR.md", "repository.json", "LICENSE", "CHANGELOG.md", "UPDATES.md", "VERSION", "CONTRIBUTING.md", "SECURITY.md", "AGENTS.md", "docs", "scripts", "tests", }, "dataset": { "README.md", "AUTHOR.md", "repository.json", "LICENSE", "CHANGELOG.md", "UPDATES.md", "VERSION", "CITATION.cff", "data", "schemas", "docs", }, } def parse_args() -> argparse.Namespace: parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("repository", type=Path, help="Repository root to inspect") parser.add_argument( "--profile", choices=sorted(PROFILES), required=True, help="Approved repository profile", ) parser.add_argument("--json", action="store_true", help="Emit JSON") return parser.parse_args() def read_version_from_citation(path: Path) -> str | None: if not path.is_file(): return None match = re.search( r"(?m)^version:\s*[\"']?([^\"'\s#]+)", path.read_text(encoding="utf-8-sig"), ) return match.group(1) if match else None def audit(root: Path, profile: str) -> dict: findings: list[dict[str, str]] = [] if not root.is_dir(): return { "repository": str(root), "profile": profile, "passed": False, "findings": [{"level": "error", "message": "Repository path is not a directory"}], } for relative in sorted(PROFILES[profile]): if not (root / relative).exists(): findings.append({"level": "error", "message": f"Missing required path: {relative}"}) manifest_path = root / "repository.json" manifest = None if manifest_path.is_file(): try: manifest = json.loads(manifest_path.read_text(encoding="utf-8-sig")) except (OSError, UnicodeError, json.JSONDecodeError) as exc: findings.append({"level": "error", "message": f"repository.json is invalid: {exc}"}) else: for key in ("schema_version", "repository_id", "title", "description", "version", "entrypoints"): if key not in manifest: findings.append({"level": "error", "message": f"repository.json lacks key: {key}"}) entrypoints = manifest.get("entrypoints", {}) if isinstance(entrypoints, dict): for audience in ("human", "ai"): declared = entrypoints.get(audience) if not declared: findings.append( {"level": "error", "message": f"Missing {audience} entrypoint declaration"} ) elif not (root / declared).exists(): findings.append( { "level": "error", "message": f"Declared {audience} entrypoint does not exist: {declared}", } ) else: findings.append({"level": "error", "message": "entrypoints must be an object"}) version_path = root / "VERSION" version = version_path.read_text(encoding="utf-8-sig").strip() if version_path.is_file() else None if version and manifest and manifest.get("version") != version: findings.append( { "level": "error", "message": f"VERSION ({version}) differs from repository.json ({manifest.get('version')})", } ) citation_version = read_version_from_citation(root / "CITATION.cff") if version and citation_version and citation_version != version: findings.append( { "level": "error", "message": f"VERSION ({version}) differs from CITATION.cff ({citation_version})", } ) readme = root / "README.md" if readme.is_file() and not readme.read_text(encoding="utf-8-sig").strip(): findings.append({"level": "error", "message": "README.md is empty"}) return { "repository": str(root.resolve()), "profile": profile, "passed": not any(item["level"] == "error" for item in findings), "findings": findings, } def emit_markdown(result: dict) -> None: print(f"# Repository Audit\n\n- Repository: `{result['repository']}`") print(f"- Profile: `{result['profile']}`") print(f"- Result: `{'PASS' if result['passed'] else 'FAIL'}`") print("\n## Findings") if not result["findings"]: print("\n- No structural errors found.") else: for finding in result["findings"]: print(f"\n- {finding['level'].upper()}: {finding['message']}") def main() -> int: args = parse_args() result = audit(args.repository, args.profile) if args.json: print(json.dumps(result, indent=2)) else: emit_markdown(result) return 0 if result["passed"] else 1 if __name__ == "__main__": sys.exit(main())