@@ -298,22 +298,23 @@ def main():
298298 nounifiedhosts = nounifiedhosts ,
299299 )
300300
301- mergefile = create_initial_file (
302- nounifiedhosts = nounifiedhosts ,
303- )
304301 remove_old_hosts_file (settings ["outputpath" ], "hosts" , settings ["backup" ])
305- if settings ["compress" ]:
306- finalfile = open (path_join_robust (settings ["outputpath" ], "hosts" ), "w+b" )
307- compressedfile = tempfile .NamedTemporaryFile ()
308- remove_dups_and_excl (mergefile , exclusionregexes , targetips , compressedfile )
309- compress_file (compressedfile , targetips , finalfile )
310- elif settings ["minimise" ]:
311- finalfile = open (path_join_robust (settings ["outputpath" ], "hosts" ), "w+b" )
312- minimisedfile = tempfile .NamedTemporaryFile ()
313- remove_dups_and_excl (mergefile , exclusionregexes , targetips , minimisedfile )
314- minimise_file (minimisedfile , targetips , finalfile )
315- else :
316- finalfile = remove_dups_and_excl (mergefile , exclusionregexes , targetips )
302+
303+ if not os .path .exists (settings ["outputpath" ]):
304+ os .makedirs (settings ["outputpath" ])
305+
306+ finalfile = open (path_join_robust (settings ["outputpath" ], "hosts" ), "w+b" )
307+ with create_initial_file (nounifiedhosts = nounifiedhosts ) as mergefile :
308+ if settings ["compress" ]:
309+ with tempfile .NamedTemporaryFile () as uncompressedfile :
310+ remove_dups_and_excl (mergefile , exclusionregexes , targetips , uncompressedfile )
311+ compress_file (uncompressedfile , targetips , finalfile )
312+ elif settings ["minimise" ]:
313+ with tempfile .NamedTemporaryFile () as unminimisedfile :
314+ remove_dups_and_excl (mergefile , exclusionregexes , targetips , unminimisedfile )
315+ minimise_file (unminimisedfile , targetips , finalfile )
316+ else :
317+ remove_dups_and_excl (mergefile , exclusionregexes , targetips , finalfile )
317318
318319 numberofrules = settings ["numberofrules" ]
319320 outputsubfolder = settings ["outputsubfolder" ]
@@ -977,7 +978,7 @@ def minimise_file(inputfile, targetips, outputfile):
977978 inputfile .close ()
978979
979980
980- def remove_dups_and_excl (mergefile , exclusionregexes , targetips , outputfile = None ):
981+ def remove_dups_and_excl (mergefile , exclusionregexes , targetips , outputfile ):
981982 """
982983 Remove duplicates and remove hosts that we are excluding.
983984
@@ -993,8 +994,7 @@ def remove_dups_and_excl(mergefile, exclusionregexes, targetips, outputfile=None
993994 targetips : list[str]
994995 The list of target IP addresses
995996 outputfile : file
996- The file object in which the result is written. If None, the file
997- 'settings["outputpath"]' will be created.
997+ The file object to which the result is written.
998998 """
999999
10001000 numberofrules = settings ["numberofrules" ]
@@ -1007,13 +1007,7 @@ def remove_dups_and_excl(mergefile, exclusionregexes, targetips, outputfile=None
10071007 if line and not line .startswith ("#" ):
10081008 settings ["exclusions" ].append (line )
10091009
1010- if not os .path .exists (settings ["outputpath" ]):
1011- os .makedirs (settings ["outputpath" ])
1012-
1013- if outputfile is None :
1014- finalfile = open (path_join_robust (settings ["outputpath" ], "hosts" ), "w+b" )
1015- else :
1016- finalfile = outputfile
1010+ finalfile = outputfile
10171011
10181012 # analyze any post.json here
10191013 post_json_path = os .path .join (os .path .dirname (finalfile .name ), "post.json" )
@@ -1084,12 +1078,6 @@ def remove_dups_and_excl(mergefile, exclusionregexes, targetips, outputfile=None
10841078 write_data (finalfile , normalized_rule )
10851079
10861080 settings ["numberofrules" ] = numberofrules
1087- mergefile .close ()
1088-
1089- if outputfile is None :
1090- return finalfile
1091- else :
1092- return outputfile
10931081
10941082
10951083def normalize_rule (rule , targetip , keep_domain_comments ):
0 commit comments