-
Notifications
You must be signed in to change notification settings - Fork 601
/
Copy pathchangelog.py
executable file
·263 lines (211 loc) · 8.92 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
#!/usr/bin/env python3
import argparse
import datetime
import os
import re
from typing import NamedTuple
import debian.changelog
def parse_arguments():
parser = argparse.ArgumentParser(description="automatic changelog writer for snapd")
parser.add_argument("version", type=str, help="new snapd version")
parser.add_argument("lpbug", type=str, help="new snapd major release LP bug")
parser.add_argument(
"changelog",
type=argparse.FileType("r"),
help="path to new changelog entry as generated by snappy-dch",
)
return parser.parse_args()
class Distro(NamedTuple):
name: str # name of the distro in the packaging directory
debian_name: str # debian distribution name
version_suffix: str # suffix to add to the version number in changelogs
debianish_distros = [
Distro("ubuntu-14.04", "trusty", "~14.04"),
Distro("ubuntu-16.04", "xenial", ""),
Distro("debian-sid", "unstable", "-1"),
]
other_distros = [
"opensuse",
"fedora",
"arch",
]
def rewrite_version_number_file(file_name, pattern, version, write):
"""rewrite_version_number_file reads a packaging file with a version number
in it and updates the version number to the new one specified using the
regex pattern.
It returns the new file contents and optionally writes out the new contents
back to the file as well.
"""
with open(file_name, "r") as fh:
file_contents = fh.read()
# replace the pattern (which should have one capturing group in it for the
# version specifier pattern) with the captured group + the new version such
# that we can keep any whitespace, etc in between the version specified and
# the version number
new_contents, n = re.subn(
pattern, r"\g<1>" + version, file_contents, flags=re.MULTILINE
)
# check that we only did one replacement in the file
if n > 1:
raise RuntimeError(
f"too many version patterns ({n}) matched in packaging file {file_name}"
)
elif n < 1:
raise RuntimeError(f"version pattern not matched in packaging file {file_name}")
if write is True:
with open(file_name, "w") as fh:
fh.write(new_contents)
return new_contents
def update_fedora_changelog(opts, snapd_packaging_dir, new_changelog_entry, maintainer):
spec_file = os.path.join(snapd_packaging_dir, "fedora", "snapd.spec")
# rewrite the snapd.spec file with the right version
spec_file_content = rewrite_version_number_file(
spec_file,
r"^(Version:\s+).*$",
opts.version,
False,
)
# now we also need to add the changelog entry to the snapd.spec file
# this is a bit tricky, since we want a different format for the
# changelog in snapd.spec than we have for debian, but luckily it's
# just trimming whitespace off the front of each line in the
# changelog
dedented_changelog_lines = []
for line in new_changelog_entry.splitlines():
# strip the first 3 characters which are space characters so
# that we only have one single whitespace
dedented_changelog_lines.append(line[3:] + "\n")
date = datetime.datetime.now().strftime("%a %b %d %Y")
date_and_maintainer_header = f"* {date} {maintainer[0]} <{maintainer[1]}>\n"
changelog_header = f"- New upstream release {opts.version}\n"
fedora_changelog_lines = [
date_and_maintainer_header,
changelog_header,
] + dedented_changelog_lines
# find the start of the changelog section in the rewritten spec file bytes
changelog_section = "\n%changelog\n"
idx = spec_file_content.find(changelog_section)
if idx < 0:
raise RuntimeError(
"'%changelog' line in fedora spec file not found (was a comment or whitespace added to that line?)"
)
# rewrite the spec file using the replaced bits up to the changelog section,
# then insert our new changelog entry lines, then add the rest of the
# replaced bits of the spec file
with open(spec_file, "w") as fh:
# write the spec file up to and including the changelog section
fh.write(spec_file_content[: idx + len(changelog_section)])
# insert our new changelog entry
for ch_line in fedora_changelog_lines:
fh.write(ch_line)
fh.write("\n")
# write the rest of the original spec file
fh.write(spec_file_content[idx + len(changelog_section) :])
def update_opensuse_changelog(
opts, snapd_packaging_dir, new_changelog_entry, maintainer
):
spec_file = os.path.join(snapd_packaging_dir, "opensuse", "snapd.spec")
changes_file = os.path.join(snapd_packaging_dir, "opensuse", "snapd.changes")
rewrite_version_number_file(
spec_file,
r"^(Version:\s+).*$",
opts.version,
True,
)
# add a template changelog to the changes file
date = datetime.datetime.now(tz=datetime.timezone.utc).strftime(
"%a %b %d %H:%M:%S %Z %Y"
)
email = maintainer[1]
templ = f"""-------------------------------------------------------------------
{date} - {email}
- Update to upstream release {opts.version}
"""
# read the existing changes file and then write the new changelog entry at
# the top and then write the rest of the file
with open(changes_file, "r") as fh:
current = fh.read()
with open(changes_file, "w") as fh:
fh.write(templ)
fh.write(current)
def write_github_release_entry(opts, new_changelog_entry):
with open(f"snapd-{opts.version}-github-release.md", "w") as fh:
# write the prefix header
fh.write(
f"""New snapd release {opts.version}
See https://forum.snapcraft.io/t/the-snapd-roadmap/1973 for high-level overview.
"""
)
# write the rest of the actual changelog
for line in new_changelog_entry.splitlines():
# strip the first 4 characters which are space characters so
# that there's no leading prefix
fh.write(line.lstrip())
fh.write("\n")
def main(opts):
this_script = os.path.realpath(__file__)
snapd_root_git_dir = os.path.dirname(os.path.dirname(this_script))
snapd_packaging_dir = os.path.join(snapd_root_git_dir, "packaging")
# read all the changelog entries, expected to be formatted by snappy-dch
new_changelog_entry = opts.changelog.read()
# verify that the changelog entry lines are all in the right format
for line_number, line in enumerate(new_changelog_entry.splitlines(), start=1):
# each line should start with either 4 spaces, a - and then another
# space, or 6 spaces
if not line.startswith(" - ") and not line.startswith(" "):
raise RuntimeError(
f"unexpected changelog line format in line {line_number}"
)
if len(line) > 72:
raise RuntimeError(
f"line {line_number} too long, should wrap properly to next line"
)
# read the name and email of the person running the script using i.e. dch
# conventions
maintainer = debian.changelog.get_maintainer()
# first handle all of the debian packaging files
for distro in debianish_distros:
debian_packaging_changelog = os.path.join(
snapd_packaging_dir, distro.name, "changelog"
)
with open(debian_packaging_changelog) as fh:
ch = debian.changelog.Changelog(fh)
# setup a new block
ch.new_block(
package="snapd",
version=opts.version + distro.version_suffix,
distributions=distro.debian_name,
urgency="medium",
author=f"{maintainer[0]} <{maintainer[1]}>",
date=debian.changelog.format_date(),
)
# add the new changelog entry with our standard header
# the spacing here is manually adjusted, the top of the comment is always
# the same
templ = f"\n * New upstream release, LP: #{opts.lpbug}\n" + new_changelog_entry
ch.add_change(templ)
# write it out back to the changelog file
with open(debian_packaging_changelog, "w") as fh:
ch.write_to_open_file(fh)
# now handle all of the non-debian packaging files
for distro in other_distros:
if distro == "arch":
# for arch all we need to do is change the PKGBUILD "pkgver" key
rewrite_version_number_file(
os.path.join(snapd_packaging_dir, "arch", "PKGBUILD"),
r"^(pkgver=).*$",
opts.version,
True,
)
elif distro == "fedora":
update_fedora_changelog(
opts, snapd_packaging_dir, new_changelog_entry, maintainer
)
elif distro == "opensuse":
update_opensuse_changelog(
opts, snapd_packaging_dir, new_changelog_entry, maintainer
)
write_github_release_entry(opts, new_changelog_entry)
if __name__ == "__main__":
opts = parse_arguments()
main(opts)