From 19562fff3011bb8ae178fc2b67674335eb7d8e75 Mon Sep 17 00:00:00 2001 From: Artturi Ramanen Date: Fri, 15 Mar 2019 12:03:48 +0200 Subject: [PATCH] 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. --- tools/export/exporters.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/tools/export/exporters.py b/tools/export/exporters.py index 43504c6c13..fa23866bad 100644 --- a/tools/export/exporters.py +++ b/tools/export/exporters.py @@ -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)