Skip to content

Commit 6ac0202

Browse files
committed
feat: Implement auto examples download system for documentation builds
1 parent 3b9dbe6 commit 6ac0202

File tree

7 files changed

+573
-1
lines changed

7 files changed

+573
-1
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
name: Generate Auto Examples
2+
3+
on:
4+
push:
5+
branches: [main]
6+
pull_request:
7+
branches: [main]
8+
release:
9+
types: [published]
10+
11+
jobs:
12+
generate-examples:
13+
runs-on: ubuntu-latest
14+
strategy:
15+
matrix:
16+
python-version: ["3.11"] # Use a single version for example generation
17+
18+
steps:
19+
- uses: actions/checkout@v4
20+
21+
- name: Set up Python ${{ matrix.python-version }}
22+
uses: actions/setup-python@v5
23+
with:
24+
python-version: ${{ matrix.python-version }}
25+
cache: "pip"
26+
27+
- name: Install system dependencies
28+
run: |
29+
sudo apt-get update
30+
sudo apt-get install -y graphviz
31+
32+
- name: Install Python dependencies
33+
run: |
34+
python -m pip install --upgrade pip
35+
pip install -r requirements.txt
36+
pip install -r requirements-dev.txt
37+
pip install -e .
38+
39+
- name: Generate auto_examples using sphinx-gallery
40+
run: |
41+
cd docs
42+
# Clean any existing auto_examples
43+
rm -rf auto_examples
44+
# Use sphinx-build to generate only the gallery
45+
sphinx-build -b html -D sphinx_gallery_conf.plot_gallery=True -D sphinx_gallery_conf.download_all_examples=True . _build/html -v
46+
47+
- name: Verify auto_examples generation
48+
run: |
49+
if [ -d "docs/auto_examples" ]; then
50+
echo "auto_examples directory created successfully"
51+
find docs/auto_examples -type f -name "*.py" | head -10
52+
echo "Total Python files: $(find docs/auto_examples -name "*.py" | wc -l)"
53+
echo "Total image files: $(find docs/auto_examples -name "*.png" -o -name "*.jpg" -o -name "*.svg" | wc -l)"
54+
else
55+
echo "ERROR: auto_examples directory was not created"
56+
exit 1
57+
fi
58+
59+
- name: Create auto_examples archive
60+
run: |
61+
cd docs
62+
# Create a zip archive of the auto_examples directory
63+
zip -r auto_examples.zip auto_examples/
64+
echo "Archive created: $(ls -lh auto_examples.zip)"
65+
66+
- name: Upload auto_examples as artifact
67+
uses: actions/upload-artifact@v4
68+
with:
69+
name: auto_examples
70+
path: docs/auto_examples.zip
71+
retention-days: 30
72+
73+
- name: Upload auto_examples to release
74+
if: github.event_name == 'release'
75+
uses: actions/upload-release-asset@v1
76+
env:
77+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
78+
with:
79+
upload_url: ${{ github.event.release.upload_url }}
80+
asset_path: docs/auto_examples.zip
81+
asset_name: auto_examples.zip
82+
asset_content_type: application/zip

.readthedocs.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@ build:
1212
python: "3.10"
1313
apt_packages:
1414
- graphviz # Install the graphviz system package
15+
jobs:
16+
pre_build:
17+
# Pre-build step: Download auto_examples if needed
18+
- python scripts/download_auto_examples.py
19+
post_build:
20+
# Post-build cleanup if needed
21+
- echo "Documentation build completed"
1522

1623
# Build documentation in the docs/ directory with Sphinx
1724
sphinx:

docs/conf.py

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -400,4 +400,46 @@ def setup(app):
400400
app: The Sphinx application object.
401401
"""
402402
app.connect("autodoc-skip-member", skip_member)
403-
app.add_config_value("current_date", get_current_date(), "env")
403+
404+
# Add config value only if it doesn't exist
405+
try:
406+
app.add_config_value("current_date", get_current_date(), "env")
407+
except AttributeError: # More specific exception
408+
pass # Config value already exists
409+
410+
# Download auto_examples before building if needed
411+
app.connect("config-inited", download_auto_examples_if_needed)
412+
413+
414+
def download_auto_examples_if_needed(app, config):
415+
"""Download auto_examples if needed during Sphinx build."""
416+
import subprocess # nosec B404 - subprocess is needed for legitimate script execution
417+
import sys
418+
from pathlib import Path
419+
420+
# Get the project root directory
421+
docs_dir = Path(app.srcdir).resolve()
422+
project_root = docs_dir.parent
423+
# Ensure download_script is a string representation of the path
424+
download_script = str(project_root / "scripts" / "download_auto_examples.py")
425+
python_executable = str(sys.executable) # Ensure python executable is a string
426+
427+
if Path(download_script).exists():
428+
try:
429+
print("[Sphinx] Running auto_examples download script...")
430+
# Ensure all parts of the command are strings
431+
# Validate that we're only executing our own script with trusted python executable
432+
if not Path(download_script).is_file() or not Path(python_executable).is_file():
433+
raise ValueError("Invalid script or python executable path")
434+
command = [python_executable, download_script]
435+
result = subprocess.run(command, capture_output=True, text=True, cwd=str(project_root), check=False) # nosec B603 - controlled execution of our own script
436+
if result.stdout:
437+
print(result.stdout)
438+
if result.stderr:
439+
print(result.stderr)
440+
if result.returncode != 0:
441+
print(f"[Sphinx] Warning: auto_examples download script failed with code {result.returncode}")
442+
except Exception as e:
443+
print(f"[Sphinx] Warning: Could not run auto_examples download script: {e}")
444+
else:
445+
print(f"[Sphinx] Warning: Download script not found at {download_script}")

requirements-dev.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@ coverage>=7.0.0
1919
seaborn
2020
ipython
2121
scikit-learn
22+
requests

scripts/auto_examples_config.ini

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Configuration for auto_examples download
2+
# This file defines where to look for pre-generated auto_examples
3+
4+
[sources]
5+
# GitHub repository information
6+
github_owner = "ipc-lab"
7+
github_repo = "kaira"
8+
9+
# Primary source: GitHub Releases
10+
# The script will look for release assets named "auto_examples.zip"
11+
use_github_releases = true
12+
13+
# Secondary source: GitHub Actions artifacts
14+
# Requires GITHUB_TOKEN environment variable
15+
use_github_artifacts = true
16+
17+
# Fallback: Use local examples directory
18+
# If local examples/ directory exists, sphinx-gallery will generate auto_examples
19+
use_local_examples = true
20+
21+
[settings]
22+
# Whether to create placeholder examples if download fails
23+
create_placeholders = false
24+
25+
# Minimum number of files to consider auto_examples "substantial"
26+
min_files_threshold = 20
27+
28+
# Whether to skip download if auto_examples already exists with substantial content
29+
skip_if_exists = true

scripts/build_docs.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
# Move to the project root directory
44
cd "$(dirname "$0")/.." || exit
55

6+
# Download auto_examples if needed (for ReadTheDocs and other CI environments)
7+
echo "Downloading auto_examples if needed..."
8+
python scripts/download_auto_examples.py
9+
610
# Generate the API reference documentation automatically
711
echo "Generating API reference documentation..."
812
python scripts/generate_api_reference.py docs/api_reference.rst

0 commit comments

Comments
 (0)