Files

197 lines
5.2 KiB
Python
Raw Permalink Normal View History

#!/usr/bin/env -S uv run --script
2020-03-08 12:34:56 +00:00
2024-05-11 23:38:45 -04:00
# /// script
2025-12-23 18:51:19 -05:00
# dependencies = ["click", "packaging", "prek"]
2024-05-11 23:38:45 -04:00
# ///
2021-01-17 14:13:26 -05:00
2021-01-06 13:50:58 -05:00
import os
import subprocess
2021-01-17 14:13:26 -05:00
import sys
import tomllib
2020-10-11 13:58:53 +01:00
import urllib.parse
2021-01-06 13:50:58 -05:00
from pathlib import Path
import click
from packaging.version import InvalidVersion, Version
2020-03-08 12:34:56 +00:00
config = [
# file path, version find/replace format
2024-05-20 02:45:05 -04:00
("pyproject.toml", 'version = "{}"'),
2021-05-03 11:45:43 -04:00
("README.md", "cibuildwheel=={}"),
2021-05-28 14:13:04 +01:00
("cibuildwheel/__init__.py", '__version__ = "{}"'),
2021-05-03 11:45:43 -04:00
("docs/faq.md", "cibuildwheel=={}"),
("docs/faq.md", "cibuildwheel@v{}"),
("examples/*", "cibuildwheel=={}"),
("examples/*", "cibuildwheel@v{}"),
2020-03-08 12:34:56 +00:00
]
2021-04-25 03:45:27 -04:00
RED = "\u001b[31m"
GREEN = "\u001b[32m"
OFF = "\u001b[0m"
2020-03-08 12:34:56 +00:00
@click.command()
2021-04-25 03:45:27 -04:00
def bump_version() -> None:
2024-05-20 02:45:05 -04:00
with open("pyproject.toml", "rb") as f:
current_version = tomllib.load(f)["project"]["version"]
try:
2021-04-30 17:56:34 -04:00
commit_date_str = subprocess.run(
[
2021-05-03 11:45:43 -04:00
"git",
"show",
"--no-patch",
"--pretty=format:%ci",
f"v{current_version}^{{commit}}",
2021-04-30 17:56:34 -04:00
],
check=True,
capture_output=True,
2021-05-03 11:45:43 -04:00
encoding="utf8",
2021-04-30 17:56:34 -04:00
).stdout
2021-05-03 11:45:43 -04:00
cd_date, cd_time, cd_tz = commit_date_str.split(" ")
2021-04-25 03:45:27 -04:00
2021-05-03 11:45:43 -04:00
url_opts = urllib.parse.urlencode({"q": f"is:pr merged:>{cd_date}T{cd_time}{cd_tz}"})
url = f"https://github.com/pypa/cibuildwheel/pulls?{url_opts}"
2021-05-03 11:45:43 -04:00
print(f"PRs merged since last release:\n {url}")
print()
except subprocess.CalledProcessError as e:
print(e)
2021-05-03 11:45:43 -04:00
print("Failed to get previous version tag information.")
git_changes_result = subprocess.run(["git diff-index --quiet HEAD --"], shell=True, check=False)
2020-03-08 12:34:56 +00:00
repo_has_uncommitted_changes = git_changes_result.returncode != 0
if repo_has_uncommitted_changes:
2021-05-03 11:45:43 -04:00
print("error: Uncommitted changes detected.")
2021-01-17 14:13:26 -05:00
sys.exit(1)
2020-03-08 12:34:56 +00:00
2021-04-25 03:45:27 -04:00
# fmt: off
2025-11-07 18:48:20 -05:00
print( "Current version:", current_version)
new_version = input(" New version: ").strip()
2021-04-25 03:45:27 -04:00
# fmt: on
2020-03-08 12:34:56 +00:00
try:
Version(new_version)
except InvalidVersion:
print("error: This version doesn't conform to PEP440")
2021-05-03 11:45:43 -04:00
print(" https://www.python.org/dev/peps/pep-0440/")
2021-01-17 14:13:26 -05:00
sys.exit(1)
2020-03-08 12:34:56 +00:00
actions = []
for path_pattern, version_pattern in config:
2025-02-28 11:56:29 -05:00
paths = list(Path().glob(path_pattern))
2020-03-08 12:34:56 +00:00
if not paths:
2021-04-25 03:45:27 -04:00
print(f"error: Pattern {path_pattern} didn't match any files")
2021-01-17 14:13:26 -05:00
sys.exit(1)
2020-03-08 12:34:56 +00:00
find_pattern = version_pattern.format(current_version)
replace_pattern = version_pattern.format(new_version)
found_at_least_one_file_needing_update = False
for path in paths:
2021-05-03 11:45:43 -04:00
contents = path.read_text(encoding="utf8")
2020-03-08 12:34:56 +00:00
if find_pattern in contents:
found_at_least_one_file_needing_update = True
actions.append(
2021-04-30 17:56:34 -04:00
(
path,
find_pattern,
replace_pattern,
)
2020-03-08 12:34:56 +00:00
)
if not found_at_least_one_file_needing_update:
2021-04-25 03:45:27 -04:00
print(f'''error: Didn't find any occurrences of "{find_pattern}" in "{path_pattern}"''')
2021-01-17 14:13:26 -05:00
sys.exit(1)
2020-03-08 12:34:56 +00:00
print()
print("Here's the plan:")
print()
for action in actions:
2021-04-25 03:45:27 -04:00
path, find, replace = action
2021-05-03 11:45:43 -04:00
print(f"{path} {RED}{find}{OFF}{GREEN}{replace}{OFF}")
2020-03-08 12:34:56 +00:00
2021-05-03 11:45:43 -04:00
print(f"Then commit, and tag as v{new_version}")
2020-03-08 12:34:56 +00:00
2021-05-03 11:45:43 -04:00
answer = input("Proceed? [y/N] ").strip()
2020-03-08 12:34:56 +00:00
2021-05-03 11:45:43 -04:00
if answer != "y":
print("Aborted")
2021-01-17 14:13:26 -05:00
sys.exit(1)
2020-03-08 12:34:56 +00:00
for path, find, replace in actions:
2021-05-03 11:45:43 -04:00
contents = path.read_text(encoding="utf8")
2020-03-08 12:34:56 +00:00
contents = contents.replace(find, replace)
2021-05-03 11:45:43 -04:00
path.write_text(contents, encoding="utf8")
2020-03-08 12:34:56 +00:00
2025-06-03 17:34:22 -04:00
print("Files updated. If you want to update docs/changelog.md as part of")
print("this commit, do that now.")
2020-10-11 13:58:53 +01:00
print()
2021-05-03 11:45:43 -04:00
while input('Type "done" to continue: ').strip().lower() != "done":
2020-03-08 12:34:56 +00:00
pass
# run pre-commit to update the README changelog
subprocess.run(
[
2025-12-23 18:51:19 -05:00
"prek",
"run",
"--files=docs/changelog.md",
],
check=False,
)
# run pre-commit to check that no errors occurred on the second run
subprocess.run(
[
2025-12-23 18:51:19 -05:00
"prek",
"run",
"--files=docs/changelog.md",
],
check=True,
)
2021-04-30 17:56:34 -04:00
subprocess.run(
[
2021-05-03 11:45:43 -04:00
"git",
"commit",
"--all",
2021-04-30 17:56:34 -04:00
f"--message=Bump version: v{new_version}",
],
check=True,
)
2020-03-08 12:34:56 +00:00
2021-04-30 17:56:34 -04:00
subprocess.run(
[
2021-05-03 11:45:43 -04:00
"git",
"tag",
"--annotate",
2021-04-30 17:56:34 -04:00
f"--message=v{new_version}",
2021-05-03 11:45:43 -04:00
f"v{new_version}",
2021-04-30 17:56:34 -04:00
],
check=True,
)
2020-03-08 12:34:56 +00:00
2021-05-03 11:45:43 -04:00
print("Done.")
2021-12-16 12:45:02 +00:00
print()
print("Push the new version to GitHub with:")
2024-03-11 21:24:21 +00:00
print(f" git push && git push origin v{new_version}")
2021-12-16 12:45:02 +00:00
print()
release_url = "https://github.com/pypa/cibuildwheel/releases/new?" + urllib.parse.urlencode(
{"tag": f"v{new_version}"}
2021-12-16 12:45:02 +00:00
)
print("Then create a release at the URL:")
print(f" {release_url}")
2020-03-08 12:34:56 +00:00
2021-05-03 11:45:43 -04:00
if __name__ == "__main__":
2021-04-25 03:45:27 -04:00
os.chdir(Path(__file__).parent.parent.resolve())
2020-03-08 12:34:56 +00:00
bump_version()