Source code for mafw.devtools.toolchain.versions
# Copyright 2026 European Union
# Author: Bulgheroni Antonio (antonio.bulgheroni@ec.europa.eu)
# SPDX-License-Identifier: EUPL-1.2
"""Collect current tool versions for toolchain reports.
This module contains the implementation logic for reporting current tool
versions. It deliberately does not query package indexes or detect latest
versions, so it can be used in CI jobs that only need to record their
environment.
"""
from __future__ import annotations
from collections.abc import Sequence
from dataclasses import dataclass
from packaging.version import Version
from mafw.devtools.toolchain.base import Category, ToolChainTool
[docs]
@dataclass(slots=True)
class ToolVersionResult:
"""Result of detecting the current version of one tool.
:param tool_name: Name of the tool.
:param category: Whether the tool is managed by the host or project.
:param version: Detected version, or ``None`` when unavailable.
:param error: Error message when version detection failed.
"""
tool_name: str
category: Category
version: Version | None
error: str | None = None
[docs]
def collect_tool_versions(tools: Sequence[ToolChainTool]) -> list[ToolVersionResult]:
"""Detect current versions for the supplied tools without online checks.
Each tool is processed independently so a detection failure does not
prevent the remaining versions from being collected.
:param tools: Tools to inspect, in the order they should be reported.
:return: One result for each supplied tool, preserving input order.
"""
results: list[ToolVersionResult] = []
for tool in tools:
try:
version = tool.detect_current_version()
except Exception as exc: # noqa: BLE001
results.append(
ToolVersionResult(
tool_name=tool.name,
category=tool.category,
version=None,
error=str(exc),
)
)
else:
results.append(
ToolVersionResult(
tool_name=tool.name,
category=tool.category,
version=version,
)
)
return results