diff options
| -rw-r--r-- | meta/lib/oe/spdx.py | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/meta/lib/oe/spdx.py b/meta/lib/oe/spdx.py index a99e54ff40..4416194e06 100644 --- a/meta/lib/oe/spdx.py +++ b/meta/lib/oe/spdx.py | |||
| @@ -2,6 +2,18 @@ | |||
| 2 | # SPDX-License-Identifier: GPL-2.0-only | 2 | # SPDX-License-Identifier: GPL-2.0-only |
| 3 | # | 3 | # |
| 4 | 4 | ||
| 5 | # | ||
| 6 | # This library is intended to capture the JSON SPDX specification in a type | ||
| 7 | # safe manner. It is not intended to encode any particular OE specific | ||
| 8 | # behaviors, see the sbom.py for that. | ||
| 9 | # | ||
| 10 | # The documented SPDX spec document doesn't cover the JSON syntax for | ||
| 11 | # particular configuration, which can make it hard to determine what the JSON | ||
| 12 | # syntax should be. I've found it is actually much simpler to read the official | ||
| 13 | # SPDX JSON schema which can be found here: https://github.com/spdx/spdx-spec | ||
| 14 | # in schemas/spdx-schema.json | ||
| 15 | # | ||
| 16 | |||
| 5 | import hashlib | 17 | import hashlib |
| 6 | import itertools | 18 | import itertools |
| 7 | import json | 19 | import json |
| @@ -9,7 +21,16 @@ import json | |||
| 9 | SPDX_VERSION = "2.2" | 21 | SPDX_VERSION = "2.2" |
| 10 | 22 | ||
| 11 | 23 | ||
| 24 | # | ||
| 25 | # The following are the support classes that are used to implement SPDX object | ||
| 26 | # | ||
| 27 | |||
| 12 | class _Property(object): | 28 | class _Property(object): |
| 29 | """ | ||
| 30 | A generic SPDX object property. The different types will derive from this | ||
| 31 | class | ||
| 32 | """ | ||
| 33 | |||
| 13 | def __init__(self, *, default=None): | 34 | def __init__(self, *, default=None): |
| 14 | self.default = default | 35 | self.default = default |
| 15 | 36 | ||
| @@ -19,6 +40,10 @@ class _Property(object): | |||
| 19 | 40 | ||
| 20 | 41 | ||
| 21 | class _String(_Property): | 42 | class _String(_Property): |
| 43 | """ | ||
| 44 | A scalar string property for an SPDX object | ||
| 45 | """ | ||
| 46 | |||
| 22 | def __init__(self, **kwargs): | 47 | def __init__(self, **kwargs): |
| 23 | super().__init__(**kwargs) | 48 | super().__init__(**kwargs) |
| 24 | 49 | ||
| @@ -39,6 +64,10 @@ class _String(_Property): | |||
| 39 | 64 | ||
| 40 | 65 | ||
| 41 | class _Object(_Property): | 66 | class _Object(_Property): |
| 67 | """ | ||
| 68 | A scalar SPDX object property of a SPDX object | ||
| 69 | """ | ||
| 70 | |||
| 42 | def __init__(self, cls, **kwargs): | 71 | def __init__(self, cls, **kwargs): |
| 43 | super().__init__(**kwargs) | 72 | super().__init__(**kwargs) |
| 44 | self.cls = cls | 73 | self.cls = cls |
| @@ -62,6 +91,10 @@ class _Object(_Property): | |||
| 62 | 91 | ||
| 63 | 92 | ||
| 64 | class _ListProperty(_Property): | 93 | class _ListProperty(_Property): |
| 94 | """ | ||
| 95 | A list of SPDX properties | ||
| 96 | """ | ||
| 97 | |||
| 65 | def __init__(self, prop, **kwargs): | 98 | def __init__(self, prop, **kwargs): |
| 66 | super().__init__(**kwargs) | 99 | super().__init__(**kwargs) |
| 67 | self.prop = prop | 100 | self.prop = prop |
| @@ -82,16 +115,28 @@ class _ListProperty(_Property): | |||
| 82 | 115 | ||
| 83 | 116 | ||
| 84 | class _StringList(_ListProperty): | 117 | class _StringList(_ListProperty): |
| 118 | """ | ||
| 119 | A list of strings as a property for an SPDX object | ||
| 120 | """ | ||
| 121 | |||
| 85 | def __init__(self, **kwargs): | 122 | def __init__(self, **kwargs): |
| 86 | super().__init__(_String(), **kwargs) | 123 | super().__init__(_String(), **kwargs) |
| 87 | 124 | ||
| 88 | 125 | ||
| 89 | class _ObjectList(_ListProperty): | 126 | class _ObjectList(_ListProperty): |
| 127 | """ | ||
| 128 | A list of SPDX objects as a property for an SPDX object | ||
| 129 | """ | ||
| 130 | |||
| 90 | def __init__(self, cls, **kwargs): | 131 | def __init__(self, cls, **kwargs): |
| 91 | super().__init__(_Object(cls), **kwargs) | 132 | super().__init__(_Object(cls), **kwargs) |
| 92 | 133 | ||
| 93 | 134 | ||
| 94 | class MetaSPDXObject(type): | 135 | class MetaSPDXObject(type): |
| 136 | """ | ||
| 137 | A metaclass that allows properties (anything derived from a _Property | ||
| 138 | class) to be defined for a SPDX object | ||
| 139 | """ | ||
| 95 | def __new__(mcls, name, bases, attrs): | 140 | def __new__(mcls, name, bases, attrs): |
| 96 | attrs["_properties"] = {} | 141 | attrs["_properties"] = {} |
| 97 | 142 | ||
| @@ -105,6 +150,9 @@ class MetaSPDXObject(type): | |||
| 105 | 150 | ||
| 106 | 151 | ||
| 107 | class SPDXObject(metaclass=MetaSPDXObject): | 152 | class SPDXObject(metaclass=MetaSPDXObject): |
| 153 | """ | ||
| 154 | The base SPDX object; all SPDX spec classes must derive from this class | ||
| 155 | """ | ||
| 108 | def __init__(self, **d): | 156 | def __init__(self, **d): |
| 109 | self._spdx = {} | 157 | self._spdx = {} |
| 110 | 158 | ||
| @@ -122,6 +170,15 @@ class SPDXObject(metaclass=MetaSPDXObject): | |||
| 122 | return | 170 | return |
| 123 | raise KeyError("%r is not a valid SPDX property" % name) | 171 | raise KeyError("%r is not a valid SPDX property" % name) |
| 124 | 172 | ||
| 173 | # | ||
| 174 | # These are the SPDX objects implemented from the spec. The *only* properties | ||
| 175 | # that can be added to these objects are ones directly specified in the SPDX | ||
| 176 | # spec, however you may add helper functions to make operations easier. | ||
| 177 | # | ||
| 178 | # Defaults should *only* be specified if the SPDX spec says there is a certain | ||
| 179 | # required value for a field (e.g. dataLicense), or if the field is mandatory | ||
| 180 | # and has some sane "this field is unknown" (e.g. "NOASSERTION") | ||
| 181 | # | ||
| 125 | 182 | ||
| 126 | class SPDXAnnotation(SPDXObject): | 183 | class SPDXAnnotation(SPDXObject): |
| 127 | annotationDate = _String() | 184 | annotationDate = _String() |
