Skip to contentSkip to conversation

create-spdx: Add check for variable contents along with quotes

PATCH In review Saul Wold 2 participants 2 messages Feb 24, 2023

lore
Share & tools
lore

Saul Wold

author
#0 lore raw

PATCH v2 1/1 create-spdx: Add check for variable contents along with quotes

This adds a check to ensure we that if a variable is empty it gets an empty string, this also adds quotes to indicate the variable contents.

"comment": "CUSTOM_SPECIAL="
or
"comment": "CUSTOM_SPECIAL=variable contents"

---
 meta/classes/create-spdx-2.2.bbclass          |  5 ++-
 .../selftest/cases/spdx_custom_annotations.py | 42 +++++++++++++++++++
 2 files changed, 46 insertions(+), 1 deletion(-)
 create mode 100644 meta/lib/oeqa/selftest/cases/spdx_custom_annotations.py
meta/classes/create-spdx-2.2.bbclass +4 -1
index 454dd7a7a07..da90bf8033a 100644
@@ -524,7 +524,10 @@ python do_create_spdx() {
 
     if d.getVar("SPDX_CUSTOM_ANNOTATION_VARS"):
         for var in d.getVar('SPDX_CUSTOM_ANNOTATION_VARS').split():
-            recipe.annotations.append(create_annotation(d, var + "=" + d.getVar(var)))
+            if d.getVar(var):
+                recipe.annotations.append(create_annotation(d, var + "=" + d.getVar(var).replace('"', '\\"')))
+            else:
+                recipe.annotations.append(create_annotation(d, var + "="))
 
     # Some CVEs may be patched during the build process without incrementing the version number,
     # so querying for CVEs based on the CPE id can lead to false positives. To account for this,
meta/lib/oeqa/selftest/cases/spdx_custom_annotations.py new file +42 -0

42 lines not on this page · raw message

Signature · 1 line
2.25.1
#1 lore raw
Quoted 21 lines · Saul Wold

This adds a check to ensure we that if a variable is empty it gets an empty string, this also adds quotes to indicate the variable contents.

"comment": "CUSTOM_SPECIAL="
or
"comment": "CUSTOM_SPECIAL=variable contents"

---
 meta/classes/create-spdx-2.2.bbclass          |  5 ++-
 .../selftest/cases/spdx_custom_annotations.py | 42 +++++++++++++++++++
 2 files changed, 46 insertions(+), 1 deletion(-)
 create mode 100644 meta/lib/oeqa/selftest/cases/spdx_custom_annotations.py
meta/classes/create-spdx-2.2.bbclass +0 -0
index 454dd7a7a07..da90bf8033a 100644

meta/classes/create-spdx-2.2.bbclass python do_create_spdx() { · line 528

Quoted 6 lines
meta/classes/create-spdx-2.2.bbclass +2 -1
@@ -524,7 +524,10 @@ python do_create_spdx() {
 
     if d.getVar("SPDX_CUSTOM_ANNOTATION_VARS"):
         for var in d.getVar('SPDX_CUSTOM_ANNOTATION_VARS').split():
-            recipe.annotations.append(create_annotation(d, var + "=" + d.getVar(var)))
+            if d.getVar(var):
+                recipe.annotations.append(create_annotation(d, var + "=" + d.getVar(var).replace('"', '\\"')))

Manually trying to escape quotes can be really hard; Is there a particular reason it's necessary? If you're trying to read this back programmatically, I'd expect:

name, value = annotation.split(=, 1)

to work

Quoted 56 lines
meta/classes/create-spdx-2.2.bbclass +2 -0
+            else:
+                recipe.annotations.append(create_annotation(d, var + "="))
 
     # Some CVEs may be patched during the build process without incrementing the version number,
     # so querying for CVEs based on the CPE id can lead to false positives. To account for this,
meta/lib/oeqa/selftest/cases/spdx_custom_annotations.py new file +42 -1
new file mode 100644
index 00000000000..a2bef998988
@@ -0,0 +1,42 @@
+#
+# Copyright OpenEmbedded Contributors
+#
+# SPDX-License-Identifier: MIT
+#
+from oeqa.selftest.case import OESelftestTestCase
+from oeqa.utils.commands import bitbake
+import json
+
+class SPDXCustomAnnotations(OESelftestTestCase):
+
+    # Build test recipes with custom-annotation
+    def test_spdx_custom_annotations(self):
+        self.write_config("INHERIT:append = ' create_spdx'")
+
+        result = bitbake('%s custom-annotation')
+        print(f"Bitbake Result: {result}")
+        try:
+            mdir = self.get_dir_from_bb_var('DEPLOY_DIR_SPDX', self.buildtarget)
+            with open(f"{mdir}/recpes/recipe-{pn}.spdx.json") as json_file:
+                spdx_json = json.load(json_file)
+                print(f'{spdx_json["packages"]["annotations"][0]["comment"]}')
+                self.assertEqual(spdx_json["packages"]["annotations"][0]["comment"], "ANNOTE1=This is the first custom annotation")
+                self.assertEqual(spdx_json["packages"]["annotations"][1]["comment"], "ANNOTE2=This is another custom annotation")
+        except:
+            print("json load failed")
+
+#class SPDXCustomeAnnotationTests(OESelftestTestCase):
+#    def default_config(self):
+#        return """
+#INHERIT:append = " create-spdx"
+#SPDX_CUSTOM_ANNOTATION_VARS:pn-core-image-minimal = "TEST_VAR"
+#TEST_VAR:pn-core-image-minimal = "This is a test annotation"
+#"""
+#
+#    def test_image_annotation(self):
+#        self.write_config(self.default_config())
+#
+#        result = bitbake('core-image-minimal', ignore_status=True)
+
+
+
--

2.25.1