From 54035f65cb1f75ec699404796ffaf376c39349e6 Mon Sep 17 00:00:00 2001 From: Richard Hughes Date: Mon, 18 Sep 2023 21:12:12 +0100 Subject: [PATCH] Allow specifying the SWID link hrefs by name as well as UUID --- README.md | 7 +++++++ uswid/link.py | 17 ++++++++++++++++- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index a9b330d..c99b37c 100644 --- a/README.md +++ b/README.md @@ -160,6 +160,13 @@ NOTE: The GUID can be constructed from the tool or library name combined with the version, e.g using `appstream-util generate-guid gcc-12.1.1` or the [online tool hosted by the LVFS](https://fwupd.org/lvfs/guid). +...or, if a component has been previously defined, you can use the name to +contruct the SWID automatically: + + [uSWID-Link:image_loading_lib] + rel = requires + href = swid:libjpeg + RAW Blobs --------- diff --git a/uswid/link.py b/uswid/link.py index cbcf3b9..4ba1ad1 100644 --- a/uswid/link.py +++ b/uswid/link.py @@ -8,6 +8,7 @@ # pylint: disable=too-few-public-methods from enum import IntEnum +import uuid from typing import Optional @@ -36,7 +37,7 @@ def __init__( href: Optional[str] = None, rel: Optional[str] = None, ): - self.href: Optional[str] = href + self._href: Optional[str] = href self._rel: Optional[str] = rel @property @@ -52,6 +53,20 @@ def rel(self) -> Optional[str]: def rel(self, rel: Optional[str]) -> None: self._rel = rel + @property + def href(self) -> Optional[str]: + return self._href + + @href.setter + def href(self, href: Optional[str]) -> None: + if href.startswith("swid:"): + maybe_uuid: str = href.split(":")[1] + try: + _ = uuid.UUID(maybe_uuid) + except ValueError: + href = f"swid:{str(uuid.uuid5(uuid.NAMESPACE_DNS, maybe_uuid))}" + self._href = href + @property def href_for_display(self) -> Optional[str]: if not self.href: