diff options
author | Enguerrand de Ribaucourt <enguerrand.de-ribaucourt@savoirfairelinux.com> | 2024-08-12 14:28:28 +0200 |
---|---|---|
committer | Richard Purdie <richard.purdie@linuxfoundation.org> | 2024-08-23 09:51:36 +0100 |
commit | 69bf37a3dd0470dc153bb7a21bd8f13a64bde2c3 (patch) | |
tree | cb531ea0bd3590e2f2b29a00156de6078027a5d8 | |
parent | 01d17cd5d42d1c9c349e1249bdacffb24d8446bb (diff) | |
download | poky-69bf37a3dd0470dc153bb7a21bd8f13a64bde2c3.tar.gz |
recipetool: create: split guess_license function
The npm recipetool handler redefines the license code the could be
unified. In order to do this refactoring, extract the bits we'll
need into separate functions.
guess_license() is renamed to find_licenses() and is split into
find_license_files() and match_licenses().
(From OE-Core rev: f1ec28feaea8ea6a2df894dd4ddba561c8a04ed2)
Signed-off-by: Enguerrand de Ribaucourt <enguerrand.de-ribaucourt@savoirfairelinux.com>
Signed-off-by: Alexandre Belloni <alexandre.belloni@bootlin.com>
Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
-rw-r--r-- | meta/classes-global/license.bbclass | 8 | ||||
-rw-r--r-- | meta/lib/oeqa/selftest/cases/recipetool.py | 1 | ||||
-rw-r--r-- | scripts/lib/recipetool/create.py | 34 | ||||
-rw-r--r-- | scripts/lib/recipetool/create_go.py | 4 | ||||
-rw-r--r-- | scripts/lib/recipetool/create_npm.py | 4 |
5 files changed, 31 insertions, 20 deletions
diff --git a/meta/classes-global/license.bbclass b/meta/classes-global/license.bbclass index b2e0d3faba..4e39ec1d44 100644 --- a/meta/classes-global/license.bbclass +++ b/meta/classes-global/license.bbclass | |||
@@ -39,7 +39,7 @@ python do_populate_lic() { | |||
39 | } | 39 | } |
40 | 40 | ||
41 | PSEUDO_IGNORE_PATHS .= ",${@','.join(((d.getVar('COMMON_LICENSE_DIR') or '') + ' ' + (d.getVar('LICENSE_PATH') or '') + ' ' + d.getVar('COREBASE') + '/meta/COPYING').split())}" | 41 | PSEUDO_IGNORE_PATHS .= ",${@','.join(((d.getVar('COMMON_LICENSE_DIR') or '') + ' ' + (d.getVar('LICENSE_PATH') or '') + ' ' + d.getVar('COREBASE') + '/meta/COPYING').split())}" |
42 | # it would be better to copy them in do_install:append, but find_license_filesa is python | 42 | # it would be better to copy them in do_install:append, but find_license_files is python |
43 | python perform_packagecopy:prepend () { | 43 | python perform_packagecopy:prepend () { |
44 | enabled = oe.data.typed_value('LICENSE_CREATE_PACKAGE', d) | 44 | enabled = oe.data.typed_value('LICENSE_CREATE_PACKAGE', d) |
45 | if d.getVar('CLASSOVERRIDE') == 'class-target' and enabled: | 45 | if d.getVar('CLASSOVERRIDE') == 'class-target' and enabled: |
@@ -149,14 +149,14 @@ def find_license_files(d): | |||
149 | # and "with exceptions" being * | 149 | # and "with exceptions" being * |
150 | # we'll just strip out the modifier and put | 150 | # we'll just strip out the modifier and put |
151 | # the base license. | 151 | # the base license. |
152 | find_license(node.s.replace("+", "").replace("*", "")) | 152 | find_licenses(node.s.replace("+", "").replace("*", "")) |
153 | self.generic_visit(node) | 153 | self.generic_visit(node) |
154 | 154 | ||
155 | def visit_Constant(self, node): | 155 | def visit_Constant(self, node): |
156 | find_license(node.value.replace("+", "").replace("*", "")) | 156 | find_licenses(node.value.replace("+", "").replace("*", "")) |
157 | self.generic_visit(node) | 157 | self.generic_visit(node) |
158 | 158 | ||
159 | def find_license(license_type): | 159 | def find_licenses(license_type): |
160 | try: | 160 | try: |
161 | bb.utils.mkdirhier(gen_lic_dest) | 161 | bb.utils.mkdirhier(gen_lic_dest) |
162 | except: | 162 | except: |
diff --git a/meta/lib/oeqa/selftest/cases/recipetool.py b/meta/lib/oeqa/selftest/cases/recipetool.py index 42202b7831..f742dd4d64 100644 --- a/meta/lib/oeqa/selftest/cases/recipetool.py +++ b/meta/lib/oeqa/selftest/cases/recipetool.py | |||
@@ -1068,6 +1068,7 @@ class RecipetoolTests(RecipetoolBase): | |||
1068 | 1068 | ||
1069 | d = DataConnectorCopy | 1069 | d = DataConnectorCopy |
1070 | d.getVar = Mock(return_value=commonlicdir) | 1070 | d.getVar = Mock(return_value=commonlicdir) |
1071 | d.expand = Mock(side_effect=lambda x: x) | ||
1071 | 1072 | ||
1072 | srctree = tempfile.mkdtemp(prefix='recipetoolqa') | 1073 | srctree = tempfile.mkdtemp(prefix='recipetoolqa') |
1073 | self.track_for_cleanup(srctree) | 1074 | self.track_for_cleanup(srctree) |
diff --git a/scripts/lib/recipetool/create.py b/scripts/lib/recipetool/create.py index 066366e34f..c626844370 100644 --- a/scripts/lib/recipetool/create.py +++ b/scripts/lib/recipetool/create.py | |||
@@ -960,7 +960,7 @@ def handle_license_vars(srctree, lines_before, handled, extravalues, d): | |||
960 | # Someone else has already handled the license vars, just return their value | 960 | # Someone else has already handled the license vars, just return their value |
961 | return lichandled[0][1] | 961 | return lichandled[0][1] |
962 | 962 | ||
963 | licvalues = guess_license(srctree, d) | 963 | licvalues = find_licenses(srctree, d) |
964 | licenses = [] | 964 | licenses = [] |
965 | lic_files_chksum = [] | 965 | lic_files_chksum = [] |
966 | lic_unknown = [] | 966 | lic_unknown = [] |
@@ -1216,13 +1216,7 @@ def crunch_license(licfile): | |||
1216 | lictext = '' | 1216 | lictext = '' |
1217 | return md5val, lictext | 1217 | return md5val, lictext |
1218 | 1218 | ||
1219 | def guess_license(srctree, d): | 1219 | def find_license_files(srctree): |
1220 | import bb | ||
1221 | md5sums = get_license_md5sums(d) | ||
1222 | |||
1223 | crunched_md5sums = crunch_known_licenses(d) | ||
1224 | |||
1225 | licenses = [] | ||
1226 | licspecs = ['*LICEN[CS]E*', 'COPYING*', '*[Ll]icense*', 'LEGAL*', '[Ll]egal*', '*GPL*', 'README.lic*', 'COPYRIGHT*', '[Cc]opyright*', 'e[dp]l-v10'] | 1220 | licspecs = ['*LICEN[CS]E*', 'COPYING*', '*[Ll]icense*', 'LEGAL*', '[Ll]egal*', '*GPL*', 'README.lic*', 'COPYRIGHT*', '[Cc]opyright*', 'e[dp]l-v10'] |
1227 | skip_extensions = (".html", ".js", ".json", ".svg", ".ts", ".go") | 1221 | skip_extensions = (".html", ".js", ".json", ".svg", ".ts", ".go") |
1228 | licfiles = [] | 1222 | licfiles = [] |
@@ -1235,11 +1229,22 @@ def guess_license(srctree, d): | |||
1235 | fullpath = os.path.join(root, fn) | 1229 | fullpath = os.path.join(root, fn) |
1236 | if not fullpath in licfiles: | 1230 | if not fullpath in licfiles: |
1237 | licfiles.append(fullpath) | 1231 | licfiles.append(fullpath) |
1232 | |||
1233 | return licfiles | ||
1234 | |||
1235 | def match_licenses(licfiles, srctree, d): | ||
1236 | import bb | ||
1237 | md5sums = get_license_md5sums(d) | ||
1238 | |||
1239 | crunched_md5sums = crunch_known_licenses(d) | ||
1240 | |||
1241 | licenses = [] | ||
1238 | for licfile in sorted(licfiles): | 1242 | for licfile in sorted(licfiles): |
1239 | md5value = bb.utils.md5_file(licfile) | 1243 | resolved_licfile = d.expand(licfile) |
1244 | md5value = bb.utils.md5_file(resolved_licfile) | ||
1240 | license = md5sums.get(md5value, None) | 1245 | license = md5sums.get(md5value, None) |
1241 | if not license: | 1246 | if not license: |
1242 | crunched_md5, lictext = crunch_license(licfile) | 1247 | crunched_md5, lictext = crunch_license(resolved_licfile) |
1243 | license = crunched_md5sums.get(crunched_md5, None) | 1248 | license = crunched_md5sums.get(crunched_md5, None) |
1244 | if lictext and not license: | 1249 | if lictext and not license: |
1245 | license = 'Unknown' | 1250 | license = 'Unknown' |
@@ -1249,13 +1254,19 @@ def guess_license(srctree, d): | |||
1249 | if license: | 1254 | if license: |
1250 | licenses.append((license, os.path.relpath(licfile, srctree), md5value)) | 1255 | licenses.append((license, os.path.relpath(licfile, srctree), md5value)) |
1251 | 1256 | ||
1257 | return licenses | ||
1258 | |||
1259 | def find_licenses(srctree, d): | ||
1260 | licfiles = find_license_files(srctree) | ||
1261 | licenses = match_licenses(licfiles, srctree, d) | ||
1262 | |||
1252 | # FIXME should we grab at least one source file with a license header and add that too? | 1263 | # FIXME should we grab at least one source file with a license header and add that too? |
1253 | 1264 | ||
1254 | return licenses | 1265 | return licenses |
1255 | 1266 | ||
1256 | def split_pkg_licenses(licvalues, packages, outlines, fallback_licenses=None, pn='${PN}'): | 1267 | def split_pkg_licenses(licvalues, packages, outlines, fallback_licenses=None, pn='${PN}'): |
1257 | """ | 1268 | """ |
1258 | Given a list of (license, path, md5sum) as returned by guess_license(), | 1269 | Given a list of (license, path, md5sum) as returned by match_licenses(), |
1259 | a dict of package name to path mappings, write out a set of | 1270 | a dict of package name to path mappings, write out a set of |
1260 | package-specific LICENSE values. | 1271 | package-specific LICENSE values. |
1261 | """ | 1272 | """ |
@@ -1418,4 +1429,3 @@ def register_commands(subparsers): | |||
1418 | parser_create.add_argument('--devtool', action="store_true", help=argparse.SUPPRESS) | 1429 | parser_create.add_argument('--devtool', action="store_true", help=argparse.SUPPRESS) |
1419 | parser_create.add_argument('--mirrors', action="store_true", help='Enable PREMIRRORS and MIRRORS for source tree fetching (disabled by default).') | 1430 | parser_create.add_argument('--mirrors', action="store_true", help='Enable PREMIRRORS and MIRRORS for source tree fetching (disabled by default).') |
1420 | parser_create.set_defaults(func=create_recipe) | 1431 | parser_create.set_defaults(func=create_recipe) |
1421 | |||
diff --git a/scripts/lib/recipetool/create_go.py b/scripts/lib/recipetool/create_go.py index a85a2f2786..5cc53931f0 100644 --- a/scripts/lib/recipetool/create_go.py +++ b/scripts/lib/recipetool/create_go.py | |||
@@ -14,7 +14,7 @@ from collections import namedtuple | |||
14 | from enum import Enum | 14 | from enum import Enum |
15 | from html.parser import HTMLParser | 15 | from html.parser import HTMLParser |
16 | from recipetool.create import RecipeHandler, handle_license_vars | 16 | from recipetool.create import RecipeHandler, handle_license_vars |
17 | from recipetool.create import guess_license, tidy_licenses, fixup_license | 17 | from recipetool.create import find_licenses, tidy_licenses, fixup_license |
18 | from recipetool.create import determine_from_url | 18 | from recipetool.create import determine_from_url |
19 | from urllib.error import URLError, HTTPError | 19 | from urllib.error import URLError, HTTPError |
20 | 20 | ||
@@ -624,7 +624,7 @@ class GoRecipeHandler(RecipeHandler): | |||
624 | 624 | ||
625 | licenses = [] | 625 | licenses = [] |
626 | lic_files_chksum = [] | 626 | lic_files_chksum = [] |
627 | licvalues = guess_license(tmp_vendor_dir, d) | 627 | licvalues = find_licenses(tmp_vendor_dir, d) |
628 | shutil.rmtree(tmp_vendor_dir) | 628 | shutil.rmtree(tmp_vendor_dir) |
629 | 629 | ||
630 | if licvalues: | 630 | if licvalues: |
diff --git a/scripts/lib/recipetool/create_npm.py b/scripts/lib/recipetool/create_npm.py index dd0ac01c3e..78dc248f31 100644 --- a/scripts/lib/recipetool/create_npm.py +++ b/scripts/lib/recipetool/create_npm.py | |||
@@ -17,7 +17,7 @@ from bb.fetch2.npm import npm_package | |||
17 | from bb.fetch2.npmsw import foreach_dependencies | 17 | from bb.fetch2.npmsw import foreach_dependencies |
18 | from recipetool.create import RecipeHandler | 18 | from recipetool.create import RecipeHandler |
19 | from recipetool.create import get_license_md5sums | 19 | from recipetool.create import get_license_md5sums |
20 | from recipetool.create import guess_license | 20 | from recipetool.create import find_licenses |
21 | from recipetool.create import split_pkg_licenses | 21 | from recipetool.create import split_pkg_licenses |
22 | logger = logging.getLogger('recipetool') | 22 | logger = logging.getLogger('recipetool') |
23 | 23 | ||
@@ -320,7 +320,7 @@ class NpmRecipeHandler(RecipeHandler): | |||
320 | return (licenses, chksums, fallback_licenses) | 320 | return (licenses, chksums, fallback_licenses) |
321 | 321 | ||
322 | (licenses, extravalues["LIC_FILES_CHKSUM"], fallback_licenses) = _guess_odd_license(licfiles) | 322 | (licenses, extravalues["LIC_FILES_CHKSUM"], fallback_licenses) = _guess_odd_license(licfiles) |
323 | split_pkg_licenses([*licenses, *guess_license(srctree, d)], packages, lines_after, fallback_licenses) | 323 | split_pkg_licenses([*licenses, *find_licenses(srctree, d)], packages, lines_after, fallback_licenses) |
324 | 324 | ||
325 | classes.append("npm") | 325 | classes.append("npm") |
326 | handled.append("buildsystem") | 326 | handled.append("buildsystem") |