|
| 1 | +import argparse |
| 2 | +import subprocess |
| 3 | +import sys |
| 4 | +import os |
| 5 | +import shutil |
| 6 | +from pathlib import Path |
| 7 | + |
| 8 | +def run_command(command, cwd=None, check=True, env=None): |
| 9 | + """Helper to run a shell command.""" |
| 10 | + print(f"Running: {' '.join(str(c) for c in command)}") |
| 11 | + result = subprocess.run(command, cwd=cwd, env=env) |
| 12 | + if check and result.returncode != 0: |
| 13 | + print(f"Error: Command failed with exit code {result.returncode}") |
| 14 | + sys.exit(result.returncode) |
| 15 | + return result |
| 16 | + |
| 17 | +def build_docs_local(source_dir: Path): |
| 18 | + """ |
| 19 | + Builds documentation for the provided source directory. |
| 20 | + Assumes it's running in an environment with necessary tools. |
| 21 | + """ |
| 22 | + print("--- Running in Local Build Mode ---") |
| 23 | + |
| 24 | + # 1. Generate source code and install in editable mode. |
| 25 | + print("\n--- Step 1: Installing in editable mode ---") |
| 26 | + try: |
| 27 | + run_command([sys.executable, "-m", "pip", "install", "-e", "."], cwd=source_dir) |
| 28 | + # Explicitly run schema generation to be sure |
| 29 | + run_command([sys.executable, "setup.py", "generate_schema"], cwd=source_dir) |
| 30 | + except SystemExit: |
| 31 | + print("Warning: 'pip install -e .' failed. This might be due to an externally managed environment.") |
| 32 | + print("Attempting to proceed with documentation build assuming dependencies are met...") |
| 33 | + |
| 34 | + # 2. Install documentation-specific dependencies. |
| 35 | + print("\n--- Step 2: Installing documentation dependencies ---") |
| 36 | + doc_reqs = source_dir / "build-tools" / "requirements.docs.txt" |
| 37 | + if not doc_reqs.exists(): |
| 38 | + print(f"Error: Documentation requirements not found at {doc_reqs}") |
| 39 | + sys.exit(1) |
| 40 | + try: |
| 41 | + run_command([sys.executable, "-m", "pip", "install", "-r", str(doc_reqs)]) |
| 42 | + except SystemExit: |
| 43 | + print("Warning: Failed to install documentation dependencies.") |
| 44 | + # Check if sphinx-build is available |
| 45 | + if shutil.which("sphinx-build") is None: |
| 46 | + print("Error: 'sphinx-build' not found and installation failed.") |
| 47 | + print("Please install dependencies manually or run this script inside a virtual environment.") |
| 48 | + sys.exit(1) |
| 49 | + print("Assuming dependencies are already installed...") |
| 50 | + |
| 51 | + # 3. Build the documentation using Sphinx. |
| 52 | + print("\n--- Step 3: Building Sphinx documentation ---") |
| 53 | + docs_source_dir = source_dir / "documentation" |
| 54 | + docs_build_dir = source_dir / "docs-test" |
| 55 | + |
| 56 | + # Schema generation is now handled in conf.py |
| 57 | + # schema_src = source_dir / "clams" / "appmetadata.jsonschema" |
| 58 | + # schema_dst = docs_source_dir / "appmetadata.jsonschema" |
| 59 | + # if schema_src.exists(): |
| 60 | + # shutil.copy(schema_src, schema_dst) |
| 61 | + |
| 62 | + sphinx_command = [ |
| 63 | + sys.executable, "-m", "sphinx.cmd.build", |
| 64 | + str(docs_source_dir), |
| 65 | + str(docs_build_dir), |
| 66 | + "-b", "html", # build html |
| 67 | + "-a", # write all files (rebuild everything) |
| 68 | + "-E", # don't use a saved environment, reread all files |
| 69 | + ] |
| 70 | + run_command(sphinx_command) |
| 71 | + |
| 72 | + print(f"\nDocumentation build complete. Output in: {docs_build_dir}") |
| 73 | + return docs_build_dir |
| 74 | + |
| 75 | +def main(): |
| 76 | + parser = argparse.ArgumentParser( |
| 77 | + description="Build documentation for the clams-python project." |
| 78 | + ) |
| 79 | + args = parser.parse_args() |
| 80 | + |
| 81 | + build_docs_local(Path.cwd()) |
| 82 | + |
| 83 | +if __name__ == "__main__": |
| 84 | + main() |
0 commit comments