Refactor licenses check (#129194)

pull/129202/head
Marc Mueller 2024-10-26 10:30:10 +02:00 committed by GitHub
parent c5ed148c52
commit 8fb7a7e4cd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 54 additions and 39 deletions

View File

@ -178,62 +178,77 @@ TODO = {
), # https://github.com/aio-libs/aiocache/blob/master/LICENSE all rights reserved? ), # https://github.com/aio-libs/aiocache/blob/master/LICENSE all rights reserved?
} }
EXCEPTIONS_AND_TODOS = EXCEPTIONS.union(TODO)
def check_licenses(args: CheckArgs) -> int: def check_licenses(args: CheckArgs) -> int:
"""Check licenses are OSI approved.""" """Check licenses are OSI approved."""
exit_code = 0 exit_code = 0
raw_licenses = json.loads(Path(args.path).read_text()) raw_licenses = json.loads(Path(args.path).read_text())
package_definitions = [PackageDefinition.from_dict(data) for data in raw_licenses] license_status = {
for package in package_definitions: pkg.name: (pkg, check_license_status(pkg))
previous_unapproved_version = TODO.get(package.name) for data in raw_licenses
approved = False if (pkg := PackageDefinition.from_dict(data))
for approved_license in OSI_APPROVED_LICENSES: }
if approved_license in package.license:
approved = True for name, version in TODO.items():
break pkg, status = license_status.get(name, (None, None))
if previous_unapproved_version is not None: if pkg is None or not (version < pkg.version):
if previous_unapproved_version < package.version: continue
if approved: assert status is not None
print(
"Approved license detected for " if status is True:
f"{package.name}@{package.version}: {package.license}" print(
) f"Approved license detected for "
print("Please remove the package from the TODO list.") f"{pkg.name}@{pkg.version}: {get_license_str(pkg)}\n"
print() "Please remove the package from the TODO list.\n"
else: )
print( else:
"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:
print( print(
"We could not detect an OSI-approved license for " "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
exit_code = 1
elif approved and package.name in EXCEPTIONS: for pkg, status in license_status.values():
if status is False and pkg.name not in EXCEPTIONS_AND_TODOS:
print( print(
"Approved license detected for " "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"
) )
print(f"Please remove the package from the EXCEPTIONS list: {package.name}")
print()
exit_code = 1 exit_code = 1
current_packages = {package.name for package in package_definitions} if status is True and pkg.name in EXCEPTIONS:
for package in [*TODO.keys(), *EXCEPTIONS]:
if package not in current_packages:
print( print(
f"Package {package} is tracked, but not used. Please remove from the licenses.py" f"Approved license detected for "
"file." f"{pkg.name}@{pkg.version}: {get_license_str(pkg)}\n"
f"Please remove the package from the EXCEPTIONS list.\n"
) )
print()
exit_code = 1 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 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: def extract_licenses(args: ExtractArgs) -> int:
"""Extract license data for installed packages.""" """Extract license data for installed packages."""
licenses = sorted( licenses = sorted(