|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import json |
| 4 | +import re |
| 5 | +import shutil |
| 6 | +from pathlib import Path |
| 7 | + |
| 8 | +from setuptools import setup |
| 9 | +from setuptools.command.build_py import build_py as _build_py |
| 10 | + |
| 11 | + |
| 12 | +ROOT = Path(__file__).parent |
| 13 | + |
| 14 | + |
| 15 | +def read_package_json() -> dict: |
| 16 | + return json.loads((ROOT / "package.json").read_text(encoding="utf-8")) |
| 17 | + |
| 18 | + |
| 19 | +def python_version(npm_version: str) -> str: |
| 20 | + if re.fullmatch(r"\d+\.\d+\.\d+", npm_version): |
| 21 | + return npm_version |
| 22 | + |
| 23 | + dated_next = re.fullmatch(r"(\d+\.\d+\.\d+)-next\.(\d{8})", npm_version) |
| 24 | + if dated_next: |
| 25 | + return f"{dated_next.group(1)}.dev{dated_next.group(2)}" |
| 26 | + |
| 27 | + numbered_next = re.fullmatch(r"(\d+\.\d+\.\d+)-next\.(\d+)", npm_version) |
| 28 | + if numbered_next: |
| 29 | + return f"{numbered_next.group(1)}.dev{numbered_next.group(2)}" |
| 30 | + |
| 31 | + raise ValueError( |
| 32 | + f"{npm_version!r} is not a supported PyPI version. " |
| 33 | + "Use a release version like 1.3.2 or a next prerelease like " |
| 34 | + "1.3.2-next.20260628." |
| 35 | + ) |
| 36 | + |
| 37 | + |
| 38 | +class build_py(_build_py): |
| 39 | + def run(self) -> None: |
| 40 | + super().run() |
| 41 | + package_root = Path(self.build_lib) / "opencc_data" |
| 42 | + self.copy_tree(ROOT / "data", package_root / "data") |
| 43 | + self.copy_tree(ROOT / "test-data", package_root / "test_data") |
| 44 | + |
| 45 | + @staticmethod |
| 46 | + def copy_tree(src: Path, dst: Path) -> None: |
| 47 | + if dst.exists(): |
| 48 | + shutil.rmtree(dst) |
| 49 | + shutil.copytree(src, dst) |
| 50 | + |
| 51 | + |
| 52 | +package_json = read_package_json() |
| 53 | + |
| 54 | +setup( |
| 55 | + name="opencc-data", |
| 56 | + version=python_version(package_json["version"]), |
| 57 | + description="OpenCC dictionary data, configs, and test data", |
| 58 | + long_description=(ROOT / "README.md").read_text(encoding="utf-8"), |
| 59 | + long_description_content_type="text/markdown", |
| 60 | + author="The Ngiox Khyen 2028 Project", |
| 61 | + license="Apache-2.0", |
| 62 | + url="https://github.com/nk2028/opencc-data", |
| 63 | + project_urls={ |
| 64 | + "Homepage": "https://github.com/nk2028/opencc-data", |
| 65 | + "Bug Tracker": "https://github.com/nk2028/opencc-data/issues", |
| 66 | + "Source": "https://github.com/nk2028/opencc-data", |
| 67 | + }, |
| 68 | + packages=["opencc_data"], |
| 69 | + include_package_data=True, |
| 70 | + python_requires=">=3.9", |
| 71 | + classifiers=[ |
| 72 | + "Development Status :: 4 - Beta", |
| 73 | + "Intended Audience :: Developers", |
| 74 | + "Natural Language :: Chinese (Simplified)", |
| 75 | + "Natural Language :: Chinese (Traditional)", |
| 76 | + "Programming Language :: Python :: 3", |
| 77 | + "Programming Language :: Python :: 3 :: Only", |
| 78 | + "Topic :: Text Processing :: Linguistic", |
| 79 | + ], |
| 80 | + cmdclass={"build_py": build_py}, |
| 81 | +) |
0 commit comments