forked from dominikbraun/timetrace
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrelease.py
72 lines (55 loc) · 2.12 KB
/
release.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
import argparse
import sys
CHANGELOG_FILE = "CHANGELOG.md"
def main():
parser = argparse.ArgumentParser()
parser.add_argument("run", type=str, help="the task to run")
parser.add_argument("--tag", type=str, help="the Git tag to work with")
args = parser.parse_args()
if args.run == "check-changelog":
check_changelog(args.tag)
elif args.run == "print-changelog":
print_changelog(args.tag)
sys.exit(0)
def check_changelog(git_tag):
"""
Check if a new release tag is mentioned in the changelog.
For a release tag like `v1.2.3`, the changelog has to contain a
release section called `[1.2.3]`. If the release isn't mentioned
in the changelog, exit with an error.
"""
# Cut off the `v` prefix to get the actual release number.
search_expr = "[{0}]".format(git_tag[1:])
with open(CHANGELOG_FILE) as changelog:
content = changelog.read()
if search_expr not in content:
msg = """You're trying to create a new release tag {0}, but that release is not mentioned
in the changelog. Add a section called {1} to {2} and try again.""" \
.format(git_tag, search_expr, CHANGELOG_FILE)
sys.exit(msg)
def print_changelog(git_tag):
"""
Print the changelog for the given release tag by reading the
changelog file. If the release tag does not exist as a release
number in the changelog, the output will be empty.
"""
start = "## [{0}]".format(git_tag[1:])
# The ## [Unreleased] heading will be ignored.
unreleased = "## [Unreleased]"
end = "## ["
capturing = False
output = ""
with open(CHANGELOG_FILE) as changelog:
lines = changelog.readlines()
for line in lines:
# Start capturing if the line contains our release heading.
if start in line and unreleased not in line:
capturing = True
continue
# Stop capturing if we've reached the end, i.e. the next heading.
if end in line and capturing:
break
if capturing:
output += line
print(output)
main()