| 1 |
# mypy: disallow-untyped-defs |
| 2 |
"""Invoke development tasks.""" |
| 3 |
|
| 4 |
from __future__ import annotations |
| 5 |
|
| 6 |
import argparse |
| 7 |
import os |
| 8 |
from pathlib import Path |
| 9 |
import re |
| 10 |
from subprocess import call |
| 11 |
from subprocess import check_call |
| 12 |
from subprocess import check_output |
| 13 |
|
| 14 |
from colorama import Fore |
| 15 |
from colorama import init |
| 16 |
|
| 17 |
|
| 18 |
def announce(version: str, template_name: str, doc_version: str) -> None: |
| 19 |
"""Generates a new release announcement entry in the docs.""" |
| 20 |
# Get our list of authors and co-authors. |
| 21 |
stdout = check_output(["git", "describe", "--abbrev=0", "--tags"], encoding="UTF-8") |
| 22 |
last_version = stdout.strip() |
| 23 |
rev_range = f"{last_version}..HEAD" |
| 24 |
|
| 25 |
authors = check_output( |
| 26 |
["git", "log", rev_range, "--format=%aN"], encoding="UTF-8" |
| 27 |
).splitlines() |
| 28 |
|
| 29 |
co_authors_output = check_output( |
| 30 |
["git", "log", rev_range, "--format=%(trailers:key=Co-authored-by) "], |
| 31 |
encoding="UTF-8", |
| 32 |
) |
| 33 |
co_authors: list[str] = [] |
| 34 |
for co_author_line in co_authors_output.splitlines(): |
| 35 |
if m := re.search(r"Co-authored-by: (.+?)<", co_author_line): |
| 36 |
co_authors.append(m.group(1).strip()) |
| 37 |
|
| 38 |
contributors = { |
| 39 |
name |
| 40 |
for name in authors + co_authors |
| 41 |
if not name.endswith("[bot]") and name != "pytest bot" |
| 42 |
} |
| 43 |
|
| 44 |
template_text = ( |
| 45 |
Path(__file__).parent.joinpath(template_name).read_text(encoding="UTF-8") |
| 46 |
) |
| 47 |
|
| 48 |
contributors_text = "\n".join(f"* {name}" for name in sorted(contributors)) + "\n" |
| 49 |
text = template_text.format( |
| 50 |
version=version, contributors=contributors_text, doc_version=doc_version |
| 51 |
) |
| 52 |
|
| 53 |
target = Path(__file__).parent.joinpath(f"../doc/en/announce/release-{version}.rst") |
| 54 |
target.write_text(text, encoding="UTF-8") |
| 55 |
print(f"{Fore.CYAN}[generate.announce] {Fore.RESET}Generated {target.name}") |
| 56 |
|
| 57 |
# Update index with the new release entry |
| 58 |
index_path = Path(__file__).parent.joinpath("../doc/en/announce/index.rst") |
| 59 |
lines = index_path.read_text(encoding="UTF-8").splitlines() |
| 60 |
indent = " " |
| 61 |
for index, line in enumerate(lines): |
| 62 |
if line.startswith(f"{indent}release-"): |
| 63 |
new_line = indent + target.stem |
| 64 |
if line != new_line: |
| 65 |
lines.insert(index, new_line) |
| 66 |
index_path.write_text("\n".join(lines) + "\n", encoding="UTF-8") |
| 67 |
print( |
| 68 |
f"{Fore.CYAN}[generate.announce] {Fore.RESET}Updated {index_path.name}" |
| 69 |
) |
| 70 |
else: |
| 71 |
print( |
| 72 |
f"{Fore.CYAN}[generate.announce] {Fore.RESET}Skip {index_path.name} (already contains release)" |
| 73 |
) |
| 74 |
break |
| 75 |
|
| 76 |
check_call(["git", "add", str(target)]) |
| 77 |
|
| 78 |
|
| 79 |
def regen(version: str) -> None: |
| 80 |
"""Call regendoc tool to update examples and pytest output in the docs.""" |
| 81 |
print(f"{Fore.CYAN}[generate.regen] {Fore.RESET}Updating docs") |
| 82 |
check_call( |
| 83 |
["tox", "-e", "regen"], |
| 84 |
env={**os.environ, "SETUPTOOLS_SCM_PRETEND_VERSION_FOR_PYTEST": version}, |
| 85 |
) |
| 86 |
|
| 87 |
|
| 88 |
def fix_formatting() -> None: |
| 89 |
"""Runs pre-commit in all files to ensure they are formatted correctly""" |
| 90 |
print( |
| 91 |
f"{Fore.CYAN}[generate.fix linting] {Fore.RESET}Fixing formatting using pre-commit" |
| 92 |
) |
| 93 |
call(["pre-commit", "run", "--all-files"]) |
| 94 |
|
| 95 |
|
| 96 |
def check_links() -> None: |
| 97 |
"""Runs sphinx-build to check links""" |
| 98 |
print(f"{Fore.CYAN}[generate.check_links] {Fore.RESET}Checking links") |
| 99 |
check_call(["tox", "-e", "docs-checklinks"]) |
| 100 |
|
| 101 |
|
| 102 |
def pre_release( |
| 103 |
version: str, template_name: str, doc_version: str, *, skip_check_links: bool |
| 104 |
) -> None: |
| 105 |
"""Generates new docs, release announcements and creates a local tag.""" |
| 106 |
announce(version, template_name, doc_version) |
| 107 |
regen(version) |
| 108 |
changelog(version, write_out=True) |
| 109 |
fix_formatting() |
| 110 |
if not skip_check_links: |
| 111 |
check_links() |
| 112 |
|
| 113 |
msg = f"Prepare release version {version}" |
| 114 |
check_call(["git", "commit", "-a", "-m", msg]) |
| 115 |
|
| 116 |
print() |
| 117 |
print(f"{Fore.CYAN}[generate.pre_release] {Fore.GREEN}All done!") |
| 118 |
print() |
| 119 |
print("Please push your branch and open a PR.") |
| 120 |
|
| 121 |
|
| 122 |
def changelog(version: str, write_out: bool = False) -> None: |
| 123 |
addopts = [] if write_out else ["--draft"] |
| 124 |
check_call(["towncrier", "build", "--yes", "--version", version, *addopts]) |
| 125 |
|
| 126 |
|
| 127 |
def main() -> None: |
| 128 |
init(autoreset=True) |
| 129 |
parser = argparse.ArgumentParser() |
| 130 |
parser.add_argument("version", help="Release version") |
| 131 |
parser.add_argument( |
| 132 |
"template_name", help="Name of template file to use for release announcement" |
| 133 |
) |
| 134 |
parser.add_argument( |
| 135 |
"doc_version", help="For prereleases, the version to link to in the docs" |
| 136 |
) |
| 137 |
parser.add_argument("--skip-check-links", action="store_true", default=False) |
| 138 |
options = parser.parse_args() |
| 139 |
pre_release( |
| 140 |
options.version, |
| 141 |
options.template_name, |
| 142 |
options.doc_version, |
| 143 |
skip_check_links=options.skip_check_links, |
| 144 |
) |
| 145 |
|
| 146 |
|
| 147 |
if __name__ == "__main__": |
| 148 |
main() |