From d0d5106ccc802071bbab12e39289208c034994db Mon Sep 17 00:00:00 2001 From: Benjamin Bertrand Date: Wed, 1 Nov 2023 10:36:38 +0100 Subject: [PATCH 1/2] Add test for GPLv3 license name This currently fails with: > if original_matches[0][1] < 0.55: E IndexError: list index out of range grayskull/license/discovery.py:105: IndexError --- tests/license/test_discovery.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/license/test_discovery.py b/tests/license/test_discovery.py index 0c99657c9..8840ee8da 100644 --- a/tests/license/test_discovery.py +++ b/tests/license/test_discovery.py @@ -64,6 +64,7 @@ def test_get_opensource_license_data(): ("MIT License", "MIT"), ("Expat", "MIT"), ("GPL 2.0", "GPL-2.0-or-later"), + ("GPLv3", "GPL-3.0-or-later"), ("2-Clause BSD License", "BSD-2-Clause"), ("3-Clause BSD License", "BSD-3-Clause"), ], From 82ed56c30238a986131c6ff697b42b20e73d0bd8 Mon Sep 17 00:00:00 2001 From: Benjamin Bertrand Date: Wed, 1 Nov 2023 12:12:01 +0100 Subject: [PATCH 2/2] Fix crash and improve license discovery - check if original_matches isn't empty - only remove "-only" license only if there is an existing "-or-later" identical license - add more test cases for GPL licenses --- grayskull/license/discovery.py | 17 +++++++++++++++-- tests/license/test_discovery.py | 6 +++++- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/grayskull/license/discovery.py b/grayskull/license/discovery.py index 9fa74f929..3bac764e5 100644 --- a/grayskull/license/discovery.py +++ b/grayskull/license/discovery.py @@ -82,7 +82,20 @@ def match_license(name: str) -> dict: spdx_license = best_matches[0] if spdx_license[1] < 100: - best_matches = [lic[0] for lic in best_matches if not lic[0].endswith("-only")] + # Prefer "-or-later" licenses over the "-only" + later_licenses = { + lic[0].replace("-or-later", "") + for lic in best_matches + if lic[0].endswith("-or-later") + } + best_matches = [ + lic[0] + for lic in best_matches + if not ( + lic[0].endswith("-only") + and lic[0].replace("-only", "") in later_licenses + ) + ] if best_matches: best_matches = process.extract( @@ -102,7 +115,7 @@ def match_license(name: str) -> dict: spdx_license = process.extractOne( name, best_matches, scorer=token_sort_ratio ) - if original_matches[0][1] < 0.55: + if original_matches and original_matches[0][1] < 0.55: spdx_license = process.extractOne( name, [m[0] for m in original_matches], scorer=token_sort_ratio ) diff --git a/tests/license/test_discovery.py b/tests/license/test_discovery.py index 8840ee8da..6bb8dff76 100644 --- a/tests/license/test_discovery.py +++ b/tests/license/test_discovery.py @@ -64,7 +64,11 @@ def test_get_opensource_license_data(): ("MIT License", "MIT"), ("Expat", "MIT"), ("GPL 2.0", "GPL-2.0-or-later"), - ("GPLv3", "GPL-3.0-or-later"), + ("GPL 3.0", "GPL-3.0-only"), + ("GPLv3", "GPL-3.0-only"), + ("GPL-3.0-only", "GPL-3.0-only"), + ("LGPL 2.0", "LGPL-2.0-or-later"), + ("LGPL-3.0-or-later", "LGPL-3.0-or-later"), ("2-Clause BSD License", "BSD-2-Clause"), ("3-Clause BSD License", "BSD-3-Clause"), ],