#!/usr/bin/env python
"""Collect documentation from source repos into this repository and regenerate
the static index and nginx configuration.
Usage:
python collect.py # copy artifacts, regenerate outputs
python collect.py --dry-run # list planned actions without writing
"""
from __future__ import annotations
import argparse
import shutil
import sys
from pathlib import Path
import yaml
ROOT = Path(__file__).resolve().parent
CONFIG_FILE = ROOT / "config.yml"
MCP_DIR = "mcp"
INDENT = " "
OLD_DIRS = ("libs", "apis", "wiki", "tutorials")
# ── helpers ──────────────────────────────────────────────────────────────────
def load_config() -> dict:
data = yaml.safe_load(CONFIG_FILE.read_text(encoding="utf-8"))
if "service" not in data or "repos" not in data:
print("ERROR: config.yml must define 'service' and 'repos' keys", file=sys.stderr)
sys.exit(1)
return data
def _copy_dir(src: Path, dst: Path) -> None:
if dst.exists():
shutil.rmtree(dst, ignore_errors=True)
dst.parent.mkdir(parents=True, exist_ok=True)
shutil.copytree(src, dst)
def _safe_title(entry: dict) -> str:
return entry.get("title") or entry["name"].replace("-", " ").title()
def _safe_description(entry: dict) -> str:
return entry.get("description") or f"Documentation for {_safe_title(entry)}"
def _find_home(dest: Path) -> str:
"""After copying a site into *dest*, return the relative URL path
where index.html lives. Checks root, lib/, api/ in order."""
if (dest / "index.html").exists():
return ""
if (dest / "lib" / "index.html").exists():
return "lib/"
if (dest / "api" / "index.html").exists():
return "api/"
return ""
def _find_home_for_kind(dest: Path, kind: str) -> str:
"""After copying a site into *dest*, return the relative URL path for a
specific doc kind. A root index.html serves every kind; otherwise each
kind is found under its own subdirectory (lib/, api/, wiki/)."""
if (dest / "index.html").exists():
return ""
sub = dest / kind
if kind in ("lib", "api", "wiki") and (sub / "index.html").exists():
return f"{kind}/"
return _find_home(dest)
# ── index.html generator ────────────────────────────────────────────────────
INDEX_TEMPLATE = r"""