]> code.ossystems Code Review - openembedded-core.git/commitdiff
create-spdx: transform license list into a dict for faster lookups
authorRoss Burton <ross@burtonini.com>
Fri, 3 Sep 2021 16:00:30 +0000 (17:00 +0100)
committerRichard Purdie <richard.purdie@linuxfoundation.org>
Fri, 3 Sep 2021 17:10:54 +0000 (18:10 +0100)
spdx-licenses.json contains an array of licenses objects. As we'll be
searching it often, convert that to a dictionary when we parse it.

Signed-off-by: Ross Burton <ross.burton@arm.com>
Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
meta/classes/create-spdx.bbclass

index a590ab596accaa57630d74d09d092940d0efa816..73ccb3c990f333a12ca963f1937554cb1b116525 100644 (file)
@@ -44,7 +44,10 @@ python() {
         return
 
     with open(d.getVar("SPDX_LICENSES"), "r") as f:
-        d.setVar("SPDX_LICENSE_DATA", json.load(f))
+        data = json.load(f)
+        # Transform the license array to a dictionary
+        data["licenses"] = {l["licenseId"]: l for l in data["licenses"]}
+        d.setVar("SPDX_LICENSE_DATA", data)
 }
 
 def convert_license_to_spdx(lic, document, d):
@@ -55,9 +58,8 @@ def convert_license_to_spdx(lic, document, d):
     def add_extracted_license(ident, name, text):
         nonlocal document
 
-        for lic_data in license_data["licenses"]:
-            if lic_data["licenseId"] == ident:
-                return False
+        if ident in license_data["licenses"]:
+            return False
 
         spdx_lic = oe.spdx.SPDXExtractedLicensingInfo()
         spdx_lic.name = name
@@ -79,9 +81,8 @@ def convert_license_to_spdx(lic, document, d):
             return "OR"
 
         spdx_license = d.getVarFlag("SPDXLICENSEMAP", l) or l
-        for lic_data in license_data["licenses"]:
-            if lic_data["licenseId"] == spdx_license:
-                return spdx_license
+        if spdx_license in license_data["licenses"]:
+            return spdx_license
 
         spdx_license = "LicenseRef-" + l