Add dependencies licenses check.

This commit is contained in:
jcamiel 2022-11-17 22:26:44 +01:00
parent 23e66e0158
commit c7f6795f14
No known key found for this signature in database
GPG Key ID: 07FF11CFD55356CC
2 changed files with 89 additions and 0 deletions

View File

@ -55,6 +55,9 @@ jobs:
- name: Run crates update
run: bin/check/crates.sh
- name: Check crates licence
run: python3 bin/check/license.py
- name: Check CHANGELOG
run: bin/check/changelog.sh

86
bin/check/license.py Executable file
View File

@ -0,0 +1,86 @@
#!/usr/bin/env python3
from typing import List, Tuple
import json
import subprocess
def main():
deps = get_deps()
check_licenses(deps)
def is_authorized(license: str) -> bool:
for l in ["MIT", "Apache-2.0", "Zlib", "CC0-1.0", "MPL-2.0", "BSD-3-Clause"]:
if l in license:
return True
return False
def is_forbidden(license: str) -> bool:
for l in ["GNU"]:
if l in license:
return True
return False
def check_licenses(deps: List[Tuple[str, str, str, str]]):
authorized = []
forbidden = []
unknown = []
for dep in deps:
lic = dep[3]
if is_authorized(lic):
authorized.append(dep)
elif is_forbidden(lic):
forbidden.append(dep)
else:
unknown.append(dep)
print("Authorized:")
for (name, repository, version, lic) in authorized:
name_str = f"\x1b[1;34m{name}\x1b[0m"
lic_str = f"\x1b[1;32m{lic}\x1b[0m"
print(f" {name_str} {version} {repository}: {lic_str}")
print("Forbidden:")
for (name, repository, version, lic) in forbidden:
name_str = f"\x1b[1;34m{name}\x1b[0m"
lic_str = f"\x1b[1;31m{lic}\x1b[0m"
print(f" {name_str} {version} {repository}: {lic_str}")
print("Unknown:")
for (name, repository, version, lic) in forbidden:
name_str = f"\x1b[1;34m{name}\x1b[0m"
lic_str = f"\x1b[1;33m{lic}\x1b[0m"
print(f" {name_str} {version} {repository}: {lic_str}")
if len(forbidden) > 0 or len(unknown) > 0:
print("There are not authorized licenses")
exit(1)
def get_deps() -> List[Tuple[str, str, str, str]]:
"""Returns a list of crates name and licenses"""
p = subprocess.run(
[
"cargo",
"metadata",
"--format-version",
"1",
],
capture_output=True,
text=True,
)
if p.returncode != 0:
print("Error calling cargo metadata")
exit(1)
data = json.loads(p.stdout)
packages = data["packages"]
licenses = [
(p["name"], p["repository"], p["version"], p["license"]) for p in packages
]
return licenses
if __name__ == "__main__":
main()