From 9438616aed379f8721c5f634c8bdabbc21a7118f Mon Sep 17 00:00:00 2001 From: Daniel-I-Am Date: Mon, 27 Dec 2021 16:05:29 +0100 Subject: [PATCH] Only change permissions if not already correct --- run.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/run.py b/run.py index 0d68bbe..1a7a9bc 100755 --- a/run.py +++ b/run.py @@ -81,7 +81,8 @@ def convert_template(env, template, key_value_store, out): with open(template, 'r') as file: source = file.read() - template_permissions = oct(os.stat(template).st_mode & 0o777) + template_permissions = os.stat(template).st_mode & 0o777 + default_permissions = 0o666 & ~get_current_umask() template = env.from_string(source) rendered = template.render(**key_value_store) @@ -103,7 +104,8 @@ def convert_template(env, template, key_value_store, out): return dryrun_safe_write(out, rendered) - dryrun_safe_chmod(out, template_permissions) + if template_permissions != default_permissions: + dryrun_safe_chmod(out, template_permissions) if args.link: first_line = source.split('\n')[0] @@ -133,6 +135,12 @@ def create_symlink(source, destination): dryrun_safe_create_symlink(source, destination) +def get_current_umask(): + current_umask = os.umask(0o022) + os.umask(current_umask) + + return current_umask + def dryrun_safe_write(location, content): if not args.dryrun: with open(location, 'w') as file: @@ -144,7 +152,7 @@ def dryrun_safe_chmod(location, permissions): if not args.dryrun: os.chmod(location, permissions) else: - print(f'Would change the permissions of {location} to {permissions}') + print(f'Would change the permissions of {location} to {oct(permissions)}') def dryrun_safe_remove(location): if not args.dryrun: