generate-changelog.py
2.67 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
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
#
# Copyright © 2017 Martine Lenders <m.lenders@fu-berlin.de>
#
# Distributed under terms of the MIT license.
from __future__ import print_function
import re
import sys
def generate_changelog(template_filename, changelog_filename, output_filename):
with open(template_filename) as template, \
open(changelog_filename) as changelog, \
open(output_filename, "w") as output:
changelog_lines = []
release_title = re.compile(r"((RIOT-\d{4}\.\d{2} - Release Notes)|(Release 2013\.08))")
notes_template = re.compile(r"\[Notes\]")
first_title = True
title = 0
prev_newline = False
# Traverse changelog file line-wise
for line in changelog:
# Remove duplicate newlines
if line == "\n" and not prev_newline:
prev_newline = True
elif line == "\n" and prev_newline:
continue
else:
prev_newline = False
if title: # if a release title was previously detected
changelog_lines.append("\n") # Remove the underline
title = False
prev_newline = True # this introduces a newline, so count it
elif release_title.match(line):
# if line contains a release title
release_match = re.search(r"(\d{4}\.\d{2})", line)
assert(release_match != None)
# parse out release number
release = release_match.group(1)
title = "Release %s" % release
tag = "release-%s" % release.replace('.', '-')
# append as level 1 title with reference anchor tag
changelog_lines.append("# %s {#%s}\n" % (title ,tag))
title = True
first_title = False
else:
# append non-title log lines as verbatim (so notation is kept)
changelog_lines.append(" %s" % line)
for line in template:
# Traverse template file line-wise
if notes_template.match(line):
# if template string is matched: start adding changelog lines
for line_log in changelog_lines:
print(line_log, file=output, end="")
else:
# just print the template line
print(line, file=output, end="")
if __name__ == "__main__":
if len(sys.argv) < 4:
print("usage %s <md template> <changelog> <output md>" % sys.argv[0], file=sys.stderr)
sys.exit(1)
generate_changelog(sys.argv[1], sys.argv[2], sys.argv[3])