|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import argparse |
| 4 | +import re |
| 5 | +import subprocess |
| 6 | +import sys |
| 7 | +from pathlib import Path |
| 8 | +from typing import List |
| 9 | + |
| 10 | + |
| 11 | +class UsageError(Exception): |
| 12 | + pass |
| 13 | + |
| 14 | + |
| 15 | +def parse_version(): |
| 16 | + with Path("setup.py").open() as infile: |
| 17 | + for line in infile: |
| 18 | + if m := re.match(r"VERSION\s*=\s*\"(.*)\"$", line): |
| 19 | + return m.groups()[0] |
| 20 | + |
| 21 | + |
| 22 | +def bump_version(path: Path, prefixes: List[str], current_version: str, new_version: str): |
| 23 | + prefixes = tuple(prefixes) |
| 24 | + lines = [] |
| 25 | + for line in path.open(): |
| 26 | + if line.startswith(prefixes): |
| 27 | + line = line.replace(current_version, new_version) |
| 28 | + lines.append(line) |
| 29 | + |
| 30 | + path.write_text("".join(lines)) |
| 31 | + |
| 32 | + |
| 33 | +def git(*args): |
| 34 | + return subprocess.check_call(["git"] + list(args)) |
| 35 | + |
| 36 | + |
| 37 | +def make(*args): |
| 38 | + return subprocess.check_call(["make"] + list(args)) |
| 39 | + |
| 40 | + |
| 41 | +def check_changelog(): |
| 42 | + old_changelog = Path("CHANGES.rst").read_text() |
| 43 | + make("changes") |
| 44 | + new_changelog = Path("CHANGES.rst").read_text() |
| 45 | + if old_changelog == new_changelog: |
| 46 | + raise UsageError("No new changelog entries") |
| 47 | + |
| 48 | + |
| 49 | +def main(): |
| 50 | + parser = argparse.ArgumentParser() |
| 51 | + parser.add_argument("new_version", help="Version to release") |
| 52 | + |
| 53 | + args = parser.parse_args() |
| 54 | + new_version: str = args.new_version |
| 55 | + |
| 56 | + current_version = parse_version() |
| 57 | + if current_version is None: |
| 58 | + raise UsageError("Unable to parse version") |
| 59 | + |
| 60 | + print(f"Current version: {current_version}") |
| 61 | + |
| 62 | + if current_version == new_version: |
| 63 | + raise UsageError("Current version is the same as new version") |
| 64 | + |
| 65 | + check_changelog() |
| 66 | + |
| 67 | + bump_version(Path("setup.py"), ["VERSION"], current_version, new_version) |
| 68 | + bump_version(Path("doc/conf.py"), ["version"], current_version, new_version) |
| 69 | + |
| 70 | + git("add", "setup.py", "doc/conf.py") |
| 71 | + git("commit", "-m", "Version bump to {}".format(new_version)) |
| 72 | + git("tag", new_version) |
| 73 | + make("changes") |
| 74 | + git("add", "CHANGES.rst") |
| 75 | + git("commit", "-m", "CHANGES.rst: add release notes for {}".format(new_version)) |
| 76 | + git("tag", "-f", new_version) |
| 77 | + |
| 78 | + |
| 79 | +if __name__ == "__main__": |
| 80 | + try: |
| 81 | + main() |
| 82 | + except UsageError as err: |
| 83 | + print(f"ERROR: {err}", file=sys.stderr) |
| 84 | + sys.exit(1) |
0 commit comments