Skip to contentSkip to conversation

SPDX: Add annotations to relationship

PATCH Superseded Saul Wold 2 participants 6 messages Oct 26, 2021 – Oct 27, 2021

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Saul Wold

author
#0 lore raw

cover SPDX: Add annotations to relationship

Add annotations to relationships and refactor code to add create_annotation() function for code reuse.

Ensure that "cross" recipes are factored into isNative also.

Sau!

Saul Wold (3):
spdx.py: Add annotation to relationship
create-spdx: add create_annotation function
create-spdx: cross recipes are native also

 classes/create-spdx.bbclass | 23 +++++++++++++++--------
 lib/oe/spdx.py              |  6 +++++-
 2 files changed, 20 insertions(+), 9 deletions(-)
Signature · 1 line
2.31.1
author
#1 lore raw

PATCH 1/3 spdx.py: Add annotation to relationship

Having annotations on relationship can provide additional information about the relationship such as how it was derived.

---
 lib/oe/spdx.py | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
lib/oe/spdx.py +5 -1
index 4416194..9e7ced5 100644
@@ -196,6 +196,7 @@ class SPDXRelationship(SPDXObject):
     relatedSpdxElement = _String()
     relationshipType = _String()
     comment = _String()
+    annotations = _ObjectList(SPDXAnnotation)
 
 
 class SPDXExternalReference(SPDXObject):
@@ -300,7 +301,7 @@ class SPDXDocument(SPDXObject):
     def from_json(cls, f):
         return cls(**json.load(f))
 
-    def add_relationship(self, _from, relationship, _to, *, comment=None):
+    def add_relationship(self, _from, relationship, _to, *, comment=None, annotation=None):
         if isinstance(_from, SPDXObject):
             from_spdxid = _from.SPDXID
         else:
@@ -320,6 +321,9 @@ class SPDXDocument(SPDXObject):
         if comment is not None:
             r.comment = comment
 
+        if annotation is not None:
+            r.annotations.append(annotation)
+
         self.relationships.append(r)
 
     def find_by_spdxid(self, spdxid):
Signature · 1 line
2.31.1
author
#2 lore raw

PATCH 2/3 create-spdx: add create_annotation function

In reply to Saul Wold

This allows code reuse and future usage with relationship annotations

---
 classes/create-spdx.bbclass | 18 ++++++++++++------
 1 file changed, 12 insertions(+), 6 deletions(-)
classes/create-spdx.bbclass +12 -6
index dd341db..72a9b55 100644
@@ -35,6 +35,17 @@ def get_doc_namespace(d, doc):
     namespace_uuid = uuid.uuid5(uuid.NAMESPACE_DNS, d.getVar("SPDX_UUID_NAMESPACE"))
     return "%s/%s-%s" % (d.getVar("SPDX_NAMESPACE_PREFIX"), doc.name, str(uuid.uuid5(namespace_uuid, doc.name)))
 
