]> git.99rst.org Git - stevenblack-hosts.git/commitdiff
Always close the output hosts file
authorXhmikosR <redacted>
Sun, 5 Jul 2026 14:53:50 +0000 (17:53 +0300)
committerXhmikosR <redacted>
Sun, 5 Jul 2026 14:53:50 +0000 (17:53 +0300)
Open it in a with statement; drop the unused self-opening branch in remove_dups_and_excl.

updateHostsFile.py

index 1db102c2acd6dc6a531836b71a5d35c97be660af..cb716b07965db3288186fbd7284da8be03bb7282 100755 (executable)
@@ -288,32 +288,29 @@ def main():
         nounifiedhosts=nounifiedhosts,
     )
     remove_old_hosts_file(settings["outputpath"], "hosts", settings["backup"])
-    if settings["compress"]:
-        finalfile = open(path_join_robust(settings["outputpath"], "hosts"), "w+b")
-        compressedfile = tempfile.NamedTemporaryFile()
-        remove_dups_and_excl(mergefile, exclusionregexes, compressedfile)
-        compress_file(compressedfile, settings["targetip"], finalfile)
-    elif settings["minimise"]:
-        finalfile = open(path_join_robust(settings["outputpath"], "hosts"), "w+b")
-        minimisedfile = tempfile.NamedTemporaryFile()
-        remove_dups_and_excl(mergefile, exclusionregexes, minimisedfile)
-        minimise_file(minimisedfile, settings["targetip"], finalfile)
-    else:
-        finalfile = remove_dups_and_excl(mergefile, exclusionregexes)
+    with open(path_join_robust(settings["outputpath"], "hosts"), "w+b") as finalfile:
+        if settings["compress"] or settings["minimise"]:
+            with tempfile.NamedTemporaryFile() as tmpfile:
+                remove_dups_and_excl(mergefile, exclusionregexes, tmpfile)
+                if settings["compress"]:
+                    compress_file(tmpfile, settings["targetip"], finalfile)
+                else:
+                    minimise_file(tmpfile, settings["targetip"], finalfile)
+        else:
+            remove_dups_and_excl(mergefile, exclusionregexes, finalfile)
 
-    numberofrules = settings["numberofrules"]
-    outputsubfolder = settings["outputsubfolder"]
-    skipstatichosts = settings["skipstatichosts"]
+        numberofrules = settings["numberofrules"]
+        outputsubfolder = settings["outputsubfolder"]
+        skipstatichosts = settings["skipstatichosts"]
 
-    write_opening_header(
-        finalfile,
-        extensions=extensions,
-        numberofrules=numberofrules,
-        outputsubfolder=outputsubfolder,
-        skipstatichosts=skipstatichosts,
-        nounifiedhosts=nounifiedhosts,
-    )
-    finalfile.close()
+        write_opening_header(
+            finalfile,
+            extensions=extensions,
+            numberofrules=numberofrules,
+            outputsubfolder=outputsubfolder,
+            skipstatichosts=skipstatichosts,
+            nounifiedhosts=nounifiedhosts,
+        )
 
     if not settings["nogendata"]:
         update_readme_data(
@@ -916,7 +913,7 @@ def minimise_file(inputfile, targetip, outputfile):
     inputfile.close()
 
 
-def remove_dups_and_excl(mergefile, exclusionregexes, outputfile=None):
+def remove_dups_and_excl(mergefile, exclusionregexes, finalfile):
     """
     Remove duplicates and remove hosts that we are excluding.
 
@@ -929,9 +926,8 @@ def remove_dups_and_excl(mergefile, exclusionregexes, outputfile=None):
         The file object that contains the hostnames that we are pruning.
     exclusionregexes : list
         The list of regex patterns used to exclude domains.
-    outputfile : file
-        The file object in which the result is written. If None, the file
-        'settings["outputpath"]' will be created.
+    finalfile : file
+        The file object in which the result is written.
     """
 
     numberofrules = settings["numberofrules"]
@@ -947,11 +943,6 @@ def remove_dups_and_excl(mergefile, exclusionregexes, outputfile=None):
     if not os.path.exists(settings["outputpath"]):
         os.makedirs(settings["outputpath"])
 
-    if outputfile is None:
-        finalfile = open(path_join_robust(settings["outputpath"], "hosts"), "w+b")
-    else:
-        finalfile = outputfile
-
     # analyze any post.json here
     post_json_path = os.path.join(os.path.dirname(finalfile.name), "post.json")
     filters = []
@@ -1018,9 +1009,6 @@ def remove_dups_and_excl(mergefile, exclusionregexes, outputfile=None):
     settings["numberofrules"] = numberofrules
     mergefile.close()
 
-    if outputfile is None:
-        return finalfile
-
 
 def normalize_rule(rule, targetip, keep_domain_comments):
     """
git clone https://git.99rst.org/PROJECT