|
| 1 | +#!/bin/bash |
| 2 | +# Build TorchRL documentation locally using uv with a temporary virtual environment. |
| 3 | +# |
| 4 | +# Usage: |
| 5 | +# ./build_local.sh # Build docs (runs tutorials) |
| 6 | +# ./build_local.sh --no-run # Build docs without running tutorials (faster) |
| 7 | +# |
| 8 | +# The script creates a temporary virtual environment, installs dependencies, |
| 9 | +# builds the documentation, and cleans up on failure. |
| 10 | + |
| 11 | +set -e |
| 12 | + |
| 13 | +# Configuration |
| 14 | +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 15 | +ROOT_DIR="$(dirname "$SCRIPT_DIR")" |
| 16 | +VENV_DIR="${ROOT_DIR}/.doc-venv" |
| 17 | +BUILD_DIR="${SCRIPT_DIR}/_local_build" |
| 18 | +PYTHON_VERSION="3.12" |
| 19 | + |
| 20 | +# Parse arguments |
| 21 | +RUN_TUTORIALS=true |
| 22 | +for arg in "$@"; do |
| 23 | + case $arg in |
| 24 | + --no-run) |
| 25 | + RUN_TUTORIALS=false |
| 26 | + shift |
| 27 | + ;; |
| 28 | + --help|-h) |
| 29 | + echo "Usage: $0 [--no-run]" |
| 30 | + echo "" |
| 31 | + echo "Options:" |
| 32 | + echo " --no-run Skip running tutorials (faster build)" |
| 33 | + echo " --help, -h Show this help message" |
| 34 | + exit 0 |
| 35 | + ;; |
| 36 | + esac |
| 37 | +done |
| 38 | + |
| 39 | +# Cleanup function - always runs on exit |
| 40 | +cleanup() { |
| 41 | + local exit_code=$? |
| 42 | + |
| 43 | + # Restore conf.py if we modified it (do this regardless of success/failure) |
| 44 | + if [ -f "$SCRIPT_DIR/source/conf.py.bak" ]; then |
| 45 | + mv "$SCRIPT_DIR/source/conf.py.bak" "$SCRIPT_DIR/source/conf.py" |
| 46 | + echo "Restored conf.py from backup" |
| 47 | + fi |
| 48 | + |
| 49 | + if [ $exit_code -ne 0 ]; then |
| 50 | + echo "" |
| 51 | + echo "============================================" |
| 52 | + echo "Build failed with exit code $exit_code" |
| 53 | + echo "Cleaning up virtual environment..." |
| 54 | + echo "============================================" |
| 55 | + rm -rf "$VENV_DIR" |
| 56 | + echo "Virtual environment removed: $VENV_DIR" |
| 57 | + fi |
| 58 | + exit $exit_code |
| 59 | +} |
| 60 | +trap cleanup EXIT INT TERM |
| 61 | + |
| 62 | +echo "============================================" |
| 63 | +echo "TorchRL Documentation Build Script" |
| 64 | +echo "============================================" |
| 65 | +echo "" |
| 66 | +echo "Root directory: $ROOT_DIR" |
| 67 | +echo "Virtual env: $VENV_DIR" |
| 68 | +echo "Build output: $BUILD_DIR" |
| 69 | +echo "Run tutorials: $RUN_TUTORIALS" |
| 70 | +echo "" |
| 71 | + |
| 72 | +# Check for uv |
| 73 | +if ! command -v uv &> /dev/null; then |
| 74 | + echo "Error: 'uv' is not installed." |
| 75 | + echo "Install it with: curl -LsSf https://astral.sh/uv/install.sh | sh" |
| 76 | + exit 1 |
| 77 | +fi |
| 78 | + |
| 79 | +# Remove existing venv if it exists |
| 80 | +if [ -d "$VENV_DIR" ]; then |
| 81 | + echo "Removing existing virtual environment..." |
| 82 | + rm -rf "$VENV_DIR" |
| 83 | +fi |
| 84 | + |
| 85 | +# Create virtual environment |
| 86 | +echo "Creating virtual environment with Python $PYTHON_VERSION..." |
| 87 | +uv venv "$VENV_DIR" --python "$PYTHON_VERSION" |
| 88 | + |
| 89 | +# Activate virtual environment |
| 90 | +echo "Activating virtual environment..." |
| 91 | +source "$VENV_DIR/bin/activate" |
| 92 | + |
| 93 | +# Upgrade pip and install build tools |
| 94 | +echo "Installing build tools..." |
| 95 | +uv pip install --upgrade pip setuptools ninja packaging "pybind11[global]" cmake |
| 96 | + |
| 97 | +# Install PyTorch (nightly for latest features) |
| 98 | +echo "Installing PyTorch..." |
| 99 | +uv pip install --pre torch torchvision --index-url https://download.pytorch.org/whl/nightly/cpu |
| 100 | + |
| 101 | +# Install tensordict from git |
| 102 | +echo "Installing tensordict..." |
| 103 | +uv pip install git+https://github.com/pytorch/tensordict.git |
| 104 | + |
| 105 | +# Install torchrl in editable mode |
| 106 | +echo "Installing torchrl..." |
| 107 | +cd "$ROOT_DIR" |
| 108 | +uv pip install -e . --no-build-isolation |
| 109 | + |
| 110 | +# Install documentation requirements |
| 111 | +echo "Installing documentation requirements..." |
| 112 | +# First install pytorch_sphinx_theme separately (editable git not supported by uv -r) |
| 113 | +uv pip install git+https://github.com/pytorch/pytorch_sphinx_theme.git |
| 114 | +# Install remaining requirements (skip the editable line) |
| 115 | +grep -v "pytorch_sphinx_theme" "$SCRIPT_DIR/requirements.txt" | uv pip install -r - |
| 116 | + |
| 117 | +# Verify installation |
| 118 | +echo "Verifying torchrl installation..." |
| 119 | +python -c "import torchrl; print(f'TorchRL version: {torchrl.__version__}')" |
| 120 | + |
| 121 | +# Set up environment for building |
| 122 | +export MAX_IDLE_COUNT=180 |
| 123 | +export BATCHED_PIPE_TIMEOUT=180 |
| 124 | +export TORCHRL_CONSOLE_STREAM=stdout |
| 125 | + |
| 126 | +# Clear old MuJoCo environment variables that might interfere with MuJoCo 3.x |
| 127 | +unset MUJOCO_PATH |
| 128 | +unset MUJOCO_PY_MUJOCO_PATH |
| 129 | +unset LD_LIBRARY_PATH # Reset to avoid old MuJoCo libs |
| 130 | + |
| 131 | +# Set plot_gallery based on mode |
| 132 | +# Create backup of conf.py for cleanup to restore |
| 133 | +cp "$SCRIPT_DIR/source/conf.py" "$SCRIPT_DIR/source/conf.py.bak" |
| 134 | + |
| 135 | +if [ "$RUN_TUTORIALS" = true ]; then |
| 136 | + echo "" |
| 137 | + echo "Note: Tutorials WILL be executed" |
| 138 | + # Enable plot_gallery (replace both True and "False" variants) |
| 139 | + sed -i '' 's/"plot_gallery": "False"/"plot_gallery": True/' "$SCRIPT_DIR/source/conf.py" |
| 140 | + sed -i '' 's/"plot_gallery": False/"plot_gallery": True/' "$SCRIPT_DIR/source/conf.py" |
| 141 | +else |
| 142 | + echo "" |
| 143 | + echo "Note: Tutorials will NOT be executed (--no-run mode)" |
| 144 | + # Disable plot_gallery |
| 145 | + sed -i '' 's/"plot_gallery": True/"plot_gallery": "False"/' "$SCRIPT_DIR/source/conf.py" |
| 146 | +fi |
| 147 | + |
| 148 | +# Build documentation |
| 149 | +echo "" |
| 150 | +echo "============================================" |
| 151 | +echo "Building documentation..." |
| 152 | +echo "============================================" |
| 153 | +cd "$SCRIPT_DIR" |
| 154 | +sphinx-build ./source "$BUILD_DIR" -v -j auto |
| 155 | + |
| 156 | +echo "" |
| 157 | +echo "============================================" |
| 158 | +echo "Documentation built successfully!" |
| 159 | +echo "============================================" |
| 160 | +echo "" |
| 161 | +echo "Output: $BUILD_DIR" |
| 162 | +echo "" |
| 163 | +echo "To view the documentation, run:" |
| 164 | +echo " python -m http.server 8000 -d $BUILD_DIR" |
| 165 | +echo " # Then open http://localhost:8000 in your browser" |
| 166 | +echo "" |
| 167 | +echo "To clean up the virtual environment, run:" |
| 168 | +echo " rm -rf $VENV_DIR" |
0 commit comments