weblate/scripts/generate-license-data
Michal Čihař b1d0c0715d chore: Consolidate copyright headers
SPDX-FileCopyrightText was mistakenly used on some files.
2023-01-11 09:25:24 +01:00

76 lines
2 KiB
Python
Executable file

#!/usr/bin/env python3
# Copyright © Michal Čihař <michal@weblate.org>
#
# SPDX-License-Identifier: GPL-3.0-or-later
"""
Helper script to generate Python code for licenses.
See https://github.com/spdx/license-list-data
"""
import json
import subprocess
HEADER = '''# Copyright © Michal Čihař <michal@weblate.org>
#
# SPDX-License-Identifier: GPL-3.0-or-later
"""License data definitions.
This is an automatically generated file, see scripts/generate-license-data
"""
'''
TEMPLATE = """ (
"{0}",
"{1}",
"{2}",
{3}
),
"""
# Some licenses are not classified as libre in SPDX
EXCEPTIONS = {
# DFSG compatible, see https://wiki.debian.org/DFSGLicenses
"CC-BY-3.0",
"CC-BY-SA-3.0",
# Public domain, see https://fedoraproject.org/wiki/Licensing/Beerware
"Beerware",
}
with open("scripts/spdx-license-list/json/licenses.json") as handle:
data = json.load(handle)
with open("weblate/utils/licensedata.py", "w") as output:
output.write(HEADER)
output.write("LICENSES = (\n")
for license in sorted(data["licenses"], key=lambda x: x["name"].lower()):
if license["isDeprecatedLicenseId"]:
continue
with open(
f"scripts/spdx-license-list/json/details/{license['licenseId']}.json"
) as handle:
details = json.load(handle)
libre = (
details.get("isFsfLibre", False)
or details["isOsiApproved"]
or license.get("isFsfLibre", False)
or license["isOsiApproved"]
or license["licenseId"] in EXCEPTIONS
)
output.write(
TEMPLATE.format(
license["licenseId"],
license["name"].replace('"', '\\"'),
license["reference"],
"True" if libre else "False",
)
)
output.write(")\n")
# Apply coding style
subprocess.run(
["pre-commit", "run", "black", "--files", "weblate/utils/licensedata.py"]
)