From: Louis Rannou <louis.ran...@syslinbit.com>

Create a function that search into a json-ld instead of completely loading it.

Signed-off-by: Louis Rannou <louis.ran...@syslinbit.com>
---
 meta/lib/oe/sbom.py | 32 ++++++++++++++++++++++++++++++++
 1 file changed, 32 insertions(+)

diff --git a/meta/lib/oe/sbom.py b/meta/lib/oe/sbom.py
index 28db9cf719..21333c0a84 100644
--- a/meta/lib/oe/sbom.py
+++ b/meta/lib/oe/sbom.py
@@ -119,3 +119,35 @@ def read_doc(fn):
         doc = oe.spdx.SPDXDocument.from_json(f)
 
     return (doc, sha1.hexdigest())
+
+
+def search_doc(fn, attr_types=None):
+    """
+    Look for all attributes in the given dictionary. Return the document
+    element, a dictionary of the required attributes and the sha1 of the file.
+    """
+    import hashlib
+    import oe.spdx3
+    import io
+    import contextlib
+
+    @contextlib.contextmanager
+    def get_file():
+        if isinstance(fn, io.IOBase):
+            yield fn
+        else:
+            with fn.open("rb") as f:
+                yield f
+
+    with get_file() as f:
+        sha1 = hashlib.sha1()
+        while True:
+            chunk = f.read(4096)
+            if not chunk:
+                break
+            sha1.update(chunk)
+
+        f.seek(0)
+        doc, attributes = oe.spdx3.SPDX3SpdxDocument.from_json(f, attr_types 
or [])
+
+    return (doc, attributes, sha1.hexdigest())
-- 
2.42.0

-=-=-=-=-=-=-=-=-=-=-=-
Links: You receive all messages sent to this group.
View/Reply Online (#189716): 
https://lists.openembedded.org/g/openembedded-core/message/189716
Mute This Topic: https://lists.openembedded.org/mt/102197345/21656
Group Owner: openembedded-core+ow...@lists.openembedded.org
Unsubscribe: https://lists.openembedded.org/g/openembedded-core/unsub 
[arch...@mail-archive.com]
-=-=-=-=-=-=-=-=-=-=-=-

Reply via email to