openembedded-core.lists.openembedded.org archive mirror
 help / color / mirror / Atom feed
From: Stefan Herbrechtsmeier <stefan.herbrechtsmeier-oss@weidmueller.com>
To: openembedded-core@lists.openembedded.org
Cc: Stefan Herbrechtsmeier <stefan.herbrechtsmeier@weidmueller.com>
Subject: [PATCH 09/12] recipetool: npm: Use README as license fallback
Date: Fri,  8 Oct 2021 09:42:25 +0200	[thread overview]
Message-ID: <20211008074228.8635-9-stefan.herbrechtsmeier-oss@weidmueller.com> (raw)
In-Reply-To: <20211008074228.8635-1-stefan.herbrechtsmeier-oss@weidmueller.com>

From: Stefan Herbrechtsmeier <stefan.herbrechtsmeier@weidmueller.com>

Use the README as license fallback if a license file is missing. Use the
linenumbers parameter of get_license_md5sums function to determine the
license text inside the README.

Signed-off-by: Stefan Herbrechtsmeier <stefan.herbrechtsmeier@weidmueller.com>

---

Changes in v2:
- Rework to add licenses to package LICENSE:${PN} and LIC_FILES_CHKSUM

 scripts/lib/recipetool/create_npm.py | 52 +++++++++++++++++++++++++++-
 1 file changed, 51 insertions(+), 1 deletion(-)

diff --git a/scripts/lib/recipetool/create_npm.py b/scripts/lib/recipetool/create_npm.py
index c939780931..3394a89970 100644
--- a/scripts/lib/recipetool/create_npm.py
+++ b/scripts/lib/recipetool/create_npm.py
@@ -6,6 +6,7 @@
 """Recipe creation tool - npm module support plugin"""
 
 import json
+import logging
 import os
 import re
 import sys
@@ -14,8 +15,10 @@ import bb
 from bb.fetch2.npm import NpmEnvironment
 from bb.fetch2.npmsw import foreach_dependencies
 from recipetool.create import RecipeHandler
+from recipetool.create import get_license_md5sums
 from recipetool.create import guess_license
 from recipetool.create import split_pkg_licenses
+logger = logging.getLogger('recipetool')
 
 TINFOIL = None
 
@@ -115,17 +118,36 @@ class NpmRecipeHandler(RecipeHandler):
 
     def _handle_licenses(self, srctree, shrinkwrap_file, dev):
         """Return the extra license files and the list of packages"""
+        licfiles = []
         packages = {}
 
         # Handle the parent package
         packages["${PN}"] = ""
 
+        def _licfiles_append_fallback_readme_files(destdir):
+            """Append README files as fallback to license files if a license files is missing"""
+
+            fallback = True
+            readmes = []
+            basedir = os.path.join(srctree, destdir)
+            for fn in os.listdir(basedir):
+                upper = fn.upper()
+                if upper.startswith("README"):
+                    fullpath = os.path.join(basedir, fn)
+                    readmes.append(fullpath)
+                if upper.startswith("COPYING") or "LICENCE" in upper or "LICENSE" in upper:
+                    fallback = False
+            if fallback:
+                for readme in readmes:
+                    licfiles.append(os.path.relpath(readme, srctree))
+
         # Handle the dependencies
         def _handle_dependency(name, params, deptree):
             suffix = "-".join([self._npm_name(dep) for dep in deptree])
             destdirs = [os.path.join("node_modules", dep) for dep in deptree]
             destdir = os.path.join(*destdirs)
             packages["${PN}-" + suffix] = destdir
+            _licfiles_append_fallback_readme_files(destdir)
 
         with open(shrinkwrap_file, "r") as f:
             shrinkwrap = json.load(f)
@@ -237,7 +259,35 @@ class NpmRecipeHandler(RecipeHandler):
 
         bb.note("Handling licences ...")
         (licfiles, packages) = self._handle_licenses(srctree, shrinkwrap_file, dev)
