|
17 | 17 | # add these directories to sys.path here. If the directory is relative to the |
18 | 18 | # documentation root, use os.path.abspath to make it absolute, like shown here. |
19 | 19 | # |
| 20 | +from __future__ import annotations |
| 21 | + |
| 22 | +import glob |
20 | 23 | import os |
21 | 24 | import sys |
| 25 | +from pathlib import Path |
| 26 | +from typing import TYPE_CHECKING, cast |
| 27 | + |
| 28 | +if TYPE_CHECKING: |
| 29 | + from sphinx.application import Sphinx |
| 30 | + from sphinx.util.typing import Inventory |
| 31 | + |
22 | 32 | # So autodoc can import our package |
23 | 33 | sys.path.insert(0, os.path.abspath('../..')) |
24 | 34 |
|
25 | | -# https://docs.readthedocs.io/en/stable/builds.html#build-environment |
26 | | -if "READTHEDOCS" in os.environ: |
27 | | - import glob |
28 | | - if glob.glob("../../newsfragments/*.*.rst"): |
29 | | - print("-- Found newsfragments; running towncrier --", flush=True) |
30 | | - import subprocess |
| 35 | +# Enable reloading with `typing.TYPE_CHECKING` being True |
| 36 | +os.environ["SPHINX_AUTODOC_RELOAD_MODULES"] = "1" |
| 37 | + |
| 38 | +# Handle writing newsfragments into the history file. |
| 39 | +# We want to keep files unchanged when testing locally. |
| 40 | +# So immediately revert the contents after running towncrier, |
| 41 | +# then substitute when Sphinx wants to read it in. |
| 42 | +history_file = Path("history.rst") |
| 43 | + |
| 44 | +history_new: str | None |
| 45 | +if glob.glob("../../newsfragments/*.*.rst"): |
| 46 | + print("-- Found newsfragments; running towncrier --", flush=True) |
| 47 | + history_orig = history_file.read_bytes() |
| 48 | + import subprocess |
| 49 | + |
| 50 | + # In case changes were staged, preserve indexed version. |
| 51 | + # This grabs the hash of the current staged version. |
| 52 | + history_staged = subprocess.run( |
| 53 | + ["git", "rev-parse", "--verify", ":docs/source/history.rst"], |
| 54 | + check=True, |
| 55 | + cwd="../..", |
| 56 | + stdout=subprocess.PIPE, |
| 57 | + encoding="ascii", |
| 58 | + ).stdout.strip() |
| 59 | + try: |
31 | 60 | subprocess.run( |
32 | | - ["towncrier", "--yes", "--date", "not released yet"], |
| 61 | + ["towncrier", "--keep", "--date", "not released yet"], |
33 | 62 | cwd="../..", |
34 | 63 | check=True, |
35 | 64 | ) |
| 65 | + history_new = history_file.read_text("utf8") |
| 66 | + finally: |
| 67 | + # Make sure this reverts even if a failure occurred. |
| 68 | + # Restore whatever was staged. |
| 69 | + print(f"Restoring history.rst = {history_staged}") |
| 70 | + subprocess.run( |
| 71 | + [ |
| 72 | + "git", |
| 73 | + "update-index", |
| 74 | + "--cacheinfo", |
| 75 | + f"100644,{history_staged},docs/source/history.rst", |
| 76 | + ], |
| 77 | + cwd="../..", |
| 78 | + check=False, |
| 79 | + ) |
| 80 | + # And restore the working copy. |
| 81 | + history_file.write_bytes(history_orig) |
| 82 | + del history_orig # We don't need this any more. |
| 83 | +else: |
| 84 | + # Leave it as is. |
| 85 | + history_new = None |
| 86 | + |
| 87 | + |
| 88 | +def on_read_source(app: Sphinx, docname: str, content: list[str]) -> None: |
| 89 | + """Substitute the modified history file.""" |
| 90 | + if docname == "history" and history_new is not None: |
| 91 | + # This is a 1-item list with the file contents. |
| 92 | + content[0] = history_new |
| 93 | + |
36 | 94 |
|
37 | 95 | # Warn about all references to unknown targets |
38 | 96 | nitpicky = True |
|
0 commit comments