Only change permissions if not already correct

This commit is contained in:
Daniel_I_Am 2021-12-27 16:05:29 +01:00
parent a117211bd2
commit 9438616aed

14
run.py
View File

@ -81,7 +81,8 @@ def convert_template(env, template, key_value_store, out):
with open(template, 'r') as file: with open(template, 'r') as file:
source = file.read() 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) template = env.from_string(source)
rendered = template.render(**key_value_store) rendered = template.render(**key_value_store)
@ -103,7 +104,8 @@ def convert_template(env, template, key_value_store, out):
return return
dryrun_safe_write(out, rendered) 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: if args.link:
first_line = source.split('\n')[0] first_line = source.split('\n')[0]
@ -133,6 +135,12 @@ def create_symlink(source, destination):
dryrun_safe_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): def dryrun_safe_write(location, content):
if not args.dryrun: if not args.dryrun:
with open(location, 'w') as file: with open(location, 'w') as file:
@ -144,7 +152,7 @@ def dryrun_safe_chmod(location, permissions):
if not args.dryrun: if not args.dryrun:
os.chmod(location, permissions) os.chmod(location, permissions)
else: 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): def dryrun_safe_remove(location):
if not args.dryrun: if not args.dryrun: