#!/usr/bin/env python3 """Validate the opportunity-intelligence repository without third-party packages.""" from __future__ import annotations import json from pathlib import Path import re import sys REQUIRED = ( "README.md", "AUTHOR.md", "repository-variables.json", "repository.json", "content-index.json", "LICENSE", "CHANGELOG.md", "UPDATES.md", "VERSION", "CITATION.cff", "AGENTS.md", "skills.md", "skills/ai-era-opportunity-intelligence/SKILL.md", "research-results/README.md", ) LINK = re.compile(r"(? list[str]: root = root.resolve() errors: list[str] = [] for relative in REQUIRED: if not (root / relative).exists(): errors.append(f"Missing required path: {relative}") parsed: dict[str, dict] = {} for name in ("repository-variables.json", "repository.json", "content-index.json"): try: parsed[name] = json.loads((root / name).read_text(encoding="utf-8-sig")) except (OSError, UnicodeError, json.JSONDecodeError) as exc: errors.append(f"{name} is invalid: {exc}") manifest = parsed.get("repository.json", {}) variables = parsed.get("repository-variables.json", {}) index = parsed.get("content-index.json", {}) version = (root / "VERSION").read_text(encoding="utf-8-sig").strip() if (root / "VERSION").is_file() else "" if not re.fullmatch(r"\d+\.\d+\.\d+(?:[-+][0-9A-Za-z.-]+)?", version): errors.append(f"VERSION is not semantic: {version!r}") if manifest.get("version") != version: errors.append("VERSION differs from repository.json") if f"## [{version}]" not in ((root / "CHANGELOG.md").read_text(encoding="utf-8-sig") if (root / "CHANGELOG.md").is_file() else ""): errors.append("CHANGELOG.md lacks the current release heading") repo_vars = variables.get("repository", {}) for key in ("repository_id", "name", "title", "canonical_url", "organization"): if repo_vars.get(key) != manifest.get(key): errors.append(f"{key} differs between repository-variables.json and repository.json") declared_entrypoints = manifest.get("entrypoints", {}) for label, relative in declared_entrypoints.items(): if relative is not None and not (root / relative).exists(): errors.append(f"Missing declared entrypoint {label}: {relative}") seen_ids: set[str] = set() seen_paths: set[str] = set() for item in index.get("items", []): content_id = item.get("content_id") relative = item.get("path") if not content_id or content_id in seen_ids: errors.append(f"Invalid or duplicate content_id: {content_id}") else: seen_ids.add(content_id) if not relative or relative in seen_paths: errors.append(f"Invalid or duplicate indexed path: {relative}") else: seen_paths.add(relative) if Path(relative).is_absolute() or ".." in Path(relative).parts: errors.append(f"Unsafe indexed path: {relative}") elif not (root / relative).is_file(): errors.append(f"Indexed report does not exist: {relative}") for path in root.rglob("*.md"): if ".git" in path.parts: continue for raw in LINK.findall(path.read_text(encoding="utf-8-sig")): target = raw.strip().split(maxsplit=1)[0].strip("<>") if not target or target.startswith(("#", "http://", "https://", "mailto:")): continue local = target.split("#", 1)[0] if local and not (path.parent / local).resolve().exists(): errors.append(f"Broken relative Markdown link in {path.relative_to(root)}: {target}") return errors def main() -> int: root = Path(sys.argv[1]) if len(sys.argv) > 1 else Path.cwd() errors = validate(root) print("# Repository Validation") print(f"\n- Repository: `{root.resolve()}`") print(f"- Result: `{'FAIL' if errors else 'PASS'}`") print("\n## Findings") if errors: for error in errors: print(f"\n- ERROR: {error}") else: print("\n- No validation errors found.") return 1 if errors else 0 if __name__ == "__main__": raise SystemExit(main())