|
| 1 | +"""Sync package metadata from codemeta.json into pyproject.toml. |
| 2 | +
|
| 3 | +Run by the CodeMeta2CFF workflow whenever codemeta.json changes. tomlkit is used |
| 4 | +rather than a plain rewrite so that comments, key order, and formatting in |
| 5 | +pyproject.toml survive the update. |
| 6 | +""" |
| 7 | + |
| 8 | +import json |
| 9 | + |
| 10 | +import tomlkit |
| 11 | + |
| 12 | +CODEMETA = "codemeta.json" |
| 13 | +PYPROJECT = "pyproject.toml" |
| 14 | + |
| 15 | + |
| 16 | +def people(entries): |
| 17 | + """Convert codemeta Person entries into PEP 621 name/email tables.""" |
| 18 | + if isinstance(entries, dict): |
| 19 | + entries = [entries] |
| 20 | + array = tomlkit.array().multiline(True) |
| 21 | + for person in entries or []: |
| 22 | + names = (person.get("givenName"), person.get("familyName")) |
| 23 | + name = " ".join(part for part in names if part) |
| 24 | + if not name: |
| 25 | + continue |
| 26 | + entry = tomlkit.inline_table() |
| 27 | + entry["name"] = name |
| 28 | + if person.get("email"): |
| 29 | + entry["email"] = person["email"] |
| 30 | + array.append(entry) |
| 31 | + return array |
| 32 | + |
| 33 | + |
| 34 | +def main(): |
| 35 | + with open(CODEMETA) as handle: |
| 36 | + codemeta = json.load(handle) |
| 37 | + |
| 38 | + with open(PYPROJECT) as handle: |
| 39 | + document = tomlkit.parse(handle.read()) |
| 40 | + |
| 41 | + project = document["project"] |
| 42 | + |
| 43 | + for key in ("name", "version", "description"): |
| 44 | + if codemeta.get(key): |
| 45 | + project[key] = codemeta[key] |
| 46 | + |
| 47 | + for key, source in (("authors", "author"), ("maintainers", "maintainer")): |
| 48 | + values = people(codemeta.get(source)) |
| 49 | + if len(values): |
| 50 | + project[key] = values |
| 51 | + |
| 52 | + urls = project.get("urls") |
| 53 | + if urls is not None: |
| 54 | + repository = codemeta.get("codeRepository") or codemeta.get("url") |
| 55 | + if repository: |
| 56 | + urls["Homepage"] = repository |
| 57 | + urls["Repository"] = repository |
| 58 | + if codemeta.get("issueTracker"): |
| 59 | + urls["Issues"] = codemeta["issueTracker"] |
| 60 | + |
| 61 | + with open(PYPROJECT, "w") as handle: |
| 62 | + handle.write(tomlkit.dumps(document)) |
| 63 | + |
| 64 | + |
| 65 | +if __name__ == "__main__": |
| 66 | + main() |
0 commit comments