weblate/scripts/generate-license-data
2022-01-07 10:36:58 +01:00

103 lines
3.2 KiB
Python
Executable file
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env python3
#
# Copyright © 20122022 Michal Čihař <michal@cihar.com>
#
# This file is part of Weblate <https://weblate.org/>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
"""
Helper script to generate Python code for licenses.
See https://github.com/spdx/license-list-data
"""
import json
import subprocess
HEADER = '''#
# Copyright © 20122022 Michal Čihař <michal@cihar.com>
#
# This file is part of Weblate <https://weblate.org/>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
"""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(["black", "weblate/utils/licensedata.py"])