Skip to content

Commit f1805b6

Browse files
committed
Accept License-Expression in metadata (PEP 639)
[PEP 639](https://peps.python.org/pep-0639/) adds the `License-Expression` header. This commit adds support for it in the metadata parser. Twisted 23.8.0 has already replaced the `Licence` header with `License-Expression` and thus is read as an `UNKNOWN` license. Signed-off-by: Aurélien Bompard <[email protected]>
1 parent 543b328 commit f1805b6

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

liccheck/command_line.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ class Reason(enum.Enum):
141141

142142

143143
def get_packages_info(requirement_file, no_deps=False):
144-
regex_license = re.compile(r"License: (?P<license>.*)?$", re.M)
144+
regex_license = re.compile(r"License(?:-Expression)?: (?P<license>.*)?$", re.M)
145145
regex_classifier = re.compile(
146146
r"Classifier: License(?: :: OSI Approved)?(?: :: (?P<classifier>.*))?$", re.M
147147
)

tests/test_get_packages_info.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import sys
22

3+
import pkg_resources
34
import pytest
45

56
from liccheck.command_line import get_packages_info
@@ -59,3 +60,21 @@ def test_deps(tmpfile, no_deps, expected_packages):
5960
packages_info = get_packages_info(tmppath, no_deps)
6061
packages = tuple(package['name'] for package in packages_info)
6162
assert packages == expected_packages
63+
64+
65+
def test_license_expression(tmp_path, mocker):
66+
resolve = mocker.patch("liccheck.command_line.resolve")
67+
req_path = tmp_path.joinpath("requirements.txt").as_posix()
68+
with open(req_path, "w") as tmpfh:
69+
tmpfh.write("Twisted\n")
70+
pkg_info_path = tmp_path.joinpath("PKG-INFO").as_posix()
71+
with open(pkg_info_path, "w") as tmpfh:
72+
tmpfh.write("Metadata-Version: 2.1\n")
73+
tmpfh.write("Name: Twisted\n")
74+
tmpfh.write("Version: 23.8.0\n")
75+
tmpfh.write("License-Expression: MIT\n")
76+
metadata = pkg_resources.FileMetadata(pkg_info_path)
77+
resolve.return_value = [
78+
pkg_resources.Distribution(project_name="Twisted", metadata=metadata)
79+
]
80+
assert get_packages_info(req_path)[0]["licenses"] == ["MIT"]

0 commit comments

Comments
 (0)