Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
#!/usr/bin/env python
import argparse
import os
import re
import time
from pathlib import Path
VERSION_PATTERN = r'[\d]+\.[\d]+\.[\d]+'
def get_args():
parser = argparse.ArgumentParser(description='Create a new release.')
parser.add_argument('version', nargs='?', help='The release version.')
parser.add_argument('--push', action='store_true', help="Push changes to origin.")
parser.add_argument('--publish', action='store_true', help="Publish changes to Pypi.")
return parser.parse_args()
def update_version(version):
with open('', 'r+') as f:
out =
out = re.sub(f"version='{VERSION_PATTERN}'", f"version='{version}'", out)
def commit_and_tag(version):
os.popen('git add')
os.popen(f'git commit -m "Bump version to {version}"')
# without sleep it tags the previous commit
print(os.popen(f'git tag -a -m "" v{version}').read())
def push():
print(os.popen('git push --tags origin master').read())
def build():
print(os.popen(f'rm -rf build dist').read())
print(os.popen('python sdist bdist_wheel').read())
def publish():
print(os.popen('twine upload dist/*').read())
if __name__ == "__main__":
# change working directory so you can run this script from the project root or in the same directory as the file.
parent_dir = Path(os.path.realpath(os.path.dirname(__file__))).parent
args = get_args()
new_version = args.version
current_version = os.popen('git tag | tail -1').read()[1:].strip()
while new_version is None or re.match(VERSION_PATTERN, new_version) is None:
new_version = input(f'What version would you like for this release (current version: {current_version})? ')
if args.push is True:
if args.publish is True:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment