From 3405d9114ca9fe4ba820e0025c91670d1a5150b1 Mon Sep 17 00:00:00 2001 From: Ross Burton Date: Fri, 3 Sep 2021 17:00:30 +0100 Subject: create-spdx: transform license list into a dict for faster lookups 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 Signed-off-by: Richard Purdie --- meta/classes/create-spdx.bbclass | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/meta/classes/create-spdx.bbclass b/meta/classes/create-spdx.bbclass index a590ab596a..73ccb3c990 100644 --- a/meta/classes/create-spdx.bbclass +++ b/meta/classes/create-spdx.bbclass @@ -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 -- cgit 1.2.3-korg