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

feat: Add pep 639 compatibility #226

Closed
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
12 changes: 10 additions & 2 deletions piplicenses.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,13 +163,15 @@ def normalize_pkg_name(pkg_name: str) -> str:
],
"license": [lambda metadata: metadata.get("license")],
"summary": [lambda metadata: metadata.get("summary")],
"license_expression": [lambda metadata: metadata.get("license-expression")],
}

# Mapping of FIELD_NAMES to METADATA_KEYS where they differ by more than case
FIELDS_TO_METADATA_KEYS = {
"URL": "home-page",
"Description": "summary",
"License-Metadata": "license",
"License-Expression-Metadata": "license_expression",
"License-Classifier": "license_classifier",
}

Expand Down Expand Up @@ -312,6 +314,7 @@ def get_python_sys_path(executable: str) -> list[str]:
args.from_,
cast(List[str], pkg_info["license_classifier"]),
cast(str, pkg_info["license"]),
cast(str, pkg_info["license_expression"]),
)

if fail_on_licenses:
Expand Down Expand Up @@ -374,6 +377,7 @@ def create_licenses_table(
args.from_,
cast(List[str], pkg["license_classifier"]),
cast(str, pkg["license"]),
cast(str, pkg["license_expression"]),
)
license_str = "; ".join(sorted(license_set))
row.append(license_str)
Expand All @@ -399,6 +403,7 @@ def create_summary_table(args: CustomNamespace) -> PrettyTable:
args.from_,
cast(List[str], pkg["license_classifier"]),
cast(str, pkg["license"]),
cast(str, pkg["license_expression"]),
)
)
)
Expand Down Expand Up @@ -613,7 +618,7 @@ def find_license_from_classifier(classifiers: list[str]) -> list[str]:


def select_license_by_source(
from_source: FromArg, license_classifier: list[str], license_meta: str
from_source: FromArg, license_classifier: list[str], license_meta: str, license_expression_meta: str
) -> set[str]:
license_classifier_set = set(license_classifier) or {LICENSE_UNKNOWN}
if (
Expand All @@ -622,8 +627,10 @@ def select_license_by_source(
and len(license_classifier) > 0
):
return license_classifier_set
else:
elif license_meta != LICENSE_UNKNOWN:
return {license_meta}
else:
return {license_expression_meta}


def get_output_fields(args: CustomNamespace) -> list[str]:
Expand All @@ -634,6 +641,7 @@ def get_output_fields(args: CustomNamespace) -> list[str]:

if args.from_ == FromArg.ALL:
output_fields.append("License-Metadata")
output_fields.append("License-Expression-Metadata")
output_fields.append("License-Classifier")
else:
output_fields.append("License")
Expand Down
26 changes: 21 additions & 5 deletions test_piplicenses.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,20 +219,29 @@ def test_from_all(self) -> None:
table = create_licenses_table(args, output_fields)

self.assertIn("License-Metadata", output_fields)
self.assertIn("License-Expression-Metadata", output_fields)
self.assertIn("License-Classifier", output_fields)

index_license_meta = output_fields.index("License-Metadata")
license_meta = []
for row in table.rows:
license_meta.append(row[index_license_meta])

index_license_expression_meta = output_fields.index("License-Expression-Metadata")
license_expression_meta = []
for row in table.rows:
license_expression_meta.append(row[index_license_expression_meta])

index_license_classifier = output_fields.index("License-Classifier")
license_classifier = []
for row in table.rows:
license_classifier.append(row[index_license_classifier])

for license_name in ("BSD", "MIT", "Apache 2.0"):
self.assertIn(license_name, license_meta)
# FIXME: The test is limited to MIT ot all packages are following PEP 639 yet.
for license_name in ("MIT",):
self.assertIn(license_name, license_expression_meta)
for license_name in (
"BSD License",
"MIT License",
Expand Down Expand Up @@ -270,30 +279,37 @@ def test_select_license_by_source(self) -> None:
self.assertEqual(
{"MIT License"},
select_license_by_source(
FromArg.CLASSIFIER, ["MIT License"], "MIT"
FromArg.CLASSIFIER, ["MIT License"], "MIT", LICENSE_UNKNOWN
),
)

self.assertEqual(
{LICENSE_UNKNOWN},
select_license_by_source(FromArg.CLASSIFIER, [], "MIT"),
select_license_by_source(FromArg.CLASSIFIER, [], "MIT", LICENSE_UNKNOWN),
)

self.assertEqual(
{"MIT License"},
select_license_by_source(FromArg.MIXED, ["MIT License"], "MIT"),
select_license_by_source(FromArg.MIXED, ["MIT License"], "MIT", LICENSE_UNKNOWN),
)

self.assertEqual(
{"MIT"}, select_license_by_source(FromArg.MIXED, [], "MIT")
{"MIT"}, select_license_by_source(FromArg.MIXED, [], "MIT", LICENSE_UNKNOWN)
)
self.assertEqual(
{"Apache License 2.0"},
select_license_by_source(
FromArg.MIXED, ["Apache License 2.0"], "Apache-2.0"
FromArg.MIXED, ["Apache License 2.0"], "Apache-2.0", ""
),
)

self.assertEqual(
{"MIT"}, select_license_by_source(FromArg.MIXED, [], LICENSE_UNKNOWN, "MIT")
)
self.assertEqual(
{"MIT"}, select_license_by_source(FromArg.META, [], LICENSE_UNKNOWN, "MIT")
)

def test_with_system(self) -> None:
with_system_args = ["--with-system"]
args = self.parser.parse_args(with_system_args)
Expand Down