Issue #733: fix – more robust if the .example files are missing.

This commit is contained in:
StevenBlack 2018-09-08 21:19:51 -04:00
parent 429cc64f29
commit ed6f65d970

View File

@ -643,8 +643,9 @@ def create_initial_file():
maybe_copy_example_file(settings["blacklistfile"]) maybe_copy_example_file(settings["blacklistfile"])
with open(settings["blacklistfile"], "r") as curFile: if os.path.isfile(settings["blacklistfile"]):
write_data(merge_file, curFile.read()) with open(settings["blacklistfile"], "r") as curFile:
write_data(merge_file, curFile.read())
return merge_file return merge_file
@ -742,11 +743,12 @@ def remove_dups_and_excl(merge_file, exclusion_regexes, output_file=None):
number_of_rules = settings["numberofrules"] number_of_rules = settings["numberofrules"]
maybe_copy_example_file(settings["whitelistfile"]) maybe_copy_example_file(settings["whitelistfile"])
with open(settings["whitelistfile"], "r") as ins: if os.path.isfile(settings["whitelistfile"]):
for line in ins: with open(settings["whitelistfile"], "r") as ins:
line = line.strip(" \t\n\r") for line in ins:
if line and not line.startswith("#"): line = line.strip(" \t\n\r")
settings["exclusions"].append(line) if line and not line.startswith("#"):
settings["exclusions"].append(line)
if not os.path.exists(settings["outputpath"]): if not os.path.exists(settings["outputpath"]):
os.makedirs(settings["outputpath"]) os.makedirs(settings["outputpath"])
@ -960,8 +962,9 @@ def write_opening_header(final_file, **header_params):
preamble = path_join_robust(BASEDIR_PATH, "myhosts") preamble = path_join_robust(BASEDIR_PATH, "myhosts")
maybe_copy_example_file(preamble) maybe_copy_example_file(preamble)
with open(preamble, "r") as f: if os.path.isfile(preamble):
write_data(final_file, f.read()) with open(preamble, "r") as f:
write_data(final_file, f.read())
final_file.write(file_contents) final_file.write(file_contents)
@ -1221,6 +1224,8 @@ def maybe_copy_example_file(file_path):
If the path does exist, nothing happens in this function. If the path does exist, nothing happens in this function.
If the path doesn't exist, and the ".example" file doesn't exist, nothing happens in this function.
Parameters Parameters
---------- ----------
file_path : str file_path : str
@ -1229,7 +1234,8 @@ def maybe_copy_example_file(file_path):
if not os.path.isfile(file_path): if not os.path.isfile(file_path):
example_file_path = file_path + ".example" example_file_path = file_path + ".example"
shutil.copyfile(example_file_path, file_path) if os.path.isfile(example_file_path):
shutil.copyfile(example_file_path, file_path)
def get_file_by_url(url): def get_file_by_url(url):