Extend objects used to build the spdx scheme: - add support for inheritance - hide all attributes starting by _spdx - add methods to list properties and item pairs - improve the serializer to match the spdx3 scheme
Signed-off-by: Louis Rannou <louis.ran...@syslinbit.com> --- meta/lib/oe/sbom.py | 2 +- meta/lib/oe/spdx.py | 30 +++++++++++++++++++++++------- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/meta/lib/oe/sbom.py b/meta/lib/oe/sbom.py index fd4b6895d8..824839378a 100644 --- a/meta/lib/oe/sbom.py +++ b/meta/lib/oe/sbom.py @@ -77,7 +77,7 @@ def write_doc(d, spdx_doc, arch, subdir, spdx_deploy=None, indent=None): dest = doc_path(spdx_deploy, spdx_doc.name, arch, subdir) dest.parent.mkdir(exist_ok=True, parents=True) with dest.open("wb") as f: - doc_sha1 = spdx_doc.to_json(f, sort_keys=True, indent=indent) + doc_sha1 = spdx_doc.to_json(f, sort_keys=False, indent=indent) l = _doc_path_by_namespace(spdx_deploy, arch, spdx_doc.documentNamespace) l.parent.mkdir(exist_ok=True, parents=True) diff --git a/meta/lib/oe/spdx.py b/meta/lib/oe/spdx.py index 7aaf2af5ed..97b9e011ad 100644 --- a/meta/lib/oe/spdx.py +++ b/meta/lib/oe/spdx.py @@ -145,9 +145,13 @@ class MetaSPDXObject(type): def __new__(mcls, name, bases, attrs): attrs["_properties"] = {} - for key in attrs.keys(): - if isinstance(attrs[key], _Property): - prop = attrs[key] + at = {} + for basecls in bases: + at.update(basecls._properties) + at.update(attrs) + for key in at.keys(): + if isinstance(at[key], _Property): + prop = at[key] attrs["_properties"][key] = prop prop.set_property(attrs, key) @@ -166,15 +170,27 @@ class SPDXObject(metaclass=MetaSPDXObject): if name in d: self._spdx[name] = prop.init(d[name]) - def serializer(self): - return self._spdx - def __setattr__(self, name, value): - if name in self._properties or name == "_spdx": + # All attributes must be in _properties or are hidden variables which + # must be prefixed with _spdx + if name in self._properties or name[:len("_spdx")] == "_spdx": super().__setattr__(name, value) return raise KeyError("%r is not a valid SPDX property" % name) + def properties(self): + return self._properties.keys() + + def items(self): + return self._properties.items() + + def serializer(self, rootElement): + main = {"type": self.__class__.__name__[len("SPDX3"):]} + for (key, value) in self._spdx.items(): + if key[0] == '_': + key = key[1:] + main.update({key: value}) + return main # # These are the SPDX objects implemented from the spec. The *only* properties # that can be added to these objects are ones directly specified in the SPDX -- 2.42.0
-=-=-=-=-=-=-=-=-=-=-=- Links: You receive all messages sent to this group. View/Reply Online (#189878): https://lists.openembedded.org/g/openembedded-core/message/189878 Mute This Topic: https://lists.openembedded.org/mt/102308604/21656 Group Owner: openembedded-core+ow...@lists.openembedded.org Unsubscribe: https://lists.openembedded.org/g/openembedded-core/unsub [arch...@mail-archive.com] -=-=-=-=-=-=-=-=-=-=-=-