-
Notifications
You must be signed in to change notification settings - Fork 242
Expand file tree
/
Copy pathbump_version.py
More file actions
344 lines (302 loc) · 11.8 KB
/
bump_version.py
File metadata and controls
344 lines (302 loc) · 11.8 KB
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
"""Bumps the version number in all appropriate files for
a nf-core pipeline.
"""
import logging
import re
from pathlib import Path
import rich.console
from ruamel.yaml import YAML
import nf_core.utils
from nf_core.pipelines.rocrate import ROCrate
from nf_core.utils import Pipeline
log = logging.getLogger(__name__)
stderr = rich.console.Console(stderr=True, force_terminal=nf_core.utils.rich_force_colors())
def bump_pipeline_version(pipeline_obj: Pipeline, new_version: str) -> None:
"""Bumps a pipeline version number.
Args:
pipeline_obj (nf_core.utils.Pipeline): A `Pipeline` object that holds information
about the pipeline contents and build files.
new_version (str): The new version tag for the pipeline. Semantic versioning only.
"""
# Collect the old and new version numbers
current_version = pipeline_obj.nf_config.get("manifest.version", "").strip(" '\"")
if new_version.startswith("v"):
log.warning("Stripping leading 'v' from new version number")
new_version = new_version[1:]
if not current_version:
raise UserWarning("Could not find config variable 'manifest.version'")
if current_version == new_version:
raise UserWarning(f"Current version is already: {current_version}")
log.info(f"Changing version number from '{current_version}' to '{new_version}'")
# nextflow.config - workflow manifest version
update_file_version(
"nextflow.config",
pipeline_obj,
[
(
rf"(version\s*=\s*['\"]){re.escape(current_version)}(['\"])",
rf"\g<1>{new_version}\g<2>",
)
],
)
# multiqc_config.yaml
multiqc_new_version = "dev" if "dev" in new_version else new_version
multiqc_current_version = "dev" if "dev" in current_version else current_version
if multiqc_current_version != "dev" and multiqc_new_version != "dev":
update_file_version(
Path("assets", "multiqc_config.yml"),
pipeline_obj,
[
(
f"/releases/tag/{current_version}",
f"/releases/tag/{new_version}",
)
],
yaml_key=["report_comment"],
)
if multiqc_current_version != "dev" and multiqc_new_version == "dev":
update_file_version(
Path("assets", "multiqc_config.yml"),
pipeline_obj,
[
(
f"/releases/tag/{current_version}",
"/tree/dev",
)
],
yaml_key=["report_comment"],
)
if multiqc_current_version == "dev" and multiqc_new_version != "dev":
update_file_version(
Path("assets", "multiqc_config.yml"),
pipeline_obj,
[
(
"/tree/dev",
f"/releases/tag/{multiqc_new_version}",
)
],
yaml_key=["report_comment"],
)
update_file_version(
Path("assets", "multiqc_config.yml"),
pipeline_obj,
[
(
f"/{multiqc_current_version}/",
f"/{multiqc_new_version}/",
),
],
yaml_key=["report_comment"],
)
# nf-test snap files
pipeline_name = pipeline_obj.nf_config.get("manifest.name", "").strip(" '\"")
snap_files = [f.relative_to(pipeline_obj.wf_path) for f in Path(pipeline_obj.wf_path).glob("tests/pipeline/*.snap")]
for snap_file in snap_files:
update_file_version(
snap_file,
pipeline_obj,
[
(
f"{pipeline_name}={current_version}",
f"{pipeline_name}={new_version}",
)
],
required=False,
)
# .nf-core.yml - pipeline version
# update entry: version: 1.0.0dev, but not `nf_core_version`, or `bump_version`
update_file_version(
".nf-core.yml",
pipeline_obj,
[
(
current_version,
new_version,
)
],
required=False,
yaml_key=["template", "version"],
)
# SVG files in docs/images/ - version badges
svg_files = [f.relative_to(pipeline_obj.wf_path) for f in Path(pipeline_obj.wf_path).glob("docs/images/*.svg")]
for svg_file in svg_files:
update_file_version(
svg_file,
pipeline_obj,
[
(
rf"(>v?){re.escape(current_version)}(<)",
rf"\g<1>{new_version}\g<2>",
)
],
required=False,
)
# Throw a warning if PDF or PNG versions of the SVG exist
svg_path = pipeline_obj._fp(svg_file)
if svg_path.exists():
png_path = svg_path.with_suffix(".png")
if png_path.exists():
# Future idea, add SVG to PNG conversion here
log.warning(f"PNG file exists: {png_path}. Please export the bumped SVG manually to PNG.")
pdf_path = svg_path.with_suffix(".pdf")
if pdf_path.exists():
# Future idea, add SVG to PDF conversion here
log.warning(f"PDF file exists: {pdf_path}. Please export the bumped SVG manually to PDF.")
# update rocrate if ro-crate is present
if Path(pipeline_obj.wf_path, "ro-crate-metadata.json").exists():
ROCrate(pipeline_obj.wf_path).update_rocrate()
def bump_nextflow_version(pipeline_obj: Pipeline, new_version: str) -> None:
"""Bumps the required Nextflow version number of a pipeline.
Args:
pipeline_obj (nf_core.utils.Pipeline): A `Pipeline` object that holds information
about the pipeline contents and build files.
new_version (str): The new version tag for the required Nextflow version.
"""
# Collect the old and new version numbers - strip leading non-numeric characters (>=)
current_version = pipeline_obj.nf_config.get("manifest.nextflowVersion", "").strip(" '\"")
current_version = re.sub(r"^[^0-9\.]*", "", current_version)
new_version = re.sub(r"^[^0-9\.]*", "", new_version)
if not current_version:
raise UserWarning("Could not find config variable 'manifest.nextflowVersion'")
log.info(f"Changing Nextlow version number from '{current_version}' to '{new_version}'")
# nextflow.config - manifest minimum nextflowVersion
update_file_version(
"nextflow.config",
pipeline_obj,
[
(
rf"(nextflowVersion\s*=\s*[\'\"]?!>=\s*)({re.escape(current_version)})([\'\"]?)",
rf"\g<1>{new_version}\g<3>",
)
],
)
# .github/workflows/nf-test.yml - Nextflow version matrix
update_file_version(
Path(".github", "workflows", "nf-test.yml"),
pipeline_obj,
[
(
# example:
# NXF_VER:
# - "20.04.0"
current_version,
new_version,
)
],
yaml_key=["jobs", "nf-test", "strategy", "matrix", "NXF_VER"],
)
# README.md - Nextflow version badge
update_file_version(
"README.md",
pipeline_obj,
[
(
rf"nextflow%20DSL2-%E2%89%A5{re.escape(current_version)}-23aa62.svg",
f"nextflow%20DSL2-%E2%89%A5{new_version}-23aa62.svg",
),
(f"version-%E2%89%A5{re.escape(current_version)}-green", f"version-%E2%89%A5{new_version}-green"),
],
False,
)
def update_file_version(
filename: str | Path,
pipeline_obj: Pipeline,
patterns: list[tuple[str, str]],
required: bool = True,
yaml_key: list[str] | None = None,
) -> None:
"""
Updates a file with a new version number.
Args:
filename (str): The name of the file to update.
pipeline_obj (nf_core.utils.Pipeline): A `Pipeline` object that holds information
about the pipeline contents.
patterns (List[Tuple[str, str]]): A list of tuples containing the regex patterns to
match and the replacement strings.
required (bool, optional): Whether the file is required to exist. Defaults to `True`.
yaml_key (List[str] | None, optional): The YAML key to update. Defaults to `None`.
"""
fn: Path = pipeline_obj._fp(filename)
if not fn.exists():
log.warning(f"File not found: '{fn}'")
return
if yaml_key:
update_yaml_file(fn, patterns, yaml_key, required)
else:
update_text_file(fn, patterns, required)
def update_yaml_file(fn: Path, patterns: list[tuple[str, str]], yaml_key: list[str], required: bool):
"""
Updates a YAML file with a new version number.
Args:
fn (Path): The name of the file to update.
patterns (List[Tuple[str, str]]): A list of tuples containing the regex patterns to
match and the replacement strings.
yaml_key (List[str]): The YAML key to update.
required (bool): Whether the file is required to exist.
"""
yaml = YAML()
yaml.preserve_quotes = True
yaml.width = 4096 # Prevent line wrapping
with open(fn) as file:
yaml_content = yaml.load(file)
try:
target = yaml_content
for key in yaml_key[:-1]:
target = target[key]
last_key = yaml_key[-1]
current_value = target[last_key]
new_value = current_value
for pattern, replacement in patterns:
# check if current value is list
if isinstance(current_value, list):
new_value = [re.sub(pattern, replacement, item) for item in current_value]
else:
new_value = re.sub(pattern, replacement, current_value)
if new_value != current_value:
target[last_key] = new_value
with open(fn, "w") as file:
yaml.indent(mapping=2, sequence=4, offset=2) # from https://stackoverflow.com/a/44389139/1696643
yaml.dump(yaml_content, file)
log.info(f"Updated version in YAML file '{fn}'")
log_change(str(current_value), str(new_value))
except KeyError as e:
handle_error(f"Could not find key {e} in the YAML structure of {fn}", required)
def update_text_file(fn: Path, patterns: list[tuple[str, str]], required: bool):
"""
Updates a text file with a new version number.
Args:
fn (Path): The name of the file to update.
patterns (List[Tuple[str, str]]): A list of tuples containing the regex patterns to
match and the replacement strings.
required (bool): Whether the file is required to exist.
"""
with open(fn) as file:
content = file.read()
updated = False
for pattern, replacement in patterns:
new_content, count = re.subn(pattern, replacement, content)
if count > 0:
log_change(content, new_content)
content = new_content
updated = True
log.info(f"Updated version in '{fn}'")
log.debug(f"Replaced pattern '{pattern}' with '{replacement}' {count} times")
else:
handle_error(f"Could not find version number in {fn}: `{pattern}`", required)
if updated:
with open(fn, "w") as file:
file.write(content)
def handle_error(message: str, required: bool):
if required:
raise ValueError(message)
else:
log.info(message)
def log_change(old_content: str, new_content: str):
old_lines = old_content.splitlines()
new_lines = new_content.splitlines()
for old_line, new_line in zip(old_lines, new_lines, strict=False):
if old_line != new_line:
stderr.print(f" [red] - {old_line.strip()}", highlight=False)
stderr.print(f" [green] + {new_line.strip()}", highlight=False)
stderr.print("\n")