|
| 1 | +import argparse |
| 2 | +import subprocess |
| 3 | +import tomllib |
| 4 | +from pathlib import Path |
| 5 | + |
| 6 | + |
| 7 | +def extract_version(toml_path: Path | str) -> str: |
| 8 | + """Load toml_path and return the version string. |
| 9 | +
|
| 10 | + Checks [project].version (PEP 621) first, then [tool.poetry].version. Raises KeyError if no version field is found. |
| 11 | + """ |
| 12 | + path = Path(toml_path) |
| 13 | + with path.open("rb") as f: |
| 14 | + data = tomllib.load(f) |
| 15 | + |
| 16 | + project = data.get("project", {}) |
| 17 | + if version := project.get("version"): |
| 18 | + return version |
| 19 | + |
| 20 | + tool = data.get("tool", {}) |
| 21 | + poetry = tool.get("poetry", {}) |
| 22 | + if version := poetry.get("version"): |
| 23 | + return version |
| 24 | + |
| 25 | + raise KeyError(f"No version field found in {path!r}") # noqa: TRY003 # not worth a custom exception |
| 26 | + |
| 27 | + |
| 28 | +def ensure_tag_not_present(tag: str, remote: str) -> None: |
| 29 | + try: |
| 30 | + _ = subprocess.run( # noqa: S603 # this is trusted input, it's our own arguments being passed in |
| 31 | + ["git", "ls-remote", "--exit-code", "--tags", remote, f"refs/tags/{tag}"], # noqa: S607 # if `git` isn't in PATH already, then there are bigger problems to solve |
| 32 | + check=True, |
| 33 | + stdout=subprocess.DEVNULL, |
| 34 | + stderr=subprocess.DEVNULL, |
| 35 | + ) |
| 36 | + raise Exception(f"Error: tag '{tag}' exists on remote '{remote}'") # noqa: TRY002,TRY003 # not worth a custom exception |
| 37 | + except subprocess.CalledProcessError: |
| 38 | + # tag not present, continue |
| 39 | + return |
| 40 | + |
| 41 | + |
| 42 | +def main(): |
| 43 | + parser = argparse.ArgumentParser( |
| 44 | + description=( |
| 45 | + "Extract the version from a pyproject.toml file, " |
| 46 | + "confirm that git tag v<version> is not present, or " |
| 47 | + "create and push the tag to a remote." |
| 48 | + ) |
| 49 | + ) |
| 50 | + _ = parser.add_argument( |
| 51 | + "file", |
| 52 | + nargs="?", |
| 53 | + default="pyproject.toml", |
| 54 | + help="Path to pyproject.toml (default: pyproject.toml)", |
| 55 | + ) |
| 56 | + _ = parser.add_argument( |
| 57 | + "--confirm-tag-not-present", |
| 58 | + action="store_true", |
| 59 | + help=("Check that git tag v<version> is NOT present on the remote. If the tag exists, exit with an error."), |
| 60 | + ) |
| 61 | + _ = parser.add_argument( |
| 62 | + "--push-tag-to-remote", |
| 63 | + action="store_true", |
| 64 | + help=( |
| 65 | + "Create git tag v<version> locally and push it to the remote. " |
| 66 | + "Internally confirms the tag is not already present." |
| 67 | + ), |
| 68 | + ) |
| 69 | + _ = parser.add_argument( |
| 70 | + "--remote", |
| 71 | + default="origin", |
| 72 | + help="Name of git remote to query/push (default: origin)", |
| 73 | + ) |
| 74 | + args = parser.parse_args() |
| 75 | + |
| 76 | + ver = extract_version(args.file) |
| 77 | + |
| 78 | + tag = f"v{ver}" |
| 79 | + |
| 80 | + if args.push_tag_to_remote: |
| 81 | + ensure_tag_not_present(tag, args.remote) |
| 82 | + _ = subprocess.run(["git", "tag", tag], check=True) # noqa: S603,S607 # this is trusted input, it's our own pyproject.toml file. and if `git` isn't in PATH, then there are larger problems anyway |
| 83 | + _ = subprocess.run(["git", "push", args.remote, tag], check=True) # noqa: S603,S607 # this is trusted input, it's our own pyproject.toml file. and if `git` isn't in PATH, then there are larger problems anyway |
| 84 | + return |
| 85 | + |
| 86 | + if args.confirm_tag_not_present: |
| 87 | + ensure_tag_not_present(tag, args.remote) |
| 88 | + return |
| 89 | + |
| 90 | + # Default behavior: just print the version |
| 91 | + print(ver) # noqa: T201 # specifically printing this out so CI pipelines can read the value from stdout |
| 92 | + |
| 93 | + |
| 94 | +if __name__ == "__main__": |
| 95 | + main() |
0 commit comments