summaryrefslogtreecommitdiffstats
path: root/meta
diff options
context:
space:
mode:
authorSaul Wold <Saul.Wold@windriver.com>2022-02-08 07:02:11 -0800
committerRichard Purdie <richard.purdie@linuxfoundation.org>2022-02-12 17:05:35 +0000
commit75f9785a2b28eddd1f6ca0c8a6c6507dd97df409 (patch)
treee420848e19d47b7c7d84ee05b6178ea50c8d4b71 /meta
parent76b3d141c027abced340c573f6c0f3836c55639c (diff)
downloadpoky-75f9785a2b28eddd1f6ca0c8a6c6507dd97df409.tar.gz
create-spdx: Get SPDX-License-Identifier from source
This patch will read the begining of source files and try to find the SPDX-License-Identifier to populate the licenseInfoInFiles field for each source file. This does not populate licenseConcluded at this time, nor rolls it up to package level. We read as binary file since some source code seem to have some binary characters, the license is then converted to ascii strings. (From OE-Core rev: 51e5f328635eb022143178c3169bae719509697a) Signed-off-by: Saul Wold <saul.wold@windriver.com> Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
Diffstat (limited to 'meta')
-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)