|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +# -*- mode: python -*- |
| 4 | +# vi: set ft=python : |
| 5 | + |
| 6 | +# pylint: disable=C0103 |
| 7 | + |
| 8 | +# Copyright (C) 2024 The C++ Plus Project. |
| 9 | +# This file is part of the Rubisco Extension Repository. |
| 10 | +# |
| 11 | +# Rubisco Extension Repository is free software: you can redistribute it and/or |
| 12 | +# modify it under the terms of the GNU General Public License as published |
| 13 | +# by the Free Software Foundation, either version 3 of the License, |
| 14 | +# or (at your option) any later version. |
| 15 | +# |
| 16 | +# Rubisco Extension Repository is distributed in the hope that it will be |
| 17 | +# useful, but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | +# GNU General Public License for more details. |
| 20 | +# |
| 21 | +# You should have received a copy of the GNU General Public License |
| 22 | +# along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 23 | + |
| 24 | +"""Utility script to trim the license metadata JSON file.""" |
| 25 | + |
| 26 | +import json |
| 27 | +import sys |
| 28 | +from pathlib import Path |
| 29 | +from typing import cast |
| 30 | + |
| 31 | + |
| 32 | +def get_typecheck[T](obj: object, expected_type: type[T]) -> T: |
| 33 | + """Check the type of an object and return it if it matches. |
| 34 | +
|
| 35 | + Returns: |
| 36 | + T: The object cast to the expected type. |
| 37 | +
|
| 38 | + """ |
| 39 | + if not isinstance(obj, expected_type): |
| 40 | + sys.exit(f"Error: Expected {expected_type}, got {type(obj)}.") |
| 41 | + return obj |
| 42 | + |
| 43 | + |
| 44 | +if __name__ == "__main__": |
| 45 | + if len(sys.argv) != 4: # noqa: PLR2004 |
| 46 | + sys.exit("Usage: licenses-meta.py metafile htmlfile destfile") |
| 47 | + src_file = Path(sys.argv[1]) |
| 48 | + license_html = Path(sys.argv[2]) |
| 49 | + dest_file = Path(sys.argv[3]) |
| 50 | + with ( |
| 51 | + src_file.open(encoding="utf-8") as in_file, |
| 52 | + dest_file.open("w", encoding="utf-8") as out_file, |
| 53 | + ): |
| 54 | + data_ = json.load(in_file) |
| 55 | + if not isinstance(data_, dict): |
| 56 | + sys.exit(f"Error: {src_file} does not contain a JSON object.") |
| 57 | + data = cast("dict[str, object]", data_) |
| 58 | + out_data: dict[str, str | bool] = {} |
| 59 | + out_data["isFsfLibre"] = get_typecheck( |
| 60 | + data.get("isFsfLibre", False), |
| 61 | + bool, |
| 62 | + ) |
| 63 | + out_data["name"] = get_typecheck(data["name"], str) |
| 64 | + out_data["isOsiApproved"] = get_typecheck( |
| 65 | + data.get("isOsiApproved", False), |
| 66 | + bool, |
| 67 | + ) |
| 68 | + license_id = get_typecheck(data["licenseId"], str) |
| 69 | + out_data["licenseHtmlText"] = license_html.read_text(encoding="utf-8") |
| 70 | + json.dump(out_data, out_file, ensure_ascii=False, sort_keys=True) |
0 commit comments