Explicitly use UTF-8 in assemble_changelog

Changelog contents should be UTF-8 text files. So explicitly open all files as
UTF-8. This makes the script independent of the ambient locale (except with
respect to exception messages, but we can live with that).

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
This commit is contained in:
Gilles Peskine 2021-05-18 14:49:02 +02:00
parent 791c40c522
commit 9c6187d8ad

View File

@ -264,7 +264,7 @@ class ChangeLog:
def write(self, filename): def write(self, filename):
"""Write the changelog to the specified file. """Write the changelog to the specified file.
""" """
with open(filename, 'w') as out: with open(filename, 'w', encoding='utf-8') as out:
out.write(self.header) out.write(self.header)
out.write(self.top_version_title) out.write(self.top_version_title)
for title, body in self.categories.items(): for title, body in self.categories.items():
@ -407,12 +407,12 @@ def check_output(generated_output_file, main_input_file, merged_files):
is also present in an output file. This is not perfect but good enough is also present in an output file. This is not perfect but good enough
for now. for now.
""" """
generated_output = set(open(generated_output_file, 'r')) generated_output = set(open(generated_output_file, 'r', encoding='utf-8'))
for line in open(main_input_file, 'r'): for line in open(main_input_file, 'r', encoding='utf-8'):
if line not in generated_output: if line not in generated_output:
raise LostContent('original file', line) raise LostContent('original file', line)
for merged_file in merged_files: for merged_file in merged_files:
for line in open(merged_file, 'r'): for line in open(merged_file, 'r', encoding='utf-8'):
if line not in generated_output: if line not in generated_output:
raise LostContent(merged_file, line) raise LostContent(merged_file, line)
@ -455,14 +455,14 @@ def merge_entries(options):
Write the new changelog to options.output. Write the new changelog to options.output.
Remove the merged entries if options.keep_entries is false. Remove the merged entries if options.keep_entries is false.
""" """
with open(options.input, 'r') as input_file: with open(options.input, 'r', encoding='utf-8') as input_file:
changelog = ChangeLog(input_file, TextChangelogFormat) changelog = ChangeLog(input_file, TextChangelogFormat)
files_to_merge = list_files_to_merge(options) files_to_merge = list_files_to_merge(options)
if not files_to_merge: if not files_to_merge:
sys.stderr.write('There are no pending changelog entries.\n') sys.stderr.write('There are no pending changelog entries.\n')
return return
for filename in files_to_merge: for filename in files_to_merge:
with open(filename, 'r') as input_file: with open(filename, 'r', encoding='utf-8') as input_file:
changelog.add_file(input_file) changelog.add_file(input_file)
finish_output(changelog, options.output, options.input, files_to_merge) finish_output(changelog, options.output, options.input, files_to_merge)
if not options.keep_entries: if not options.keep_entries: