Create a hostfile or directory of it does not exist

This commit is contained in:
Dennis van de Hoef
2023-05-24 17:51:35 +02:00
committed by Steven Black
parent 060ebf2f47
commit 0a76a88a38

View File

@@ -274,9 +274,7 @@ def main():
) )
merge_file = create_initial_file() merge_file = create_initial_file()
remove_old_hosts_file( remove_old_hosts_file(settings["outputpath"], "hosts", settings["backup"])
path_join_robust(settings["outputpath"], "hosts"), settings["backup"]
)
if settings["compress"]: if settings["compress"]:
final_file = open(path_join_robust(settings["outputpath"], "hosts"), "w+b") final_file = open(path_join_robust(settings["outputpath"], "hosts"), "w+b")
compressed_file = tempfile.NamedTemporaryFile() compressed_file = tempfile.NamedTemporaryFile()
@@ -1411,7 +1409,7 @@ def flush_dns_cache():
print_failure("Unable to determine DNS management tool.") print_failure("Unable to determine DNS management tool.")
def remove_old_hosts_file(old_file_path, backup): def remove_old_hosts_file(path_to_file, file_name, backup):
""" """
Remove the old hosts file. Remove the old hosts file.
@@ -1424,21 +1422,25 @@ def remove_old_hosts_file(old_file_path, backup):
Whether or not to backup the existing hosts file. Whether or not to backup the existing hosts file.
""" """
# Create if already removed, so remove won't raise an error. full_file_path = path_join_robust(path_to_file, file_name)
open(old_file_path, "a").close()
if backup: if os.path.exists(full_file_path):
backup_file_path = old_file_path + "-{}".format( if backup:
time.strftime("%Y-%m-%d-%H-%M-%S") backup_file_path = full_file_path + "-{}".format(
) time.strftime("%Y-%m-%d-%H-%M-%S")
)
# Make a backup copy, marking the date in which the list was updated # Make a backup copy, marking the date in which the list was updated
shutil.copy(old_file_path, backup_file_path) shutil.copy(full_file_path, backup_file_path)
os.remove(old_file_path) os.remove(full_file_path)
# Create directory if not exists
if not os.path.exists(path_to_file):
os.makedirs(path_to_file)
# Create new empty hosts file # Create new empty hosts file
open(old_file_path, "a").close() open(full_file_path, "a").close()
# End File Logic # End File Logic