-
Notifications
You must be signed in to change notification settings - Fork 107
/
build.py
326 lines (283 loc) · 8.62 KB
/
build.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
# Copyright (c) Amber Brown, 2015
# See LICENSE for details.
"""
Build a combined news file from news fragments.
"""
from __future__ import annotations
import os
import sys
from datetime import date
import click
from click import Context, Option, UsageError
from towncrier import _git
from ._builder import find_fragments, render_fragments, split_fragments
from ._project import get_project_name, get_version
from ._settings import ConfigError, config_option_help, load_config_from_options
from ._writer import append_to_newsfile
if sys.version_info < (3, 10):
import importlib_resources as resources
else:
from importlib import resources
def _get_date() -> str:
return date.today().isoformat()
def _validate_answer(ctx: Context, param: Option, value: bool) -> bool:
value_check = (
ctx.params.get("answer_yes")
if param.name == "answer_keep"
else ctx.params.get("answer_keep")
)
if value_check and value:
click.echo("You can not choose both --yes and --keep at the same time")
ctx.abort()
return value
@click.command(name="build")
@click.option(
"--draft",
"draft",
default=False,
flag_value=True,
help=(
"Render the news fragments to standard output. "
"Don't write to files, don't check versions."
),
)
@click.option(
"--config",
"config_file",
default=None,
metavar="FILE_PATH",
help=config_option_help,
)
@click.option(
"--dir",
"directory",
default=None,
metavar="PATH",
help="Build fragment in directory. Default to current directory.",
)
@click.option(
"--name",
"project_name",
default=None,
help="Pass a custom project name.",
)
@click.option(
"--version",
"project_version",
default=None,
help="Render the news fragments using given version.",
)
@click.option(
"--date",
"project_date",
default=None,
help="Render the news fragments using the given date.",
)
@click.option(
"--yes",
"answer_yes",
default=None,
flag_value=True,
help="Do not ask for confirmation to remove news fragments.",
callback=_validate_answer,
)
@click.option(
"--keep",
"answer_keep",
default=None,
flag_value=True,
help="Do not ask for confirmations. But keep news fragments.",
callback=_validate_answer,
)
def _main(
draft: bool,
directory: str | None,
config_file: str | None,
project_name: str | None,
project_version: str | None,
project_date: str | None,
answer_yes: bool,
answer_keep: bool,
) -> None:
"""
Build a combined news file from news fragment.
"""
try:
return __main(
draft,
directory,
config_file,
project_name,
project_version,
project_date,
answer_yes,
answer_keep,
)
except ConfigError as e:
print(e, file=sys.stderr)
sys.exit(1)
def __main(
draft: bool,
directory: str | None,
config_file: str | None,
project_name: str | None,
project_version: str | None,
project_date: str | None,
answer_yes: bool,
answer_keep: bool,
) -> None:
"""
The main entry point.
"""
base_directory, config = load_config_from_options(directory, config_file)
to_err = draft
if project_version is None:
project_version = config.version
if project_version is None:
if not config.package:
raise UsageError(
"'--version' is required since the config file does "
"not contain 'version' or 'package'."
)
project_version = get_version(
os.path.join(base_directory, config.package_dir), config.package
).strip()
click.echo("Loading template...", err=to_err)
if isinstance(config.template, tuple):
template = resources.read_text(*config.template)
else:
with open(config.template, encoding="utf-8") as tmpl:
template = tmpl.read()
click.echo("Finding news fragments...", err=to_err)
if config.directory is not None:
fragment_base_directory = os.path.abspath(config.directory)
fragment_directory = None
else:
fragment_base_directory = os.path.abspath(
os.path.join(base_directory, config.package_dir, config.package)
)
fragment_directory = "newsfragments"
fragment_contents, fragment_filenames = find_fragments(
fragment_base_directory,
config.sections,
fragment_directory,
config.types,
config.orphan_prefix,
)
click.echo("Rendering news fragments...", err=to_err)
fragments = split_fragments(
fragment_contents, config.types, all_bullets=config.all_bullets
)
if project_name is None:
project_name = config.name
if not project_name:
package = config.package
if package:
project_name = get_project_name(
os.path.abspath(os.path.join(base_directory, config.package_dir)),
package,
)
else:
# Can't determine a project_name, but maybe it is not needed.
project_name = ""
if project_date is None:
project_date = _get_date().strip()
if config.title_format:
top_line = config.title_format.format(
name=project_name, version=project_version, project_date=project_date
)
render_title_with_fragments = False
render_title_separately = True
elif config.title_format is False:
# This is an odd check but since we support both "" and False with
# different effects we have to do something a bit abnormal here.
top_line = ""
render_title_separately = False
render_title_with_fragments = False
else:
top_line = ""
render_title_separately = False
render_title_with_fragments = True
rendered = render_fragments(
# The 0th underline is used for the top line
template,
config.issue_format,
fragments,
config.types,
config.underlines[1:],
config.wrap,
{"name": project_name, "version": project_version, "date": project_date},
top_underline=config.underlines[0],
all_bullets=config.all_bullets,
render_title=render_title_with_fragments,
)
if render_title_separately:
content = "\n".join(
[
top_line,
config.underlines[0] * len(top_line),
rendered,
]
)
else:
content = rendered
if draft:
click.echo(
"Draft only -- nothing has been written.\n"
"What is seen below is what would be written.\n",
err=to_err,
)
click.echo(content)
return
click.echo("Writing to newsfile...", err=to_err)
news_file = config.filename
if config.single_file is False:
# The release notes for each version are stored in a separate file.
# The name of that file is generated based on the current version and project.
news_file = news_file.format(
name=project_name, version=project_version, project_date=project_date
)
append_to_newsfile(
base_directory,
news_file,
config.start_string,
top_line,
content,
single_file=config.single_file,
)
click.echo("Staging newsfile...", err=to_err)
_git.stage_newsfile(base_directory, news_file)
if should_remove_fragment_files(
fragment_filenames,
answer_yes,
answer_keep,
):
click.echo("Removing news fragments...", err=to_err)
_git.remove_files(fragment_filenames)
click.echo("Done!", err=to_err)
def should_remove_fragment_files(
fragment_filenames: list[str],
answer_yes: bool,
answer_keep: bool,
) -> bool:
if not fragment_filenames:
click.echo("No news fragments to remove. Skipping!")
return False
try:
if answer_keep:
click.echo("Keeping the following files:")
# Not proceeding with the removal of the files.
return False
if answer_yes:
click.echo("Removing the following files:")
else:
click.echo("I want to remove the following files:")
finally:
# Will always be printed, even for answer_keep to help with possible troubleshooting
for filename in fragment_filenames:
click.echo(filename)
if answer_yes or click.confirm("Is it okay if I remove those files?", default=True):
return True
return False
if __name__ == "__main__": # pragma: no cover
_main()