#!/usr/bin/env python3 """ issue_index.py — rebuild tmp/issues/INDEX.md from what is on disk. No network. The index is a map of the cache, nothing else: issues that were never fetched do not appear. issue_get.py and issue_push.py call it automatically; run it by hand only after deleting files. Usage: issue_index.py [--out tmp/issues] """ import argparse import os import re import sys sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) from _tea import ISSUE_ROOT, parse_meta, read_file, write_file # noqa: E402 def cell(v): if isinstance(v, list): return ", ".join(v) or "—" v = (v or "").strip() return v.replace("|", "\\|") or "—" def build(root): rows = [] for name in sorted(os.listdir(root)) if os.path.isdir(root) else []: m = re.match(r'^(\d+)\.md$', name) if not m: continue n = int(m.group(1)) meta, title, _ = parse_meta(read_file(os.path.join(root, name))) labels = meta.get("labels") or [] if isinstance(labels, str): labels = [labels] types = [l for l in labels if l.startswith("type/")] rest = [l for l in labels if not l.startswith("type/")] has_comments = os.path.isfile(os.path.join(root, "%d.comments.md" % n)) rows.append({ "n": n, "state": cell(meta.get("state")), "type": cell(types[0].split("/", 1)[1] if types else ""), "labels": cell(rest), "title": cell(title), "milestone": cell(meta.get("milestone")), "depends": cell(meta.get("depends")), "comments": ("[%s](%d.comments.md)" % (cell(meta.get("comments")), n) if has_comments else "—"), "fetched": cell(meta.get("fetched"))[:10], }) rows.sort(key=lambda r: r["n"]) trees = sorted(f for f in (os.listdir(root) if os.path.isdir(root) else []) if re.match(r'^tree-\d+\.md$', f)) out = ["# Issue cache", "", "Local cache of fetched issues — not a mirror. Refresh with " "`issue_get.py `; issues absent here were never fetched.", ""] if rows: out += ["| # | state | type | labels | title | milestone | depends | comments | fetched |", "|---|---|---|---|---|---|---|---|---|"] out += ["| [#%d](%d.md) | %s | %s | %s | %s | %s | %s | %s | %s |" % ( r["n"], r["n"], r["state"], r["type"], r["labels"], r["title"], r["milestone"], r["depends"], r["comments"], r["fetched"]) for r in rows] else: out.append("_empty_") if trees: out += ["", "## Dependency trees", ""] out += ["- [%s](%s)" % (t, t) for t in trees] drafts_dir = os.path.join(root, "drafts") drafts = sorted(f for f in (os.listdir(drafts_dir) if os.path.isdir(drafts_dir) else []) if f.endswith(".md")) if drafts: out += ["", "## Drafts (not yet pushed)", ""] out += ["- [drafts/%s](drafts/%s)" % (d, d) for d in drafts] out.append("") return write_file(os.path.join(root, "INDEX.md"), "\n".join(out)), len(rows) def main(): ap = argparse.ArgumentParser(description="Rebuild the issue cache index (no network)") ap.add_argument("--out", default=ISSUE_ROOT, help="cache root (default: tmp/issues)") args = ap.parse_args() path, n = build(args.out) print("%s — %d issue(s)" % (path, n)) if __name__ == "__main__": main()