Repository navigation
[doc-only] Fix shared Sphinx inventory links #3624
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 | ||
|
|
||
|
|
||
| @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 | ||
| 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) | ||||||||||||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit, not blocking: a line that does not fit Counting the matches makes it fail loudly instead:
Suggested change
Untested. A test that feeds in a junk line and expects
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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 pytestrun from the top of the checkout. Barepytest ci/tools/tests, or running from another directory, fails at collection withModuleNotFoundError: cuda_python.One way to fix it without touching the test: add this under
[pytest]in the rootpytest.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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Addressed in #3640