2
0
mirror of https://github.com/frappe/bench.git synced 2024-11-14 09:14:04 +00:00

[fix] release script

This commit is contained in:
Anand Doshi 2016-06-09 15:39:54 +05:30
parent 6a0a7223d2
commit 7c86581897
3 changed files with 137 additions and 100 deletions

View File

@ -117,13 +117,14 @@ def backup_all_sites():
@click.command('release') @click.command('release')
@click.argument('app') @click.argument('app')
@click.argument('bump-type', type=click.Choice(['major', 'minor', 'patch'])) @click.argument('bump-type', type=click.Choice(['major', 'minor', 'patch', 'stable', 'prerelease']))
@click.option('--develop', default='develop') @click.option('--develop', default='develop')
@click.option('--master', default='master') @click.option('--master', default='master')
@click.option('--remote', default='upstream')
@click.option('--owner', default='frappe') @click.option('--owner', default='frappe')
@click.option('--repo-name') @click.option('--repo-name')
def release(app, bump_type, develop, master, owner, repo_name): def release(app, bump_type, develop, master, owner, repo_name, remote):
"Release app (internal to the Frappe team)" "Release app (internal to the Frappe team)"
from bench.release import release from bench.release import release
repo = os.path.join('apps', repo_name) if repo_name else os.path.join('apps', app) release(bench_path='.', app=app, bump_type=bump_type, develop=develop, master=master,
release(repo, bump_type, develop, master, owner, app) remote=remote, owner=owner, repo_name=repo_name)

View File

@ -15,27 +15,36 @@ from time import sleep
from .config.common_site_config import get_config from .config.common_site_config import get_config
import click import click
github_username = None github_username = None
github_password = None github_password = None
def release(repo, bump_type, develop, master, owner, app): def release(bench_path, app, bump_type, develop='develop', master='master',
if not get_config(".").get('release_bench'): remote='upstream', owner='frappe', repo_name=None):
validate(bench_path)
bump(bench_path, app, bump_type, develop=develop, master=master, owner=owner,
repo_name=repo_name, remote=remote)
def validate(bench_path):
if not get_config(bench_path).get('release_bench'):
print 'bench not configured to release' print 'bench not configured to release'
sys.exit(1) sys.exit(1)
global github_username, github_password global github_username, github_password
github_username = raw_input('username:') github_username = raw_input('Username: ')
github_password = getpass.getpass() github_password = getpass.getpass()
r = requests.get('https://api.github.com/user', auth=HTTPBasicAuth(github_username, github_password)) r = requests.get('https://api.github.com/user', auth=HTTPBasicAuth(github_username, github_password))
r.raise_for_status() r.raise_for_status()
bump(repo, bump_type, develop=develop, master=master, owner=owner, app=app) def bump(bench_path, app, bump_type, develop, master, remote, owner, repo_name=None):
assert bump_type in ['minor', 'major', 'patch', 'stable', 'prerelease']
def bump(repo, bump_type, develop='develop', master='master', remote='upstream', owner='frappe', app=None): repo_path = os.path.join(bench_path, 'apps', app)
update_branches_and_check_for_changelog(repo_path, develop, master, remote=remote)
message = get_release_message(repo_path, develop=develop, master=master, remote=remote)
assert bump_type in ['minor', 'major', 'patch']
update_branches_and_check_for_changelog(repo, bump_type, develop=develop, master=master, remote=remote, app=app)
message = get_release_message(repo, develop_branch=develop, master_branch=master, app=app)
if not message: if not message:
print 'No commits to release' print 'No commits to release'
return return
@ -46,71 +55,63 @@ def bump(repo, bump_type, develop='develop', master='master', remote='upstream',
click.confirm('Do you want to continue?', abort=True) click.confirm('Do you want to continue?', abort=True)
new_version = bump_repo(repo, bump_type, develop=develop, master=master, remote=remote, app=app) new_version = bump_repo(repo_path, bump_type, develop=develop, master=master)
commit_changes(repo, new_version, app=app) commit_changes(repo_path, new_version)
tag_name = create_release(repo, new_version, develop_branch=develop, master_branch=master, app=app) tag_name = create_release(repo_path, new_version, develop=develop, master=master)
push_release(repo, develop_branch=develop, master_branch=master, app=app) push_release(repo_path, develop=develop, master=master, remote=remote)
create_github_release(owner, repo, tag_name, message, app) create_github_release(repo_path, tag_name, message, remote=remote, owner=owner, repo_name=repo_name)
print 'Released {tag} for {repo}'.format(tag=tag_name, repo=repo) print 'Released {tag} for {repo_path}'.format(tag=tag_name, repo_path=repo_path)
def update_branches_and_check_for_changelog(repo, bump_type, develop='develop', master='master', remote='upstream', app=None): def update_branches_and_check_for_changelog(repo_path, develop='develop', master='master', remote='upstream'):
update_branch(repo, master, remote=remote, app=app) update_branch(repo_path, master, remote=remote)
update_branch(repo, develop, remote=remote, app=app) update_branch(repo_path, develop, remote=remote)
if develop != 'develop': if develop != 'develop':
update_branch(repo, 'develop', remote=remote, app=app) update_branch(repo_path, 'develop', remote=remote)
repo = os.path.join('apps', app) if os.path.basename(repo) != app else repo git.Repo(repo_path).git.checkout(develop)
check_for_unmerged_changelog(repo_path)
git.Repo(repo).git.checkout(develop) def update_branch(repo_path, branch, remote):
check_for_unmerged_changelog(repo)
def update_branch(repo_path, branch, remote='origin', app=None):
print "updating local branch of", repo_path, 'using', remote + '/' + branch print "updating local branch of", repo_path, 'using', remote + '/' + branch
if os.path.basename(repo_path) != app:
repo_path = app
repo_path = os.path.join('apps', repo_path)
repo = git.Repo(repo_path) repo = git.Repo(repo_path)
g = repo.git g = repo.git
g.fetch(remote) g.fetch(remote)
g.checkout(branch) g.checkout(branch)
g.reset('--hard', remote+'/'+branch) g.reset('--hard', remote+'/'+branch)
def check_for_unmerged_changelog(repo): def check_for_unmerged_changelog(repo_path):
current = os.path.join(repo, os.path.basename(repo), 'change_log', 'current') current = os.path.join(repo_path, os.path.basename(repo_path), 'change_log', 'current')
if os.path.exists(current) and [f for f in os.listdir(current) if f != "readme.md"]: if os.path.exists(current) and [f for f in os.listdir(current) if f != "readme.md"]:
raise Exception("Unmerged change log! in " + repo) raise Exception("Unmerged change log! in " + repo_path)
def get_release_message(repo_path, develop_branch='develop', master_branch='master', app=None): def get_release_message(repo_path, develop='develop', master='master', remote='upstream'):
print 'getting release message for', repo_path, 'comparing', master, '...', develop
repo_path = os.path.join('apps', app) if os.path.basename(repo_path) != app else repo_path
print 'getting release message for', repo_path, 'comparing', master_branch, '...', develop_branch
repo = git.Repo(repo_path) repo = git.Repo(repo_path)
g = repo.git g = repo.git
log = g.log('upstream/{master_branch}..upstream/{develop_branch}'.format(master_branch=master_branch, develop_branch=develop_branch), '--format=format:%s', '--no-merges') log = g.log('{remote}/{master}..{remote}/{develop}'.format(
remote=remote, master=master, develop=develop), '--format=format:%s', '--no-merges')
if log: if log:
return "* " + log.replace('\n', '\n* ') return "* " + log.replace('\n', '\n* ')
def bump_repo(repo_path, bump_type, develop='develop', master='master'):
def bump_repo(repo, bump_type, develop='develop', master='master', remote='upstream', app=None): current_version = get_current_version(repo_path)
repo = os.path.join('apps', app) if os.path.basename(repo) != app else repo
current_version = get_current_version(repo)
new_version = get_bumped_version(current_version, bump_type) new_version = get_bumped_version(current_version, bump_type)
print 'bumping version from', current_version, 'to', new_version print 'bumping version from', current_version, 'to', new_version
set_version(repo, new_version) set_version(repo_path, new_version)
return new_version return new_version
def get_current_version(repo): def get_current_version(repo_path):
filename = os.path.join(repo, 'setup.py') # TODO clean this up!
filename = os.path.join(repo_path, os.path.basename(repo_path), '__init__.py')
with open(filename) as f: with open(filename) as f:
contents = f.read() contents = f.read()
match = re.search(r"^(\s*%s\s*=\s*['\\\"])(.+?)(['\"])(?sm)" % 'version', match = re.search(r"^(\s*%s\s*=\s*['\\\"])(.+?)(['\"])(?sm)" % '__version__',
contents) contents)
return match.group(2) return match.group(2)
@ -119,27 +120,50 @@ def get_bumped_version(version, bump_type):
if bump_type == 'minor': if bump_type == 'minor':
v.minor += 1 v.minor += 1
v.patch = 0 v.patch = 0
v.prerelease = None
elif bump_type == 'major': elif bump_type == 'major':
v.major += 1 v.major += 1
v.minor = 0 v.minor = 0
v.patch = 0 v.patch = 0
v.prerelease = None
elif bump_type == 'patch': elif bump_type == 'patch':
v.patch += 1 v.patch += 1
v.prerelease = None
elif bump_type == 'stable':
# remove pre-release tag
v.prerelease = None
elif bump_type == 'prerelease':
if v.prerelease == None:
v.prerelease = ('beta',)
elif len(v.prerelease)==1:
v.prerelease[1] = '1'
else:
v.prerelease[1] = str(int(v.prerelease[1]) + 1)
return unicode(v) return unicode(v)
def set_version(repo, version): def set_version(repo_path, version):
set_setuppy_version(repo, version) set_filename_version(os.path.join(repo_path, os.path.basename(repo_path),'__init__.py'), version, '__version__')
set_versionpy_version(repo, version)
set_hooks_version(repo, version)
def set_setuppy_version(repo, version): # TODO fix this
set_filename_version(os.path.join(repo, 'setup.py'), version, 'version') # set_setuppy_version(repo_path, version)
# set_versionpy_version(repo_path, version)
# set_hooks_version(repo_path, version)
def set_versionpy_version(repo, version): # def set_setuppy_version(repo_path, version):
set_filename_version(os.path.join(repo, os.path.basename(repo),'__version__.py'), version, '__version__') # set_filename_version(os.path.join(repo_path, 'setup.py'), version, 'version')
#
def set_hooks_version(repo, version): # def set_versionpy_version(repo_path, version):
set_filename_version(os.path.join(repo, os.path.basename(repo),'hooks.py'), version, 'app_version') # set_filename_version(os.path.join(repo_path, os.path.basename(repo_path),'__version__.py'), version, '__version__')
#
# def set_hooks_version(repo_path, version):
# set_filename_version(os.path.join(repo_path, os.path.basename(repo_path),'hooks.py'), version, 'app_version')
def set_filename_version(filename, version_number, pattern): def set_filename_version(filename, version_number, pattern):
changed = [] changed = []
@ -159,60 +183,70 @@ def set_filename_version(filename, version_number, pattern):
with open(filename, 'w') as f: with open(filename, 'w') as f:
f.write(contents) f.write(contents)
def commit_changes(repo_path, version, app=None): def commit_changes(repo_path, new_version):
repo_path = os.path.join('apps', app) if os.path.basename(repo_path) != app else repo_path
print 'committing version change to', repo_path print 'committing version change to', repo_path
repo = git.Repo(repo_path) repo = git.Repo(repo_path)
repo_name = os.path.basename(repo_path) app_name = os.path.basename(repo_path)
repo.index.add(['setup.py']) repo.index.add([os.path.join(app_name, '__init__.py')])
repo.index.add([os.path.join(repo_name, '__version__.py')]) repo.index.commit('bumped to version {}'.format(new_version))
repo.index.add([os.path.join(repo_name, 'hooks.py')])
repo.index.commit('bumped to version {}'.format(version))
def create_release(repo_path, version, remote='origin', develop_branch='develop', master_branch='master', app=None): def create_release(repo_path, new_version, develop='develop', master='master'):
print 'creating release for version', new_version
repo_path = os.path.join('apps', app) if os.path.basename(repo_path) != app else repo_path
print 'creating release for version', version
repo = git.Repo(repo_path) repo = git.Repo(repo_path)
g = repo.git g = repo.git
g.checkout(master_branch) g.checkout(master)
g.merge(develop_branch, '--no-ff') try:
tag_name = 'v' + version g.merge(develop, '--no-ff')
repo.create_tag(tag_name, message='Release {}'.format(version)) except git.exc.GitCommandError, e:
g.checkout(develop_branch) handle_merge_error(e, source=develop, target=master)
g.merge(master_branch)
if develop_branch != 'develop': tag_name = 'v' + new_version
repo.create_tag(tag_name, message='Release {}'.format(new_version))
g.checkout(develop)
try:
g.merge(master)
except git.exc.GitCommandError, e:
handle_merge_error(e, source=master, target=develop)
if develop != 'develop':
print 'merging master into develop' print 'merging master into develop'
g.checkout('develop') g.checkout('develop')
g.merge(master_branch) try:
g.merge(master)
except git.exc.GitCommandError, e:
handle_merge_error(e, source=master, target='develop')
return tag_name return tag_name
def push_release(repo_path, develop_branch='develop', master_branch='master', app=None): def handle_merge_error(e, source, target):
print '-'*80
print 'Error when merging {source} into {target}'.format(source=source, target=target)
print e
print 'You can open a new terminal, try to manually resolve the conflict/error and continue'
print '-'*80
click.confirm('Have you manually resolved the error?', abort=True)
repo_path = os.path.join('apps', app) if os.path.basename(repo_path) != app else repo_path def push_release(repo_path, develop='develop', master='master', remote='upstream'):
print 'pushing branches', master, develop, 'of', repo_path
print 'pushing branches', master_branch, develop_branch, 'of', repo_path
repo = git.Repo(repo_path) repo = git.Repo(repo_path)
g = repo.git g = repo.git
args = [ args = [
'{master}:{master}'.format(master=master_branch), '{master}:{master}'.format(master=master),
'{develop}:{develop}'.format(develop=develop_branch) '{develop}:{develop}'.format(develop=develop)
] ]
if develop_branch != 'develop': if develop != 'develop':
print 'pushing develop branch of', repo_path print 'pushing develop branch of', repo_path
args.append('develop:develop') args.append('develop:develop')
args.append('--tags') args.append('--tags')
print g.push('upstream', *args) print g.push(remote, *args)
def create_github_release(owner, repo, tag_name, log, gh_username=None, gh_password=None): def create_github_release(repo_path, tag_name, message, remote='upstream', owner='frappe', repo_name=None,
gh_username=None, gh_password=None):
print 'creating release on github' print 'creating release on github'
@ -222,18 +256,21 @@ def create_github_release(owner, repo, tag_name, log, gh_username=None, gh_passw
raise Exception, "No credentials" raise Exception, "No credentials"
gh_username = github_username gh_username = github_username
gh_password = github_password gh_password = github_password
repo = os.path.basename(repo)
repo_name = repo_name or os.path.basename(repo_path)
data = { data = {
'tag_name': tag_name, 'tag_name': tag_name,
'target_commitish': 'master', 'target_commitish': 'master',
'name': 'Release ' + tag_name, 'name': 'Release ' + tag_name,
'body': log, 'body': message,
'draft': False, 'draft': False,
'prerelease': False 'prerelease': False
} }
for i in xrange(3): for i in xrange(3):
try: try:
r = requests.post('https://api.github.com/repos/{owner}/{repo}/releases'.format(owner=owner, repo=repo), auth=HTTPBasicAuth(gh_username, gh_password), data=json.dumps(data)) r = requests.post('https://api.github.com/repos/{owner}/{repo_name}/releases'.format(
owner=owner, repo_name=repo_name),
auth=HTTPBasicAuth(gh_username, gh_password), data=json.dumps(data))
r.raise_for_status() r.raise_for_status()
break break
except requests.exceptions.HTTPError: except requests.exceptions.HTTPError:

View File

@ -1,7 +1,6 @@
from setuptools import setup, find_packages from setuptools import setup, find_packages
from pip.req import parse_requirements from pip.req import parse_requirements
import re import re, ast
import ast
# get version from __version__ variable in bench/__init__.py # get version from __version__ variable in bench/__init__.py
_version_re = re.compile(r'__version__\s+=\s+(.*)') _version_re = re.compile(r'__version__\s+=\s+(.*)')