-        split_pkg_licenses(guess_license(srctree, d), packages, lines_after, [])
+
+        def _guess_odd_license(licfiles):
+            import bb
+
+            md5sums = get_license_md5sums(d, linenumbers=True)
+
+            chksums = []
+            licenses = []
+            for licfile in licfiles:
+                f = os.path.join(srctree, licfile)
+                md5value = bb.utils.md5_file(f)
+                (license, beginline, endline, md5) = md5sums.get(md5value,
+                    (None, "", "", ""))
+                if not license:
+                    license = "Unknown"
+                    logger.info("Please add the following line for '%s' to a "
+                        "'lib/recipetool/licenses.csv' and replace `Unknown`, "
+                        "`X`, `Y` and `MD5` with the license, begin line, "
+                        "end line and partial MD5 checksum:\n" \
+                        "%s,Unknown,X,Y,MD5" % (licfile, md5value))
+                chksums.append("file://%s%s%s;md5=%s" % (licfile,
+                    ";beginline=%s" % (beginline) if beginline else "",
+                    ";endline=%s" % (endline) if endline else "",
+                    md5 if md5 else md5value))
+                licenses.append((license, licfile, md5value))
+            return (licenses, chksums)
+
+        (licenses, extravalues["LIC_FILES_CHKSUM"]) = _guess_odd_license(licfiles)
+        split_pkg_licenses([*licenses, *guess_license(srctree, d)], packages, lines_after)
 
         classes.append("npm")
         handled.append("buildsystem")
-- 
2.20.1



  parent reply	other threads:[~2021-10-08  7:43 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-10-08  7:42 [PATCH 01/12] npm: Add support for EXTRA_OENPM arguments Stefan Herbrechtsmeier
2021-10-08  7:42 ` [PATCH 02/12] recipetool: Move license md5sums into CSV files Stefan Herbrechtsmeier
2021-10-08  7:42 ` [PATCH 03/12] recipetool: Skip common source files in guess_license Stefan Herbrechtsmeier
2021-10-08  7:45   ` [OE-core] " Konrad Weihmann
2021-10-08  7:55     ` Stefan Herbrechtsmeier
2021-10-08 12:26     ` Richard Purdie
2021-10-12 12:27       ` Stefan Herbrechtsmeier
2021-10-08  7:42 ` [PATCH 04/12] recipetool: Change default paramter fallback_licenses of function split_pkg_licenses from None to [] Stefan Herbrechtsmeier
2022-04-13 21:35   ` [OE-core] " Paul Eggleton
2021-10-08  7:42 ` [PATCH 05/12] recipetool: ignore empty license files Stefan Herbrechtsmeier
2021-10-08  7:42 ` [PATCH 06/12] recipetool: Add logger info for missing license entries Stefan Herbrechtsmeier
2021-10-08  7:42 ` [PATCH 07/12] recipetool: Add support for linenumbers to licenses.csv Stefan Herbrechtsmeier
2021-10-08  7:42 ` [PATCH 08/12] recipetool: npm: Do not add package.json files to LIC_FILES_CHKSUM Stefan Herbrechtsmeier
2021-10-08  7:42 ` Stefan Herbrechtsmeier [this message]
2021-10-08  7:42 ` [PATCH 10/12] npm: Add variable NPM_NODEDIR with default value Stefan Herbrechtsmeier
2021-10-08  7:42 ` [PATCH 11/12] npm: Use configs for npm environment and args for npm run command Stefan Herbrechtsmeier
2021-10-08  7:42 ` [PATCH 12/12] recipetool: Rework crunch_license to recognize more variants Stefan Herbrechtsmeier

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=20211008074228.8635-9-stefan.herbrechtsmeier-oss@weidmueller.com \
    --to=stefan.herbrechtsmeier-oss@weidmueller.com \
    --cc=openembedded-core@lists.openembedded.org \
    --cc=stefan.herbrechtsmeier@weidmueller.com \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).