|
| 1 | +#!/usr/bin/env pipx run |
| 2 | +# /// script |
| 3 | +# requires-python = ">=3.9" |
| 4 | +# dependencies = [ |
| 5 | +# "click ~= 8.0", |
| 6 | +# "in-place ~= 1.0", |
| 7 | +# ] |
| 8 | +# /// |
| 9 | + |
| 10 | +from __future__ import annotations |
| 11 | +import os |
| 12 | +from pathlib import Path |
| 13 | +import shlex |
| 14 | +import subprocess |
| 15 | +import sys |
| 16 | +from typing import Any |
| 17 | +import click |
| 18 | +from in_place import InPlace |
| 19 | + |
| 20 | + |
| 21 | +@click.command() |
| 22 | +@click.option("--git/--no-git", default=True) |
| 23 | +@click.argument( |
| 24 | + "dirpath", |
| 25 | + type=click.Path(exists=True, file_okay=False, path_type=Path), |
| 26 | + default=os.curdir, |
| 27 | +) |
| 28 | +def main(dirpath: Path, git: bool) -> None: |
| 29 | + update_pyproject(dirpath) |
| 30 | + if git: |
| 31 | + log("Committing ...") |
| 32 | + runcmd( |
| 33 | + "git", "commit", "-m", "Update for PEP 639", "pyproject.toml", cwd=dirpath |
| 34 | + ) |
| 35 | + |
| 36 | + |
| 37 | +def update_pyproject(dirpath: Path) -> None: |
| 38 | + log("Updating pyproject.toml ...") |
| 39 | + with InPlace(dirpath / "pyproject.toml", encoding="utf-8") as fp: |
| 40 | + for line in fp: |
| 41 | + if line.startswith("license-files ="): |
| 42 | + line = 'license-files = ["LICENSE"]\n' |
| 43 | + elif line.strip() == '"License :: OSI Approved :: MIT License",': |
| 44 | + continue |
| 45 | + print(line, end="", file=fp) |
| 46 | + |
| 47 | + |
| 48 | +def runcmd(*args: str | Path, **kwargs: Any) -> None: |
| 49 | + argstrs = [str(a) for a in args] |
| 50 | + click.secho("+" + shlex.join(argstrs), err=True, fg="green") |
| 51 | + r = subprocess.run(argstrs, **kwargs) |
| 52 | + if r.returncode != 0: |
| 53 | + sys.exit(r.returncode) |
| 54 | + |
| 55 | + |
| 56 | +def log(msg: str) -> None: |
| 57 | + click.secho(msg, err=True, bold=True) |
| 58 | + |
| 59 | + |
| 60 | +if __name__ == "__main__": |
| 61 | + main() |
0 commit comments