This repository has been archived by the owner on Nov 1, 2024. It is now read-only.
forked from new-frontiers-14/frontier-station-14
-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathmanual_changelog.py
executable file
·150 lines (108 loc) · 4.03 KB
/
manual_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
#!/usr/bin/python
# Update the changelog manually, for when you don't want to bother setting up the bot.
import argparse
import datetime
from re import compile as re_compile, I as re_I
from sys import stdin
from yaml import safe_load as yaml_safe_load, dump as yaml_dump
def make_timestamp():
now = datetime.datetime.now(datetime.timezone.utc)
return now.isoformat()
def load_changelog(infile):
print(f"Loading changelog {infile.name} ...")
return yaml_safe_load(infile)
def make_change(change_type, message):
change_type = change_type.lower().capitalize()
assert(type(message) == str)
assert(change_type in ['Add', 'Remove', 'Fix', 'Tweak'])
return {
'message': message,
'type': change_type,
}
def get_last_id(changelog):
return changelog['Entries'][-1]['id']
def insert_entry(changelog, author, changes):
new_id = 1 + get_last_id(changelog)
assert(type(author) == str)
entry = {
'author': author,
'changes': changes,
'id': new_id,
'time': make_timestamp(),
}
changelog['Entries'].append(entry)
def prune_entries(changelog, max_entries=500):
print(f"Pruning changelog to a maxmimum of {max_entries} entries ...")
changelog['Entries'] = changelog['Entries'][-max_entries:]
def save_changelog(changelog, outfile):
print(f"Saving changelog to {outfile.name} ...")
yaml_dump(changelog, outfile)
def parse_github_pull_request(changelog, stream):
re_changelog_header = re_compile(r'^\s*:?cl:?\s*(?P<author>.*)', re_I)
re_change = re_compile(r'^\s*-?\s*(?P<type>add|fix|remove|tweak):(?P<message>.+)', re_I)
pending_entries_with_unspecified_author = []
author = ''
stored_changes = []
def flush_changes(changes):
if len(changes) > 0:
if author == '':
pending_entries_with_unspecified_author.append(changes[:])
else:
insert_entry(changelog, author, changes[:])
changes[:] = []
print("Type or paste in changelog entries from GitHub.\n")
try:
# Parse the stream.
for line in stream:
line = line.strip()
# Read change lines.
match = re_change.match(line)
if match:
group = match.groupdict()
ctype = group['type'].strip()
cmessage = group['message'].strip()
stored_changes.append(make_change(
ctype,
cmessage,
))
continue
# Read author lines.
match = re_changelog_header.match(line)
if match:
# Flush any changes before setting the new author.
flush_changes(stored_changes)
group = match.groupdict()
author = group['author'].strip()
except KeyboardInterrupt:
pass
# Finished reading from stream.
# Flush any changes.
flush_changes(stored_changes)
print("")
# Deal with unspecified authors.
for entry in pending_entries_with_unspecified_author:
print(f"[!] Entry with unspecified author:\n{entry}")
print("\nEnter author> ", end='')
author = input()
insert_entry(changelog, author, entry)
def main():
default_filename = 'Resources/Changelog/Changelog.yml'
parser = argparse.ArgumentParser(description='Update the changelog manually.')
parser.add_argument('--infile', type=str,
default=default_filename,
help='which file to load current entries from')
parser.add_argument('--outfile', type=str,
default=default_filename,
help='which file to save results to')
args = parser.parse_args()
infile = open(args.infile, 'r')
cl = load_changelog(infile)
infile.close()
parse_github_pull_request(cl, stdin)
prune_entries(cl)
outfile = open(args.outfile, 'w')
save_changelog(cl, outfile)
outfile.close()
print("Done!")
if __name__ == "__main__":
main()