test_ok2/scripts/release.py

118 lines
3.9 KiB
Python
Raw Normal View History

"""Invoke development tasks."""
import argparse
2020-07-09 05:38:29 +08:00
import os
from pathlib import Path
from subprocess import call
from subprocess import check_call
from subprocess import check_output
from colorama import Fore
from colorama import init
def announce(version):
"""Generates a new release announcement entry in the docs."""
# Get our list of authors
2018-05-23 22:48:46 +08:00
stdout = check_output(["git", "describe", "--abbrev=0", "--tags"])
stdout = stdout.decode("utf-8")
last_version = stdout.strip()
2020-10-03 04:16:22 +08:00
stdout = check_output(["git", "log", f"{last_version}..HEAD", "--format=%aN"])
2018-05-23 22:48:46 +08:00
stdout = stdout.decode("utf-8")
contributors = {name for name in stdout.splitlines() if not name.endswith("[bot]")}
template_name = (
"release.minor.rst" if version.endswith(".0") else "release.patch.rst"
)
template_text = (
Path(__file__).parent.joinpath(template_name).read_text(encoding="UTF-8")
2018-05-23 22:48:46 +08:00
)
2020-10-03 04:16:22 +08:00
contributors_text = "\n".join(f"* {name}" for name in sorted(contributors)) + "\n"
text = template_text.format(version=version, contributors=contributors_text)
2020-10-03 04:16:22 +08:00
target = Path(__file__).parent.joinpath(f"../doc/en/announce/release-{version}.rst")
2018-05-23 22:48:46 +08:00
target.write_text(text, encoding="UTF-8")
print(f"{Fore.CYAN}[generate.announce] {Fore.RESET}Generated {target.name}")
# Update index with the new release entry
2018-05-23 22:48:46 +08:00
index_path = Path(__file__).parent.joinpath("../doc/en/announce/index.rst")
lines = index_path.read_text(encoding="UTF-8").splitlines()
indent = " "
for index, line in enumerate(lines):
2020-10-03 04:16:22 +08:00
if line.startswith(f"{indent}release-"):
new_line = indent + target.stem
if line != new_line:
lines.insert(index, new_line)
2018-05-23 22:48:46 +08:00
index_path.write_text("\n".join(lines) + "\n", encoding="UTF-8")
print(
f"{Fore.CYAN}[generate.announce] {Fore.RESET}Updated {index_path.name}"
)
else:
2018-05-23 22:48:46 +08:00
print(
f"{Fore.CYAN}[generate.announce] {Fore.RESET}Skip {index_path.name} (already contains release)"
2018-05-23 22:48:46 +08:00
)
break
2018-05-23 22:48:46 +08:00
check_call(["git", "add", str(target)])
2020-07-09 05:38:29 +08:00
def regen(version):
"""Call regendoc tool to update examples and pytest output in the docs."""
print(f"{Fore.CYAN}[generate.regen] {Fore.RESET}Updating docs")
2020-07-09 05:38:29 +08:00
check_call(
["tox", "-e", "regen"],
env={**os.environ, "SETUPTOOLS_SCM_PRETEND_VERSION": version},
)
def fix_formatting():
"""Runs pre-commit in all files to ensure they are formatted correctly"""
print(
f"{Fore.CYAN}[generate.fix linting] {Fore.RESET}Fixing formatting using pre-commit"
)
call(["pre-commit", "run", "--all-files"])
2019-07-13 22:06:56 +08:00
def check_links():
"""Runs sphinx-build to check links"""
print(f"{Fore.CYAN}[generate.check_links] {Fore.RESET}Checking links")
check_call(["tox", "-e", "docs-checklinks"])
def pre_release(version, *, skip_check_links):
"""Generates new docs, release announcements and creates a local tag."""
announce(version)
2020-07-09 05:38:29 +08:00
regen(version)
changelog(version, write_out=True)
fix_formatting()
if not skip_check_links:
check_links()
2020-10-03 04:16:22 +08:00
msg = f"Prepare release version {version}"
2018-05-23 22:48:46 +08:00
check_call(["git", "commit", "-a", "-m", msg])
print()
print(f"{Fore.CYAN}[generate.pre_release] {Fore.GREEN}All done!")
print()
2020-01-17 12:53:54 +08:00
print("Please push your branch and open a PR.")
2017-05-23 06:06:53 +08:00
def changelog(version, write_out=False):
addopts = [] if write_out else ["--draft"]
2018-05-23 22:48:46 +08:00
check_call(["towncrier", "--yes", "--version", version] + addopts)
def main():
init(autoreset=True)
parser = argparse.ArgumentParser()
parser.add_argument("version", help="Release version")
parser.add_argument("--skip-check-links", action="store_true", default=False)
options = parser.parse_args()
pre_release(options.version, skip_check_links=options.skip_check_links)
if __name__ == "__main__":
main()