Optimize things that need to run if already correct

This commit is contained in:
Daniel_I_Am 2021-12-27 16:39:52 +01:00
parent 2c1000af45
commit c959f14172

36
run.py
View File

@ -91,21 +91,27 @@ def convert_template(env, template, key_value_store, out):
if not os.path.isdir(out_dir_name): if not os.path.isdir(out_dir_name):
dryrun_safe_mkdir(out_dir_name) dryrun_safe_mkdir(out_dir_name)
if os.path.isfile(out) and not args.force: if os.path.isfile(out):
if args.diff: if is_content_different(out, rendered):
with open(out) as file: if args.diff:
current = file.read() 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=''): for line in difflib.unified_diff(current.split('\n'), rendered.split('\n'), fromfile=out, tofile=out+'.new', lineterm=''):
print(line, file=sys.stderr) print(line, file=sys.stderr)
if not args.force: if not args.force:
print(f"File `{out}` already exists, will not overwrite. Rerun with `-f` to force overwriting existing files.", file=sys.stderr) print(f"File `{out}` already exists, will not overwrite. Rerun with `-f` to force overwriting existing files.", file=sys.stderr)
return return
dryrun_safe_write(out, rendered) dryrun_safe_write(out, rendered)
if template_permissions != default_permissions:
dryrun_safe_chmod(out, template_permissions) 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: if args.link:
first_line = source.split('\n')[0] first_line = source.split('\n')[0]
@ -135,6 +141,12 @@ def create_symlink(source, destination):
dryrun_safe_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(): def get_current_umask():
current_umask = os.umask(0o022) current_umask = os.umask(0o022)
os.umask(current_umask) os.umask(current_umask)