From c959f14172cef9fee564d179ff71d20afd6f7884 Mon Sep 17 00:00:00 2001 From: Daniel-I-Am Date: Mon, 27 Dec 2021 16:39:52 +0100 Subject: [PATCH] Optimize things that need to run if already correct --- run.py | 36 ++++++++++++++++++++++++------------ 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/run.py b/run.py index 1a7a9bc..4496c43 100755 --- a/run.py +++ b/run.py @@ -91,21 +91,27 @@ def convert_template(env, template, key_value_store, out): if not os.path.isdir(out_dir_name): dryrun_safe_mkdir(out_dir_name) - if os.path.isfile(out) and not args.force: - if args.diff: - with open(out) as file: - current = file.read() + if os.path.isfile(out): + if is_content_different(out, rendered): + if args.diff: + with open(out) as file: + current = file.read() - for line in difflib.unified_diff(current.split('\n'), rendered.split('\n'), fromfile=out, tofile=out+'.new', lineterm=''): - print(line, file=sys.stderr) + for line in difflib.unified_diff(current.split('\n'), rendered.split('\n'), fromfile=out, tofile=out+'.new', lineterm=''): + print(line, file=sys.stderr) - if not args.force: - print(f"File `{out}` already exists, will not overwrite. Rerun with `-f` to force overwriting existing files.", file=sys.stderr) - return + if not args.force: + print(f"File `{out}` already exists, will not overwrite. Rerun with `-f` to force overwriting existing files.", file=sys.stderr) + return - dryrun_safe_write(out, rendered) - if template_permissions != default_permissions: - dryrun_safe_chmod(out, template_permissions) + dryrun_safe_write(out, rendered) + + if template_permissions != default_permissions: + dryrun_safe_chmod(out, template_permissions) + else: + current_permissions = os.stat(out).st_mode & 0o777 + if template_permissions != current_permissions: + dryrun_safe_chmod(out, template_permissions) if args.link: first_line = source.split('\n')[0] @@ -135,6 +141,12 @@ def create_symlink(source, destination): dryrun_safe_create_symlink(source, destination) +def is_content_different(file_location, test_content): + with open(file_location) as file: + content = file.read() + + return content != test_content + def get_current_umask(): current_umask = os.umask(0o022) os.umask(current_umask)