Skip to content

Commit

Permalink
Refactor licenses check (#129194)
Browse files Browse the repository at this point in the history
  • Loading branch information
cdce8p authored Oct 26, 2024
1 parent c5ed148 commit 8fb7a7e
Showing 1 changed file with 54 additions and 39 deletions.
93 changes: 54 additions & 39 deletions script/licenses.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,62 +178,77 @@ def from_dict(cls, data: PackageMetadata) -> PackageDefinition:
), # https://github.com/aio-libs/aiocache/blob/master/LICENSE all rights reserved?
}

EXCEPTIONS_AND_TODOS = EXCEPTIONS.union(TODO)


def check_licenses(args: CheckArgs) -> int:
"""Check licenses are OSI approved."""
exit_code = 0
raw_licenses = json.loads(Path(args.path).read_text())
package_definitions = [PackageDefinition.from_dict(data) for data in raw_licenses]
for package in package_definitions:
previous_unapproved_version = TODO.get(package.name)
approved = False
for approved_license in OSI_APPROVED_LICENSES:
if approved_license in package.license:
approved = True
break
if previous_unapproved_version is not None:
if previous_unapproved_version < package.version:
if approved:
print(
"Approved license detected for "
f"{package.name}@{package.version}: {package.license}"
)
print("Please remove the package from the TODO list.")
print()
else:
print(
"We could not detect an OSI-approved license for "
f"{package.name}@{package.version}: {package.license}"
)
print()
exit_code = 1
elif not approved and package.name not in EXCEPTIONS:
license_status = {
pkg.name: (pkg, check_license_status(pkg))
for data in raw_licenses
if (pkg := PackageDefinition.from_dict(data))
}

for name, version in TODO.items():
pkg, status = license_status.get(name, (None, None))
if pkg is None or not (version < pkg.version):
continue
assert status is not None

if status is True:
print(
f"Approved license detected for "
f"{pkg.name}@{pkg.version}: {get_license_str(pkg)}\n"
"Please remove the package from the TODO list.\n"
)
else:
print(
"We could not detect an OSI-approved license for "
f"{package.name}@{package.version}: {package.license}"
f"{pkg.name}@{pkg.version}: {get_license_str(pkg)}\n"
"Please update the package version on the TODO list.\n"
)
print()
exit_code = 1
elif approved and package.name in EXCEPTIONS:
exit_code = 1

for pkg, status in license_status.values():
if status is False and pkg.name not in EXCEPTIONS_AND_TODOS:
print(
"Approved license detected for "
f"{package.name}@{package.version}: {package.license}"
"We could not detect an OSI-approved license for "
f"{pkg.name}@{pkg.version}: {get_license_str(pkg)}\n"
)
print(f"Please remove the package from the EXCEPTIONS list: {package.name}")
print()
exit_code = 1
current_packages = {package.name for package in package_definitions}
for package in [*TODO.keys(), *EXCEPTIONS]:
if package not in current_packages:
if status is True and pkg.name in EXCEPTIONS:
print(
f"Package {package} is tracked, but not used. Please remove from the licenses.py"
"file."
f"Approved license detected for "
f"{pkg.name}@{pkg.version}: {get_license_str(pkg)}\n"
f"Please remove the package from the EXCEPTIONS list.\n"
)
print()
exit_code = 1

for name in EXCEPTIONS_AND_TODOS.difference(license_status):
print(
f"Package {name} is tracked, but not used. "
"Please remove it from the licenses.py file.\n"
)
exit_code = 1

return exit_code


def check_license_status(package: PackageDefinition) -> bool:
"""Check if package licenses is OSI approved."""
for approved_license in OSI_APPROVED_LICENSES:
if approved_license in package.license:
return True
return False


def get_license_str(package: PackageDefinition) -> str:
"""Return license string."""
return f"{package.license}"


def extract_licenses(args: ExtractArgs) -> int:
"""Extract license data for installed packages."""
licenses = sorted(
Expand Down

0 comments on commit 8fb7a7e

Please sign in to comment.