forked from mamba-org/mamba
-
Notifications
You must be signed in to change notification settings - Fork 0
/
releaser.py
193 lines (148 loc) · 5.5 KB
/
releaser.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
# script to release any of the mamba packages
import copy
import datetime
import re
template = {"version": None, "changes": []}
templates = {
"libmamba": "libmamba/include/mamba/version.hpp.tmpl",
"micromamba": "micromamba/src/version.hpp.tmpl",
"libmambapy": "libmambapy/libmambapy/_version.py.tmpl",
"mamba": "mamba/mamba/_version.py.tmpl",
}
def apply_changelog(name, version, changes):
res = ""
today = datetime.date.today()
fmt_today = today.strftime("%B %d, %Y")
header_line = f"{name} {version} ({fmt_today})"
res += f"{header_line}\n{'=' * len(header_line)}\n\n"
for idx, c in enumerate(changes):
if c.startswith("-"):
if idx > 0 and not changes[idx - 1].startswith("- "):
res += f"\n{c}\n"
else:
res += f"{c}\n"
else:
res += f"{c}\n"
res += "\n"
cl_file = name + "/CHANGELOG.md"
with open(cl_file, "r") as fi:
prev_cl = fi.read()
with open(cl_file, "w") as fo:
fo.write(res + prev_cl)
version_major, version_minor, version_patch = version.split(".")
def template_substitute(contents):
x = contents.replace("{{ version_major }}", version_major)
x = x.replace("{{ version_minor }}", version_minor)
x = x.replace("{{ version_patch }}", version_patch)
return x
if name in templates:
template = templates[name]
with open(template, "r") as fi:
final = template_substitute(fi.read())
with open(template[: -len(".tmpl")], "w") as fo:
fo.write(final)
def commands(changes):
commit_msg = ", ".join([f"{x} {changes[x]['version']}" for x in changes])
today = datetime.date.today()
date_stamp = today.strftime("%Y.%m.%d")
files_to_commit = ""
for c in changes:
files_to_commit += f" {c}/CHANGELOG.md \\\n"
files_to_commit += f" {templates[c][:-len('.tmpl')]} \\\n"
files_to_commit = files_to_commit[:-3]
files_to_revert = ""
for c in changes:
files_to_commit += f" {templates[c][:-len('.tmpl')]} \\\n"
print("\n\n--- REVERT ---\n\n")
print(f"git checkout origin/master -- \\\n{files_to_commit[:-3]}\n\n")
print("\n\n--- COMMIT ---\n\n")
print("pre-commit run --all")
print("git diff")
print(f"git commit -m 'release {commit_msg}' \\\n{files_to_revert}")
print(f"git tag {date_stamp}")
for c in changes:
print(f"git tag {c}-{changes[c]['version']}")
class Section:
def __init__(self):
self.items = []
self.applies_to = ["all"]
self.text = ""
class Item:
def __init__(self):
self.applies_to = ["all"]
self.text = ""
def populate_changes(name, sections, changes):
el = changes[name]
def applies(x):
return "all" in x or name in x
for s in sections:
s_applies = applies(s.applies_to)
if s_applies and len(s.items):
s_applies = any(applies(i.applies_to) for i in s.items)
if s_applies:
if s != sections[0]:
el["changes"].append("\n" + s.text.strip())
else:
el["changes"].append(s.text.strip())
for i in s.items:
if applies(i.applies_to):
el["changes"].append(f"- {i.text.strip()}")
def main():
changes = {}
with open("CHANGELOG.md", "r") as fi:
contents = fi.readlines()
for idx, line in enumerate(contents):
if line.startswith("====="):
release_start = idx + 1
break
brackets_re = re.compile(r"\[(.*)\]")
# section with groups, heading + items
sections = []
in_section = False
contents = contents[release_start:]
for idx, c in enumerate(contents):
if c.startswith("Releases"):
releases = [x.strip() for x in c[len("Releases: ") :].split(",")]
for r in releases:
rsplit = r.split()
changes[rsplit[0].strip()] = copy.deepcopy(template)
changes[rsplit[0].strip()]["version"] = rsplit[1].strip()
continue
if contents[idx + 1].startswith("===="):
break
if c.strip() == "" or c[0] == "-":
in_section = False
if c.strip() == "":
continue
if c[0] != "-":
if not in_section:
sections.append(Section())
in_section = True
sections[-1].text += c
if m := re.search(brackets_re, c):
if in_section:
sections[-1].applies_to = [x.strip() for x in m.groups(1)[0].split(",")]
else:
sections[-1].items.append(Item())
sections[-1].items[-1].text = c[m.end() :].strip()
sections[-1].items[-1].applies_to = [
x.strip() for x in m.groups(1)[0].split(",")
]
else:
if c.startswith(" "):
if in_section:
sections[-1].text += " " + c.strip()
else:
sections[-1].items[-1].text += c.strip()
else:
if not in_section:
sections[-1].items.append(Item())
sections[-1].items[-1].text = c.strip()
sections[-1].items[-1].applies_to = ["all"]
for c in changes:
populate_changes(c, sections, changes)
for el in changes:
apply_changelog(el, changes[el]["version"], changes[el]["changes"])
commands(changes)
if __name__ == "__main__":
main()