| 1 |
import re |
| 2 |
import os |
| •3 |
from packaging.requirements import Requirement |
| 4 |
import tomllib |
| 5 |
import json |
| 6 |
import sys |
| 7 |
|
| 8 |
|
| 9 |
if __name__ == "__main__": |
| 10 |
if not len(sys.argv) == 2: |
| 11 |
raise ValueError("Usage: python create_codemeta.py <version>") |
| 12 |
basedir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..") |
| 13 |
|
| 14 |
with open(os.path.join(basedir, "pyproject.toml"), "rb") as f: |
| 15 |
pyproject = tomllib.load(f) |
| 16 |
with open(os.path.join(basedir, ".codemeta", "codemeta_base.json"), "r", encoding="utf-8") as f: |
| 17 |
codemeta = json.load(f) |
| 18 |
with open(os.path.join(basedir, "CONTRIBUTORS"), "r", encoding="utf-8") as f: |
| 19 |
contributors = f.read().splitlines()[4:] # Skip comments at the beginning |
| 20 |
|
| 21 |
# Add software requirements from pyproject.toml |
| •22 |
parsed_deps = [Requirement(dep) for dep in pyproject['project']['dependencies']] |
| 23 |
codemeta["softwareRequirements"] = [] |
| 24 |
for dep in parsed_deps: |
| •25 |
version = ",".join(f"{spec.operator}{spec.version}" for spec in dep.specifier) |
| 26 |
requirement = {"name": dep.name, "@type": "SoftwareApplication", "runtimePlatform": "Python 3"} |
| 27 |
if version: |
| 28 |
requirement["version"] = version |
| 29 |
codemeta["softwareRequirements"].append(requirement) |
| 30 |
|
| 31 |
# Add contributors from AUTHORS |
| 32 |
codemeta["contributor"] = [] |
| 33 |
for contributor in contributors: |
| 34 |
matches = re.match(r"^(\w[\w-]*?) ([\w]+[.]? )??(\w+) \(@(.*)\)$", contributor) |
| 35 |
if not matches: |
| 36 |
raise ValueError("author not matched:", contributor) |
| 37 |
given_name, middle_name, family_name, github = matches.groups() |
| 38 |
|
| 39 |
contributor = {"@type": "Person", "givenName": given_name, "familyName": family_name, "identifier": f"https://github.com/{github}"} |
| 40 |
# FIXME: additionalName does not seem to be recognized by codemeta (validation fails) |
| 41 |
if middle_name: |
| 42 |
contributor["givenName"] += " " + middle_name.strip() |
| 43 |
codemeta["contributor"].append(contributor) |
| 44 |
|
| 45 |
# Add version from setuptools_scm |
| 46 |
version = sys.argv[1] |
| 47 |
codemeta["version"] = version |
| 48 |
codemeta["softwareVersion"] = version |
| 49 |
|
| 50 |
# Write codemeta.json |
| 51 |
with open(os.path.join(basedir, "codemeta.json"), "w", encoding="utf-8") as f: |
| 52 |
json.dump(codemeta, f, indent=2, ensure_ascii=False) |