|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# |
| 3 | +# A script to generate documentation for previous releases of MUSE2. |
| 4 | + |
| 5 | +import os |
| 6 | +import shutil |
| 7 | +import subprocess as sp |
| 8 | +from pathlib import Path |
| 9 | +from tempfile import TemporaryDirectory |
| 10 | + |
| 11 | +from release import get_releases |
| 12 | + |
| 13 | +DOCS_SITE_ROOT = "https://energysystemsmodellinglab.github.io/MUSE2" |
| 14 | +REPO_ROOT = Path(__file__).parent.parent.absolute() |
| 15 | +DOCS_DIR = REPO_ROOT / "docs" |
| 16 | + |
| 17 | + |
| 18 | +def clone_repo_to(dest: Path): |
| 19 | + """Clone this repo somewhere else.""" |
| 20 | + print("Making a copy of repo") |
| 21 | + sp.run(("git", "clone", REPO_ROOT, dest), check=True, capture_output=True) |
| 22 | + |
| 23 | + # Add a symlink to cargo cache dir |
| 24 | + try: |
| 25 | + os.symlink(REPO_ROOT / "target", dest / "target") |
| 26 | + except (NotImplementedError, OSError): |
| 27 | + # Only newer versions of Windows support symlinks and these require the user to have |
| 28 | + # additional privileges (or to be in developer mode) |
| 29 | + print( |
| 30 | + "WARN: Could not create symlink to cache directory; cache will not be stored" |
| 31 | + ) |
| 32 | + |
| 33 | + |
| 34 | +def apply_patches_for_release(release: str, repo_path: Path) -> None: |
| 35 | + """Apply patches (if any) for the given release.""" |
| 36 | + patches_dir = DOCS_DIR / "release" / "patches" / release |
| 37 | + for patch_path in sorted(patches_dir.glob("*.patch")): |
| 38 | + sp.run(("git", "-C", str(repo_path), "am", str(patch_path)), check=True) |
| 39 | + |
| 40 | + |
| 41 | +def build_docs_for_release(release: str, repo_path: Path, outdir: Path) -> None: |
| 42 | + """Build documentation for a given release.""" |
| 43 | + print(f"Building docs for {release}") |
| 44 | + |
| 45 | + # Check out release |
| 46 | + sp.run( |
| 47 | + ("git", "-C", str(repo_path), "checkout", release), |
| 48 | + check=True, |
| 49 | + capture_output=True, |
| 50 | + ) |
| 51 | + |
| 52 | + # Apply patches, if any |
| 53 | + apply_patches_for_release(release, repo_path) |
| 54 | + |
| 55 | + # Build docs |
| 56 | + sp.run(("just", f"{repo_path!s}/build-docs"), check=True) |
| 57 | + |
| 58 | + # Patch versions.html to redirect to main versions page |
| 59 | + with (repo_path / "book" / "versions.html").open("w", encoding="utf-8") as f: |
| 60 | + f.write(f"""<head> |
| 61 | + <meta http-equiv="Refresh" content="0; URL={DOCS_SITE_ROOT}/versions.html" /> |
| 62 | +</head>""") |
| 63 | + |
| 64 | + # Move to output directory |
| 65 | + release_outdir = outdir / release |
| 66 | + print(f"Copying to {release_outdir}") |
| 67 | + shutil.move((repo_path / "book"), release_outdir) |
| 68 | + |
| 69 | + |
| 70 | +def build_old_docs() -> None: |
| 71 | + """Build documentation for previous releases.""" |
| 72 | + outdir = REPO_ROOT / "book" / "release" |
| 73 | + outdir.mkdir(parents=True, exist_ok=True) |
| 74 | + |
| 75 | + # Clone this repo to a temporary directory |
| 76 | + with TemporaryDirectory() as tmpdir: |
| 77 | + repo_path = Path(tmpdir) |
| 78 | + clone_repo_to(repo_path) |
| 79 | + |
| 80 | + # Generate documentation for each previous release |
| 81 | + for release in get_releases(): |
| 82 | + build_docs_for_release(release, repo_path, outdir) |
| 83 | + |
| 84 | + |
| 85 | +if __name__ == "__main__": |
| 86 | + build_old_docs() |
0 commit comments