Selectively append changes to exported config files

In case of a non-overwriting change to an exported config file
the previous logic appended a new block of text to the previous file
every time the to-be-written block of text was not exactly matched.

This parses the old config file and the to-be-written changes into
sets, which can then be compared. If all of the incoming lines are
found in the old config file set, no changes are made. If some
incoming lines are not found in the old config file, only these are
appended.
pull/10118/head
Artturi Ramanen 2019-03-15 12:03:48 +02:00
parent cde7d0be58
commit 19562fff30
1 changed files with 6 additions and 4 deletions

View File

@ -206,15 +206,17 @@ class Exporter(object):
self.generated_files += [target_path]
def gen_file_nonoverwrite(self, template_file, data, target_file, **kwargs):
"""Generates a project file from a template using jinja"""
"""Generates or selectively appends a project file from a template"""
target_text = self._gen_file_inner(template_file, data, target_file, **kwargs)
target_path = self.gen_file_dest(target_file)
if exists(target_path):
with open(target_path) as fdin:
old_text = fdin.read()
if target_text not in old_text:
old_lines_set = set(fdin.read().splitlines())
target_set = set(target_text.splitlines())
to_append = target_set - old_lines_set
if len(to_append) > 0:
with open(target_path, "a") as fdout:
fdout.write(target_text)
fdout.write("\n".join(to_append))
else:
logging.debug("Generating: %s", target_path)
open(target_path, "w").write(target_text)