2017-02-21 04:58:29 +01:00
|
|
|
"""Trigger a release.
|
|
|
|
|
|
|
|
This script is to be run locally (not on a build server).
|
|
|
|
"""
|
|
|
|
import argparse
|
|
|
|
import contextlib
|
|
|
|
import logging
|
|
|
|
import os
|
|
|
|
import re
|
|
|
|
import subprocess
|
|
|
|
import sys
|
|
|
|
|
|
|
|
import git
|
|
|
|
import github
|
|
|
|
import requests
|
|
|
|
|
2017-02-22 05:29:23 +01:00
|
|
|
import changelog
|
2017-02-21 04:58:29 +01:00
|
|
|
|
2017-02-22 16:39:54 +01:00
|
|
|
|
2017-02-21 20:48:29 +01:00
|
|
|
NO_CHANGE = ('No change since the last release. This release is simply a placeholder'
|
|
|
|
' so that LBRY and LBRY App track the same version')
|
2017-03-06 15:37:25 +01:00
|
|
|
TOKEN_MSG = """
|
|
|
|
Please enter your personal access token. If you don't have one
|
|
|
|
See https://github.com/lbryio/lbry-app/wiki/Release-Script#generate-a-personal-access-token
|
|
|
|
for instructions on how to generate one.
|
|
|
|
|
|
|
|
You can also set the GH_TOKEN environment variable to avoid seeing this message
|
|
|
|
in the future"""
|
2017-02-22 18:44:05 +01:00
|
|
|
DEFAULT_BRANCHES = {
|
|
|
|
'lbry': 'master',
|
|
|
|
'lbry-app': 'master',
|
|
|
|
'lbry-web-ui': 'development',
|
|
|
|
'lbryum': 'master'
|
|
|
|
}
|
2017-03-06 15:37:25 +01:00
|
|
|
# TODO: ask bumpversion for these
|
|
|
|
LBRY_PARTS = ('major', 'minor', 'patch', 'release', 'candidate')
|
|
|
|
LBRYUM_PARTS = ('major', 'minor', 'patch')
|
|
|
|
LBRYUM_MSG = """The lbryum repo has changes but you didn't specify how to bump the
|
|
|
|
version. Please enter one of {}""".format(', '.join(LBRYUM_PARTS))
|
|
|
|
|
2017-02-21 04:58:29 +01:00
|
|
|
|
|
|
|
def main():
|
|
|
|
parser = argparse.ArgumentParser()
|
2017-03-06 15:37:25 +01:00
|
|
|
parser.add_argument(
|
|
|
|
"lbry_part", help="part of lbry version to bump",
|
|
|
|
choices=LBRY_PARTS
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
2017-03-06 17:10:36 +01:00
|
|
|
"--lbryum-part", help="part of lbryum version to bump",
|
2017-03-06 15:37:25 +01:00
|
|
|
choices=LBRYUM_PARTS
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"--ui-part", help="part of the ui to bump",
|
|
|
|
choices=LBRY_PARTS
|
|
|
|
)
|
2017-02-22 18:44:05 +01:00
|
|
|
parser.add_argument("--branch", help="branch to use for each repo; useful for testing")
|
2017-03-06 15:37:25 +01:00
|
|
|
parser.add_argument(
|
|
|
|
"--last-release",
|
|
|
|
help=("manually set the last release version. The default is to query and parse the"
|
|
|
|
" value from the rlease page.")
|
|
|
|
)
|
2017-02-21 04:58:29 +01:00
|
|
|
parser.add_argument("--skip-sanity-checks", action="store_true")
|
2017-03-06 15:37:25 +01:00
|
|
|
parser.add_argument(
|
|
|
|
"--require-changelog", action="store_true",
|
|
|
|
help=("Set this flag to raise an exception if a submodules has changes without a"
|
|
|
|
" corresponding changelog entry. The default is to log a warning")
|
|
|
|
)
|
2017-02-22 18:52:46 +01:00
|
|
|
parser.add_argument("--skip-push", action="store_true",
|
|
|
|
help="Set to not push changes to remote repo")
|
2017-02-21 04:58:29 +01:00
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
base = git.Repo(os.getcwd())
|
2017-02-22 18:44:05 +01:00
|
|
|
branch = get_branch('lbry-app', args.branch)
|
2017-02-21 04:58:29 +01:00
|
|
|
|
|
|
|
if not args.skip_sanity_checks:
|
2017-02-22 18:44:05 +01:00
|
|
|
run_sanity_checks(base, branch)
|
2017-02-21 04:58:29 +01:00
|
|
|
|
2017-03-06 17:10:36 +01:00
|
|
|
base_repo = Repo('lbry-app', args.lbry_part, os.getcwd())
|
|
|
|
base_repo.assert_new_tag_is_absent()
|
|
|
|
|
2017-02-21 04:58:29 +01:00
|
|
|
if args.last_release:
|
|
|
|
last_release = args.last_release
|
|
|
|
else:
|
|
|
|
response = requests.get('https://api.github.com/repos/lbryio/lbry-app/releases/latest')
|
|
|
|
data = response.json()
|
|
|
|
last_release = data['tag_name']
|
2017-02-22 18:50:39 +01:00
|
|
|
logging.info('Last release: %s', last_release)
|
2017-02-21 04:58:29 +01:00
|
|
|
|
2017-03-06 15:37:25 +01:00
|
|
|
gh_token = get_gh_token()
|
2017-02-21 04:58:29 +01:00
|
|
|
auth = github.Github(gh_token)
|
|
|
|
github_repo = auth.get_repo('lbryio/lbry-app')
|
|
|
|
|
|
|
|
names = ['lbry', 'lbry-web-ui', 'lbryum']
|
2017-03-06 15:37:25 +01:00
|
|
|
repos = {name: Repo(name, get_part(args, name)) for name in names}
|
2017-02-21 04:58:29 +01:00
|
|
|
|
|
|
|
# in order to see if we've had any change in the submodule, we need to checkout
|
|
|
|
# our last release, see what commit we were on, and then compare that to
|
|
|
|
# current
|
|
|
|
base.git.checkout(last_release)
|
2017-02-22 18:52:46 +01:00
|
|
|
base.git.submodule('update')
|
2017-03-06 15:37:25 +01:00
|
|
|
for repo in repos.values():
|
2017-02-21 04:58:29 +01:00
|
|
|
repo.save_commit()
|
|
|
|
|
|
|
|
base.git.checkout(branch)
|
2017-02-22 18:52:46 +01:00
|
|
|
base.git.submodule('update')
|
2017-02-21 04:58:29 +01:00
|
|
|
|
|
|
|
changelogs = {}
|
|
|
|
|
2017-03-06 15:37:25 +01:00
|
|
|
get_lbryum_part_if_needed(repos['lbryum'])
|
|
|
|
|
2017-03-06 17:10:36 +01:00
|
|
|
# bumpversion will fail if there is already the tag we want in the repo
|
|
|
|
for repo in repos.values():
|
|
|
|
repo.assert_new_tag_is_absent()
|
|
|
|
|
2017-03-06 15:37:25 +01:00
|
|
|
for repo in repos.values():
|
2017-02-22 16:39:54 +01:00
|
|
|
logging.info('Processing repo: %s', repo.name)
|
2017-02-22 18:44:05 +01:00
|
|
|
repo.checkout(args.branch)
|
2017-02-21 22:13:41 +01:00
|
|
|
if repo.has_changes():
|
2017-02-22 16:39:54 +01:00
|
|
|
entry = repo.get_changelog_entry()
|
2017-02-22 20:06:38 +01:00
|
|
|
if entry:
|
|
|
|
changelogs[repo.name] = entry.strip()
|
|
|
|
repo.add_changelog()
|
|
|
|
else:
|
2017-02-22 18:52:46 +01:00
|
|
|
msg = 'Changelog entry is missing for {}'.format(repo.name)
|
|
|
|
if args.require_changelog:
|
|
|
|
raise Exception(msg)
|
|
|
|
else:
|
|
|
|
logging.warning(msg)
|
2017-02-21 22:13:41 +01:00
|
|
|
else:
|
2017-02-22 16:39:54 +01:00
|
|
|
logging.warning('Submodule %s has no changes.', repo.name)
|
2017-02-21 22:13:41 +01:00
|
|
|
if repo.name == 'lbryum':
|
|
|
|
# The other repos have their version track each other so need to bump
|
|
|
|
# them even if there aren't any changes, but lbryum should only be
|
|
|
|
# bumped if it has changes
|
|
|
|
continue
|
2017-02-22 16:39:54 +01:00
|
|
|
repo.bumpversion()
|
2017-02-21 04:58:29 +01:00
|
|
|
|
|
|
|
release_msg = get_release_msg(changelogs, names)
|
|
|
|
|
|
|
|
for name in names:
|
|
|
|
base.git.add(name)
|
|
|
|
|
2017-03-06 17:10:36 +01:00
|
|
|
base_repo.bumpversion()
|
2017-02-21 04:58:29 +01:00
|
|
|
current_tag = base.git.describe()
|
|
|
|
|
|
|
|
github_repo.create_git_release(current_tag, current_tag, release_msg, draft=True)
|
2017-02-21 20:48:29 +01:00
|
|
|
lbrynet_daemon_release_msg = changelogs.get('lbry', NO_CHANGE)
|
2017-02-23 19:26:31 +01:00
|
|
|
auth.get_repo('lbryio/lbry').create_git_release(
|
2017-02-21 20:48:29 +01:00
|
|
|
current_tag, current_tag, lbrynet_daemon_release_msg, draft=True)
|
|
|
|
|
2017-02-22 18:52:46 +01:00
|
|
|
if not args.skip_push:
|
2017-03-06 23:47:10 +01:00
|
|
|
for repo in repos.values():
|
2017-02-22 18:52:46 +01:00
|
|
|
repo.git.push(follow_tags=True)
|
|
|
|
base.git.push(follow_tags=True, recurse_submodules='check')
|
|
|
|
else:
|
|
|
|
logging.info('Skipping push; you will have to reset and delete tags if '
|
2017-03-06 17:10:36 +01:00
|
|
|
'you want to run this script again. Take a look at reset.sh; '
|
|
|
|
'it probably does what you want.')
|
2017-02-21 04:58:29 +01:00
|
|
|
|
|
|
|
|
2017-03-06 15:37:25 +01:00
|
|
|
def get_gh_token():
|
|
|
|
if 'GH_TOKEN' in os.environ:
|
|
|
|
gh_token = os.environ['GH_TOKEN']
|
|
|
|
else:
|
|
|
|
print TOKEN_MSG
|
|
|
|
inpt = raw_input('token: ')
|
|
|
|
gh_token = inpt.strip()
|
|
|
|
return gh_token
|
|
|
|
|
|
|
|
|
|
|
|
def get_lbryum_part_if_needed(repo):
|
|
|
|
if repo.has_changes():
|
|
|
|
if not repo.part:
|
|
|
|
get_lbryum_part(repo)
|
|
|
|
|
|
|
|
|
|
|
|
def get_lbryum_part(repo):
|
|
|
|
print LBRYUM_MSG
|
|
|
|
while True:
|
|
|
|
part = raw_input('part: ')
|
|
|
|
if part in LBRYUM_PARTS:
|
|
|
|
repo.part = part
|
|
|
|
break
|
|
|
|
print 'Invalid part. Enter one of {}'.format(', '.join(LBRYUM_PARTS))
|
|
|
|
|
|
|
|
|
2017-02-22 18:44:05 +01:00
|
|
|
def get_branch(repo_name, override=None):
|
|
|
|
if override:
|
|
|
|
return override
|
|
|
|
return DEFAULT_BRANCHES[repo_name]
|
|
|
|
|
2017-02-22 20:06:38 +01:00
|
|
|
|
2017-02-21 04:58:29 +01:00
|
|
|
def get_release_msg(changelogs, names):
|
|
|
|
lines = []
|
|
|
|
for name in names:
|
|
|
|
entry = changelogs.get(name)
|
|
|
|
if not entry:
|
|
|
|
continue
|
|
|
|
lines.append('## {}\n'.format(name))
|
|
|
|
lines.append('{}\n'.format(entry))
|
|
|
|
return '\n'.join(lines)
|
|
|
|
|
|
|
|
|
2017-02-22 18:44:05 +01:00
|
|
|
def run_sanity_checks(base, branch):
|
2017-02-21 04:58:29 +01:00
|
|
|
if base.is_dirty():
|
|
|
|
print 'Cowardly refusing to release a dirty repo'
|
|
|
|
sys.exit(1)
|
|
|
|
if base.active_branch.name != branch:
|
|
|
|
print 'Cowardly refusing to release when not on the {} branch'.format(branch)
|
|
|
|
sys.exit(1)
|
2017-02-22 16:39:54 +01:00
|
|
|
if is_behind(base, branch):
|
|
|
|
print 'Cowardly refusing to release when behind origin'
|
2017-02-21 04:58:29 +01:00
|
|
|
sys.exit(1)
|
|
|
|
check_bumpversion()
|
|
|
|
|
|
|
|
|
2017-02-22 16:39:54 +01:00
|
|
|
def is_behind(base, branch):
|
|
|
|
base.remotes.origin.fetch()
|
|
|
|
rev_list = '{branch}...origin/{branch}'.format(branch=branch)
|
|
|
|
commits_behind = base.git.rev_list(rev_list, right_only=True, count=True)
|
|
|
|
commits_behind = int(commits_behind)
|
|
|
|
return commits_behind > 0
|
|
|
|
|
|
|
|
|
2017-02-21 04:58:29 +01:00
|
|
|
def check_bumpversion():
|
|
|
|
|
2017-03-03 04:43:34 +01:00
|
|
|
def require_new_version():
|
2017-02-21 04:58:29 +01:00
|
|
|
print 'Install bumpversion: pip install -U git+https://github.com/lbryio/bumpversion.git'
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
try:
|
|
|
|
output = subprocess.check_output(['bumpversion', '-v'], stderr=subprocess.STDOUT)
|
|
|
|
output = output.strip()
|
|
|
|
if output != 'bumpversion 0.5.4-lbry':
|
2017-03-03 04:43:34 +01:00
|
|
|
require_new_version()
|
2017-02-21 20:33:11 +01:00
|
|
|
except (subprocess.CalledProcessError, OSError) as err:
|
2017-03-03 04:43:34 +01:00
|
|
|
require_new_version()
|
2017-02-21 04:58:29 +01:00
|
|
|
|
2017-02-22 16:39:54 +01:00
|
|
|
|
2017-02-21 04:58:29 +01:00
|
|
|
def get_part(args, name):
|
|
|
|
if name == 'lbry-web-ui':
|
2017-03-03 05:14:48 +01:00
|
|
|
return args.ui_part or args.lbry_part
|
2017-02-21 04:58:29 +01:00
|
|
|
else:
|
|
|
|
return getattr(args, name + '_part')
|
|
|
|
|
|
|
|
|
|
|
|
class Repo(object):
|
2017-03-06 17:10:36 +01:00
|
|
|
def __init__(self, name, part, directory=None):
|
2017-02-21 04:58:29 +01:00
|
|
|
self.name = name
|
2017-02-22 05:29:23 +01:00
|
|
|
self.part = part
|
2017-03-06 17:10:36 +01:00
|
|
|
self.directory = directory or os.path.join(os.getcwd(), name)
|
2017-02-21 04:58:29 +01:00
|
|
|
self.git_repo = git.Repo(self.directory)
|
|
|
|
self.saved_commit = None
|
2017-02-22 05:29:23 +01:00
|
|
|
self._bumped = False
|
2017-02-21 04:58:29 +01:00
|
|
|
|
|
|
|
def has_changes(self):
|
2017-02-22 16:39:54 +01:00
|
|
|
logging.info('%s =? %s', self.git_repo.commit(), self.saved_commit)
|
|
|
|
return self.git_repo.commit() != self.saved_commit
|
2017-02-21 04:58:29 +01:00
|
|
|
|
|
|
|
def save_commit(self):
|
|
|
|
self.saved_commit = self.git_repo.commit()
|
|
|
|
|
2017-02-22 18:44:05 +01:00
|
|
|
def checkout(self, override=None):
|
|
|
|
branch = get_branch(self.name, override)
|
2017-02-21 04:58:29 +01:00
|
|
|
self.git_repo.git.checkout(branch)
|
2017-02-22 18:44:05 +01:00
|
|
|
self.git_repo.git.pull(rebase=True)
|
2017-02-21 04:58:29 +01:00
|
|
|
|
|
|
|
def get_changelog_entry(self):
|
|
|
|
filename = os.path.join(self.directory, 'CHANGELOG.md')
|
2017-02-22 05:29:23 +01:00
|
|
|
return changelog.bump(filename, self.new_version())
|
|
|
|
|
2017-02-22 20:06:38 +01:00
|
|
|
def add_changelog(self):
|
|
|
|
with pushd(self.directory):
|
|
|
|
self.git_repo.git.add('CHANGELOG.md')
|
|
|
|
|
2017-02-22 05:29:23 +01:00
|
|
|
def new_version(self):
|
|
|
|
if self._bumped:
|
|
|
|
raise Exception('Cannot calculate a new version on an already bumped repo')
|
2017-02-22 16:39:54 +01:00
|
|
|
if not self.part:
|
|
|
|
raise Exception('Cannot calculate a new version without a part')
|
2017-02-22 05:29:23 +01:00
|
|
|
with pushd(self.directory):
|
|
|
|
output = subprocess.check_output(
|
|
|
|
['bumpversion', '--dry-run', '--list', '--allow-dirty', self.part])
|
2017-02-22 16:39:54 +01:00
|
|
|
return re.search('^new_version=(.*)$', output, re.M).group(1)
|
2017-02-21 04:58:29 +01:00
|
|
|
|
2017-02-22 05:29:23 +01:00
|
|
|
def bumpversion(self):
|
|
|
|
if self._bumped:
|
|
|
|
raise Exception('Cowardly refusing to bump a repo twice')
|
2017-02-22 16:39:54 +01:00
|
|
|
if not self.part:
|
|
|
|
raise Exception('Cannot bump version for {}: no part specified'.format(repo.name))
|
2017-02-21 04:58:29 +01:00
|
|
|
with pushd(self.directory):
|
2017-02-22 05:29:23 +01:00
|
|
|
subprocess.check_call(['bumpversion', '--allow-dirty', self.part])
|
|
|
|
self._bumped = True
|
2017-02-21 04:58:29 +01:00
|
|
|
|
2017-03-06 17:10:36 +01:00
|
|
|
def assert_new_tag_is_absent(self):
|
|
|
|
new_tag = 'v' + self.new_version()
|
|
|
|
tags = self.git_repo.git.tag()
|
|
|
|
if new_tag in tags.split('\n'):
|
|
|
|
raise Exception('Tag {} is already present in repo {}.'.format(new_tag, self.name))
|
|
|
|
|
|
|
|
def reset(self):
|
|
|
|
branch = get_branch(self.name)
|
|
|
|
self.git_repo.git.reset(branch, hard=True)
|
|
|
|
# TODO: also delete any extra tags that might have been added
|
|
|
|
|
2017-02-21 04:58:29 +01:00
|
|
|
@property
|
|
|
|
def git(self):
|
|
|
|
return self.git_repo.git
|
|
|
|
|
|
|
|
|
|
|
|
@contextlib.contextmanager
|
|
|
|
def pushd(new_dir):
|
|
|
|
previous_dir = os.getcwd()
|
|
|
|
os.chdir(new_dir)
|
|
|
|
yield
|
|
|
|
os.chdir(previous_dir)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2017-02-22 16:39:54 +01:00
|
|
|
logging.basicConfig(
|
|
|
|
format="%(asctime)s %(levelname)-8s %(name)s:%(lineno)d: %(message)s",
|
|
|
|
level='INFO'
|
|
|
|
)
|
2017-02-21 04:58:29 +01:00
|
|
|
sys.exit(main())
|
2017-02-21 22:13:41 +01:00
|
|
|
else:
|
|
|
|
log = logging.getLogger('__name__')
|