Skip to content

Commit d56d623

Browse files
committed
fix #142
1 parent d7df70c commit d56d623

3 files changed

Lines changed: 34 additions & 12 deletions

File tree

licensecheck/packageinforesolver.py

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from __future__ import annotations
44

5+
from boolean.boolean import Expression
56
import configparser
67
import contextlib
78
import re
@@ -103,22 +104,26 @@ def _get_package_info(self, package: Requirement) -> PackageInfo:
103104
errorCode=rpi.http_code if rpi.http_code != HTTP_OK else 0,
104105
)
105106

107+
# normailzing the license
106108
if pkg_info.license:
107-
licensing = Licensing()
108-
parsed = None
109-
with contextlib.suppress(license_expression.ExpressionParseError):
110-
parsed = licensing.parse(
111-
re.sub(r"[^a-zA-Z0-9_.:\- ]", "_", pkg_info.license.splitlines()[0])
112-
)
113-
if parsed is None:
114-
return pkg_info
115-
116-
tokens = sorted(parsed.literals)
117-
pkg_info.license = str(JOINS.join(x.key for x in tokens))
109+
pkg_info.license = normalize_license(pkg_info.license)
118110

119111
return pkg_info
120112

121113

114+
def normalize_license(lice: str) -> str:
115+
licensing = Licensing()
116+
parsed = None
117+
with contextlib.suppress(license_expression.ExpressionParseError):
118+
parsed = licensing.parse(
119+
re.sub(r"[^a-zA-Z0-9_.:\- ]", "_", lice.splitlines()[0])
120+
)
121+
if parsed is None:
122+
return lice
123+
124+
tokens: list[Expression] = sorted(parsed.literals)
125+
return str(JOINS.join(getattr(x, "key", str(x)) for x in tokens))
126+
122127
class LocalPackageInfo:
123128
"""Handles retrieval of package info from local installation."""
124129

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "licensecheck"
3-
version = "2026.0.4"
3+
version = "2026.0.5"
44
description = "Output the licenses used by dependencies and check if these are compatible with the project license"
55
authors = [{ name = "FredHappyface" }]
66
requires-python = ">=3.12"

tests/test_packageinfo.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
PackageInfoManager,
1212
RemotePackageInfo,
1313
from_classifiers,
14+
normalize_license,
1415
)
1516

1617
THISDIR = str(Path(__file__).resolve().parent)
@@ -100,3 +101,19 @@ def test_licenseFromEmptyClassifierlist() -> None:
100101
def test_getModuleSize() -> None:
101102
local_package_info = LocalPackageInfo(aux_packageinfo("this_package_does_not_exist"))
102103
local_package_info.get_size()
104+
105+
106+
@pytest.mark.parametrize(
107+
("lice", "normalized"),
108+
[
109+
("mit", "mit"),
110+
("BSD-2-Clause", "BSD-2-Clause"),
111+
("BSD-2-Clause AND Apache-2.0", "Apache-2.0;; BSD-2-Clause"),
112+
(
113+
"BSD-2-Clause AND Apache-2.0 WITH LLVM-exception",
114+
"Apache-2.0 WITH LLVM-exception;; BSD-2-Clause",
115+
),
116+
],
117+
)
118+
def test_normalize_license(lice: str, normalized: str) -> None:
119+
assert normalize_license(lice) == normalized

0 commit comments

Comments
 (0)