|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +# -------------------------------------------------------------------------------------------- |
| 4 | +# Copyright (c) Microsoft Corporation. All rights reserved. |
| 5 | +# Licensed under the MIT License. See License.txt in the project root for license information. |
| 6 | +# -------------------------------------------------------------------------------------------- |
| 7 | + |
| 8 | +# This script is used to execute sphinx documentation build within a tox environment. |
| 9 | +# It uses a central sphinx configuration and validates docstrings by running sphinx-build. |
| 10 | + |
| 11 | +from subprocess import check_call, CalledProcessError |
| 12 | +import os |
| 13 | +import logging |
| 14 | +import sys |
| 15 | +from pathlib import Path |
| 16 | +from util import run_check |
| 17 | + |
| 18 | +logging.getLogger().setLevel(logging.INFO) |
| 19 | + |
| 20 | +# Get the central Sphinx config directory |
| 21 | +SPHINX_CONF_DIR = os.path.abspath(os.path.dirname(__file__)) |
| 22 | + |
| 23 | + |
| 24 | +def _create_minimal_index_rst(docs_dir, package_name, module_names): |
| 25 | + """Create a minimal index.rst file for sphinx to process.""" |
| 26 | + index_rst_content = f"""{package_name} |
| 27 | +{"=" * len(package_name)} |
| 28 | +
|
| 29 | +""" |
| 30 | + |
| 31 | + for module_name in module_names: |
| 32 | + index_rst_content += f""" |
| 33 | +{module_name} |
| 34 | +{"-" * len(module_name)} |
| 35 | +
|
| 36 | +.. automodule:: {module_name} |
| 37 | + :members: |
| 38 | + :undoc-members: |
| 39 | + :show-inheritance: |
| 40 | +
|
| 41 | +""" |
| 42 | + |
| 43 | + index_rst_path = docs_dir / "index.rst" |
| 44 | + with open(index_rst_path, "w") as f: |
| 45 | + f.write(index_rst_content) |
| 46 | + |
| 47 | + |
| 48 | +def _single_dir_sphinx(mod): |
| 49 | + """Run sphinx-build on a single package directory.""" |
| 50 | + |
| 51 | + # Find the actual Python package directories |
| 52 | + package_dirs = [ |
| 53 | + d for d in mod.iterdir() if d.is_dir() and not d.name.startswith("_") and (d / "__init__.py").exists() |
| 54 | + ] |
| 55 | + |
| 56 | + if not package_dirs: |
| 57 | + logging.info(f"No Python package found in {mod}, skipping sphinx build") |
| 58 | + return True |
| 59 | + |
| 60 | + # Get the main package directory |
| 61 | + main_package = package_dirs[0] |
| 62 | + |
| 63 | + # Find submodules |
| 64 | + module_names = [] |
| 65 | + for item in main_package.iterdir(): |
| 66 | + if item.is_dir() and (item / "__init__.py").exists(): |
| 67 | + module_names.append(f"{main_package.name}.{item.name}") |
| 68 | + |
| 69 | + # If no submodules, just use the main package |
| 70 | + if not module_names: |
| 71 | + module_names = [main_package.name] |
| 72 | + |
| 73 | + # Create docs directory structure |
| 74 | + docs_dir = mod / "docs" |
| 75 | + docs_dir.mkdir(exist_ok=True) |
| 76 | + |
| 77 | + # Create index.rst with the correct module names |
| 78 | + _create_minimal_index_rst(docs_dir, mod.stem, module_names) |
| 79 | + |
| 80 | + # Set up output directory |
| 81 | + output_dir = mod / "_build" / "html" |
| 82 | + output_dir.mkdir(parents=True, exist_ok=True) |
| 83 | + |
| 84 | + # Add the package to sys.path so sphinx can import it |
| 85 | + sys.path.insert(0, str(mod.absolute())) |
| 86 | + |
| 87 | + try: |
| 88 | + result = check_call( |
| 89 | + [ |
| 90 | + sys.executable, |
| 91 | + "-m", |
| 92 | + "sphinx", |
| 93 | + "-b", |
| 94 | + "html", # Build HTML output |
| 95 | + "-c", |
| 96 | + SPHINX_CONF_DIR, # Use central config |
| 97 | + "-W", # Treat warnings as errors |
| 98 | + "--keep-going", # Continue to get all errors |
| 99 | + "-E", # Don't use cached environment |
| 100 | + "-q", # Quiet mode (only show warnings/errors) |
| 101 | + str(docs_dir.absolute()), # Source directory |
| 102 | + str(output_dir.absolute()), # Output directory |
| 103 | + ] |
| 104 | + ) |
| 105 | + logging.info(f"Sphinx build completed successfully for {mod.stem}") |
| 106 | + return True |
| 107 | + except CalledProcessError as e: |
| 108 | + logging.error(f"{mod.stem} exited with sphinx build error {e.returncode}") |
| 109 | + return False |
| 110 | + finally: |
| 111 | + # Remove from sys.path |
| 112 | + if str(mod.absolute()) in sys.path: |
| 113 | + sys.path.remove(str(mod.absolute())) |
| 114 | + |
| 115 | + |
| 116 | +if __name__ == "__main__": |
| 117 | + run_check("sphinx", _single_dir_sphinx, "Sphinx documentation build") |
0 commit comments