forked from diffpy/diffpy.snmf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget-latest-changelog.py
58 lines (46 loc) · 1.71 KB
/
get-latest-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
import re
import sys
def get_tag_news_items(tag, filepath):
"""Collect news items after the specified tag until the next version is found."""
collect = False
collected_lines = []
# Regex to match version numbers
version_pattern = re.compile(r"^\d+\.\d+\.\d+")
with open(filepath, "r") as file:
for line in file:
if line.strip().startswith(tag):
collect = True
continue
elif collect and version_pattern.match(line.strip()):
break
elif collect:
collected_lines.append(line.rstrip())
return collected_lines
def remove_two_lines(lines):
"""Remove two lines after the tag in CHANGELOG.rst."""
if lines:
# Remove the first line containing "===="
if "====" in lines[0]:
lines.pop(0)
# Remove the second empty line
if lines[1] == "":
lines.pop(1)
return lines
def save_to_txt_file(lines, filename):
"""Save collected lines to a .txt file used for GH release notes."""
output = "\n".join(lines)
with open(filename, "w") as file:
file.write(output)
return output
if __name__ == "__main__":
if len(sys.argv) < 2:
assert (
False
), "No tag has been provided. Please provide a tag by running python get-latest-changelog.py <tag>"
tag = sys.argv[1]
CHANGELOG_PATH = "CHANGELOG.rst"
LATEST_CHANGELOG_PATH = "CHANGELOG.txt"
collected_lines = get_tag_news_items(tag, CHANGELOG_PATH)
cleaned_lines = remove_two_lines(collected_lines)
latest_changelog_output = save_to_txt_file(cleaned_lines, LATEST_CHANGELOG_PATH)
print(f"CHANGELOG for {tag}:\n{latest_changelog_output}")