summaryrefslogtreecommitdiffstats
path: root/meta/classes/create-spdx.bbclass
diff options
context:
space:
mode:
Diffstat (limited to 'meta/classes/create-spdx.bbclass')
-rw-r--r--meta/classes/create-spdx.bbclass22
1 files changed, 22 insertions, 0 deletions
diff --git a/meta/classes/create-spdx.bbclass b/meta/classes/create-spdx.bbclass
index 8b4203fdb5..64aada8593 100644
--- a/meta/classes/create-spdx.bbclass
+++ b/meta/classes/create-spdx.bbclass
@@ -37,6 +37,23 @@ SPDX_SUPPLIER[doc] = "The SPDX PackageSupplier field for SPDX packages created f
37 37
38do_image_complete[depends] = "virtual/kernel:do_create_spdx" 38do_image_complete[depends] = "virtual/kernel:do_create_spdx"
39 39
40def extract_licenses(filename):
41 import re
42
43 lic_regex = re.compile(b'^\W*SPDX-License-Identifier:\s*([ \w\d.()+-]+?)(?:\s+\W*)?$', re.MULTILINE)
44
45 try:
46 with open(filename, 'rb') as f:
47 size = min(15000, os.stat(filename).st_size)
48 txt = f.read(size)
49 licenses = re.findall(lic_regex, txt)
50 if licenses:
51 ascii_licenses = [lic.decode('ascii') for lic in licenses]
52 return ascii_licenses
53 except Exception as e:
54 bb.warn(f"Exception reading {filename}: {e}")
55 return None
56
40def get_doc_namespace(d, doc): 57def get_doc_namespace(d, doc):
41 import uuid 58 import uuid
42 namespace_uuid = uuid.uuid5(uuid.NAMESPACE_DNS, d.getVar("SPDX_UUID_NAMESPACE")) 59 namespace_uuid = uuid.uuid5(uuid.NAMESPACE_DNS, d.getVar("SPDX_UUID_NAMESPACE"))
@@ -232,6 +249,11 @@ def add_package_files(d, doc, spdx_pkg, topdir, get_spdxid, get_types, *, archiv
232 checksumValue=bb.utils.sha256_file(filepath), 249 checksumValue=bb.utils.sha256_file(filepath),
233 )) 250 ))
234 251
252 if "SOURCE" in spdx_file.fileTypes:
253 extracted_lics = extract_licenses(filepath)
254 if extracted_lics:
255 spdx_file.licenseInfoInFiles = extracted_lics
256
235 doc.files.append(spdx_file) 257 doc.files.append(spdx_file)
236 doc.add_relationship(spdx_pkg, "CONTAINS", spdx_file) 258 doc.add_relationship(spdx_pkg, "CONTAINS", spdx_file)
237 spdx_pkg.hasFiles.append(spdx_file.SPDXID) 259 spdx_pkg.hasFiles.append(spdx_file.SPDXID)