From: Luis <luis.augenstein@tngtech.com>
To: nathan@kernel.org, nsc@kernel.org
Cc: linux-kbuild@vger.kernel.org, linux-kernel@vger.kernel.org,
akpm@linux-foundation.org, gregkh@linuxfoundation.org,
kstewart@linuxfoundation.org, maximilian.huber@tngtech.com,
Luis Augenstein <luis.augenstein@tngtech.com>
Subject: [PATCH v7 03/15] scripts/sbom: setup sbom logging
Date: Mon, 18 May 2026 08:20:50 +0200 [thread overview]
Message-ID: <20260518062102.2051814-4-luis.augenstein@tngtech.com> (raw)
In-Reply-To: <20260518062102.2051814-1-luis.augenstein@tngtech.com>
From: Luis Augenstein <luis.augenstein@tngtech.com>
Add logging infrastructure for warnings and errors.
Errors and warnings are accumulated and summarized in the end.
Assisted-by: Cursor:claude-sonnet-4-5
Assisted-by: OpenCode:GLM-4-7
Co-developed-by: Maximilian Huber <maximilian.huber@tngtech.com>
Signed-off-by: Maximilian Huber <maximilian.huber@tngtech.com>
Signed-off-by: Luis Augenstein <luis.augenstein@tngtech.com>
---
scripts/sbom/sbom.py | 26 ++++++++-
scripts/sbom/sbom/__init__.py | 0
scripts/sbom/sbom/config.py | 46 +++++++++++++++
scripts/sbom/sbom/sbom_logging.py | 94 +++++++++++++++++++++++++++++++
4 files changed, 165 insertions(+), 1 deletion(-)
create mode 100644 scripts/sbom/sbom/__init__.py
create mode 100644 scripts/sbom/sbom/config.py
create mode 100644 scripts/sbom/sbom/sbom_logging.py
diff --git a/scripts/sbom/sbom.py b/scripts/sbom/sbom.py
index 9c2e4c7f17c..3bd466720b0 100644
--- a/scripts/sbom/sbom.py
+++ b/scripts/sbom/sbom.py
@@ -6,9 +6,33 @@
Compute software bill of materials in SPDX format describing a kernel build.
"""
+import logging
+import sys
+import sbom.sbom_logging as sbom_logging
+from sbom.config import get_config
+
+
+def _exit_with_summary(write_output_on_error: bool = False) -> None:
+ warning_summary = sbom_logging.summarize_warnings()
+ error_summary = sbom_logging.summarize_errors()
+ if warning_summary:
+ logging.warning(warning_summary)
+ if error_summary:
+ logging.error(error_summary)
+ sys.exit(1)
+
def main():
- pass
+ # Read config
+ config = get_config()
+
+ # Configure logging
+ logging.basicConfig(
+ level=logging.DEBUG if config.debug else logging.INFO,
+ format="[%(levelname)s] %(message)s",
+ )
+
+ _exit_with_summary(config.write_output_on_error)
# Call main method
diff --git a/scripts/sbom/sbom/__init__.py b/scripts/sbom/sbom/__init__.py
new file mode 100644
index 00000000000..e69de29bb2d
diff --git a/scripts/sbom/sbom/config.py b/scripts/sbom/sbom/config.py
new file mode 100644
index 00000000000..c1ac9ad5737
--- /dev/null
+++ b/scripts/sbom/sbom/config.py
@@ -0,0 +1,46 @@
+# SPDX-License-Identifier: GPL-2.0-only OR MIT
+# Copyright (C) 2025 TNG Technology Consulting GmbH
+
+import argparse
+from dataclasses import dataclass
+
+
+@dataclass
+class KernelSbomConfig:
+ debug: bool
+ """Whether to enable debug logging."""
+
+
+def _parse_cli_arguments(parser: argparse.ArgumentParser) -> dict[str, bool]:
+ """
+ Parse command-line arguments using argparse.
+
+ Returns:
+ Dictionary of parsed arguments.
+ """
+ parser.add_argument(
+ "--debug",
+ action="store_true",
+ default=False,
+ help="Enable debug logs (default: False)",
+ )
+
+ args = vars(parser.parse_args())
+ return args
+
+
+def get_config() -> KernelSbomConfig:
+ """
+ Parse command-line arguments and construct the configuration object.
+
+ Returns:
+ KernelSbomConfig: Configuration object with all settings for SBOM generation.
+ """
+ parser = argparse.ArgumentParser(
+ description="Generate SPDX SBOM documents for kernel builds",
+ )
+ args = _parse_cli_arguments(parser)
+
+ debug = args["debug"]
+
+ return KernelSbomConfig(debug=debug)
diff --git a/scripts/sbom/sbom/sbom_logging.py b/scripts/sbom/sbom/sbom_logging.py
new file mode 100644
index 00000000000..fbc53cc77ef
--- /dev/null
+++ b/scripts/sbom/sbom/sbom_logging.py
@@ -0,0 +1,94 @@
+# SPDX-License-Identifier: GPL-2.0-only OR MIT
+# Copyright (C) 2025 TNG Technology Consulting GmbH
+
+import logging
+import inspect
+from typing import Literal
+
+
+MessageTemplate = str
+
+
+class MessageLogger:
+ """Logger that suppresses repeated messages and stores a summary of all logged messages."""
+
+ _messages: dict[MessageTemplate, list[str]]
+ _message_counts: dict[MessageTemplate, int]
+ _repeated_logs_limit: int
+ """Maximum number of repeated messages of the same type to log before suppressing further output."""
+
+ def __init__(self, level: Literal["error", "warning"], repeated_logs_limit: int = 3) -> None:
+ self._level = level
+ self._messages = {}
+ self._message_counts = {}
+ self._repeated_logs_limit = repeated_logs_limit
+
+ def log(self, template: MessageTemplate, /, **kwargs: str) -> None:
+ """Log a message based on a template and optional variables. Example: `log("Missing {path}", path=str(p))`."""
+ message = template
+ for key, value in kwargs.items():
+ message = message.replace("{" + key + "}", value)
+ if template not in self._messages:
+ self._messages[template] = []
+ self._message_counts[template] = 0
+ self._message_counts[template] += 1
+ if self._message_counts[template] <= self._repeated_logs_limit:
+ if self._level == "error":
+ logging.error(message)
+ elif self._level == "warning":
+ logging.warning(message)
+ self._messages[template].append(message)
+
+ def get_summary(self) -> str:
+ if len(self._messages) == 0:
+ return ""
+ summary: list[str] = [f"Summarize {self._level}s:"]
+ for template, messages in self._messages.items():
+ for message in messages:
+ summary.append(message)
+ n_suppressed_messages = self._message_counts[template] - self._repeated_logs_limit
+ if n_suppressed_messages > 0:
+ instances = "instance" if n_suppressed_messages == 1 else "instances"
+ summary.append(f"... (Found {n_suppressed_messages} more {instances} of this {self._level})")
+ return "\n".join(summary)
+
+ def has_messages(self) -> bool:
+ return len(self._message_counts) > 0
+
+
+_warning_logger: MessageLogger
+_error_logger: MessageLogger
+
+
+def warning(msg_template: MessageTemplate, /, **kwargs: str) -> None:
+ _warning_logger.log(msg_template, **kwargs)
+
+
+def error(msg_template: MessageTemplate, /, **kwargs: str) -> None:
+ frame = inspect.currentframe()
+ caller_frame = frame.f_back if frame else None
+ info = inspect.getframeinfo(caller_frame) if caller_frame else None
+ if info:
+ msg_template = f'File "{info.filename}", line {info.lineno}, in {info.function}\n{msg_template}'
+ _error_logger.log(msg_template, **kwargs)
+
+
+def summarize_warnings() -> str:
+ return _warning_logger.get_summary()
+
+
+def summarize_errors() -> str:
+ return _error_logger.get_summary()
+
+
+def has_errors() -> bool:
+ return _error_logger.has_messages()
+
+
+def init() -> None:
+ global _warning_logger, _error_logger
+ _warning_logger = MessageLogger("warning")
+ _error_logger = MessageLogger("error")
+
+
+init()
--
2.43.0
next prev parent reply other threads:[~2026-05-18 6:21 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-05-18 6:20 [PATCH v7 00/15] add SPDX SBOM generation script Luis
2026-05-18 6:20 ` [PATCH v7 01/15] scripts/sbom: add documentation Luis
2026-05-18 6:20 ` [PATCH v7 02/15] scripts/sbom: integrate script in make process Luis
2026-05-18 6:20 ` Luis [this message]
2026-05-18 6:20 ` [PATCH v7 04/15] scripts/sbom: add command parsers Luis
2026-05-18 6:20 ` [PATCH v7 05/15] scripts/sbom: add cmd graph generation Luis
2026-05-18 6:20 ` [PATCH v7 06/15] scripts/sbom: add additional dependency sources for cmd graph Luis
2026-05-18 6:20 ` [PATCH v7 07/15] scripts/sbom: add SPDX classes Luis
2026-05-18 6:20 ` [PATCH v7 08/15] scripts/sbom: add JSON-LD serialization Luis
2026-05-18 6:20 ` [PATCH v7 09/15] scripts/sbom: add shared SPDX elements Luis
2026-05-18 6:20 ` [PATCH v7 10/15] scripts/sbom: collect file metadata Luis
2026-05-18 6:20 ` [PATCH v7 11/15] scripts/sbom: add SPDX output graph Luis
2026-05-18 6:20 ` [PATCH v7 12/15] scripts/sbom: add SPDX source graph Luis
2026-05-18 6:21 ` [PATCH v7 13/15] scripts/sbom: add SPDX build graph Luis
2026-05-18 6:21 ` [PATCH v7 14/15] scripts/sbom: add unit tests for command parsers Luis
2026-05-18 6:21 ` [PATCH v7 15/15] scripts/sbom: add unit tests for SPDX-License-Identifier parsing Luis
2026-05-22 11:18 ` [PATCH v7 00/15] add SPDX SBOM generation script Greg KH
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260518062102.2051814-4-luis.augenstein@tngtech.com \
--to=luis.augenstein@tngtech.com \
--cc=akpm@linux-foundation.org \
--cc=gregkh@linuxfoundation.org \
--cc=kstewart@linuxfoundation.org \
--cc=linux-kbuild@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=maximilian.huber@tngtech.com \
--cc=nathan@kernel.org \
--cc=nsc@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®