File size: 3,935 Bytes
69ed2c6 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 | """Scripts related to updating of version."""
import datetime as dt
import re
import subprocess
from pathlib import Path
from typing import Tuple
def bump_major():
"""Add one to the major version."""
major, _, _ = get_current_version()
set_new_version(major + 1, 0, 0)
def bump_minor():
"""Add one to the minor version."""
major, minor, _ = get_current_version()
set_new_version(major, minor + 1, 0)
def bump_patch():
"""Add one to the patch version."""
major, minor, patch = get_current_version()
set_new_version(major, minor, patch + 1)
def set_new_version(major: int, minor: int, patch: int):
"""Sets a new version.
Args:
major (int):
The major version. This only changes when the code stops being backwards
compatible.
minor (int):
The minor version. This changes when a backwards compatible change
happened.
patch (init):
The patch version. This changes when the only new changes are bug fixes.
"""
version = f"{major}.{minor}.{patch}"
# Get current changelog and ensure that it has an [Unreleased] entry
changelog_path = Path("CHANGELOG.md")
changelog = changelog_path.read_text()
if "[Unreleased]" not in changelog:
raise RuntimeError("No [Unreleased] entry in CHANGELOG.md.")
# Add version to CHANGELOG
today = dt.date.today().strftime("%Y-%m-%d")
new_changelog = re.sub(r"\[Unreleased\].*", f"[v{version}] - {today}", changelog)
changelog_path.write_text(new_changelog)
# Update the version in the `pyproject.toml` file
pyproject_path = Path("pyproject.toml")
pyproject = pyproject_path.read_text()
pyproject = re.sub(
r'version = "[^"]+"',
f'version = "{version}"',
pyproject,
count=1,
)
pyproject_path.write_text(pyproject)
# Add to version control
subprocess.run(["git", "add", "CHANGELOG.md"])
subprocess.run(["git", "add", "pyproject.toml"])
subprocess.run(["git", "commit", "-m", f"feat: v{version}"])
subprocess.run(["git", "tag", f"v{version}"])
subprocess.run(["git", "push"])
subprocess.run(["git", "push", "--tags"])
def get_current_version() -> Tuple[int, int, int]:
"""Fetch the current version of the package.
Returns:
triple of ints:
The current version, separated into major, minor and patch versions.
"""
# Get all the version candidates from pyproject.toml
version_candidates = re.search(
r'(?<=version = ")[^"]+(?=")', Path("pyproject.toml").read_text()
)
# If no version candidates were found, raise an error
if version_candidates is None:
raise RuntimeError("No version found in pyproject.toml.")
# Otherwise, extract the version, split it into major, minor and patch parts and
# return these
else:
version_str = version_candidates.group(0)
major, minor, patch = map(int, version_str.split("."))
return major, minor, patch
if __name__ == "__main__":
from argparse import ArgumentParser
parser = ArgumentParser()
parser.add_argument(
"--major",
const=True,
nargs="?",
default=False,
help="Bump the major version by one.",
)
parser.add_argument(
"--minor",
const=True,
nargs="?",
default=False,
help="Bump the minor version by one.",
)
parser.add_argument(
"--patch",
const=True,
nargs="?",
default=False,
help="Bump the patch version by one.",
)
args = parser.parse_args()
if args.major + args.minor + args.patch != 1:
raise RuntimeError(
"Exactly one of --major, --minor and --patch must be selected."
)
elif args.major:
bump_major()
elif args.minor:
bump_minor()
elif args.patch:
bump_patch()
|