Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed pdm list output containing full license text in some cases #2539

Merged
merged 6 commits into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions news/2538.bugfix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed pdm list output containing full license text in some cases
10 changes: 9 additions & 1 deletion src/pdm/cli/commands/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,15 @@ def __init__(self, dist: im.Distribution, groups: set[str]):
# so generate a pipe separated list (to avoid complexity with CSV export).
self.licenses: str | None = dist.metadata["License"]
self.licenses = None if self.licenses == "UNKNOWN" else self.licenses
if not self.licenses:

# Sometimes package metadata contains the full license text.
# e.g. license = { file="LICENSE" } in pyproject.toml
# To identify this, check for newlines or very long strings.
# 50 chars is picked because the longest OSI license (WTFPL) full name is 43 characters.
is_full_text = self.licenses and "\n" in self.licenses or len(self.licenses or "") > 50

# If that is the case, look at the classifiers instead.
if not self.licenses or is_full_text:
classifier_licenses = [v for v in dist.metadata.get_all("Classifier", []) if v.startswith("License")]
alternatives = [parts.split("::") for parts in classifier_licenses]
alternatives = [part[-1].strip() for part in alternatives if part]
Expand Down
15 changes: 13 additions & 2 deletions tests/cli/test_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -513,8 +513,16 @@ class _MockPackagePath(pathlib.PurePosixPath):
def read_text(self, *args, **kwargs):
return self.license_text

# Foo package.
foo = Distribution("foo", "0.1.0", metadata={"License": "A License"})
# Foo package (adapted to contain newlines in License field)
# e.g. via license = { file="LICENSE" }
foo = Distribution(
"foo",
"0.1.0",
metadata={
"License": "A License\n\nextra\ntext",
"Classifier": "License :: A License",
},
)
foo_l = _MockPackagePath("foo-0.1.0.dist-info", "LICENSE")
foo_l.license_text = "license text for foo here"
foo.files = [foo_l]
Expand Down Expand Up @@ -679,6 +687,9 @@ def test_list_json_fields_licences(project, pdm):

@pytest.mark.usefixtures("fake_working_set")
def test_list_markdown_fields_licences(project, pdm):
# Note that in "foo" the "License" metadata field ("License": "A License\n\nextra\ntext")
# is ignored, in favour of the classifier and the LICENSE file.
# This behaviour could be improved.
result = pdm(["list", "--markdown", "--fields", "name,version,licenses"], obj=project)
expected = (
"# test-project licenses\n"
Expand Down