Preserve file permissions from template

This commit is contained in:
Daniel_I_Am 2021-12-27 15:51:08 +01:00
parent 95c237d2e8
commit c169e557cd
3 changed files with 9 additions and 0 deletions

9
run.py
View File

@ -81,6 +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 = env.from_string(source)
rendered = template.render(**key_value_store)
@ -101,6 +103,7 @@ def convert_template(env, template, key_value_store, out):
return
dryrun_safe_write(out, rendered)
dryrun_safe_chmod(out, template_permissions)
if args.link:
first_line = source.split('\n')[0]
@ -134,6 +137,12 @@ def dryrun_safe_write(location, content):
else:
print(f'Would write to `{location}`.')
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}')
def dryrun_safe_remove(location):
if not args.dryrun:
os.remove(location)

0
templates/rofi/askpass-rofi Normal file → Executable file
View File

0
templates/rofi/rofisudo Normal file → Executable file
View File