|
19 | 19 | from __future__ import annotations
|
20 | 20 |
|
21 | 21 | import collections.abc
|
| 22 | +import glob |
22 | 23 | import os
|
23 | 24 | import sys
|
24 | 25 | import types
|
| 26 | +from pathlib import Path |
25 | 27 | from typing import TYPE_CHECKING, cast
|
26 | 28 |
|
27 | 29 | if TYPE_CHECKING:
|
|
36 | 38 | # Enable reloading with `typing.TYPE_CHECKING` being True
|
37 | 39 | os.environ["SPHINX_AUTODOC_RELOAD_MODULES"] = "1"
|
38 | 40 |
|
39 |
| -# https://docs.readthedocs.io/en/stable/builds.html#build-environment |
40 |
| -if "READTHEDOCS" in os.environ: |
41 |
| - import glob |
42 |
| - |
43 |
| - if glob.glob("../../newsfragments/*.*.rst"): |
44 |
| - print("-- Found newsfragments; running towncrier --", flush=True) |
45 |
| - import subprocess |
46 |
| - |
| 41 | +# Handle writing newsfragments into the history file. |
| 42 | +# We want to keep files unchanged when testing locally. |
| 43 | +# So immediately revert the contents after running towncrier, |
| 44 | +# then substitute when Sphinx wants to read it in. |
| 45 | +history_file = Path("history.rst") |
| 46 | + |
| 47 | +history_new: str | None |
| 48 | +if glob.glob("../../newsfragments/*.*.rst"): |
| 49 | + print("-- Found newsfragments; running towncrier --", flush=True) |
| 50 | + history_orig = history_file.read_bytes() |
| 51 | + import subprocess |
| 52 | + |
| 53 | + # In case changes were staged, preserve indexed version. |
| 54 | + # This grabs the hash of the current staged version. |
| 55 | + history_staged = subprocess.run( |
| 56 | + ["git", "rev-parse", "--verify", ":docs/source/history.rst"], |
| 57 | + check=True, |
| 58 | + cwd="../..", |
| 59 | + stdout=subprocess.PIPE, |
| 60 | + encoding="ascii", |
| 61 | + ).stdout.strip() |
| 62 | + try: |
47 | 63 | subprocess.run(
|
48 |
| - ["towncrier", "--yes", "--date", "not released yet"], |
| 64 | + ["towncrier", "--keep", "--date", "not released yet"], |
49 | 65 | cwd="../..",
|
50 | 66 | check=True,
|
51 | 67 | )
|
| 68 | + history_new = history_file.read_text("utf8") |
| 69 | + finally: |
| 70 | + # Make sure this reverts even if a failure occurred. |
| 71 | + # Restore whatever was staged. |
| 72 | + print(f"Restoring history.rst = {history_staged}") |
| 73 | + subprocess.run( |
| 74 | + [ |
| 75 | + "git", |
| 76 | + "update-index", |
| 77 | + "--cacheinfo", |
| 78 | + f"100644,{history_staged},docs/source/history.rst", |
| 79 | + ], |
| 80 | + cwd="../..", |
| 81 | + check=False, |
| 82 | + ) |
| 83 | + # And restore the working copy. |
| 84 | + history_file.write_bytes(history_orig) |
| 85 | + del history_orig # We don't need this any more. |
| 86 | +else: |
| 87 | + # Leave it as is. |
| 88 | + history_new = None |
| 89 | + |
| 90 | + |
| 91 | +def on_read_source(app: Sphinx, docname: str, content: list[str]) -> None: |
| 92 | + """Substitute the modified history file.""" |
| 93 | + if docname == "history" and history_new is not None: |
| 94 | + # This is a 1-item list with the file contents. |
| 95 | + content[0] = history_new |
| 96 | + |
52 | 97 |
|
53 | 98 | # Sphinx is very finicky, and somewhat buggy, so we have several different
|
54 | 99 | # methods to help it resolve links.
|
@@ -153,6 +198,7 @@ def setup(app: Sphinx) -> None:
|
153 | 198 | app.connect("autodoc-process-signature", autodoc_process_signature)
|
154 | 199 | # After Intersphinx runs, add additional mappings.
|
155 | 200 | app.connect("builder-inited", add_intersphinx, priority=1000)
|
| 201 | + app.connect("source-read", on_read_source) |
156 | 202 |
|
157 | 203 |
|
158 | 204 | # -- General configuration ------------------------------------------------
|
|
0 commit comments