enhance tar compression function to skip recompilation if not needed

This commit is contained in:
Eduardo Silva
2025-12-31 18:12:53 -03:00
parent 3f238ce7b9
commit a435fc3172

View File

@@ -14,12 +14,33 @@ def compress_dnsmasq_config():
os.remove(output_file) os.remove(output_file)
return None return None
with tarfile.open(output_file, "w:gz") as tar: if not os.path.isdir(base_dir):
for filename in os.listdir(base_dir): if os.path.exists(output_file):
if filename.endswith(".conf"): os.remove(output_file)
fullpath = os.path.join(base_dir, filename) return None
tar.add(fullpath, arcname=filename)
conf_files = [
fn for fn in os.listdir(base_dir)
if fn.endswith(".conf") and os.path.isfile(os.path.join(base_dir, fn))
]
# If tar exists and is newer (or equal) than all .conf, do not recompile
if os.path.exists(output_file):
tar_mtime = os.path.getmtime(output_file)
newest_conf_mtime = max(
os.path.getmtime(os.path.join(base_dir, fn)) for fn in conf_files
)
if newest_conf_mtime <= tar_mtime:
return output_file
# Create tar.gz
tmp_output = output_file + ".tmp"
with tarfile.open(tmp_output, "w:gz") as tar:
for fn in conf_files:
fullpath = os.path.join(base_dir, fn)
tar.add(fullpath, arcname=fn)
os.replace(tmp_output, output_file)
return output_file return output_file