All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Ross Burton" <ross@burtonini.com>
To: openembedded-core@lists.openembedded.org
Subject: [PATCH v2 3/4] create-spdx: embed unknown license texts
Date: Fri,  3 Sep 2021 17:00:32 +0100	[thread overview]
Message-ID: <20210903160033.3141022-3-ross.burton@arm.com> (raw)
In-Reply-To: <20210903160033.3141022-1-ross.burton@arm.com>

For licenses which are not known to SPDX, find and embed the actual
license text in an ExtractedLicesingInfo block.

Signed-off-by: Ross Burton <ross.burton@arm.com>
---
 meta/classes/create-spdx.bbclass | 51 +++++++++++++++++++++++---------
 1 file changed, 37 insertions(+), 14 deletions(-)

diff --git a/meta/classes/create-spdx.bbclass b/meta/classes/create-spdx.bbclass
index 529dee22918..cbb9239991c 100644
--- a/meta/classes/create-spdx.bbclass
+++ b/meta/classes/create-spdx.bbclass
@@ -51,21 +51,49 @@ python() {
 }
 
 def convert_license_to_spdx(lic, document, d):
+    from pathlib import Path
     import oe.spdx
 
+    available_licenses = d.getVar("AVAILABLE_LICENSES").split()
     license_data = d.getVar("SPDX_LICENSE_DATA")
+    extracted = {}
 
-    def add_extracted_license(ident, name, text):
+    def add_extracted_license(ident, name):
         nonlocal document
 
-        spdx_lic = oe.spdx.SPDXExtractedLicensingInfo()
-        spdx_lic.name = name
-        spdx_lic.licenseId = ident
-        spdx_lic.extractedText = text
-
-        document.hasExtractedLicensingInfos.append(spdx_lic)
+        if name in extracted:
+            return
+
+        extracted_info = oe.spdx.SPDXExtractedLicensingInfo()
+        extracted_info.name = name
+        extracted_info.licenseId = ident
+
+        if name == "PD":
+            # Special-case this.
+            extracted_info.extractedText = "Software released to the public domain"
+        elif name in available_licenses:
+            # This license can be found in COMMON_LICENSE_DIR or LICENSE_PATH
+            for directory in [d.getVar('COMMON_LICENSE_DIR')] + d.getVar('LICENSE_PATH').split():
+                try:
+                    with (Path(directory) / name).open(errors="replace") as f:
+                        extracted_info.extractedText = f.read()
+                        break
+                except Exception as e:
+                    # Error out, as the license was in available_licenses so
+                    # should be on disk somewhere.
+                    bb.error(f"Cannot find text for license {name}: {e}")
+        else:
+            # If it's not SPDX, or PD, or in available licenses, then NO_GENERIC_LICENSE must be set
+            filename = d.getVarFlag('NO_GENERIC_LICENSE', name)
+            if filename:
+                filename = d.expand("${S}/" + filename)
+                with open(filename, errors="replace") as f:
+                    extracted_info.extractedText = f.read()
+            else:
+                bb.error(f"Cannot find any text for license {name}")
 
-        return True
+        extracted[name] = extracted_info
+        document.hasExtractedLicensingInfos.append(extracted_info)
 
     def convert(l):
         if l == "(" or l == ")":
@@ -82,12 +110,7 @@ def convert_license_to_spdx(lic, document, d):
             return spdx_license
 
         spdx_license = "LicenseRef-" + l
-
-        if l == "PD":
-            add_extracted_license(spdx_license, l, "Software released to the public domain")
-        elif add_extracted_license(spdx_license, l, "This software is licensed under the %s license" % l):
-            pass
-            #bb.warn("No SPDX License found for %s. Creating a place holder" % l)
+        add_extracted_license(spdx_license, l)
 
         return spdx_license
 
-- 
2.25.1


  parent reply	other threads:[~2021-09-03 16:00 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-09-03 16:00 [PATCH v2 1/4] create-spdx: transform license list into a dict for faster lookups Ross Burton
2021-09-03 16:00 ` [PATCH v2 2/4] create-spdx: remove redundant test Ross Burton
2021-09-03 16:00 ` Ross Burton [this message]
2021-09-03 16:00 ` [PATCH v2 4/4] create-spex: don't duplicate license texts in each package Ross Burton
2021-09-03 22:14   ` [OE-core] " Peter Kjellerstedt

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210903160033.3141022-3-ross.burton@arm.com \
    --to=ross@burtonini.com \
    --cc=openembedded-core@lists.openembedded.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.