|
| 1 | +""" |
| 2 | +Copyright (c) 2026 Nordic Semiconductor ASA |
| 3 | +
|
| 4 | +SPDX-License-Identifier: LicenseRef-Nordic-5-Clause |
| 5 | +
|
| 6 | +Expose west manifest revisions as Sphinx substitutions for documentation links. |
| 7 | +
|
| 8 | +Reads the Matter (sdk-connectedhomeip) revision from west.yml and registers |
| 9 | +|sdk-connectedhomeip-revision| for use in links.txt and RST sources. |
| 10 | +""" |
| 11 | + |
| 12 | +from __future__ import annotations |
| 13 | + |
| 14 | +import subprocess |
| 15 | +from pathlib import Path |
| 16 | +from typing import TYPE_CHECKING, Any |
| 17 | + |
| 18 | +import yaml |
| 19 | + |
| 20 | +if TYPE_CHECKING: |
| 21 | + from sphinx.application import Sphinx |
| 22 | + |
| 23 | +__version__ = "0.1.0" |
| 24 | + |
| 25 | +MATTER_WEST_PROJECT = "matter" |
| 26 | +SDK_CONNECTEDHOMEIP_REVISION = "sdk-connectedhomeip-revision" |
| 27 | + |
| 28 | + |
| 29 | +def _load_west_manifest(west_manifest: Path) -> dict[str, Any]: |
| 30 | + if not west_manifest.is_file(): |
| 31 | + raise FileNotFoundError(f"west manifest not found: {west_manifest}") |
| 32 | + data = yaml.safe_load(west_manifest.read_text(encoding="utf-8")) |
| 33 | + if not isinstance(data, dict): |
| 34 | + raise ValueError(f"Invalid west manifest: {west_manifest}") |
| 35 | + return data |
| 36 | + |
| 37 | + |
| 38 | +def _matter_project(manifest: dict[str, Any]) -> dict[str, Any]: |
| 39 | + projects = manifest.get("manifest", {}).get("projects", []) |
| 40 | + if not isinstance(projects, list): |
| 41 | + raise ValueError("west manifest has no projects list") |
| 42 | + |
| 43 | + for project in projects: |
| 44 | + if isinstance(project, dict) and project.get("name") == MATTER_WEST_PROJECT: |
| 45 | + return project |
| 46 | + |
| 47 | + raise ValueError(f"west manifest has no project named {MATTER_WEST_PROJECT!r}") |
| 48 | + |
| 49 | + |
| 50 | +def _git_short_revision(matter_module: Path, revision: str) -> str | None: |
| 51 | + if not matter_module.is_dir(): |
| 52 | + return None |
| 53 | + |
| 54 | + for ref in (revision, "HEAD"): |
| 55 | + proc = subprocess.run( |
| 56 | + ["git", "-C", str(matter_module), "rev-parse", "--short", ref], |
| 57 | + capture_output=True, |
| 58 | + text=True, |
| 59 | + check=False, |
| 60 | + ) |
| 61 | + if proc.returncode == 0: |
| 62 | + short_sha = proc.stdout.strip() |
| 63 | + if short_sha: |
| 64 | + return short_sha |
| 65 | + return None |
| 66 | + |
| 67 | + |
| 68 | +def connectedhomeip_revision( |
| 69 | + west_manifest: Path, |
| 70 | + *, |
| 71 | + matter_module: Path | None = None, |
| 72 | +) -> str: |
| 73 | + """Return the sdk-connectedhomeip revision string for documentation links.""" |
| 74 | + project = _matter_project(_load_west_manifest(west_manifest)) |
| 75 | + revision = str(project.get("revision", "")).strip() |
| 76 | + if not revision: |
| 77 | + raise ValueError( |
| 78 | + f"Project {MATTER_WEST_PROJECT!r} in {west_manifest} has no revision" |
| 79 | + ) |
| 80 | + |
| 81 | + if matter_module is not None: |
| 82 | + short_sha = _git_short_revision(matter_module, revision) |
| 83 | + if short_sha: |
| 84 | + return short_sha |
| 85 | + |
| 86 | + return revision |
| 87 | + |
| 88 | + |
| 89 | +def load_west_substitutions( |
| 90 | + west_manifest: Path, |
| 91 | + *, |
| 92 | + matter_module: Path | None = None, |
| 93 | +) -> dict[str, str]: |
| 94 | + """Build substitution mapping from west.yml for conf.py preprocessing.""" |
| 95 | + return { |
| 96 | + SDK_CONNECTEDHOMEIP_REVISION: connectedhomeip_revision( |
| 97 | + west_manifest, |
| 98 | + matter_module=matter_module, |
| 99 | + ), |
| 100 | + } |
| 101 | + |
| 102 | + |
| 103 | +def _config_inited(app, config) -> None: |
| 104 | + west_manifest = Path(config.west_manifest_path) |
| 105 | + matter_module = Path(config.matter_module_path) if config.matter_module_path else None |
| 106 | + substitutions = load_west_substitutions(west_manifest, matter_module=matter_module) |
| 107 | + |
| 108 | + prolog_lines = [ |
| 109 | + f".. |{name}| replace:: {value}" for name, value in substitutions.items() |
| 110 | + ] |
| 111 | + existing_prolog = config.rst_prolog or "" |
| 112 | + if existing_prolog and not existing_prolog.endswith("\n"): |
| 113 | + existing_prolog = f"{existing_prolog}\n" |
| 114 | + config.rst_prolog = "\n".join(prolog_lines) + "\n" + existing_prolog |
| 115 | + |
| 116 | + |
| 117 | +def setup(app): |
| 118 | + app.add_config_value("west_manifest_path", None, "env", [str]) |
| 119 | + app.add_config_value("matter_module_path", None, "env", [str]) |
| 120 | + app.connect("config-inited", _config_inited) |
| 121 | + |
| 122 | + return { |
| 123 | + "version": __version__, |
| 124 | + "parallel_read_safe": True, |
| 125 | + "parallel_write_safe": True, |
| 126 | + } |
0 commit comments