2018-12-09 14:13:53 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
from argparse import ArgumentParser, Namespace
|
2018-12-09 16:16:36 +00:00
|
|
|
from collections import OrderedDict
|
|
|
|
from configparser import RawConfigParser
|
2018-12-09 14:13:53 +00:00
|
|
|
from datetime import datetime
|
2018-12-09 16:16:36 +00:00
|
|
|
from json import dumps
|
2019-07-14 09:47:38 +01:00
|
|
|
from os import path, chdir
|
2018-12-09 14:13:53 +00:00
|
|
|
from subprocess import Popen, PIPE
|
|
|
|
|
2018-12-09 16:16:36 +00:00
|
|
|
from jinja2 import Environment, BaseLoader
|
|
|
|
from requests import patch
|
2018-12-09 14:13:53 +00:00
|
|
|
|
|
|
|
GIT_COMMANDS = {
|
|
|
|
"get_tag": ["git describe --tags --abbrev=0"],
|
2018-12-09 16:16:36 +00:00
|
|
|
"commit_version_change": [
|
2018-12-15 11:14:19 +00:00
|
|
|
"git add . && git commit -m 'Bumping up version from "
|
|
|
|
"{current_version} to {new_version}'"
|
|
|
|
],
|
|
|
|
"create_new_tag": [
|
|
|
|
"git tag -a {new_version} -m 'Bumping up version from "
|
|
|
|
"{current_version} to {new_version}'"
|
|
|
|
],
|
2018-12-09 14:13:53 +00:00
|
|
|
"push_tag": ["git push origin {new_version}"],
|
2018-12-15 11:14:19 +00:00
|
|
|
"get_change_log": [
|
|
|
|
'git log --no-merges --pretty=format:"%h::: %cn::: %s" '
|
|
|
|
"{current_version}.."
|
|
|
|
],
|
2018-12-09 14:13:53 +00:00
|
|
|
}
|
|
|
|
|
2018-12-15 11:14:19 +00:00
|
|
|
|
|
|
|
RELEASE_NOTE_TEMPLATE = """
|
2018-12-09 16:16:36 +00:00
|
|
|
# {{ release_name }} - {% now 'utc', '%Y-%m-%d' %}
|
|
|
|
|
|
|
|
To see the exhaustive list of pull requests included in this release see:
|
|
|
|
https://github.com/huge-success/sanic/milestone/{{milestone}}?closed=1
|
|
|
|
|
|
|
|
# Changelog
|
|
|
|
{% for row in changelogs %}
|
|
|
|
* {{ row -}}
|
|
|
|
{% endfor %}
|
|
|
|
|
|
|
|
# Credits
|
|
|
|
{% for author in authors %}
|
|
|
|
* {{ author -}}
|
|
|
|
{% endfor %}
|
|
|
|
"""
|
|
|
|
|
2018-12-15 11:14:19 +00:00
|
|
|
JINJA_RELEASE_NOTE_TEMPLATE = Environment(
|
|
|
|
loader=BaseLoader, extensions=["jinja2_time.TimeExtension"]
|
|
|
|
).from_string(RELEASE_NOTE_TEMPLATE)
|
2018-12-09 16:16:36 +00:00
|
|
|
|
2018-12-15 11:14:19 +00:00
|
|
|
RELEASE_NOTE_UPDATE_URL = (
|
|
|
|
"https://api.github.com/repos/huge-success/sanic/releases/tags/"
|
|
|
|
"{new_version}?access_token={token}"
|
|
|
|
)
|
2018-12-09 16:16:36 +00:00
|
|
|
|
2018-12-09 14:13:53 +00:00
|
|
|
|
2019-07-14 09:47:38 +01:00
|
|
|
class Directory:
|
|
|
|
def __init__(self):
|
|
|
|
self._old_path = path.dirname(path.abspath(__file__))
|
|
|
|
self._new_path = path.dirname(self._old_path)
|
|
|
|
|
|
|
|
def __enter__(self):
|
|
|
|
chdir(self._new_path)
|
|
|
|
|
|
|
|
def __exit__(self, exc_type, exc_val, exc_tb):
|
|
|
|
chdir(self._old_path)
|
|
|
|
|
|
|
|
|
2018-12-09 14:13:53 +00:00
|
|
|
def _run_shell_command(command: list):
|
|
|
|
try:
|
2018-12-15 11:14:19 +00:00
|
|
|
process = Popen(
|
|
|
|
command, stderr=PIPE, stdout=PIPE, stdin=PIPE, shell=True
|
|
|
|
)
|
2018-12-09 14:13:53 +00:00
|
|
|
output, error = process.communicate()
|
|
|
|
return_code = process.returncode
|
2018-12-09 16:16:36 +00:00
|
|
|
return output.decode("utf-8"), error, return_code
|
2018-12-09 14:13:53 +00:00
|
|
|
except:
|
|
|
|
return None, None, -1
|
|
|
|
|
|
|
|
|
|
|
|
def _fetch_default_calendar_release_version():
|
|
|
|
return datetime.now().strftime("%y.%m.0")
|
|
|
|
|
|
|
|
|
|
|
|
def _fetch_current_version(config_file: str) -> str:
|
|
|
|
if path.isfile(config_file):
|
|
|
|
config_parser = RawConfigParser()
|
|
|
|
with open(config_file) as cfg:
|
|
|
|
config_parser.read_file(cfg)
|
2018-12-15 11:14:19 +00:00
|
|
|
return (
|
|
|
|
config_parser.get("version", "current_version")
|
|
|
|
or _fetch_default_calendar_release_version()
|
|
|
|
)
|
2018-12-09 14:13:53 +00:00
|
|
|
else:
|
|
|
|
return _fetch_default_calendar_release_version()
|
|
|
|
|
|
|
|
|
|
|
|
def _change_micro_version(current_version: str):
|
|
|
|
version_string = current_version.split(".")
|
|
|
|
version_string[-1] = str((int(version_string[-1]) + 1))
|
|
|
|
return ".".join(version_string)
|
|
|
|
|
|
|
|
|
2018-12-15 11:14:19 +00:00
|
|
|
def _get_new_version(
|
|
|
|
config_file: str = "./setup.cfg",
|
|
|
|
current_version: str = None,
|
|
|
|
micro_release: bool = False,
|
|
|
|
):
|
2018-12-09 14:13:53 +00:00
|
|
|
if micro_release:
|
|
|
|
if current_version:
|
|
|
|
return _change_micro_version(current_version)
|
|
|
|
elif config_file:
|
|
|
|
return _change_micro_version(_fetch_current_version(config_file))
|
|
|
|
else:
|
|
|
|
return _fetch_default_calendar_release_version()
|
|
|
|
else:
|
|
|
|
return _fetch_default_calendar_release_version()
|
|
|
|
|
|
|
|
|
|
|
|
def _get_current_tag(git_command_name="get_tag"):
|
|
|
|
global GIT_COMMANDS
|
|
|
|
command = GIT_COMMANDS.get(git_command_name)
|
|
|
|
out, err, ret = _run_shell_command(command)
|
|
|
|
if len(str(out)):
|
|
|
|
return str(out).split("\n")[0]
|
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
2018-12-15 11:14:19 +00:00
|
|
|
def _update_release_version_for_sanic(
|
|
|
|
current_version, new_version, config_file
|
|
|
|
):
|
2018-12-09 14:13:53 +00:00
|
|
|
config_parser = RawConfigParser()
|
|
|
|
with open(config_file) as cfg:
|
|
|
|
config_parser.read_file(cfg)
|
|
|
|
config_parser.set("version", "current_version", new_version)
|
|
|
|
|
2019-07-13 08:22:41 +01:00
|
|
|
version_files = config_parser.get("version", "files")
|
2018-12-15 11:14:19 +00:00
|
|
|
current_version_line = config_parser.get(
|
|
|
|
"version", "current_version_pattern"
|
|
|
|
).format(current_version=current_version)
|
|
|
|
new_version_line = config_parser.get(
|
|
|
|
"version", "new_version_pattern"
|
|
|
|
).format(new_version=new_version)
|
2018-12-09 16:16:36 +00:00
|
|
|
|
2019-07-13 08:22:41 +01:00
|
|
|
for version_file in version_files.split(","):
|
|
|
|
with open(version_file) as init_file:
|
|
|
|
data = init_file.read()
|
2018-12-09 16:16:36 +00:00
|
|
|
|
2019-07-13 08:22:41 +01:00
|
|
|
new_data = data.replace(current_version_line, new_version_line)
|
|
|
|
with open(version_file, "w") as init_file:
|
|
|
|
init_file.write(new_data)
|
2018-12-09 16:16:36 +00:00
|
|
|
|
2018-12-09 14:13:53 +00:00
|
|
|
with open(config_file, "w") as config:
|
|
|
|
config_parser.write(config)
|
|
|
|
|
|
|
|
command = GIT_COMMANDS.get("commit_version_change")
|
2018-12-15 11:14:19 +00:00
|
|
|
command[0] = command[0].format(
|
|
|
|
new_version=new_version, current_version=current_version
|
|
|
|
)
|
2018-12-09 16:16:36 +00:00
|
|
|
_, err, ret = _run_shell_command(command)
|
2018-12-09 14:13:53 +00:00
|
|
|
if int(ret) != 0:
|
2018-12-15 11:14:19 +00:00
|
|
|
print(
|
|
|
|
"Failed to Commit Version upgrade changes to Sanic: {}".format(
|
|
|
|
err.decode("utf-8")
|
|
|
|
)
|
|
|
|
)
|
2018-12-09 16:16:36 +00:00
|
|
|
exit(1)
|
|
|
|
|
|
|
|
|
|
|
|
def _generate_change_log(current_version: str = None):
|
|
|
|
global GIT_COMMANDS
|
|
|
|
command = GIT_COMMANDS.get("get_change_log")
|
|
|
|
command[0] = command[0].format(current_version=current_version)
|
|
|
|
output, error, ret = _run_shell_command(command=command)
|
|
|
|
if not len(str(output)):
|
|
|
|
print("Unable to Fetch Change log details to update the Release Note")
|
2018-12-09 14:13:53 +00:00
|
|
|
exit(1)
|
|
|
|
|
2018-12-09 16:16:36 +00:00
|
|
|
commit_details = OrderedDict()
|
|
|
|
commit_details["authors"] = dict()
|
|
|
|
commit_details["commits"] = list()
|
|
|
|
|
|
|
|
for line in str(output).split("\n"):
|
|
|
|
commit, author, description = line.split(":::")
|
2018-12-15 11:14:19 +00:00
|
|
|
if "GitHub" not in author:
|
2018-12-09 16:16:36 +00:00
|
|
|
commit_details["authors"][author] = 1
|
|
|
|
commit_details["commits"].append(" - ".join([commit, description]))
|
|
|
|
|
|
|
|
return commit_details
|
2018-12-09 14:13:53 +00:00
|
|
|
|
2018-12-09 16:16:36 +00:00
|
|
|
|
2018-12-15 11:14:19 +00:00
|
|
|
def _generate_markdown_document(
|
|
|
|
milestone, release_name, current_version, release_version
|
|
|
|
):
|
|
|
|
global JINJA_RELEASE_NOTE_TEMPLATE
|
2018-12-09 16:16:36 +00:00
|
|
|
release_name = release_name or release_version
|
|
|
|
change_log = _generate_change_log(current_version=current_version)
|
2018-12-15 11:14:19 +00:00
|
|
|
return JINJA_RELEASE_NOTE_TEMPLATE.render(
|
|
|
|
release_name=release_name,
|
|
|
|
milestone=milestone,
|
|
|
|
changelogs=change_log["commits"],
|
|
|
|
authors=change_log["authors"].keys(),
|
|
|
|
)
|
2018-12-09 16:16:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
def _tag_release(new_version, current_version, milestone, release_name, token):
|
2018-12-09 14:13:53 +00:00
|
|
|
global GIT_COMMANDS
|
2018-12-09 16:16:36 +00:00
|
|
|
global RELEASE_NOTE_UPDATE_URL
|
|
|
|
for command_name in ["create_new_tag", "push_tag"]:
|
2018-12-09 14:13:53 +00:00
|
|
|
command = GIT_COMMANDS.get(command_name)
|
2018-12-15 11:14:19 +00:00
|
|
|
command[0] = command[0].format(
|
|
|
|
new_version=new_version, current_version=current_version
|
|
|
|
)
|
2018-12-09 14:13:53 +00:00
|
|
|
out, error, ret = _run_shell_command(command=command)
|
|
|
|
if int(ret) != 0:
|
|
|
|
print("Failed to execute the command: {}".format(command[0]))
|
|
|
|
exit(1)
|
|
|
|
|
2018-12-15 11:14:19 +00:00
|
|
|
change_log = _generate_markdown_document(
|
|
|
|
milestone, release_name, current_version, new_version
|
|
|
|
)
|
2018-12-09 16:16:36 +00:00
|
|
|
|
2018-12-15 11:14:19 +00:00
|
|
|
body = {"name": release_name or new_version, "body": change_log}
|
2018-12-09 16:16:36 +00:00
|
|
|
|
2018-12-15 11:14:19 +00:00
|
|
|
headers = {"content-type": "application/json"}
|
2018-12-09 16:16:36 +00:00
|
|
|
|
2018-12-15 11:14:19 +00:00
|
|
|
response = patch(
|
|
|
|
RELEASE_NOTE_UPDATE_URL.format(new_version=new_version, token=token),
|
|
|
|
data=dumps(body),
|
|
|
|
headers=headers,
|
|
|
|
)
|
2018-12-09 16:16:36 +00:00
|
|
|
response.raise_for_status()
|
|
|
|
|
2018-12-09 14:13:53 +00:00
|
|
|
|
|
|
|
def release(args: Namespace):
|
|
|
|
current_tag = _get_current_tag()
|
|
|
|
current_version = _fetch_current_version(args.config)
|
|
|
|
if current_tag and current_version not in current_tag:
|
2018-12-15 11:14:19 +00:00
|
|
|
print(
|
|
|
|
"Tag mismatch between what's in git and what was provided by "
|
|
|
|
"--current-version. Existing: {}, Give: {}".format(
|
|
|
|
current_tag, current_version
|
|
|
|
)
|
|
|
|
)
|
2018-12-09 14:13:53 +00:00
|
|
|
exit(1)
|
2018-12-15 11:14:19 +00:00
|
|
|
new_version = args.release_version or _get_new_version(
|
|
|
|
args.config, current_version, args.micro_release
|
|
|
|
)
|
|
|
|
_update_release_version_for_sanic(
|
|
|
|
current_version=current_version,
|
|
|
|
new_version=new_version,
|
|
|
|
config_file=args.config,
|
|
|
|
)
|
|
|
|
_tag_release(
|
|
|
|
current_version=current_version,
|
|
|
|
new_version=new_version,
|
|
|
|
milestone=args.milestone,
|
|
|
|
release_name=args.release_name,
|
|
|
|
token=args.token,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2018-12-09 14:13:53 +00:00
|
|
|
cli = ArgumentParser(description="Sanic Release Manager")
|
2018-12-15 11:14:19 +00:00
|
|
|
cli.add_argument(
|
|
|
|
"--release-version",
|
|
|
|
"-r",
|
|
|
|
help="New Version to use for Release",
|
|
|
|
default=_fetch_default_calendar_release_version(),
|
|
|
|
required=False,
|
|
|
|
)
|
|
|
|
cli.add_argument(
|
|
|
|
"--current-version",
|
|
|
|
"-cv",
|
|
|
|
help="Current Version to default in case if you don't want to "
|
|
|
|
"use the version configuration files",
|
|
|
|
default=None,
|
|
|
|
required=False,
|
|
|
|
)
|
|
|
|
cli.add_argument(
|
|
|
|
"--config",
|
|
|
|
"-c",
|
|
|
|
help="Configuration file used for release",
|
|
|
|
default="./setup.cfg",
|
|
|
|
required=False,
|
|
|
|
)
|
|
|
|
cli.add_argument(
|
|
|
|
"--token",
|
|
|
|
"-t",
|
|
|
|
help="Git access token with necessary access to Huge Sanic Org",
|
|
|
|
required=True,
|
|
|
|
)
|
|
|
|
cli.add_argument(
|
|
|
|
"--milestone",
|
|
|
|
"-ms",
|
|
|
|
help="Git Release milestone information to include in relase note",
|
|
|
|
required=True,
|
|
|
|
)
|
|
|
|
cli.add_argument(
|
|
|
|
"--release-name",
|
|
|
|
"-n",
|
|
|
|
help="Release Name to use if any",
|
|
|
|
required=False,
|
|
|
|
)
|
|
|
|
cli.add_argument(
|
|
|
|
"--micro-release",
|
|
|
|
"-m",
|
|
|
|
help="Micro Release with patches only",
|
|
|
|
default=False,
|
|
|
|
action="store_true",
|
|
|
|
required=False,
|
|
|
|
)
|
2018-12-09 14:13:53 +00:00
|
|
|
args = cli.parse_args()
|
2019-07-23 23:58:11 +01:00
|
|
|
with Directory():
|
2019-07-14 09:47:38 +01:00
|
|
|
release(args)
|