Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions ci/tools/tests/test_rebase_sphinx_inventory.py
Original file line number Diff line number Diff line change
@@ -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

@rparolin rparolin Nov 26, 2026 •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit, not blocking: this import only resolves when the repo root is on sys.path, i.e. python -m pytest run from the top of the checkout. Bare pytest ci/tools/tests, or running from another directory, fails at collection with ModuleNotFoundError: cuda_python.

One way to fix it without touching the test: add this under [pytest] in the root pytest.ini.

pythonpath = .

Pytest then always puts the repo root on the import path, so the import here works as written. Note it applies to every test that uses the root config. Untested against the full suite.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in #3640



@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
3 changes: 1 addition & 2 deletions cuda_bindings_12/docs/build_docs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 1 addition & 2 deletions cuda_bindings_13/docs/build_docs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 1 addition & 2 deletions cuda_core/docs/build_docs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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/
3 changes: 1 addition & 2 deletions cuda_pathfinder/docs/build_docs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 1 addition & 2 deletions cuda_python/docs/build_docs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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/
44 changes: 44 additions & 0 deletions cuda_python/docs/copy_rebased_sphinx_inventory_one_level_up.py
Original file line number Diff line number Diff line change
@@ -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)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit, not blocking: a line that does not fit _ENTRY is copied without the latest/ prefix and no error is raised. If the inventory format ever shifts, the 404s come back and the build stays green.

Counting the matches makes it fail loudly instead:

Suggested change
rebased = _ENTRY.sub(rebase_entry, entries)
rebased, matched = _ENTRY.subn(rebase_entry, entries)
expected = sum(1 for line in entries.split("\n") if line.strip())
if matched != expected:
raise ValueError(
f"{source}: {expected - matched} of {expected} inventory entries did not match the expected format"
)

Untested. A test that feeds in a junk line and expects ValueError would lock it in.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in #3640

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)
Loading