|
| 1 | +# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +# SPDX-License-Identifier: LicenseRef-NVIDIA-SOFTWARE-LICENSE |
| 3 | + |
| 4 | +"""Sphinx extension to auto-inject release dates from git tags. |
| 5 | +
|
| 6 | +For every release-notes page (``release/<version>-notes``), this |
| 7 | +extension looks up the corresponding git tag and injects a |
| 8 | +``Released on <date>`` line after the RST title. Pages that already |
| 9 | +contain such a line, or whose version has no tag yet, are left |
| 10 | +untouched. |
| 11 | +""" |
| 12 | + |
| 13 | +from __future__ import annotations |
| 14 | + |
| 15 | +import re |
| 16 | +import subprocess |
| 17 | +from datetime import UTC, datetime |
| 18 | + |
| 19 | +from sphinx.application import Sphinx |
| 20 | + |
| 21 | +_RELEASED_ON_RE = re.compile(r"Released on ", re.IGNORECASE) |
| 22 | +_RELEASE_NOTE_RE = re.compile(r"^release/(.+)-notes$") |
| 23 | +_UNDERLINE_RE = re.compile(r"^={3,}[ \t]*$", re.MULTILINE) |
| 24 | + |
| 25 | +# project name (from conf.py) -> git tag prefix |
| 26 | +_TAG_PREFIXES: dict[str, str] = { |
| 27 | + "cuda.core": "cuda-core-v", |
| 28 | + "cuda.pathfinder": "cuda-pathfinder-v", |
| 29 | + "cuda.bindings": "v", |
| 30 | + "CUDA Python": "v", |
| 31 | +} |
| 32 | + |
| 33 | + |
| 34 | +def _format_date(iso_date: str) -> str: |
| 35 | + """``2026-03-06`` -> ``Mar 6, 2026``.""" |
| 36 | + dt = datetime.strptime(iso_date, "%Y-%m-%d").replace(tzinfo=UTC) |
| 37 | + return f"{dt.strftime('%b')} {dt.day}, {dt.year}" |
| 38 | + |
| 39 | + |
| 40 | +def _git_tag_date(tag: str) -> str | None: |
| 41 | + """Return the creator date (YYYY-MM-DD) for *tag*, or None.""" |
| 42 | + try: |
| 43 | + result = subprocess.run( # noqa: S603 |
| 44 | + [ # noqa: S607 |
| 45 | + "git", |
| 46 | + "for-each-ref", |
| 47 | + "--format=%(creatordate:short)", |
| 48 | + f"refs/tags/{tag}", |
| 49 | + ], |
| 50 | + capture_output=True, |
| 51 | + text=True, |
| 52 | + timeout=5, |
| 53 | + ) |
| 54 | + date_str = result.stdout.strip() |
| 55 | + except (subprocess.TimeoutExpired, FileNotFoundError): |
| 56 | + date_str = "" |
| 57 | + return date_str or None |
| 58 | + |
| 59 | + |
| 60 | +def _on_source_read(app: Sphinx, docname: str, source: list[str]) -> None: |
| 61 | + m = _RELEASE_NOTE_RE.match(docname) |
| 62 | + if not m: |
| 63 | + return |
| 64 | + |
| 65 | + text = source[0] |
| 66 | + if _RELEASED_ON_RE.search(text): |
| 67 | + return |
| 68 | + |
| 69 | + version = m.group(1) |
| 70 | + prefix = _TAG_PREFIXES.get(app.config.project) |
| 71 | + if prefix is None: |
| 72 | + return |
| 73 | + |
| 74 | + tag = prefix + version |
| 75 | + iso_date = _git_tag_date(tag) |
| 76 | + if not iso_date: |
| 77 | + return |
| 78 | + |
| 79 | + underline = _UNDERLINE_RE.search(text) |
| 80 | + if not underline: |
| 81 | + return |
| 82 | + |
| 83 | + date_line = f"Released on {_format_date(iso_date)}" |
| 84 | + |
| 85 | + # Insert after the title underline: skip any blank lines, then place |
| 86 | + # the date line surrounded by single blank lines before the content. |
| 87 | + after = text[underline.end() :] |
| 88 | + stripped = after.lstrip("\n") |
| 89 | + source[0] = text[: underline.end()] + f"\n\n{date_line}\n\n" + stripped |
| 90 | + |
| 91 | + |
| 92 | +def setup(app: Sphinx) -> dict: |
| 93 | + app.connect("source-read", _on_source_read) |
| 94 | + return {"version": "1.0", "parallel_read_safe": True} |
0 commit comments