+def create_annotation(d, comment):
+    from datetime import datetime, timezone
+
+    creation_time = datetime.now(tz=timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ")
+    annotation = oe.spdx.SPDXAnnotation()
+    annotation.annotationDate = creation_time
+    annotation.annotationType = "OTHER"
+    annotation.annotator = "Tool: %s - %s" % (d.getVar("SPDX_TOOL_NAME"), d.getVar("SPDX_TOOL_VERSION"))
+    annotation.comment = comment
+    return annotation
+
 def recipe_spdx_is_native(d, recipe):
     return any(a.annotationType == "OTHER" and
       a.annotator == "Tool: %s - %s" % (d.getVar("SPDX_TOOL_NAME"), d.getVar("SPDX_TOOL_VERSION")) and
@@ -408,12 +419,7 @@ python do_create_spdx() {
     recipe.versionInfo = d.getVar("PV")
     recipe.SPDXID = oe.sbom.get_recipe_spdxid(d)
     if bb.data.inherits_class("native", d):
-        annotation = oe.spdx.SPDXAnnotation()
-        annotation.annotationDate = creation_time
-        annotation.annotationType = "OTHER"
-        annotation.annotator = "Tool: %s - %s" % (d.getVar("SPDX_TOOL_NAME"), d.getVar("SPDX_TOOL_VERSION"))
-        annotation.comment = "isNative"
-        recipe.annotations.append(annotation)
+        recipe.annotations.append(create_annotation(d, "isNative"))
 
     for s in d.getVar('SRC_URI').split():
         if not s.startswith("file://"):
Signature · 1 line
2.31.1
author
#3 lore raw

PATCH 3/3 create-spdx: cross recipes are native also

In reply to Saul Wold

Recipes that inherit cross should also be categorized as isNative

---
 classes/create-spdx.bbclass | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
classes/create-spdx.bbclass +3 -2
index 72a9b55..883bc23 100644
@@ -418,8 +418,9 @@ python do_create_spdx() {
     recipe.name = d.getVar("PN")
     recipe.versionInfo = d.getVar("PV")
     recipe.SPDXID = oe.sbom.get_recipe_spdxid(d)
-    if bb.data.inherits_class("native", d):
+    if bb.data.inherits_class("native", d) or bb.data.inherits_class("cross", d):
         recipe.annotations.append(create_annotation(d, "isNative"))
+    recipe.annotations.append(create_annotation(d, "SPDXDIR:%s" % str(spdx_workdir).replace(str(top_dir) +'/', '')))
 
     for s in d.getVar('SRC_URI').split():
         if not s.startswith("file://"):
@@ -610,7 +611,7 @@ python do_create_runtime_spdx() {
 
     deploy_dir_spdx = Path(d.getVar("DEPLOY_DIR_SPDX"))
     spdx_deploy = Path(d.getVar("SPDXRUNTIMEDEPLOY"))
-    is_native = bb.data.inherits_class("native", d)
+    is_native = bb.data.inherits_class("native", d) or bb.data.inherits_class("cross", d)
 
     creation_time = datetime.now(tz=timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ")
 
Signature · 1 line
2.31.1
#4 lore raw

Re: [PATCH 3/3] create-spdx: cross recipes are native also

Quoted 13 lines · Saul Wold

Recipes that inherit cross should also be categorized as isNative

---
 classes/create-spdx.bbclass | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
classes/create-spdx.bbclass +0 -0
index 72a9b55..883bc23 100644

classes/create-spdx.bbclass python do_create_spdx() { · line 423

Quoted 7 lines
classes/create-spdx.bbclass +2 -1
@@ -418,8 +418,9 @@ python do_create_spdx() {
     recipe.name = d.getVar("PN")
     recipe.versionInfo = d.getVar("PV")
     recipe.SPDXID = oe.sbom.get_recipe_spdxid(d)
-    if bb.data.inherits_class("native", d):
+    if bb.data.inherits_class("native", d) or bb.data.inherits_class("cross", d):
         recipe.annotations.append(create_annotation(d, "isNative"))
+    recipe.annotations.append(create_annotation(d, "SPDXDIR:%s" % str(spdx_workdir).replace(str(top_dir) +'/', '')))

This seems to be a leftover from something else and is unused?

Quoted 15 lines
classes/create-spdx.bbclass +1 -2
     for s in d.getVar('SRC_URI').split():
         if not s.startswith("file://"):
@@ -610,7 +611,7 @@ python do_create_runtime_spdx() {
 
     deploy_dir_spdx = Path(d.getVar("DEPLOY_DIR_SPDX"))
     spdx_deploy = Path(d.getVar("SPDXRUNTIMEDEPLOY"))
-    is_native = bb.data.inherits_class("native", d)
+    is_native = bb.data.inherits_class("native", d) or bb.data.inherits_class("cross", d)
 
     creation_time = datetime.now(tz=timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ")
 
--

2.31.1

#5 lore raw
In reply to Saul Wold
Quoted 5 lines · Saul Wold

Add annotations to relationships and refactor code to add create_annotation() function for code reuse.

Ensure that "cross" recipes are factored into isNative also.

Other than the comment in the later patch, LGTM

Quoted 15 lines

Sau!

Saul Wold (3):
spdx.py: Add annotation to relationship
create-spdx: add create_annotation function
create-spdx: cross recipes are native also

 classes/create-spdx.bbclass | 23 +++++++++++++++--------
 lib/oe/spdx.py              |  6 +++++-
 2 files changed, 20 insertions(+), 9 deletions(-)

--
2.31.1