Last active
August 3, 2016 08:24
-
-
Save mattvonrocketstein/c4c46d0f3753f3dbb77018c21ec0e767 to your computer and use it in GitHub Desktop.
release fabfile
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
import os | |
import sys | |
from fabric import api | |
from fabric import colors | |
from fabric.contrib.console import confirm | |
PKG_NAME = 'PKG_NAME' | |
THIS_DIR = os.path.dirname(os.path.abspath(__file__)) | |
def get_pkg_version(): | |
""" """ | |
sys.path.append(os.path.join(THIS_DIR, PKG_NAME)) | |
from version import __version__ as release_version # flake8: noqa | |
sys.path.pop() | |
return release_version | |
@api.task | |
def show_version(): | |
""" show current version of this package """ | |
print get_pkg_version() | |
def git_branch(): | |
""" returns (branch-name, hash) """ | |
with api.quiet(): | |
cmd = 'git rev-parse --abbrev-ref HEAD' | |
current_branch = api.local(cmd, capture=True) | |
cmd = 'git rev-parse HEAD' | |
current_hash = api.local(cmd, capture=True) | |
return current_hash, current_branch | |
@api.task | |
def release(force=False): | |
""" releases the master branch at the current version to pypi """ | |
with api.lcd(THIS_DIR): | |
return _release(force=force) | |
def _release(force=False): | |
""" helper for pypi releases """ | |
def assert_master(branch_name): | |
if current_branch != 'master': | |
err = "you must do releases from master, but this is {0}" | |
print colors.red("ERROR:") + err.format(current_branch) | |
raise SystemExit(1) | |
def assert_git(hash, branch_name): | |
is_git_repo = all([current_branch.succeeded, current_hash.succeeded]) | |
if not is_git_repo: | |
print colors.red("ERROR: ") + \ | |
"wait a minute, is this even a git repo?" | |
raise SystemExit(1) | |
def assert_confirm(): | |
print colors.red("WARNING:\n") + \ | |
(" did you commit local master?\n" | |
" did you bump the version string?\n") | |
question = '\nproceed with pypi update?' | |
ans = confirm(colors.red(question)) | |
if not ans: | |
raise SystemExit(1) | |
current_version = get_pkg_version() | |
current_hash, current_branch = git_branch() | |
assert_git(current_hash, current_branch) | |
assert_master(current_branch) | |
print colors.blue("current branch: ") + current_branch | |
print colors.blue("current version: ") + str(current_version) | |
print colors.blue("current dir: ") + "{0}".format(THIS_DIR) | |
if not force: | |
assert_confirm() | |
result = api.local("git stash") # stash local changes if there are any | |
stashed_changes = result.succeeded and str( | |
result) != 'No local changes to save' | |
# warn-only, because if the branch already exists the command fails | |
with api.settings(warn_only=True): | |
tmp_checkout = api.local("git checkout -b pypi") | |
if tmp_checkout.failed: | |
api.local("git checkout pypi") | |
api.local("git reset --hard master") | |
api.local("python setup.py register -r pypi") | |
api.local("python setup.py sdist upload -r pypi") | |
print colors.red("release successful") | |
print " hash {0} for branch {1} will be tagged as {2}".format( | |
colors.blue(current_hash[:6]), | |
colors.blue(current_branch), | |
colors.blue(current_version)) | |
cmd = 'git tag -a "{0}" {1} -m "version {0}"' | |
api.local(cmd.format(current_version, current_hash)) | |
api.local("git push origin --tags") | |
api.local('git checkout {0}'.format(current_branch)) | |
if stashed_changes: | |
with api.settings(warn_only=True): | |
api.local('git stash pop') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment