Skip to content

Commit

Permalink
Add SPDX license data
Browse files Browse the repository at this point in the history
  • Loading branch information
ofek committed May 10, 2024
1 parent 32deafe commit 4ab216d
Show file tree
Hide file tree
Showing 6 changed files with 812 additions and 1 deletion.
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ exclude = ["docs/_build", "tests/manylinux/build-hello-world.sh", "tests/musllin

[tool.coverage.run]
branch = true
omit = ["src/packaging/_spdx.py"]

[tool.coverage.report]
exclude_lines = ["pragma: no cover", "@abc.abstractmethod", "@abc.abstractproperty"]
Expand Down Expand Up @@ -98,3 +99,4 @@ ignore = [
"ISC001",
"ISC002",
]
per-file-ignores = { "src/packaging/_spdx.py" = ["E501"] }
715 changes: 715 additions & 0 deletions src/packaging/_spdx.py

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion tasks/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import invoke

from . import check
from . import check, licenses

ns = invoke.Collection(check)
ns.add_collection(licenses)
91 changes: 91 additions & 0 deletions tasks/licenses.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import json
import time
from contextlib import closing
from io import StringIO

import httpx
import invoke

from .paths import SPDX_LICENSES

LATEST_API = "https://api.github.com/repos/spdx/license-list-data/releases/latest"
LICENSES_URL = (
"https://raw.githubusercontent.com/spdx/license-list-data/v{}/json/licenses.json"
)
EXCEPTIONS_URL = (
"https://raw.githubusercontent.com/spdx/license-list-data/v{}/json/exceptions.json"
)


def download_data(url):
for _ in range(600):
try:
response = httpx.get(url)
response.raise_for_status()
except Exception: # noqa: BLE001
time.sleep(1)
continue
else:
return json.loads(response.content.decode("utf-8"))

message = "Download failed"
raise ConnectionError(message)


@invoke.task
def update(ctx):
print("Updating SPDX licenses...")

latest_version = download_data(LATEST_API)["tag_name"][1:]
print(f"Latest version: {latest_version}")

license_payload = download_data(LICENSES_URL.format(latest_version))["licenses"]
print(f"Licenses: {len(license_payload)}")

exception_payload = download_data(EXCEPTIONS_URL.format(latest_version))[
"exceptions"
]
print(f"Exceptions: {len(exception_payload)}")

licenses = {}
for license_data in license_payload:
license_id = license_data["licenseId"]
license_key = license_id.casefold()
deprecated = license_data["isDeprecatedLicenseId"]
licenses[license_key] = {"id": license_id, "deprecated": deprecated}

exceptions = {}
for exception_data in exception_payload:
exception_id = exception_data["licenseExceptionId"]
exception_key = exception_id.casefold()
deprecated = exception_data["isDeprecatedLicenseId"]
exceptions[exception_key] = {"id": exception_id, "deprecated": deprecated}

with closing(StringIO()) as file_contents:
file_contents.write(
f"""\
from __future__ import annotations
VERSION = {latest_version!r}
# fmt: off
LICENSES: dict[str, dict[str, str | bool]] = {{
"""
)

for normalized_name, data in sorted(licenses.items()):
file_contents.write(f" {normalized_name!r}: {data!r},\n")

file_contents.write("}\n\nEXCEPTIONS: dict[str, dict[str, str | bool]] = {\n")

for normalized_name, data in sorted(exceptions.items()):
file_contents.write(f" {normalized_name!r}: {data!r},\n")

file_contents.write("}\n# fmt: on\n")

# Replace default Python single quotes with double quotes to adhere
# to this project's desired formatting
contents = file_contents.getvalue().replace("'", '"')

with open(SPDX_LICENSES, "w", encoding="utf-8") as f:
f.write(contents)
1 change: 1 addition & 0 deletions tasks/paths.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
PROJECT = os.path.abspath(os.path.dirname(os.path.dirname(__file__)))

CACHE = os.path.join(PROJECT, ".cache")
SPDX_LICENSES = os.path.join(PROJECT, "src", "packaging", "_spdx.py")
1 change: 1 addition & 0 deletions tasks/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# The requirements required to invoke the tasks
invoke
progress
httpx

0 comments on commit 4ab216d

Please sign in to comment.