Debian Codegraph @

setup/changelog.py

raw
1 #!/usr/bin/env python
2 # License: GPL v3 Copyright: 2020, Kovid Goyal <kovid at kovidgoyal.net>
3
4 from datetime import date
5
6
7 def parse(raw, parse_dates=True):
8 entries = []
9 current_entry = None
10 current_section = 'new features'
11
12 def normal(linenum, line, stripped_line):
13 nonlocal current_entry, current_section
14 if not stripped_line:
15 return normal
16 if stripped_line.startswith('{{{'):
17 parts = line.split()[1:]
18 if len(parts) != 2:
19 raise ValueError(f'The entry start line is malformed: {line}')
20 if current_entry is not None:
21 raise ValueError(f'Start of entry while previous entry is still active at line: {linenum}')
22 version, draw = parts
23 if parse_dates:
24 d = date(*map(int, draw.split('-')))
25 else:
26 d = draw
27 current_entry = {'version': version, 'date': d, 'new features': [], 'bug fixes': [], 'improved recipes': [], 'new recipes': []}
28 current_section = 'new features'
29 return in_entry
30 raise ValueError(f'Invalid content at line {linenum}: {line}')
31
32 def in_entry(linenum, line, stripped_line):
33 nonlocal current_section, current_entry
34 if stripped_line == '}}}':
35 if current_entry is None:
36 raise ValueError(f'Entry terminator without active entry at line: {linenum}')
37 entries.append(current_entry)
38 current_entry = None
39 return normal
40 if line.startswith(':: '):
41 current_section = line[3:].strip()
42 if current_section not in ('new features', 'bug fixes', 'new recipes', 'improved recipes'):
43 raise ValueError(f'Unknown section: {current_section}')
44 return in_entry
45 if line.startswith('-'):
46 return start_item(linenum, line, stripped_line)
47 if not stripped_line:
48 return in_entry
49 raise ValueError(f'Invalid content at line {linenum}: {line}')
50
51 def start_item(linenum, line, stripped_line):
52 line = line[1:].lstrip()
53 items = current_entry[current_section]
54 if current_section == 'improved recipes':
55 items.append(line.rstrip())
56 return in_entry
57 if current_section == 'new recipes':
58 idx = line.rfind('by ')
59 if idx == -1:
60 items.append({'title': line.strip()})
61 else:
62 items.append({'title': line[:idx].strip(), 'author': line[idx + 3 :].strip()})
63 return in_entry
64 item = {}
65 if line.startswith('['):
66 idx = line.find(']')
67 if idx == -1:
68 raise ValueError(f'No closing ] found in line: {linenum}')
69 for x in line[1:idx].split():
70 if x == 'major':
71 item['type'] = x
72 continue
73 num = int(x)
74 item.setdefault('tickets', []).append(num)
75 item['title'] = line[idx + 1 :].strip()
76 else:
77 item['title'] = line.strip()
78 items.append(item)
79 return in_item
80
81 def finalize_item(item):
82 if 'description' in item and not item['description']:
83 del item['description']
84 if 'description' in item:
85 item['description'] = item['description'].strip()
86 return item
87
88 def in_item(linenum, line, stripped_line):
89 item = current_entry[current_section][-1]
90 if line.startswith('::'):
91 finalize_item(item)
92 return in_entry(linenum, line, stripped_line)
93 if line.startswith('-'):
94 finalize_item(item)
95 return start_item(linenum, line, stripped_line)
96 if line.startswith('}}}'):
97 return in_entry(linenum, line, stripped_line)
98 if not stripped_line:
99 if 'description' not in item:
100 item['description'] = ''
101 return in_item
102 if 'description' in item:
103 item['description'] += stripped_line + ' '
104 else:
105 item['title'] += ' ' + stripped_line
106 return in_item
107
108 state = normal
109 for i, line in enumerate(raw.splitlines()):
110 if line.startswith('#'):
111 continue
112 stripped_line = line.strip()
113 state = state(i + 1, line, stripped_line)
114 return entries
115
116
117 def migrate():
118 from yaml import safe_load
119
120 def output_item(item, lines):
121 meta = []
122 if item.get('type') == 'major':
123 meta.append(item['type'])
124 for x in item.get('tickets', ()):
125 meta.append(str(x))
126 title = item['title']
127 if meta:
128 meta = ' '.join(meta)
129 title = f'[{meta}] {title}'
130 lines.append(f'- {title}')
131 d = item.get('description')
132 if d:
133 lines.append(''), lines.append(d)
134 lines.append('')
135
136 for name in ('Changelog.yaml', 'Changelog.old.yaml'):
137 entries = safe_load(open(name).read())
138 lines = []
139 for entry in entries:
140 lines.append('')
141 lines.append('{{{' + f' {entry["version"]} {entry["date"]}')
142 for w in ('new features', 'bug fixes'):
143 nf = entry.get(w)
144 if nf:
145 lines.append(f':: {w}'), lines.append('')
146 for x in nf:
147 output_item(x, lines)
148 lines.append('')
149 nr = entry.get('new recipes')
150 if nr:
151 lines.append(':: new recipes'), lines.append('')
152 for r in nr:
153 aut = r.get('author') or r.get('authors')
154 title = r['title']
155 if title:
156 if aut:
157 lines.append(f'- {title} by {aut}')
158 else:
159 lines.append(f'- {title}')
160 lines.append('')
161 ir = entry.get('improved recipes')
162 if ir:
163 lines.append(':: improved recipes'), lines.append('')
164 for r in ir:
165 lines.append(f'- {r}')
166 lines.append('')
167 with open(name.replace('yaml', 'txt'), 'w') as f:
168 f.write('\n'.join(lines))
169 lines.append(''), lines.append('}}}'), lines.append('')
170
171
172 if __name__ == '__main__':
173 migrate()