]> git.ipfire.org Git - thirdparty/openembedded/openembedded-core-contrib.git/commitdiff
oe/license_finder: add first_only argument to find_licenses()
authorRoss Burton <ross.burton@arm.com>
Fri, 13 Jun 2025 13:16:14 +0000 (14:16 +0100)
committerRichard Purdie <richard.purdie@linuxfoundation.org>
Mon, 16 Jun 2025 16:56:27 +0000 (17:56 +0100)
It may be desired to find only the "top-level" license file instead of
every potential candidate, so add a first_only argument (defaulting to
False to preserve existing behaviour) to return just the first license
found.

Signed-off-by: Ross Burton <ross.burton@arm.com>
Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
meta/lib/oe/license_finder.py

index d5030c033e7c097ba4c5937c76f8b00becf2c6fe..96961658e8bb4e7b5c3f9a6fe2d676d965c5c2d4 100644 (file)
@@ -191,12 +191,18 @@ def crunch_license(licfile):
     return md5val, lictext
 
 
-def find_license_files(srctree):
+def find_license_files(srctree, first_only=False):
+    """
+    Search srctree for files that look like they could be licenses.
+    If first_only is True, only return the first file found.
+    """
     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 = []
     for root, dirs, files in os.walk(srctree):
-        for fn in files:
+        # Sort files so that LICENSE is before LICENSE.subcomponent, which is
+        # meaningful if first_only is set.
+        for fn in sorted(files):
             if fn.endswith(skip_extensions):
                 continue
             for spec in licspecs:
@@ -204,6 +210,8 @@ def find_license_files(srctree):
                     fullpath = os.path.join(root, fn)
                     if not fullpath in licfiles:
                         licfiles.append(fullpath)
+                        if first_only:
+                            return licfiles
 
     return licfiles
 
@@ -233,8 +241,8 @@ def match_licenses(licfiles, srctree, d):
     return licenses
 
 
-def find_licenses(srctree, d):
-    licfiles = find_license_files(srctree)
+def find_licenses(srctree, d, first_only=False):
+    licfiles = find_license_files(srctree, first_only)
     licenses = match_licenses(licfiles, srctree, d)
 
     # FIXME should we grab at least one source file with a license header and add that too?