diff --git a/build_util/generate_licenses.py b/build_util/generate_licenses.py index 6ee30aa14..82b2f7250 100644 --- a/build_util/generate_licenses.py +++ b/build_util/generate_licenses.py @@ -30,14 +30,17 @@ def __init__( # ライセンステキストをローカルのライセンスファイルから抽出する self.license_text = Path(license_text).read_text(encoding="utf8") elif license_text_type == "remote_address": - # ライセンステキストをリモートのライセンスファイルから抽出する - with urllib.request.urlopen(license_text) as res: - _license_text: str = res.read().decode() - self.license_text = _license_text + replace_license_text(self, license_text) else: raise Exception("型で保護され実行されないはずのパスが実行されました") +def replace_license_text(license: License, text_url: str) -> None: + """ライセンステキストを URL が指すテキストで置き換える。""" + with urllib.request.urlopen(text_url) as res: + license.license_text = res.read().decode() + + def generate_licenses() -> list[License]: licenses: list[License] = [] @@ -81,73 +84,51 @@ def generate_licenses() -> list[License]: raise LicenseError( f"ライセンス違反: {license.package_name} is {license.license_name}" ) + + package_name = license.package_name.lower() + # FIXME: assert license type if license.license_text == "UNKNOWN": - if ( - license.package_name.lower() == "core" - and license.package_version == "0.0.0" - ): + if package_name == "core" and license.package_version == "0.0.0": continue - elif license.package_name.lower() == "future": - with urllib.request.urlopen( - "https://raw.githubusercontent.com/PythonCharmers/python-future/master/LICENSE.txt" # noqa: B950 - ) as res: - license.license_text = res.read().decode() - elif license.package_name.lower() == "pefile": - with urllib.request.urlopen( - "https://raw.githubusercontent.com/erocarrera/pefile/master/LICENSE" # noqa: B950 - ) as res: - license.license_text = res.read().decode() - elif license.package_name.lower() == "pyopenjtalk": - with urllib.request.urlopen( - "https://raw.githubusercontent.com/r9y9/pyopenjtalk/master/LICENSE.md" - ) as res: - license.license_text = res.read().decode() - elif license.package_name.lower() == "python-multipart": - with urllib.request.urlopen( - "https://raw.githubusercontent.com/andrew-d/python-multipart/master/LICENSE.txt" # noqa: B950 - ) as res: - license.license_text = res.read().decode() - elif license.package_name.lower() == "romkan": - with urllib.request.urlopen( - "https://raw.githubusercontent.com/soimort/python-romkan/master/LICENSE" - ) as res: - license.license_text = res.read().decode() - elif license.package_name.lower() == "distlib": - with urllib.request.urlopen( - "https://bitbucket.org/pypa/distlib/raw/7d93712134b28401407da27382f2b6236c87623a/LICENSE.txt" # noqa: B950 - ) as res: - license.license_text = res.read().decode() - elif license.package_name.lower() == "jsonschema": - with urllib.request.urlopen( - "https://raw.githubusercontent.com/python-jsonschema/jsonschema/dbc398245a583cb2366795dc529ae042d10c1577/COPYING" - ) as res: - license.license_text = res.read().decode() - elif license.package_name.lower() == "lockfile": - with urllib.request.urlopen( - "https://opendev.org/openstack/pylockfile/raw/tag/0.12.2/LICENSE" - ) as res: - license.license_text = res.read().decode() - elif license.package_name.lower() == "platformdirs": - with urllib.request.urlopen( - "https://raw.githubusercontent.com/platformdirs/platformdirs/aa671aaa97913c7b948567f4d9c77d4f98bfa134/LICENSE" - ) as res: - license.license_text = res.read().decode() - elif license.package_name.lower() == "webencodings": - with urllib.request.urlopen( - "https://raw.githubusercontent.com/gsnedders/python-webencodings/fa2cb5d75ab41e63ace691bc0825d3432ba7d694/LICENSE" - ) as res: - license.license_text = res.read().decode() + elif package_name == "future": + text_url = "https://raw.githubusercontent.com/PythonCharmers/python-future/master/LICENSE.txt" # noqa: B950 + replace_license_text(license, text_url) + elif package_name == "pefile": + text_url = "https://raw.githubusercontent.com/erocarrera/pefile/master/LICENSE" # noqa: B950 + replace_license_text(license, text_url) + elif package_name == "pyopenjtalk": + text_url = "https://raw.githubusercontent.com/r9y9/pyopenjtalk/master/LICENSE.md" # noqa: B950 + replace_license_text(license, text_url) + elif package_name == "python-multipart": + text_url = "https://raw.githubusercontent.com/andrew-d/python-multipart/master/LICENSE.txt" # noqa: B950 + replace_license_text(license, text_url) + elif package_name == "romkan": + text_url = "https://raw.githubusercontent.com/soimort/python-romkan/master/LICENSE" # noqa: B950 + replace_license_text(license, text_url) + elif package_name == "distlib": + text_url = "https://bitbucket.org/pypa/distlib/raw/7d93712134b28401407da27382f2b6236c87623a/LICENSE.txt" # noqa: B950 + replace_license_text(license, text_url) + elif package_name == "jsonschema": + text_url = "https://raw.githubusercontent.com/python-jsonschema/jsonschema/dbc398245a583cb2366795dc529ae042d10c1577/COPYING" # noqa: B950 + replace_license_text(license, text_url) + elif package_name == "lockfile": + text_url = "https://opendev.org/openstack/pylockfile/raw/tag/0.12.2/LICENSE" # noqa: B950 + replace_license_text(license, text_url) + elif package_name == "platformdirs": + text_url = "https://raw.githubusercontent.com/platformdirs/platformdirs/aa671aaa97913c7b948567f4d9c77d4f98bfa134/LICENSE" # noqa: B950 + replace_license_text(license, text_url) + elif package_name == "webencodings": + text_url = "https://raw.githubusercontent.com/gsnedders/python-webencodings/fa2cb5d75ab41e63ace691bc0825d3432ba7d694/LICENSE" # noqa: B950 + replace_license_text(license, text_url) else: # ライセンスがpypiに無い raise Exception(f"No License info provided for {license.package_name}") # soxr - if license.package_name.lower() == "soxr": - with urllib.request.urlopen( - "https://raw.githubusercontent.com/dofuuz/python-soxr/v0.3.6/LICENSE.txt" - ) as res: - license.license_text = res.read().decode() + if package_name == "soxr": + text_url = "https://raw.githubusercontent.com/dofuuz/python-soxr/v0.3.6/LICENSE.txt" # noqa: B950 + replace_license_text(license, text_url) licenses.append(license)