diff mbox series

[1/3] oe/license_finder: Add find_licenses_up function

Message ID 20250902140647.126140-2-christli@axis.com
State New
Headers show
Series Go module dependencies | expand

Commit Message

Christian Lindeberg Sept. 2, 2025, 2:06 p.m. UTC
From: Christian Lindeberg <christian.lindeberg@axis.com>

Add a function for finding licenses in a directory or upwards but not
above a top directory.

Signed-off-by: Christian Lindeberg <christian.lindeberg@axis.com>
---
 meta/lib/oe/license_finder.py | 27 ++++++++++++++++++++++++++-
 1 file changed, 26 insertions(+), 1 deletion(-)
diff mbox series

Patch

diff --git a/meta/lib/oe/license_finder.py b/meta/lib/oe/license_finder.py
index 16f5d7c94c..4f2bb661fd 100644
--- a/meta/lib/oe/license_finder.py
+++ b/meta/lib/oe/license_finder.py
@@ -120,14 +120,34 @@  def _crunch_license(licfile):
     return md5val
 
 
-def find_license_files(srctree, first_only=False):
+def find_license_files(srctree, first_only=False, bottom=""):
     """
     Search srctree for files that look like they could be licenses.
     If first_only is True, only return the first file found.
+    If bottom is not empty, start at bottom and continue upwards to the top.
     """
     licspecs = ['*LICEN[CS]E*', 'COPYING*', '*[Ll]icense*', 'LEGAL*', '[Ll]egal*', '*GPL*', 'README.lic*', 'COPYRIGHT*', '[Cc]opyright*', 'e[dp]l-v10']
     skip_extensions = (".html", ".js", ".json", ".svg", ".ts", ".go", ".sh")
     licfiles = []
+    if bottom:
+        srcdir = bottom
+        while srcdir.startswith(srctree):
+            files = []
+            with os.scandir(srcdir) as it:
+                for entry in it:
+                    if entry.is_file():
+                        files.append(entry.name)
+            for name in sorted(files):
+                if name.endswith(skip_extensions):
+                    continue
+                for spec in licspecs:
+                    if fnmatch.fnmatch(name, spec):
+                        licfiles.append(os.path.join(srcdir, name))
+                        if first_only:
+                            return licfiles
+            srcdir = os.path.dirname(srcdir)
+        return licfiles
+
     for root, dirs, files in os.walk(srctree):
         # Sort files so that LICENSE is before LICENSE.subcomponent, which is
         # meaningful if first_only is set.
@@ -177,3 +197,8 @@  def find_licenses(srctree, d, first_only=False, extra_hashes={}):
     # FIXME should we grab at least one source file with a license header and add that too?
 
     return licenses
+
+
+def find_licenses_up(srcdir, topdir, d, first_only=False, extra_hashes={}):
+    licfiles = find_license_files(topdir, first_only, srcdir)
+    return match_licenses(licfiles, topdir, d, extra_hashes)