Skip to content

Commit 8fb7a7e

Browse files
authored
Refactor licenses check (#129194)
1 parent c5ed148 commit 8fb7a7e

File tree

1 file changed

+54
-39
lines changed

1 file changed

+54
-39
lines changed

script/licenses.py

Lines changed: 54 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -178,62 +178,77 @@ def from_dict(cls, data: PackageMetadata) -> PackageDefinition:
178178
), # https://github.com/aio-libs/aiocache/blob/master/LICENSE all rights reserved?
179179
}
180180

181+
EXCEPTIONS_AND_TODOS = EXCEPTIONS.union(TODO)
182+
181183

182184
def check_licenses(args: CheckArgs) -> int:
183185
"""Check licenses are OSI approved."""
184186
exit_code = 0
185187
raw_licenses = json.loads(Path(args.path).read_text())
186-
package_definitions = [PackageDefinition.from_dict(data) for data in raw_licenses]
187-
for package in package_definitions:
188-
previous_unapproved_version = TODO.get(package.name)
189-
approved = False
190-
for approved_license in OSI_APPROVED_LICENSES:
191-
if approved_license in package.license:
192-
approved = True
193-
break
194-
if previous_unapproved_version is not None:
195-
if previous_unapproved_version < package.version:
196-
if approved:
197-
print(
198-
"Approved license detected for "
199-
f"{package.name}@{package.version}: {package.license}"
200-
)
201-
print("Please remove the package from the TODO list.")
202-
print()
203-
else:
204-
print(
205-
"We could not detect an OSI-approved license for "
206-
f"{package.name}@{package.version}: {package.license}"
207-
)
208-
print()
209-
exit_code = 1
210-
elif not approved and package.name not in EXCEPTIONS:
188+
license_status = {
189+
pkg.name: (pkg, check_license_status(pkg))
190+
for data in raw_licenses
191+
if (pkg := PackageDefinition.from_dict(data))
192+
}
193+
194+
for name, version in TODO.items():
195+
pkg, status = license_status.get(name, (None, None))
196+
if pkg is None or not (version < pkg.version):
197+
continue
198+
assert status is not None
199+
200+
if status is True:
201+
print(
202+
f"Approved license detected for "
203+
f"{pkg.name}@{pkg.version}: {get_license_str(pkg)}\n"
204+
"Please remove the package from the TODO list.\n"
205+
)
206+
else:
211207
print(
212208
"We could not detect an OSI-approved license for "
213-
f"{package.name}@{package.version}: {package.license}"
209+
f"{pkg.name}@{pkg.version}: {get_license_str(pkg)}\n"
210+
"Please update the package version on the TODO list.\n"
214211
)
215-
print()
216-
exit_code = 1
217-
elif approved and package.name in EXCEPTIONS:
212+
exit_code = 1
213+
214+
for pkg, status in license_status.values():
215+
if status is False and pkg.name not in EXCEPTIONS_AND_TODOS:
218216
print(
219-
"Approved license detected for "
220-
f"{package.name}@{package.version}: {package.license}"
217+
"We could not detect an OSI-approved license for "
218+
f"{pkg.name}@{pkg.version}: {get_license_str(pkg)}\n"
221219
)
222-
print(f"Please remove the package from the EXCEPTIONS list: {package.name}")
223-
print()
224220
exit_code = 1
225-
current_packages = {package.name for package in package_definitions}
226-
for package in [*TODO.keys(), *EXCEPTIONS]:
227-
if package not in current_packages:
221+
if status is True and pkg.name in EXCEPTIONS:
228222
print(
229-
f"Package {package} is tracked, but not used. Please remove from the licenses.py"
230-
"file."
223+
f"Approved license detected for "
224+
f"{pkg.name}@{pkg.version}: {get_license_str(pkg)}\n"
225+
f"Please remove the package from the EXCEPTIONS list.\n"
231226
)
232-
print()
233227
exit_code = 1
228+
229+
for name in EXCEPTIONS_AND_TODOS.difference(license_status):
230+
print(
231+
f"Package {name} is tracked, but not used. "
232+
"Please remove it from the licenses.py file.\n"
233+
)
234+
exit_code = 1
235+
234236
return exit_code
235237

236238

239+
def check_license_status(package: PackageDefinition) -> bool:
240+
"""Check if package licenses is OSI approved."""
241+
for approved_license in OSI_APPROVED_LICENSES:
242+
if approved_license in package.license:
243+
return True
244+
return False
245+
246+
247+
def get_license_str(package: PackageDefinition) -> str:
248+
"""Return license string."""
249+
return f"{package.license}"
250+
251+
237252
def extract_licenses(args: ExtractArgs) -> int:
238253
"""Extract license data for installed packages."""
239254
licenses = sorted(

0 commit comments

Comments
 (0)