blob: a5a114c6722fec9a1b1a7b365bb4ee17fdaf1c5a [file] [log] [blame]
Gilles Peskine40b3f412019-10-13 21:44:25 +02001#!/usr/bin/env python3
2
3"""Assemble Mbed Crypto change log entries into the change log file.
4"""
5
6# Copyright (C) 2019, Arm Limited, All Rights Reserved
7# SPDX-License-Identifier: Apache-2.0
8#
9# Licensed under the Apache License, Version 2.0 (the "License"); you may
10# not use this file except in compliance with the License.
11# You may obtain a copy of the License at
12#
13# http://www.apache.org/licenses/LICENSE-2.0
14#
15# Unless required by applicable law or agreed to in writing, software
16# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18# See the License for the specific language governing permissions and
19# limitations under the License.
20#
21# This file is part of Mbed Crypto (https://tls.mbed.org)
22
23import argparse
Gilles Peskined8b6c772020-01-28 18:57:47 +010024from collections import OrderedDict
Gilles Peskine40b3f412019-10-13 21:44:25 +020025import glob
26import os
27import re
28import sys
29
30class InputFormatError(Exception):
31 def __init__(self, filename, line_number, message, *args, **kwargs):
Gilles Peskine566407d2020-01-22 15:55:36 +010032 message = '{}:{}: {}'.format(filename, line_number,
33 message.format(*args, **kwargs))
34 super().__init__(message)
Gilles Peskine40b3f412019-10-13 21:44:25 +020035
Gilles Peskine2b242492020-01-22 15:41:50 +010036class LostContent(Exception):
37 def __init__(self, filename, line):
38 message = ('Lost content from {}: "{}"'.format(filename, line))
39 super().__init__(message)
40
Gilles Peskine40b3f412019-10-13 21:44:25 +020041STANDARD_SECTIONS = (
42 b'Interface changes',
43 b'Default behavior changes',
44 b'Requirement changes',
45 b'New deprecations',
46 b'Removals',
47 b'New features',
48 b'Security',
49 b'Bug fixes',
50 b'Performance improvements',
51 b'Other changes',
52)
53
54class ChangeLog:
55 """An Mbed Crypto changelog.
56
57 A changelog is a file in Markdown format. Each level 2 section title
58 starts a version, and versions are sorted in reverse chronological
59 order. Lines with a level 2 section title must start with '##'.
60
61 Within a version, there are multiple sections, each devoted to a kind
62 of change: bug fix, feature request, etc. Section titles should match
63 entries in STANDARD_SECTIONS exactly.
64
65 Within each section, each separate change should be on a line starting
66 with a '*' bullet. There may be blank lines surrounding titles, but
67 there should not be any blank line inside a section.
68 """
69
70 _title_re = re.compile(br'#*')
71 def title_level(self, line):
72 """Determine whether the line is a title.
73
74 Return (level, content) where level is the Markdown section level
75 (1 for '#', 2 for '##', etc.) and content is the section title
76 without leading or trailing whitespace. For a non-title line,
77 the level is 0.
78 """
79 level = re.match(self._title_re, line).end()
80 return level, line[level:].strip()
81
Gilles Peskine40b3f412019-10-13 21:44:25 +020082 def __init__(self, input_stream):
83 """Create a changelog object.
84
Gilles Peskine974232f2020-01-22 12:43:29 +010085 Populate the changelog object from the content of the file
86 input_stream. This is typically a file opened for reading, but
87 can be any generator returning the lines to read.
Gilles Peskine40b3f412019-10-13 21:44:25 +020088 """
Gilles Peskine37d670a2020-01-28 19:14:15 +010089 # Content before the level-2 section where the new entries are to be
90 # added.
Gilles Peskine40b3f412019-10-13 21:44:25 +020091 self.header = []
Gilles Peskine37d670a2020-01-28 19:14:15 +010092 # Content of the level-3 sections of where the new entries are to
93 # be added.
Gilles Peskined8b6c772020-01-28 18:57:47 +010094 self.section_content = OrderedDict()
95 for section in STANDARD_SECTIONS:
96 self.section_content[section] = []
Gilles Peskine37d670a2020-01-28 19:14:15 +010097 # Content of level-2 sections for already-released versions.
Gilles Peskine40b3f412019-10-13 21:44:25 +020098 self.trailer = []
Gilles Peskine8c4a84c2020-01-22 15:40:39 +010099 self.read_main_file(input_stream)
100
101 def read_main_file(self, input_stream):
102 """Populate the changelog object from the content of the file.
103
104 This method is only intended to be called as part of the constructor
105 of the class and may not act sensibly on an object that is already
106 partially populated.
107 """
Gilles Peskine37d670a2020-01-28 19:14:15 +0100108 # Parse the first level-2 section. Everything before the first
109 # level-3 section title ("###...") following the first level-2
110 # section title ("##...") is passed through as the header
111 # and everything after the second level-2 section title is passed
112 # through as the trailer. Inside the first level-2 section,
113 # split out the level-3 sections.
Gilles Peskine8c4a84c2020-01-22 15:40:39 +0100114 level_2_seen = 0
115 current_section = None
Gilles Peskine40b3f412019-10-13 21:44:25 +0200116 for line in input_stream:
117 level, content = self.title_level(line)
118 if level == 2:
119 level_2_seen += 1
Gilles Peskine40b3f412019-10-13 21:44:25 +0200120 elif level == 3 and level_2_seen == 1:
121 current_section = content
Gilles Peskined8b6c772020-01-28 18:57:47 +0100122 self.section_content.setdefault(content, [])
Gilles Peskine37d670a2020-01-28 19:14:15 +0100123 if level_2_seen == 1 and current_section is not None:
124 if level != 3 and line.strip():
Gilles Peskine40b3f412019-10-13 21:44:25 +0200125 self.section_content[current_section].append(line)
126 elif level_2_seen <= 1:
127 self.header.append(line)
128 else:
129 self.trailer.append(line)
130
131 def add_file(self, input_stream):
132 """Add changelog entries from a file.
133
134 Read lines from input_stream, which is typically a file opened
135 for reading. These lines must contain a series of level 3
136 Markdown sections with recognized titles. The corresponding
137 content is injected into the respective sections in the changelog.
138 The section titles must be either one of the hard-coded values
Gilles Peskine974232f2020-01-22 12:43:29 +0100139 in STANDARD_SECTIONS in assemble_changelog.py or already present
140 in ChangeLog.md. Section titles must match byte-for-byte except that
141 leading or trailing whitespace is ignored.
Gilles Peskine40b3f412019-10-13 21:44:25 +0200142 """
143 filename = input_stream.name
144 current_section = None
145 for line_number, line in enumerate(input_stream, 1):
146 if not line.strip():
147 continue
148 level, content = self.title_level(line)
149 if level == 3:
150 current_section = content
151 if current_section not in self.section_content:
152 raise InputFormatError(filename, line_number,
153 'Section {} is not recognized',
154 str(current_section)[1:])
155 elif level == 0:
156 if current_section is None:
157 raise InputFormatError(filename, line_number,
158 'Missing section title at the beginning of the file')
159 self.section_content[current_section].append(line)
160 else:
161 raise InputFormatError(filename, line_number,
162 'Only level 3 headers (###) are permitted')
163
164 def write(self, filename):
165 """Write the changelog to the specified file.
166 """
167 with open(filename, 'wb') as out:
168 for line in self.header:
169 out.write(line)
Gilles Peskined8b6c772020-01-28 18:57:47 +0100170 for section, lines in self.section_content.items():
Gilles Peskine40b3f412019-10-13 21:44:25 +0200171 if not lines:
172 continue
173 out.write(b'### ' + section + b'\n\n')
174 for line in lines:
175 out.write(line)
176 out.write(b'\n')
177 for line in self.trailer:
178 out.write(line)
179
Gilles Peskine2b242492020-01-22 15:41:50 +0100180def check_output(generated_output_file, main_input_file, merged_files):
181 """Make sanity checks on the generated output.
182
183 The intent of these sanity checks is to have reasonable confidence
184 that no content has been lost.
185
186 The sanity check is that every line that is present in an input file
187 is also present in an output file. This is not perfect but good enough
188 for now.
189 """
190 generated_output = set(open(generated_output_file, 'rb'))
191 for line in open(main_input_file, 'rb'):
192 if line not in generated_output:
193 raise LostContent('original file', line)
194 for merged_file in merged_files:
195 for line in open(merged_file, 'rb'):
196 if line not in generated_output:
197 raise LostContent(merged_file, line)
198
199def finish_output(changelog, output_file, input_file, merged_files):
Gilles Peskine40b3f412019-10-13 21:44:25 +0200200 """Write the changelog to the output file.
201
Gilles Peskine2b242492020-01-22 15:41:50 +0100202 The input file and the list of merged files are used only for sanity
203 checks on the output.
Gilles Peskine40b3f412019-10-13 21:44:25 +0200204 """
205 if os.path.exists(output_file) and not os.path.isfile(output_file):
206 # The output is a non-regular file (e.g. pipe). Write to it directly.
207 output_temp = output_file
208 else:
209 # The output is a regular file. Write to a temporary file,
210 # then move it into place atomically.
211 output_temp = output_file + '.tmp'
212 changelog.write(output_temp)
Gilles Peskine2b242492020-01-22 15:41:50 +0100213 check_output(output_temp, input_file, merged_files)
Gilles Peskine40b3f412019-10-13 21:44:25 +0200214 if output_temp != output_file:
215 os.rename(output_temp, output_file)
216
Gilles Peskine5e39c9e2020-01-22 14:55:37 +0100217def remove_merged_entries(files_to_remove):
218 for filename in files_to_remove:
219 os.remove(filename)
220
Gilles Peskine40b3f412019-10-13 21:44:25 +0200221def merge_entries(options):
222 """Merge changelog entries into the changelog file.
223
224 Read the changelog file from options.input.
225 Read entries to merge from the directory options.dir.
226 Write the new changelog to options.output.
227 Remove the merged entries if options.keep_entries is false.
228 """
229 with open(options.input, 'rb') as input_file:
230 changelog = ChangeLog(input_file)
231 files_to_merge = glob.glob(os.path.join(options.dir, '*.md'))
232 if not files_to_merge:
233 sys.stderr.write('There are no pending changelog entries.\n')
234 return
235 for filename in files_to_merge:
236 with open(filename, 'rb') as input_file:
237 changelog.add_file(input_file)
Gilles Peskine2b242492020-01-22 15:41:50 +0100238 finish_output(changelog, options.output, options.input, files_to_merge)
Gilles Peskine5e39c9e2020-01-22 14:55:37 +0100239 if not options.keep_entries:
240 remove_merged_entries(files_to_merge)
Gilles Peskine40b3f412019-10-13 21:44:25 +0200241
242def set_defaults(options):
243 """Add default values for missing options."""
244 output_file = getattr(options, 'output', None)
245 if output_file is None:
246 options.output = options.input
247 if getattr(options, 'keep_entries', None) is None:
248 options.keep_entries = (output_file is not None)
249
250def main():
251 """Command line entry point."""
252 parser = argparse.ArgumentParser(description=__doc__)
253 parser.add_argument('--dir', '-d', metavar='DIR',
254 default='ChangeLog.d',
Gilles Peskine6e910092020-01-22 15:58:18 +0100255 help='Directory to read entries from'
256 ' (default: ChangeLog.d)')
Gilles Peskine40b3f412019-10-13 21:44:25 +0200257 parser.add_argument('--input', '-i', metavar='FILE',
258 default='ChangeLog.md',
Gilles Peskine6e910092020-01-22 15:58:18 +0100259 help='Existing changelog file to read from and augment'
260 ' (default: ChangeLog.md)')
Gilles Peskine40b3f412019-10-13 21:44:25 +0200261 parser.add_argument('--keep-entries',
262 action='store_true', dest='keep_entries', default=None,
Gilles Peskine6e910092020-01-22 15:58:18 +0100263 help='Keep the files containing entries'
264 ' (default: remove them if --output/-o is not specified)')
Gilles Peskine40b3f412019-10-13 21:44:25 +0200265 parser.add_argument('--no-keep-entries',
266 action='store_false', dest='keep_entries',
Gilles Peskine6e910092020-01-22 15:58:18 +0100267 help='Remove the files containing entries after they are merged'
268 ' (default: remove them if --output/-o is not specified)')
Gilles Peskine40b3f412019-10-13 21:44:25 +0200269 parser.add_argument('--output', '-o', metavar='FILE',
Gilles Peskine6e910092020-01-22 15:58:18 +0100270 help='Output changelog file'
271 ' (default: overwrite the input)')
Gilles Peskine40b3f412019-10-13 21:44:25 +0200272 options = parser.parse_args()
273 set_defaults(options)
274 merge_entries(options)
275
276if __name__ == '__main__':
277 main()