diff --git a/ci/tools/tests/test_rebase_sphinx_inventory.py b/ci/tools/tests/test_rebase_sphinx_inventory.py new file mode 100644 index 000000000000..a7a07a14c621 --- /dev/null +++ b/ci/tools/tests/test_rebase_sphinx_inventory.py @@ -0,0 +1,41 @@ +# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 + +import zlib +from pathlib import Path + +import pytest + +from cuda_python.docs.copy_rebased_sphinx_inventory_one_level_up import copy_rebased_inventory_one_level_up + + +@pytest.mark.agent_authored(model="gpt-6") +def test_copy_rebases_relative_inventory_targets(tmp_path, monkeypatch): + header = ( + b"# Sphinx inventory version 2\n" + b"# Project: cuda.bindings\n" + b"# Version: 13.5.0\n" + b"# The remainder of this file is compressed using zlib.\n" + ) + entries = ( + b"cuda.bindings.driver.cuInit py:function 1 module/driver.html#$ -\n" + b"figure 1 std:label -1 overview.html#figure-1 Example with spaces\n" + b"external std:label -1 https://example.org/overview.html#target -\n" + ) + original = header + zlib.compress(entries) + source = tmp_path / "latest" / "objects.inv" + source.parent.mkdir() + source.write_bytes(original) + destination = tmp_path / "objects.inv" + + monkeypatch.chdir(source.parent) + copy_rebased_inventory_one_level_up(Path("objects.inv")) + + rebased = destination.read_bytes() + assert rebased.startswith(header) + assert zlib.decompress(rebased[len(header) :]) == ( + b"cuda.bindings.driver.cuInit py:function 1 latest/module/driver.html#$ -\n" + b"figure 1 std:label -1 latest/overview.html#figure-1 Example with spaces\n" + b"external std:label -1 https://example.org/overview.html#target -\n" + ) + assert source.read_bytes() == original diff --git a/cuda_bindings_12/docs/build_docs.sh b/cuda_bindings_12/docs/build_docs.sh index 351b1a27c452..6a94022b9c16 100755 --- a/cuda_bindings_12/docs/build_docs.sh +++ b/cuda_bindings_12/docs/build_docs.sh @@ -46,5 +46,4 @@ else mv build/html/${SPHINX_CUDA_BINDINGS_VER} build/html/latest fi -# ensure that the Sphinx reference uses the latest docs -cp build/html/latest/objects.inv build/html +python ../../cuda_python/docs/copy_rebased_sphinx_inventory_one_level_up.py build/html/latest/objects.inv diff --git a/cuda_bindings_13/docs/build_docs.sh b/cuda_bindings_13/docs/build_docs.sh index 53d0e14d0ea2..010725e908ad 100755 --- a/cuda_bindings_13/docs/build_docs.sh +++ b/cuda_bindings_13/docs/build_docs.sh @@ -55,5 +55,4 @@ else mv build/html/${SPHINX_CUDA_BINDINGS_VER} build/html/latest fi -# ensure that the Sphinx reference uses the latest docs -cp build/html/latest/objects.inv build/html +python ../../cuda_python/docs/copy_rebased_sphinx_inventory_one_level_up.py build/html/latest/objects.inv diff --git a/cuda_core/docs/build_docs.sh b/cuda_core/docs/build_docs.sh index 28ae6dd07fd1..caa9e3357ce7 100755 --- a/cuda_core/docs/build_docs.sh +++ b/cuda_core/docs/build_docs.sh @@ -51,8 +51,7 @@ else mv build/html/${SPHINX_CUDA_CORE_VER} build/html/latest fi -# ensure that the Sphinx reference uses the latest docs -cp build/html/latest/objects.inv build/html +python ../../cuda_python/docs/copy_rebased_sphinx_inventory_one_level_up.py build/html/latest/objects.inv # clean up previously auto-generated files rm -rf source/generated/ diff --git a/cuda_pathfinder/docs/build_docs.sh b/cuda_pathfinder/docs/build_docs.sh index 4720cd458f6a..b260a4d12128 100755 --- a/cuda_pathfinder/docs/build_docs.sh +++ b/cuda_pathfinder/docs/build_docs.sh @@ -55,5 +55,4 @@ else mv build/html/${SPHINX_CUDA_PATHFINDER_VER} build/html/latest fi -# ensure that the Sphinx reference uses the latest docs -cp build/html/latest/objects.inv build/html +python ../../cuda_python/docs/copy_rebased_sphinx_inventory_one_level_up.py build/html/latest/objects.inv diff --git a/cuda_python/docs/build_docs.sh b/cuda_python/docs/build_docs.sh index 0b2067ed60c7..b06b85f6c141 100755 --- a/cuda_python/docs/build_docs.sh +++ b/cuda_python/docs/build_docs.sh @@ -51,8 +51,7 @@ else mv build/html/${SPHINX_CUDA_PYTHON_VER} build/html/latest fi -# ensure that the Sphinx reference uses the latest docs -cp build/html/latest/objects.inv build/html +python ./copy_rebased_sphinx_inventory_one_level_up.py build/html/latest/objects.inv # clean up previously auto-generated files rm -rf source/generated/ diff --git a/cuda_python/docs/copy_rebased_sphinx_inventory_one_level_up.py b/cuda_python/docs/copy_rebased_sphinx_inventory_one_level_up.py new file mode 100644 index 000000000000..3b428fee0293 --- /dev/null +++ b/cuda_python/docs/copy_rebased_sphinx_inventory_one_level_up.py @@ -0,0 +1,44 @@ +# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 + +"""Copy a Sphinx inventory one level up and rebase its relative URLs.""" + +import argparse +import re +import zlib +from os.path import abspath +from pathlib import Path +from urllib.parse import urlsplit + +# Match the Sphinx v2 fields around the location. Names and display text can +# contain spaces, and Sphinx expands a trailing $ in the location after loading. +_ENTRY = re.compile(r"^(.+?[ \t]+\S+[ \t]+-?\d+[ \t]+?)(\S*)([ \t]+.*)$", re.MULTILINE) + + +def copy_rebased_inventory_one_level_up(source: Path) -> None: + source = Path(abspath(source)) + destination = source.parent.parent / source.name + if destination == source: + raise ValueError(f"{source}: cannot copy an inventory up from the filesystem root") + parts = source.read_bytes().split(b"\n", 4) + if len(parts) != 5 or parts[0] != b"# Sphinx inventory version 2": + raise ValueError(f"{source}: expected a Sphinx v2 inventory") + + url_prefix = source.parent.name + "/" + + def rebase_entry(match: re.Match[str]) -> str: + prefix, location, suffix = match.groups() + if location.startswith("/") or urlsplit(location).scheme: + return match[0] + return f"{prefix}{url_prefix}{location}{suffix}" + + header = b"\n".join(parts[:4]) + b"\n" + entries = zlib.decompress(parts[4]).decode() + rebased = _ENTRY.sub(rebase_entry, entries) + destination.write_bytes(header + zlib.compress(rebased.encode())) + + +if __name__ == "__main__": + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("source", type=Path) + copy_rebased_inventory_one_level_up(parser.parse_args().source)