From 6dd9d8e07c90ed0af59def919daa9f96df38c5ca Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Wed, 25 Nov 2026 18:06:23 -0800 Subject: [PATCH 1/4] Fix shared Sphinx inventory targets --- .github/workflows/build-docs.yml | 4 ++ ci/tools/rebase_sphinx_inventory.py | 46 +++++++++++++++++++ .../tests/test_rebase_sphinx_inventory.py | 42 +++++++++++++++++ 3 files changed, 92 insertions(+) create mode 100644 ci/tools/rebase_sphinx_inventory.py create mode 100644 ci/tools/tests/test_rebase_sphinx_inventory.py diff --git a/.github/workflows/build-docs.yml b/.github/workflows/build-docs.yml index fa30d6195d63..95d72260e470 100644 --- a/.github/workflows/build-docs.yml +++ b/.github/workflows/build-docs.yml @@ -360,6 +360,10 @@ jobs: rm -f -- "$root"/{nv-versions.json,versions.json,objects.inv} done + - name: Rebase shared docs inventory targets + if: ${{ !inputs.is-release }} + run: python .ci-control/ci/tools/rebase_sphinx_inventory.py artifacts/docs + # Check links and anchors in the rendered HTML built above. # Links in authored Markdown and reST files are checked separately by the # ordinary Linux/Windows pre-commit jobs. diff --git a/ci/tools/rebase_sphinx_inventory.py b/ci/tools/rebase_sphinx_inventory.py new file mode 100644 index 000000000000..a73776018732 --- /dev/null +++ b/ci/tools/rebase_sphinx_inventory.py @@ -0,0 +1,46 @@ +# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 + +"""Make staged root Sphinx inventories point into their latest/ directories.""" + +import argparse +import re +import zlib +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 _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}latest/{location}{suffix}" + + +def rebase_root_inventories(docs_root: Path) -> None: + latest_inventories = sorted(docs_root.glob("*/latest/objects.inv")) + metapackage_inventory = docs_root / "latest" / "objects.inv" + if metapackage_inventory.is_file(): + latest_inventories.insert(0, metapackage_inventory) + if not latest_inventories: + raise FileNotFoundError(f"No staged latest/objects.inv under {docs_root}") + + for inventory in latest_inventories: + parts = inventory.read_bytes().split(b"\n", 4) + if len(parts) != 5 or parts[0] != b"# Sphinx inventory version 2": + raise ValueError(f"{inventory}: expected a Sphinx v2 inventory") + + header = b"\n".join(parts[:4]) + b"\n" + entries = zlib.decompress(parts[4]).decode() + rebased = _ENTRY.sub(_rebase_entry, entries) + (inventory.parent.parent / "objects.inv").write_bytes(header + zlib.compress(rebased.encode())) + + +if __name__ == "__main__": + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("docs_root", type=Path) + rebase_root_inventories(parser.parse_args().docs_root) 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..2812d723423a --- /dev/null +++ b/ci/tools/tests/test_rebase_sphinx_inventory.py @@ -0,0 +1,42 @@ +# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 + +import zlib + +import pytest + +from ci.tools.rebase_sphinx_inventory import rebase_root_inventories + + +@pytest.mark.agent_authored(model="gpt-6") +def test_only_root_inventory_targets_are_rebased(tmp_path): + 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) + roots = [tmp_path, *(tmp_path / f"cuda-{part}" for part in ("bindings", "core", "pathfinder"))] + for root in roots: + for path in (root, root / "latest", root / "13.5.0"): + path.mkdir(parents=True, exist_ok=True) + (path / "objects.inv").write_bytes(original) + + rebase_root_inventories(tmp_path) + + for root in roots: + rebased = (root / "objects.inv").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 (root / "latest" / "objects.inv").read_bytes() == original + assert (root / "13.5.0" / "objects.inv").read_bytes() == original From c296574515e73a914244c51945f9b2f7fb9707de Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Wed, 25 Nov 2026 18:06:28 -0800 Subject: [PATCH 2/4] ci: require manual copy-pr-bot approval From 6efd1e20228a5738dcfd7a371e98fa2f2084f960 Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Wed, 25 Nov 2026 18:30:42 -0800 Subject: [PATCH 3/4] Copy and rebase Sphinx inventories in the docs builders --- .github/workflows/build-docs.yml | 4 -- ci/tools/rebase_sphinx_inventory.py | 46 ------------------- .../tests/test_rebase_sphinx_inventory.py | 33 ++++++------- cuda_bindings_12/docs/build_docs.sh | 3 +- cuda_bindings_13/docs/build_docs.sh | 3 +- cuda_core/docs/build_docs.sh | 3 +- cuda_pathfinder/docs/build_docs.sh | 3 +- cuda_python/docs/build_docs.sh | 3 +- .../docs/copy_rebased_sphinx_inventory.py | 42 +++++++++++++++++ 9 files changed, 62 insertions(+), 78 deletions(-) delete mode 100644 ci/tools/rebase_sphinx_inventory.py create mode 100644 cuda_python/docs/copy_rebased_sphinx_inventory.py diff --git a/.github/workflows/build-docs.yml b/.github/workflows/build-docs.yml index 95d72260e470..fa30d6195d63 100644 --- a/.github/workflows/build-docs.yml +++ b/.github/workflows/build-docs.yml @@ -360,10 +360,6 @@ jobs: rm -f -- "$root"/{nv-versions.json,versions.json,objects.inv} done - - name: Rebase shared docs inventory targets - if: ${{ !inputs.is-release }} - run: python .ci-control/ci/tools/rebase_sphinx_inventory.py artifacts/docs - # Check links and anchors in the rendered HTML built above. # Links in authored Markdown and reST files are checked separately by the # ordinary Linux/Windows pre-commit jobs. diff --git a/ci/tools/rebase_sphinx_inventory.py b/ci/tools/rebase_sphinx_inventory.py deleted file mode 100644 index a73776018732..000000000000 --- a/ci/tools/rebase_sphinx_inventory.py +++ /dev/null @@ -1,46 +0,0 @@ -# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. -# SPDX-License-Identifier: Apache-2.0 - -"""Make staged root Sphinx inventories point into their latest/ directories.""" - -import argparse -import re -import zlib -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 _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}latest/{location}{suffix}" - - -def rebase_root_inventories(docs_root: Path) -> None: - latest_inventories = sorted(docs_root.glob("*/latest/objects.inv")) - metapackage_inventory = docs_root / "latest" / "objects.inv" - if metapackage_inventory.is_file(): - latest_inventories.insert(0, metapackage_inventory) - if not latest_inventories: - raise FileNotFoundError(f"No staged latest/objects.inv under {docs_root}") - - for inventory in latest_inventories: - parts = inventory.read_bytes().split(b"\n", 4) - if len(parts) != 5 or parts[0] != b"# Sphinx inventory version 2": - raise ValueError(f"{inventory}: expected a Sphinx v2 inventory") - - header = b"\n".join(parts[:4]) + b"\n" - entries = zlib.decompress(parts[4]).decode() - rebased = _ENTRY.sub(_rebase_entry, entries) - (inventory.parent.parent / "objects.inv").write_bytes(header + zlib.compress(rebased.encode())) - - -if __name__ == "__main__": - parser = argparse.ArgumentParser(description=__doc__) - parser.add_argument("docs_root", type=Path) - rebase_root_inventories(parser.parse_args().docs_root) diff --git a/ci/tools/tests/test_rebase_sphinx_inventory.py b/ci/tools/tests/test_rebase_sphinx_inventory.py index 2812d723423a..24f5c3666450 100644 --- a/ci/tools/tests/test_rebase_sphinx_inventory.py +++ b/ci/tools/tests/test_rebase_sphinx_inventory.py @@ -5,11 +5,11 @@ import pytest -from ci.tools.rebase_sphinx_inventory import rebase_root_inventories +from cuda_python.docs.copy_rebased_sphinx_inventory import copy_rebased_inventory @pytest.mark.agent_authored(model="gpt-6") -def test_only_root_inventory_targets_are_rebased(tmp_path): +def test_copy_rebases_relative_inventory_targets(tmp_path): header = ( b"# Sphinx inventory version 2\n" b"# Project: cuda.bindings\n" @@ -22,21 +22,18 @@ def test_only_root_inventory_targets_are_rebased(tmp_path): b"external std:label -1 https://example.org/overview.html#target -\n" ) original = header + zlib.compress(entries) - roots = [tmp_path, *(tmp_path / f"cuda-{part}" for part in ("bindings", "core", "pathfinder"))] - for root in roots: - for path in (root, root / "latest", root / "13.5.0"): - path.mkdir(parents=True, exist_ok=True) - (path / "objects.inv").write_bytes(original) + source = tmp_path / "latest" / "objects.inv" + source.parent.mkdir() + source.write_bytes(original) + destination = tmp_path / "objects.inv" - rebase_root_inventories(tmp_path) + copy_rebased_inventory(source, destination) - for root in roots: - rebased = (root / "objects.inv").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 (root / "latest" / "objects.inv").read_bytes() == original - assert (root / "13.5.0" / "objects.inv").read_bytes() == original + 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..3977b84fb531 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.py build/html/latest/objects.inv build/html/objects.inv diff --git a/cuda_bindings_13/docs/build_docs.sh b/cuda_bindings_13/docs/build_docs.sh index 53d0e14d0ea2..66e3be1fac1a 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.py build/html/latest/objects.inv build/html/objects.inv diff --git a/cuda_core/docs/build_docs.sh b/cuda_core/docs/build_docs.sh index 28ae6dd07fd1..0e271095d168 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.py build/html/latest/objects.inv build/html/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..ad8cdb37869d 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.py build/html/latest/objects.inv build/html/objects.inv diff --git a/cuda_python/docs/build_docs.sh b/cuda_python/docs/build_docs.sh index 0b2067ed60c7..aa76047d6662 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.py build/html/latest/objects.inv build/html/objects.inv # clean up previously auto-generated files rm -rf source/generated/ diff --git a/cuda_python/docs/copy_rebased_sphinx_inventory.py b/cuda_python/docs/copy_rebased_sphinx_inventory.py new file mode 100644 index 000000000000..779e538ca0a3 --- /dev/null +++ b/cuda_python/docs/copy_rebased_sphinx_inventory.py @@ -0,0 +1,42 @@ +# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 + +"""Copy a Sphinx inventory and rebase its relative URLs for the destination.""" + +import argparse +import re +import zlib +from os.path import relpath +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(source: Path, destination: Path) -> None: + 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") + + relative_dir = Path(relpath(source.parent, destination.parent)).as_posix() + + 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}{relative_dir}/{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) + parser.add_argument("destination", type=Path) + args = parser.parse_args() + copy_rebased_inventory(args.source, args.destination) From c01f1b6a4e4deb9415948a7ac1c2ab5e443d8382 Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Wed, 25 Nov 2026 18:44:18 -0800 Subject: [PATCH 4/4] Make the inventory copy a one-level operation --- ci/tools/tests/test_rebase_sphinx_inventory.py | 8 +++++--- cuda_bindings_12/docs/build_docs.sh | 2 +- cuda_bindings_13/docs/build_docs.sh | 2 +- cuda_core/docs/build_docs.sh | 2 +- cuda_pathfinder/docs/build_docs.sh | 2 +- cuda_python/docs/build_docs.sh | 2 +- ...y_rebased_sphinx_inventory_one_level_up.py} | 18 ++++++++++-------- 7 files changed, 20 insertions(+), 16 deletions(-) rename cuda_python/docs/{copy_rebased_sphinx_inventory.py => copy_rebased_sphinx_inventory_one_level_up.py} (69%) diff --git a/ci/tools/tests/test_rebase_sphinx_inventory.py b/ci/tools/tests/test_rebase_sphinx_inventory.py index 24f5c3666450..a7a07a14c621 100644 --- a/ci/tools/tests/test_rebase_sphinx_inventory.py +++ b/ci/tools/tests/test_rebase_sphinx_inventory.py @@ -2,14 +2,15 @@ # SPDX-License-Identifier: Apache-2.0 import zlib +from pathlib import Path import pytest -from cuda_python.docs.copy_rebased_sphinx_inventory import copy_rebased_inventory +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): +def test_copy_rebases_relative_inventory_targets(tmp_path, monkeypatch): header = ( b"# Sphinx inventory version 2\n" b"# Project: cuda.bindings\n" @@ -27,7 +28,8 @@ def test_copy_rebases_relative_inventory_targets(tmp_path): source.write_bytes(original) destination = tmp_path / "objects.inv" - copy_rebased_inventory(source, destination) + monkeypatch.chdir(source.parent) + copy_rebased_inventory_one_level_up(Path("objects.inv")) rebased = destination.read_bytes() assert rebased.startswith(header) diff --git a/cuda_bindings_12/docs/build_docs.sh b/cuda_bindings_12/docs/build_docs.sh index 3977b84fb531..6a94022b9c16 100755 --- a/cuda_bindings_12/docs/build_docs.sh +++ b/cuda_bindings_12/docs/build_docs.sh @@ -46,4 +46,4 @@ else mv build/html/${SPHINX_CUDA_BINDINGS_VER} build/html/latest fi -python ../../cuda_python/docs/copy_rebased_sphinx_inventory.py build/html/latest/objects.inv build/html/objects.inv +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 66e3be1fac1a..010725e908ad 100755 --- a/cuda_bindings_13/docs/build_docs.sh +++ b/cuda_bindings_13/docs/build_docs.sh @@ -55,4 +55,4 @@ else mv build/html/${SPHINX_CUDA_BINDINGS_VER} build/html/latest fi -python ../../cuda_python/docs/copy_rebased_sphinx_inventory.py build/html/latest/objects.inv build/html/objects.inv +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 0e271095d168..caa9e3357ce7 100755 --- a/cuda_core/docs/build_docs.sh +++ b/cuda_core/docs/build_docs.sh @@ -51,7 +51,7 @@ else mv build/html/${SPHINX_CUDA_CORE_VER} build/html/latest fi -python ../../cuda_python/docs/copy_rebased_sphinx_inventory.py build/html/latest/objects.inv build/html/objects.inv +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 ad8cdb37869d..b260a4d12128 100755 --- a/cuda_pathfinder/docs/build_docs.sh +++ b/cuda_pathfinder/docs/build_docs.sh @@ -55,4 +55,4 @@ else mv build/html/${SPHINX_CUDA_PATHFINDER_VER} build/html/latest fi -python ../../cuda_python/docs/copy_rebased_sphinx_inventory.py build/html/latest/objects.inv build/html/objects.inv +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 aa76047d6662..b06b85f6c141 100755 --- a/cuda_python/docs/build_docs.sh +++ b/cuda_python/docs/build_docs.sh @@ -51,7 +51,7 @@ else mv build/html/${SPHINX_CUDA_PYTHON_VER} build/html/latest fi -python ./copy_rebased_sphinx_inventory.py build/html/latest/objects.inv build/html/objects.inv +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.py b/cuda_python/docs/copy_rebased_sphinx_inventory_one_level_up.py similarity index 69% rename from cuda_python/docs/copy_rebased_sphinx_inventory.py rename to cuda_python/docs/copy_rebased_sphinx_inventory_one_level_up.py index 779e538ca0a3..3b428fee0293 100644 --- a/cuda_python/docs/copy_rebased_sphinx_inventory.py +++ b/cuda_python/docs/copy_rebased_sphinx_inventory_one_level_up.py @@ -1,12 +1,12 @@ # SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. # SPDX-License-Identifier: Apache-2.0 -"""Copy a Sphinx inventory and rebase its relative URLs for the destination.""" +"""Copy a Sphinx inventory one level up and rebase its relative URLs.""" import argparse import re import zlib -from os.path import relpath +from os.path import abspath from pathlib import Path from urllib.parse import urlsplit @@ -15,18 +15,22 @@ _ENTRY = re.compile(r"^(.+?[ \t]+\S+[ \t]+-?\d+[ \t]+?)(\S*)([ \t]+.*)$", re.MULTILINE) -def copy_rebased_inventory(source: Path, destination: Path) -> None: +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") - relative_dir = Path(relpath(source.parent, destination.parent)).as_posix() + 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}{relative_dir}/{location}{suffix}" + return f"{prefix}{url_prefix}{location}{suffix}" header = b"\n".join(parts[:4]) + b"\n" entries = zlib.decompress(parts[4]).decode() @@ -37,6 +41,4 @@ def rebase_entry(match: re.Match[str]) -> str: if __name__ == "__main__": parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("source", type=Path) - parser.add_argument("destination", type=Path) - args = parser.parse_args() - copy_rebased_inventory(args.source, args.destination) + copy_rebased_inventory_one_level_up(parser.parse_args().source)