Skip to content

Commit b583a58

Browse files
jeremymanningclaude
andcommitted
Fix post-build script to auto-detect Read the Docs build directory
- Enhanced find_build_dirs() to search multiple possible build locations - Added better error reporting with path diagnostics - Supports both local builds and Read the Docs directory structures - Script now dynamically detects correct _images and gallery HTML paths - Should resolve build directory not found error on Read the Docs 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
1 parent d528b09 commit b583a58

File tree

1 file changed

+58
-6
lines changed

1 file changed

+58
-6
lines changed

docs/post_build.py

Lines changed: 58 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,40 @@
1313
# Base paths
1414
DOCS_DIR = os.path.dirname(os.path.abspath(__file__))
1515
STATIC_THUMBS_DIR = os.path.join(DOCS_DIR, "_static", "thumbnails")
16-
BUILD_IMAGES_DIR = os.path.join(DOCS_DIR, "_build", "html", "_images")
17-
GALLERY_HTML = os.path.join(DOCS_DIR, "_build", "html", "auto_examples", "index.html")
16+
17+
# Auto-detect build directory (Read the Docs vs local)
18+
def find_build_dirs():
19+
"""Find the actual build directory paths"""
20+
possible_build_dirs = [
21+
# Local build
22+
os.path.join(DOCS_DIR, "_build", "html"),
23+
# Read the Docs build (from docs dir)
24+
os.path.join(DOCS_DIR, "..", "_readthedocs", "html"),
25+
# Read the Docs alternative paths
26+
os.path.join(DOCS_DIR, "..", "..", "_readthedocs", "html"),
27+
# Additional Read the Docs patterns based on error message
28+
"/tmp/_readthedocs_build/html",
29+
os.path.join(os.getcwd(), "..", "_readthedocs", "html"),
30+
os.path.join(os.getcwd(), "_readthedocs", "html"),
31+
# Check if we're already in the output directory
32+
os.path.join(os.getcwd(), "_images", ".."),
33+
]
34+
35+
# Also check environment variables that Read the Docs might set
36+
rtd_output = os.environ.get('READTHEDOCS_OUTPUT', '')
37+
if rtd_output:
38+
possible_build_dirs.insert(0, rtd_output)
39+
40+
for build_dir in possible_build_dirs:
41+
if build_dir and os.path.exists(build_dir):
42+
images_dir = os.path.join(build_dir, "_images")
43+
gallery_html = os.path.join(build_dir, "auto_examples", "index.html")
44+
if os.path.exists(images_dir) and os.path.exists(gallery_html):
45+
return images_dir, gallery_html
46+
47+
return None, None
48+
49+
BUILD_IMAGES_DIR, GALLERY_HTML = find_build_dirs()
1850

1951
# Mapping of PNG to GIF thumbnails that should be replaced
2052
GIF_REPLACEMENTS = {
@@ -30,8 +62,23 @@ def copy_gif_thumbnails():
3062
"""Copy GIF thumbnails from _static/thumbnails to _build/html/_images"""
3163
print("Copying GIF thumbnails...")
3264

33-
if not os.path.exists(BUILD_IMAGES_DIR):
34-
print(f"Error: Build images directory not found: {BUILD_IMAGES_DIR}")
65+
# Re-detect directories if needed
66+
global BUILD_IMAGES_DIR, GALLERY_HTML
67+
if not BUILD_IMAGES_DIR:
68+
BUILD_IMAGES_DIR, GALLERY_HTML = find_build_dirs()
69+
70+
if not BUILD_IMAGES_DIR or not os.path.exists(BUILD_IMAGES_DIR):
71+
print(f"Error: Build images directory not found.")
72+
print(f"Searched paths:")
73+
possible_dirs = [
74+
os.path.join(DOCS_DIR, "_build", "html", "_images"),
75+
os.path.join(DOCS_DIR, "..", "_readthedocs", "html", "_images"),
76+
os.path.join(DOCS_DIR, "..", "..", "_readthedocs", "html", "_images"),
77+
]
78+
for d in possible_dirs:
79+
print(f" {d} - {'EXISTS' if os.path.exists(d) else 'NOT FOUND'}")
80+
print(f"Current working directory: {os.getcwd()}")
81+
print(f"DOCS_DIR: {DOCS_DIR}")
3582
return False
3683

3784
if not os.path.exists(STATIC_THUMBS_DIR):
@@ -55,8 +102,13 @@ def update_html_references():
55102
"""Update HTML gallery to reference GIF files instead of PNG"""
56103
print("Updating HTML references...")
57104

58-
if not os.path.exists(GALLERY_HTML):
59-
print(f"Error: Gallery HTML not found: {GALLERY_HTML}")
105+
# Re-detect directories if needed
106+
global BUILD_IMAGES_DIR, GALLERY_HTML
107+
if not GALLERY_HTML:
108+
BUILD_IMAGES_DIR, GALLERY_HTML = find_build_dirs()
109+
110+
if not GALLERY_HTML or not os.path.exists(GALLERY_HTML):
111+
print(f"Error: Gallery HTML not found: {GALLERY_HTML if GALLERY_HTML else 'None'}")
60112
return False
61113

62114
# Read the HTML file

0 commit comments

Comments
 (0)