-
-
Notifications
You must be signed in to change notification settings - Fork 678
/
Copy pathchangelog.py
executable file
·287 lines (227 loc) · 8.33 KB
/
changelog.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#!/usr/bin/env python3
import datetime
from pathlib import Path
import re
import subprocess
import click
from typing import Iterator
LINK_RE = re.compile(r"\[#(\d+)\]")
ISSUE_URL = "https://github.com/trezor/trezor-firmware/pull/{issue}"
VERSION_HEADER_RE = re.compile(r"## \[([.0-9]+)\]")
DIFF_LINK = "[{new}]: https://github.com/trezor/trezor-firmware/compare/{tag_prefix}{old}...{tag_prefix}{new}\n"
MODELS_RE = re.compile(r"\[([A-Z0-9]{4})(,[A-Z0-9]{4})*\][ ]?")
INTERNAL_MODELS = ("T2T1", "T2B1", "T3B1", "T3T1", "D001")
INTERNAL_MODELS_SKIP = ("D001",)
ROOT = Path(__file__).parent.parent.resolve()
# Source of truth for all managed changelogs in this repository.
# Please extend it when adding a new project with a managed changelog.
KNOWN_PROJECTS = (
ROOT / "core",
ROOT / "core/embed/projects/boardloader",
ROOT / "core/embed/projects/bootloader",
ROOT / "core/embed/projects/bootloader_ci",
ROOT / "core/embed/projects/prodtest",
ROOT / "legacy/bootloader",
ROOT / "legacy/firmware",
ROOT / "legacy/intermediate_fw",
ROOT / "python",
)
for project in KNOWN_PROJECTS:
assert project.is_dir(), f"Project {project} does not exist"
IGNORED_FILES = (".gitignore", ".keep")
def linkify_changelog(changelog_file: Path, only_check: bool = False) -> bool:
links = {}
orig_links = {}
result_lines = []
with open(changelog_file, "r+") as changelog:
for line in changelog:
m = LINK_RE.match(line)
if m: # line *starts with* issue identifier
# keep existing links as-is
orig_links[int(m[1])] = line.replace(m[0] + ": ", "").strip()
else:
for issue in LINK_RE.findall(line):
links[int(issue)] = ISSUE_URL.format(issue=issue)
result_lines.append(line)
if only_check:
missing_links = set(links.keys()) - set(orig_links.keys())
if missing_links:
click.echo(f"missing links: {missing_links}")
return False
else:
return True
links.update(orig_links)
changelog.seek(0)
changelog.truncate(0)
for line in result_lines:
changelog.write(line)
for marker, url in sorted(links.items()):
changelog.write(f"[#{marker}]: {url}\n")
return True
def linkify_gh_diff(changelog_file: Path, tag_prefix: str):
linkified = False
versions = []
result_lines = []
with open(changelog_file, "r+") as changelog:
for line in changelog:
m = VERSION_HEADER_RE.match(line)
if m:
versions.append(m[1])
result_lines.append(line)
changelog.seek(0)
changelog.truncate(0)
for line in result_lines:
changelog.write(line)
if not linkified and VERSION_HEADER_RE.match(line):
changelog.write(
DIFF_LINK.format(
tag_prefix=tag_prefix, new=versions[0], old=versions[1]
)
)
linkified = True
def current_date(project: Path) -> str:
parts = project.parts
today = datetime.datetime.now()
if (
parts[-3:] == ("core", "embed", "boardloader")
or parts[-3:] == ("core", "embed", "bootloader")
or parts[-3:] == ("core", "embed", "bootloader_ci")
or parts[-2:] == ("legacy", "bootloader")
or parts[-2:] == ("legacy", "intermediate_fw")
):
return today.strftime("%B %Y")
elif parts[-1] == "python":
return today.strftime("%Y-%m-%d")
else:
daysuffix = {1: "st", 2: "nd", 3: "rd"}.get(today.day % 10, "th")
return today.strftime(f"%-d{daysuffix} %B %Y")
def filter_changelog(changelog_file: Path, internal_name: str):
def filter_line(line: str) -> str | None:
m = MODELS_RE.search(line)
if not m:
return line
if internal_name in m[0]:
return MODELS_RE.sub("", line, count=1)
else:
return None
destination_file = changelog_file.with_suffix(f".{internal_name}.md")
with open(changelog_file, "r") as changelog, open(
destination_file, "w"
) as destination:
for line in changelog:
res = filter_line(line)
if res is not None:
destination.write(res)
def _iter_fragments(project: Path) -> Iterator[Path]:
fragements_dir = project / ".changelog.d"
for fragment in fragements_dir.iterdir():
if fragment.name in IGNORED_FILES:
continue
yield fragment
def check_fragments_style(project: Path):
success = True
for fragment in _iter_fragments(project):
fragment_text = fragment.read_text().rstrip()
if not fragment_text.endswith("."):
click.echo(f"Changelog '{fragment}' must end with a period.")
success = False
if not success:
raise click.ClickException(f"Changelog style error: {project}")
else:
click.echo(f"Changelog style OK: {project}")
def fix_fragments_style(project: Path):
for fragment in _iter_fragments(project):
fragment_text = fragment.read_text().rstrip()
if not fragment_text.endswith("."):
fragment.write_text(fragment_text + ".\n")
click.echo(f"Changelog '{fragment}' style fixed.")
def generate_filtered(project: Path, changelog: Path):
if project.parts[-1] != "core":
return
for internal_name in INTERNAL_MODELS:
if internal_name in INTERNAL_MODELS_SKIP:
continue
filter_changelog(changelog, internal_name)
@click.group()
def cli():
pass
@cli.command()
def check():
"""Check the style of all changelog fragments."""
for project in KNOWN_PROJECTS:
check_fragments_style(project)
@cli.command()
def style():
"""Fix the style of all changelog fragments."""
for project in KNOWN_PROJECTS:
fix_fragments_style(project)
@cli.command()
@click.argument(
"project",
type=click.Path(
exists=True,
dir_okay=True,
file_okay=False,
resolve_path=True,
path_type=Path,
),
)
@click.argument(
"version",
type=str,
required=False,
)
@click.option("--date", help="Specify release date (default: today).")
@click.option(
"--check", is_flag=True, help="Dry run, do not actually create changelog."
)
@click.option(
"--only-models",
is_flag=True,
help="Only regenerate the model-changelogs from the main one.",
)
def generate(project, version, date, check, only_models):
"""Generate changelog for given project (core, python, legacy/firmware,
legacy/bootloader).
- Run towncrier to assemble changelog from fragments in .changelog.d/.
- Find all occurences of "[#123]" in text, and add a Markdown link to the
referenced issue.
- Tell git to stage changed files.
"""
if project not in KNOWN_PROJECTS:
try:
path_for_print = project.relative_to(ROOT)
except ValueError:
raise click.ClickException(
f"Paths outside the repository are not supported (path was: {project})."
)
raise click.ClickException(
f"Please add '{path_for_print}' to `KNOWN_PROJECTS` to be part of our managed changelogs."
)
changelog = project / "CHANGELOG.md"
if not changelog.exists():
raise click.ClickException(f"{changelog} not found")
if version is None:
if not check and not only_models:
raise click.ClickException("Version argument is required.")
version = "unreleased"
if date is None:
date = current_date(project)
if only_models:
generate_filtered(project, changelog)
return 0
args = ["towncrier", "build", "--yes", "--version", version, "--date", date]
if check:
args.append("--draft")
subprocess.run(args, cwd=project, check=True)
if not check:
linkify_changelog(changelog)
# python changelog has links to github diffs
if project.parts[-1] == "python":
linkify_gh_diff(changelog, tag_prefix="python/v")
# core changelog for each model
generate_filtered(project, changelog)
# towncrier calls git add before we do linkification, stage the changes too
subprocess.run(["git", "add", changelog], check=True)
if __name__ == "__main__":
cli()