-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate-metainfo.py
79 lines (67 loc) · 2.3 KB
/
generate-metainfo.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
import os
import sys
import subprocess
import json
RELEASE_LIST_ITEM_TEMPLATE = '''<li>%s</li>'''
RELEASE_BODY_TEMPLATE = '''
<p>%s</p>
<ul>
%s
</ul>'''
RELEASE_TEMPLATE = '''
<release version="%s" date="%s">
<description>
%s
</description>
</release>
'''
def group_by(grouper, rows):
groups = []
current_group = []
for row in rows:
if row.strip() == "":
continue
if row.startswith(' '):
# sub-item, and sub-list-items are not allowed, ignore :(
continue
if grouper(row):
groups.append(current_group)
current_group = []
current_group.append(row)
groups.append(current_group)
return groups
def main(args):
release = args[0]
metainfo_template_path = "metainfo.xml.template"
changelog_path = "strongbox/CHANGELOG.md"
assert os.path.exists(metainfo_template_path)
assert os.path.exists(changelog_path)
# parse given changelog into json
output = subprocess.check_output(["./parse-changelog", changelog_path, "--json"])
data = json.loads(output)
releases = []
for _, release_map in data.items():
title = release_map["title"]
version, dt = title.split(" - ")
notes = release_map['notes'].splitlines()
notes = group_by(lambda line: line.startswith('#'), notes)
release_body_template_list = []
for group in notes:
if group == []:
continue
header = group[0].strip('# ')
li_list = [RELEASE_LIST_ITEM_TEMPLATE % (row.strip('*#- ')) for row in group[1:]]
if not group[1:]:
# handling for release 1.0.0
continue
release_body_template_list.append(RELEASE_BODY_TEMPLATE % (header, "\n ".join(li_list)))
releases.append(RELEASE_TEMPLATE % (version, dt, "\n".join(release_body_template_list)))
context = {
"release": release,
"releases_str": " <releases>\n" + "\n".join(releases) + " </releases>",
}
with open(metainfo_template_path) as fh:
metainfo = fh.read().format(**context)
print(metainfo)
if __name__ == '__main__':
main(sys.argv[1:])