Skip to content

Commit c2c3afb

Browse files
committed
More things to match Trio's readthedocs
1 parent b3b4905 commit c2c3afb

File tree

1 file changed

+65
-7
lines changed

1 file changed

+65
-7
lines changed

docs/source/conf.py

Lines changed: 65 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,80 @@
1717
# add these directories to sys.path here. If the directory is relative to the
1818
# documentation root, use os.path.abspath to make it absolute, like shown here.
1919
#
20+
from __future__ import annotations
21+
22+
import glob
2023
import os
2124
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+
2232
# So autodoc can import our package
2333
sys.path.insert(0, os.path.abspath('../..'))
2434

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:
3160
subprocess.run(
32-
["towncrier", "--yes", "--date", "not released yet"],
61+
["towncrier", "--keep", "--date", "not released yet"],
3362
cwd="../..",
3463
check=True,
3564
)
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+
3694

3795
# Warn about all references to unknown targets
3896
nitpicky = True

0 commit comments

Comments
 (0)