Merge pull request #1216 from djnym/clean-checkout-fixes

Keep checkout unmodified when adding outputpath.
This commit is contained in:
Steven Black
2020-04-08 16:23:58 -04:00
committed by GitHub
3 changed files with 50 additions and 51 deletions

View File

@@ -131,6 +131,11 @@ in a subfolder. If the subfolder does not exist, it will be created.
section at the top, containing lines like `127.0.0.1 localhost`. This is
useful for configuring proximate DNS services on the local network.
`--nogendata`, or `-g`: `false` (default) or `true`, skip the generation of the
readmeData.json file used for generating readme.md files. This is useful if you are
generating host files with additional whitelists or blacklists and want to keep your
local checkout of this repo unmodified.
`--compress`, or `-c`: `false` (default) or `true`, *Compress* the hosts file
ignoring non-necessary lines (empty lines and comments) and putting multiple
domains in each line. Reducing the number of lines of the hosts file improves

View File

@@ -1269,15 +1269,6 @@ class TestFlushDnsCache(BaseStdout):
self.assertIn(expected, output)
def mock_path_join_robust(*args):
# We want to hard-code the backup hosts filename
# instead of parametrizing based on current time.
if len(args) == 2 and args[1].startswith("hosts-"):
return os.path.join(args[0], "hosts-new")
else:
return os.path.join(*args)
class TestRemoveOldHostsFile(BaseMockDir):
def setUp(self):
super(TestRemoveOldHostsFile, self).setUp()
@@ -1286,16 +1277,14 @@ class TestRemoveOldHostsFile(BaseMockDir):
def test_remove_hosts_file(self):
old_dir_count = self.dir_count
with self.mock_property("updateHostsFile.BASEDIR_PATH"):
updateHostsFile.BASEDIR_PATH = self.test_dir
remove_old_hosts_file(backup=False)
remove_old_hosts_file(self.hosts_file, backup=False)
new_dir_count = old_dir_count + 1
self.assertEqual(self.dir_count, new_dir_count)
new_dir_count = old_dir_count + 1
self.assertEqual(self.dir_count, new_dir_count)
with open(self.hosts_file, "r") as f:
contents = f.read()
self.assertEqual(contents, "")
with open(self.hosts_file, "r") as f:
contents = f.read()
self.assertEqual(contents, "")
def test_remove_hosts_file_exists(self):
with open(self.hosts_file, "w") as f:
@@ -1303,40 +1292,36 @@ class TestRemoveOldHostsFile(BaseMockDir):
old_dir_count = self.dir_count
with self.mock_property("updateHostsFile.BASEDIR_PATH"):
updateHostsFile.BASEDIR_PATH = self.test_dir
remove_old_hosts_file(backup=False)
remove_old_hosts_file(self.hosts_file, backup=False)
new_dir_count = old_dir_count
self.assertEqual(self.dir_count, new_dir_count)
new_dir_count = old_dir_count
self.assertEqual(self.dir_count, new_dir_count)
with open(self.hosts_file, "r") as f:
contents = f.read()
self.assertEqual(contents, "")
with open(self.hosts_file, "r") as f:
contents = f.read()
self.assertEqual(contents, "")
@mock.patch("updateHostsFile.path_join_robust", side_effect=mock_path_join_robust)
@mock.patch("time.strftime", return_value="-new")
def test_remove_hosts_file_backup(self, _):
with open(self.hosts_file, "w") as f:
f.write("foo")
old_dir_count = self.dir_count
with self.mock_property("updateHostsFile.BASEDIR_PATH"):
updateHostsFile.BASEDIR_PATH = self.test_dir
remove_old_hosts_file(backup=True)
remove_old_hosts_file(self.hosts_file, backup=True)
new_dir_count = old_dir_count + 1
self.assertEqual(self.dir_count, new_dir_count)
new_dir_count = old_dir_count + 1
self.assertEqual(self.dir_count, new_dir_count)
with open(self.hosts_file, "r") as f:
contents = f.read()
self.assertEqual(contents, "")
with open(self.hosts_file, "r") as f:
contents = f.read()
self.assertEqual(contents, "")
new_hosts_file = self.hosts_file + "-new"
new_hosts_file = self.hosts_file + "-new"
with open(new_hosts_file, "r") as f:
contents = f.read()
self.assertEqual(contents, "foo")
with open(new_hosts_file, "r") as f:
contents = f.read()
self.assertEqual(contents, "foo")
# End File Logic

View File

@@ -147,6 +147,14 @@ def main():
action="store_true",
help="Skip static localhost entries " "in the final hosts file.",
)
parser.add_argument(
"--nogendata",
"-g",
dest="nogendata",
default=False,
action="store_true",
help="Skip generation of readmeData.json",
)
parser.add_argument(
"--output",
"-o",
@@ -248,7 +256,9 @@ def main():
)
merge_file = create_initial_file()
remove_old_hosts_file(settings["backup"])
remove_old_hosts_file(
path_join_robust(settings["outputpath"], "hosts"), settings["backup"]
)
if settings["compress"]:
final_file = open(path_join_robust(settings["outputpath"], "hosts"), "w+b")
compressed_file = tempfile.NamedTemporaryFile()
@@ -275,13 +285,14 @@ def main():
)
final_file.close()
update_readme_data(
settings["readmedatafilename"],
extensions=extensions,
numberofrules=number_of_rules,
outputsubfolder=output_subfolder,
sourcesdata=sources_data,
)
if not settings["nogendata"]:
update_readme_data(
settings["readmedatafilename"],
extensions=extensions,
numberofrules=number_of_rules,
outputsubfolder=output_subfolder,
sourcesdata=sources_data,
)
print_success(
"Success! The hosts file has been saved in folder "
@@ -1272,7 +1283,7 @@ def flush_dns_cache():
print_failure("Unable to determine DNS management tool.")
def remove_old_hosts_file(backup):
def remove_old_hosts_file(old_file_path, backup):
"""
Remove the old hosts file.
@@ -1285,14 +1296,12 @@ def remove_old_hosts_file(backup):
Whether or not to backup the existing hosts file.
"""
old_file_path = path_join_robust(BASEDIR_PATH, "hosts")
# Create if already removed, so remove won't raise an error.
open(old_file_path, "a").close()
if backup:
backup_file_path = path_join_robust(
BASEDIR_PATH, "hosts-{}".format(time.strftime("%Y-%m-%d-%H-%M-%S"))
backup_file_path = old_file_path + "{}".format(
time.strftime("%Y-%m-%d-%H-%M-%S")
)
# Make a backup copy, marking the date in which the list was updated