-
Notifications
You must be signed in to change notification settings - Fork 72
/
bump_version.py
232 lines (197 loc) · 8.57 KB
/
bump_version.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
# Copyright (c) 2021-2022 CRS4
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
"""\
Bump workflow repository version.
Workflow repositories are searched for starting from the specified root
directories (the default is to search below the current directory). Uses the
same searching logic and definition of repository as the ci_find_repos Planemo
command (any directory with a .shed.yml or .dockstore.yml file).
Workflow repositories are expected to contain:
- the .ga workflow file (e.g., "consensus-from-variation.ga"), which is
expected to list its version in the "release" metadata field
- a CHANGELOG.md file with atx-style Markdown headers (the ones with the
leading hash marks), formatted according to https://keepachangelog.com
(but not necessarily following semantic versioning).
Example:
# Changelog
## Unreleased
### Fixed
- This
- That
## [0.4] - 2021-06-16
### Changed
- Upgrade multiqc to 1.9+galaxy1
"""
import argparse
import datetime
import json
import os
import re
import string
from enum import Enum
from pathlib import Path
import marko
from marko.md_renderer import MarkdownRenderer
from packaging.version import parse as vparse, Version, LegacyVersion
from planemo.context import PlanemoContext
from planemo.shed import find_raw_repositories
from planemo.ci import filter_paths
DATE_FMT = "%Y-%m-%d"
ENTRY_TYPES = "Added", "Changed", "Fixed"
NEW_LOG_ENTRY_HEAD = string.Template("""\
## [${version}] ${date}
""")
NEW_LOG_ENTRY_BODY = string.Template("""\
### ${entry_type}
- ${msg}
""")
class Bump(Enum):
MAJOR = "major"
MINOR = "minor"
MICRO = "micro" # PEP 440
PATCH = "micro" # Semantic Versioning
# Allow "PATCH" alias, but leave it out of the help text to avoid confusion
BUMP_CHOICES = list(Bump.__members__) # includes aliases
BUMP_METAVAR = "|".join(_.name for _ in Bump) # does not include aliases
class Renderer(MarkdownRenderer):
"""\
A Markdown renderer that indents all lines in a paragraph.
"""
def render_paragraph(self, element):
new_line = False
rendered = []
for child in element.children:
prefix = self._second_prefix if new_line else ""
rendered.append(prefix + self.render(child))
new_line = isinstance(child, marko.inline.LineBreak)
children = "".join(rendered)
line = self._prefix + children + "\n"
self._prefix = self._second_prefix
return line
def get_new_release(rel, bump=Bump.MICRO):
if len(rel) == 1 and bump is Bump.MINOR or len(rel) == 2 and bump is Bump.MICRO:
return rel + (1,)
if len(rel) == 1 and bump is Bump.MICRO:
return rel + (0, 1)
return tuple(r + 1 if bump is b else r for r, b in zip(rel, Bump))
def vbump(version, bump=Bump.MICRO):
if not isinstance(version, (Version, LegacyVersion)):
version = vparse(version)
new_release = get_new_release(version.release, bump)
# same as packaging.version.Version.__str__, but release is the new one
parts = []
if version.epoch != 0:
parts.append(f"{version.epoch}!")
parts.append(".".join(str(x) for x in new_release))
if version.pre is not None:
parts.append("".join(str(x) for x in version.pre))
if version.post is not None:
parts.append(f".post{version.post}")
if version.dev is not None:
parts.append(f".dev{version.dev}")
if version.local is not None:
parts.append(f"+{version.local}")
return "".join(parts)
def get_wf_id(repo_dir):
ids = [_.name for _ in os.scandir(repo_dir) if _.name.endswith(".ga")]
if not ids:
raise RuntimeError(f".ga workflow file not found in {repo_dir}")
return ids[0]
def update_changelog(changelog, md, version, msg, date=datetime.date.today(),
entry_type="Changed"):
with open(changelog, "rt") as f:
tree = md.parse(f.read())
date = date.strftime(DATE_FMT)
entry = NEW_LOG_ENTRY_HEAD.substitute(version=version, date=date)
if msg:
entry += NEW_LOG_ENTRY_BODY.substitute(entry_type=entry_type, msg=msg)
for ins_pos, elem in enumerate(tree.children):
if isinstance(elem, marko.block.Heading) and elem.level == 2:
if elem.children[0].children.strip(" []").lower() == "unreleased":
# Insert after the "Unreleased" heading but before its contents
# Any unreleased changes become part of the new version
ins_pos += 1
entry = "\n" + entry
break
else:
ins_pos = len(tree.children)
entry_tree = marko.block.Document(entry)
entry_tree.children.extend(marko.block.Document("\n").children)
tree.children[ins_pos:ins_pos] = entry_tree.children
with open(changelog, "wt") as f:
f.write(md.render(tree))
def update_workflow(workflow, version=None, bump=None):
with open(workflow, "rt") as f:
txt = f.read()
code = json.loads(txt)
try:
old_version = code["release"]
except KeyError:
raise RuntimeError(f'{workflow} does not have a "release" metadata entry')
if not version:
if not old_version:
raise RuntimeError(f'{workflow}: "release" metadata entry is empty')
version = vbump(old_version, bump=bump)
# no json tools for the update (we'd get a huge diff due to whitespace change)
pattern = fr'"release":\s*"{old_version}"'
repl = f'"release": "{version}"'
with open(workflow, "wt") as f:
f.write(re.sub(pattern, repl, txt, 1))
return version
def find_repos(paths, exclude=()):
"""\
Find all workflow directories below each path in ``paths``.
Same as ``planemo ci_find_repos``.
"""
ctx = PlanemoContext()
kwargs = dict(recursive=True, fail_fast=True, chunk_count=1, chunk=0, exclude=exclude)
raw_repos = [_.path for _ in find_raw_repositories(ctx, paths, **kwargs)]
return [Path(_) for _ in filter_paths(ctx, raw_repos, path_type="repo", **kwargs)]
def main(args):
if args.date:
args.date = datetime.datetime.strptime(args.date, DATE_FMT)
else:
args.date = datetime.date.today()
args.bump = Bump[args.bump]
md = marko.Markdown(renderer=Renderer)
for repo in find_repos(args.root, exclude=args.exclude):
print(f"processing {repo}")
wf_id = get_wf_id(repo)
version = update_workflow(repo / wf_id, version=args.version, bump=args.bump)
msg = f"Update for version {version}." if args.msg is None else args.msg
update_changelog(repo / "CHANGELOG.md", md, version, msg, date=args.date,
entry_type=args.entry_type)
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description=__doc__, formatter_class=argparse.RawTextHelpFormatter
)
parser.add_argument("root", metavar="ROOT_DIR", help="top-level directory",
nargs="*", default=[os.getcwd()])
parser.add_argument("--exclude", metavar="PATH", nargs="*", default=(),
help="paths to exclude while searching for workflow repos")
parser.add_argument("-v", "--version", metavar="STRING",
help="set new workflow version to this value (ignores '-b')")
parser.add_argument("-m", "--msg", metavar="STRING", help="log message")
parser.add_argument("-d", "--date", metavar="STRING", help="log date as YYYY-MM-DD")
parser.add_argument("-t", "--entry-type", metavar="|".join(ENTRY_TYPES),
choices=ENTRY_TYPES, default="Changed", help="log entry type")
parser.add_argument("-b", "--bump", metavar=BUMP_METAVAR, choices=BUMP_CHOICES,
default="MICRO", help="version part to bump")
main(parser.parse_args())