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

12
run.py
View File

@ -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,6 +104,7 @@ def convert_template(env, template, key_value_store, out):
return
dryrun_safe_write(out, rendered)
if template_permissions != default_permissions:
dryrun_safe_chmod(out, template_permissions)
if args.link:
@ -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: