Skip to content

Commit

Permalink
Fix crash and improve license discovery
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
beenje committed Nov 1, 2023
1 parent d0d5106 commit 82ed56c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
17 changes: 15 additions & 2 deletions grayskull/license/discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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
)
Expand Down
6 changes: 5 additions & 1 deletion tests/license/test_discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
],
Expand Down

0 comments on commit 82ed56c

Please sign in to comment.