mirror of
https://github.com/StevenBlack/hosts.git
synced 2026-09-24 06:10:20 +01:00
Merge pull request #3188 from XhmikosR/xmr/codeql
A couple of CodeQL fixes
This commit is contained in:
+27
-36
@@ -288,32 +288,29 @@ def main():
|
|||||||
nounifiedhosts=nounifiedhosts,
|
nounifiedhosts=nounifiedhosts,
|
||||||
)
|
)
|
||||||
remove_old_hosts_file(settings["outputpath"], "hosts", settings["backup"])
|
remove_old_hosts_file(settings["outputpath"], "hosts", settings["backup"])
|
||||||
if settings["compress"]:
|
with open(path_join_robust(settings["outputpath"], "hosts"), "w+b") as finalfile:
|
||||||
finalfile = open(path_join_robust(settings["outputpath"], "hosts"), "w+b")
|
if settings["compress"] or settings["minimise"]:
|
||||||
compressedfile = tempfile.NamedTemporaryFile()
|
with tempfile.NamedTemporaryFile() as tmpfile:
|
||||||
remove_dups_and_excl(mergefile, exclusionregexes, compressedfile)
|
remove_dups_and_excl(mergefile, exclusionregexes, tmpfile)
|
||||||
compress_file(compressedfile, settings["targetip"], finalfile)
|
if settings["compress"]:
|
||||||
elif settings["minimise"]:
|
compress_file(tmpfile, settings["targetip"], finalfile)
|
||||||
finalfile = open(path_join_robust(settings["outputpath"], "hosts"), "w+b")
|
else:
|
||||||
minimisedfile = tempfile.NamedTemporaryFile()
|
minimise_file(tmpfile, settings["targetip"], finalfile)
|
||||||
remove_dups_and_excl(mergefile, exclusionregexes, minimisedfile)
|
else:
|
||||||
minimise_file(minimisedfile, settings["targetip"], finalfile)
|
remove_dups_and_excl(mergefile, exclusionregexes, finalfile)
|
||||||
else:
|
|
||||||
finalfile = remove_dups_and_excl(mergefile, exclusionregexes)
|
|
||||||
|
|
||||||
numberofrules = settings["numberofrules"]
|
numberofrules = settings["numberofrules"]
|
||||||
outputsubfolder = settings["outputsubfolder"]
|
outputsubfolder = settings["outputsubfolder"]
|
||||||
skipstatichosts = settings["skipstatichosts"]
|
skipstatichosts = settings["skipstatichosts"]
|
||||||
|
|
||||||
write_opening_header(
|
write_opening_header(
|
||||||
finalfile,
|
finalfile,
|
||||||
extensions=extensions,
|
extensions=extensions,
|
||||||
numberofrules=numberofrules,
|
numberofrules=numberofrules,
|
||||||
outputsubfolder=outputsubfolder,
|
outputsubfolder=outputsubfolder,
|
||||||
skipstatichosts=skipstatichosts,
|
skipstatichosts=skipstatichosts,
|
||||||
nounifiedhosts=nounifiedhosts,
|
nounifiedhosts=nounifiedhosts,
|
||||||
)
|
)
|
||||||
finalfile.close()
|
|
||||||
|
|
||||||
if not settings["nogendata"]:
|
if not settings["nogendata"]:
|
||||||
update_readme_data(
|
update_readme_data(
|
||||||
@@ -916,7 +913,7 @@ def minimise_file(inputfile, targetip, outputfile):
|
|||||||
inputfile.close()
|
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.
|
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.
|
The file object that contains the hostnames that we are pruning.
|
||||||
exclusionregexes : list
|
exclusionregexes : list
|
||||||
The list of regex patterns used to exclude domains.
|
The list of regex patterns used to exclude domains.
|
||||||
outputfile : file
|
finalfile : file
|
||||||
The file object in which the result is written. If None, the file
|
The file object in which the result is written.
|
||||||
'settings["outputpath"]' will be created.
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
numberofrules = settings["numberofrules"]
|
numberofrules = settings["numberofrules"]
|
||||||
@@ -947,11 +943,6 @@ def remove_dups_and_excl(mergefile, exclusionregexes, outputfile=None):
|
|||||||
if not os.path.exists(settings["outputpath"]):
|
if not os.path.exists(settings["outputpath"]):
|
||||||
os.makedirs(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
|
# analyze any post.json here
|
||||||
post_json_path = os.path.join(os.path.dirname(finalfile.name), "post.json")
|
post_json_path = os.path.join(os.path.dirname(finalfile.name), "post.json")
|
||||||
filters = []
|
filters = []
|
||||||
@@ -1018,9 +1009,6 @@ def remove_dups_and_excl(mergefile, exclusionregexes, outputfile=None):
|
|||||||
settings["numberofrules"] = numberofrules
|
settings["numberofrules"] = numberofrules
|
||||||
mergefile.close()
|
mergefile.close()
|
||||||
|
|
||||||
if outputfile is None:
|
|
||||||
return finalfile
|
|
||||||
|
|
||||||
|
|
||||||
# Dot-separated labels of [a-z0-9_-], hyphens not at label ends, at least two
|
# Dot-separated labels of [a-z0-9_-], hyphens not at label ends, at least two
|
||||||
# labels. Expects a lowercased hostname.
|
# labels. Expects a lowercased hostname.
|
||||||
@@ -1477,6 +1465,8 @@ def move_hosts_file_into_place(finalfile):
|
|||||||
print_failure(f"Replacing {target_file} failed.")
|
print_failure(f"Replacing {target_file} failed.")
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
def flush_dns_cache():
|
def flush_dns_cache():
|
||||||
"""
|
"""
|
||||||
@@ -1679,6 +1669,7 @@ def domain_to_idna(line):
|
|||||||
|
|
||||||
splited_line[index] = splited_line[index].encode("IDNA").decode("UTF-8")
|
splited_line[index] = splited_line[index].encode("IDNA").decode("UTF-8")
|
||||||
except IndexError:
|
except IndexError:
|
||||||
|
# No hostname field to encode, leave the line as-is.
|
||||||
pass
|
pass
|
||||||
return separator.join(splited_line)
|
return separator.join(splited_line)
|
||||||
return line.encode("IDNA").decode("UTF-8")
|
return line.encode("IDNA").decode("UTF-8")
|
||||||
|
|||||||
+4
-2
@@ -90,8 +90,10 @@ def main():
|
|||||||
"wt",
|
"wt",
|
||||||
encoding="utf-8",
|
encoding="utf-8",
|
||||||
newline="\n",
|
newline="\n",
|
||||||
) as out:
|
) as out, open(
|
||||||
for line in open(README_TEMPLATE, encoding="utf-8", newline="\n"):
|
README_TEMPLATE, encoding="utf-8", newline="\n"
|
||||||
|
) as template:
|
||||||
|
for line in template:
|
||||||
line = line.replace(
|
line = line.replace(
|
||||||
"@GEN_DATE@", time.strftime("%B %d %Y", time.gmtime())
|
"@GEN_DATE@", time.strftime("%B %d %Y", time.gmtime())
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user