Skip to content

Instantly share code, notes, and snippets.

@tito
Last active March 26, 2017 18:50
Show Gist options
  • Save tito/90669b975667eb9e725ed1ff11bf038e to your computer and use it in GitHub Desktop.
Save tito/90669b975667eb9e725ed1ff11bf038e to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
from __future__ import print_function
from os.path import join, basename, realpath, exists
import sh
import re
import sys
import argparse
sed = sh.sed
if sys.platform == "darwin":
sed = sh.gsed
def ask_version(version):
while True:
print("Is version {} ok [Y/n] ".format(version), end="")
resp = raw_input()
if resp in ("", "Y"):
return version
print("Enter the new version: ", end="")
version = raw_input()
def run(args):
# ensure we start from a clean git
git_status = sh.git.status("--porcelain")
if list(git_status):
print("UNCOMMITED CHANGES ----")
print(git_status)
print("-----------------------")
raise Exception("Cannot release with uncommitted changes")
# project name
project_name = args.p
if project_name is None:
project_name = basename(realpath("."))
print("Release new version for {}".format(project_name))
# get the current version number
filename_init = join(project_name, "__init__.py")
if not exists(filename_init):
if filename_init.startswith("py"):
filename_init = filename_init[2:]
if not exists(filename_init):
print("Unable to find __init__")
sys.exit(1)
current_version = open(filename_init).readlines()
current_version = [x for x in current_version if x.startswith("__version__")][
0]
current_version = current_version.split()[2][1:-1]
print("Current version is {}".format(current_version))
# remove the -dev
dev_prefixes = ("-dev", ".dev", "dev")
dev_prefix = None
for prefix in dev_prefixes:
if prefix in current_version:
dev_prefix = prefix
break
if dev_prefix is not None:
version = current_version.split(dev_prefix, 1)[0]
else:
version = current_version
version = ask_version(version)
print("Releasing {} {}".format(project_name, version))
# calculate next version
current_version_int = map(int, version.split("."))
next_version = map(int, version.split("."))
next_version[-1] += 1
next_version = ".".join(map(str, next_version)) + ".dev0"
next_version = ask_version(next_version)
# commit current version
print(sed(
"-i", "s/{}/{}/g".format(current_version, version),
filename_init))
print(sh.git.commit("-am", "Bump to {}".format(version)))
print(sh.git.tag(version))
# release
try:
print(sh.python("setup.py", "sdist", "upload"))
# print sh.python("setup.py", "bdist_wheel", "--universal")
# print sh.twine.upload(sh.glob("dist/*"))
except:
import traceback
traceback.print_exc()
print("Type ENTER to Cancel release...")
raw_input()
print(sh.git.reset("--hard", "HEAD~1"))
print(sh.git.tag("-d", version))
# get next version
print(sed(
"-i", "s/{}/{}/g".format(version, next_version),
filename_init))
print(sh.git.commit("-am", "Bump to {}".format(next_version)))
print(sh.git.push("--tags", "origin", "master"))
# create changelog
# extract branch
lines = sh.git("remote", "-v")
repo = None
for line in lines.splitlines():
if line.startswith("origin") and "(push)" in line:
repo = re.match(".*[email protected][:/](.*) \(push\)", line).group(1)
break
print(sh.github_changelog_generator(repo))
print(sh.git("commit", "-am", "Update changelog"))
print(sh.git.push("--tags", "origin", "master"))
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Release a software to pypi")
parser.add_argument("-p", default=None, help="Project name")
args = parser.parse_args()
run(args)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment