|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Apply cached intros/keywords from .llm_cache/ into .qmd frontmatter (no API). |
| 3 | +
|
| 4 | +Run before group_docs_by_category - the cache is keyed by the original path. |
| 5 | +Usage: apply_cached_intros.py [DOCS_DIR] # default: DOCS |
| 6 | +""" |
| 7 | + |
| 8 | +from __future__ import annotations |
| 9 | + |
| 10 | +import sys |
| 11 | +from pathlib import Path |
| 12 | + |
| 13 | +# repo root is three levels up from .github/scripts/build |
| 14 | +SCRIPT_DIR = Path(__file__).resolve().parent |
| 15 | +SCRIPTS_ROOT = SCRIPT_DIR.parent |
| 16 | +sys.path.insert(0, str(SCRIPTS_ROOT)) |
| 17 | + |
| 18 | +from helpers.qmd_utils import find_qmd_files # noqa: E402 |
| 19 | +from helpers.file_updater import apply_all_updates # noqa: E402 |
| 20 | + |
| 21 | +ROOT_DIR = (SCRIPT_DIR / "../../..").resolve() |
| 22 | +CACHE_DIR = (ROOT_DIR / ".llm_cache").resolve() |
| 23 | +# skip non-doc dirs, same set as update_documentation.py |
| 24 | +BLACKLISTED_DIRS = { |
| 25 | + "templates", |
| 26 | + "includes", |
| 27 | + "theme", |
| 28 | + "_meta", |
| 29 | + "assets", |
| 30 | + "_site", |
| 31 | + ".quarto", |
| 32 | +} |
| 33 | + |
| 34 | + |
| 35 | +def main() -> int: |
| 36 | + docs_arg = sys.argv[1] if len(sys.argv) > 1 else "DOCS" |
| 37 | + input_dir = Path(docs_arg).resolve() |
| 38 | + |
| 39 | + if not input_dir.is_dir(): |
| 40 | + print(f"[apply_cached_intros] '{input_dir}' is not a directory - skipping") |
| 41 | + return 0 |
| 42 | + |
| 43 | + if not CACHE_DIR.is_dir(): |
| 44 | + print(f"[apply_cached_intros] no cache at {CACHE_DIR} - nothing to apply") |
| 45 | + return 0 |
| 46 | + |
| 47 | + # cache keys start with the DOCS dir name, so resolve against its parent |
| 48 | + lookup_root = input_dir.parent |
| 49 | + |
| 50 | + qmd_files = set(find_qmd_files(input_dir, BLACKLISTED_DIRS)) |
| 51 | + if not qmd_files: |
| 52 | + print(f"[apply_cached_intros] no .qmd files under {input_dir}") |
| 53 | + return 0 |
| 54 | + |
| 55 | + stats = apply_all_updates( |
| 56 | + qmd_files, |
| 57 | + CACHE_DIR, |
| 58 | + dry_run=False, |
| 59 | + root_dir=lookup_root, |
| 60 | + apply_versions=False, |
| 61 | + ) |
| 62 | + |
| 63 | + # don't fail the build over a frontmatter glitch |
| 64 | + if stats.get("errors"): |
| 65 | + print(f"[apply_cached_intros] done, with {len(stats['errors'])} error(s)") |
| 66 | + return 0 |
| 67 | + |
| 68 | + |
| 69 | +if __name__ == "__main__": |
| 70 | + sys.exit(main()) |
0 commit comments