From 2d696a42d98212310b80f6085172bd6d67eea7ea Mon Sep 17 00:00:00 2001 From: Anthony Molinaro Date: Tue, 7 Apr 2020 20:08:23 -0700 Subject: [PATCH 1/9] keep checkout clean --- updateHostsFile.py | 36 ++++++++++++++++++++++-------------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/updateHostsFile.py b/updateHostsFile.py index 89e70c944..15a33a0cc 100644 --- a/updateHostsFile.py +++ b/updateHostsFile.py @@ -147,6 +147,14 @@ def main(): action="store_true", help="Skip static localhost entries " "in the final hosts file.", ) + parser.add_argument( + "--skip-readme-data-update", + "-d", + dest="skipreadmedataupdate", + default=False, + action="store_true", + help="Skip update of readme data", + ) 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["skipreadmedataupdate"]: + 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 " @@ -333,6 +344,7 @@ def prompt_for_update(freshen, update_auto): if not os.path.isfile(hosts_file): try: + print("HERE HERE") open(hosts_file, "w+").close() except (IOError, OSError): # Starting in Python 3.3, IOError is aliased @@ -1272,7 +1284,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,15 +1297,11 @@ 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 shutil.copy(old_file_path, backup_file_path) From 699b52e1bdc1a65658e8c1bd8df5eb7d4745c092 Mon Sep 17 00:00:00 2001 From: Anthony Molinaro Date: Tue, 7 Apr 2020 20:17:04 -0700 Subject: [PATCH 2/9] forgot to remove a debug --- updateHostsFile.py | 1 - 1 file changed, 1 deletion(-) diff --git a/updateHostsFile.py b/updateHostsFile.py index 15a33a0cc..fc24aace3 100644 --- a/updateHostsFile.py +++ b/updateHostsFile.py @@ -344,7 +344,6 @@ def prompt_for_update(freshen, update_auto): if not os.path.isfile(hosts_file): try: - print("HERE HERE") open(hosts_file, "w+").close() except (IOError, OSError): # Starting in Python 3.3, IOError is aliased From aec37388388a5130ef8238b4ede245eabfad7c71 Mon Sep 17 00:00:00 2001 From: Anthony Molinaro Date: Tue, 7 Apr 2020 22:18:35 -0700 Subject: [PATCH 3/9] fix unit test --- testUpdateHostsFile.py | 52 +++++++++++++++++++----------------------- 1 file changed, 23 insertions(+), 29 deletions(-) diff --git a/testUpdateHostsFile.py b/testUpdateHostsFile.py index 22fb613c6..8de506dae 100644 --- a/testUpdateHostsFile.py +++ b/testUpdateHostsFile.py @@ -1286,16 +1286,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 +1301,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 From 9c1b8b2579b4c134ed97944e7e7e51ac31df5006 Mon Sep 17 00:00:00 2001 From: Anthony Molinaro Date: Tue, 7 Apr 2020 22:40:30 -0700 Subject: [PATCH 4/9] use more succinct name for arg --- testUpdateHostsFile.py | 10 ---------- updateHostsFile.py | 14 +++++++------- 2 files changed, 7 insertions(+), 17 deletions(-) diff --git a/testUpdateHostsFile.py b/testUpdateHostsFile.py index 8de506dae..3326b9e29 100644 --- a/testUpdateHostsFile.py +++ b/testUpdateHostsFile.py @@ -1268,16 +1268,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() diff --git a/updateHostsFile.py b/updateHostsFile.py index fc24aace3..99b914f37 100644 --- a/updateHostsFile.py +++ b/updateHostsFile.py @@ -148,12 +148,12 @@ def main(): help="Skip static localhost entries " "in the final hosts file.", ) parser.add_argument( - "--skip-readme-data-update", - "-d", - dest="skipreadmedataupdate", - default=False, - action="store_true", - help="Skip update of readme data", + "--genreadmedata", + "-g", + dest="genreadmedata", + default=True, + action="store_false", + help="Skip generation of readmeData.json", ) parser.add_argument( "--output", @@ -285,7 +285,7 @@ def main(): ) final_file.close() - if not settings["skipreadmedataupdate"]: + if settings["genreadmedata"]: update_readme_data( settings["readmedatafilename"], extensions=extensions, From 37bc3821f862d1b9a35bb6fb0bef12389823c16c Mon Sep 17 00:00:00 2001 From: Anthony Molinaro Date: Tue, 7 Apr 2020 22:44:37 -0700 Subject: [PATCH 5/9] applied black --- updateHostsFile.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/updateHostsFile.py b/updateHostsFile.py index 99b914f37..e23762999 100644 --- a/updateHostsFile.py +++ b/updateHostsFile.py @@ -257,8 +257,8 @@ def main(): merge_file = create_initial_file() remove_old_hosts_file( - path_join_robust(settings["outputpath"], "hosts"), - settings["backup"]) + 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() @@ -1300,7 +1300,9 @@ def remove_old_hosts_file(old_file_path, backup): open(old_file_path, "a").close() if backup: - backup_file_path = old_file_path + "{}".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 shutil.copy(old_file_path, backup_file_path) From cac15b1c11c3b9754ec10a439de53c4422f8a329 Mon Sep 17 00:00:00 2001 From: Anthony Molinaro Date: Tue, 7 Apr 2020 22:55:02 -0700 Subject: [PATCH 6/9] use nogendata for arg --- updateHostsFile.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/updateHostsFile.py b/updateHostsFile.py index e23762999..859ff708d 100644 --- a/updateHostsFile.py +++ b/updateHostsFile.py @@ -148,11 +148,11 @@ def main(): help="Skip static localhost entries " "in the final hosts file.", ) parser.add_argument( - "--genreadmedata", + "--nogendata", "-g", - dest="genreadmedata", - default=True, - action="store_false", + dest="nogendata", + default=False, + action="store_true", help="Skip generation of readmeData.json", ) parser.add_argument( @@ -285,7 +285,7 @@ def main(): ) final_file.close() - if settings["genreadmedata"]: + if not settings["nogendata"]: update_readme_data( settings["readmedatafilename"], extensions=extensions, From 7dd582dac3a73e09f09e8b8b061f79b297f7520c Mon Sep 17 00:00:00 2001 From: Anthony Molinaro Date: Tue, 7 Apr 2020 23:07:40 -0700 Subject: [PATCH 7/9] 2 blank lines? --- testUpdateHostsFile.py | 1 + 1 file changed, 1 insertion(+) diff --git a/testUpdateHostsFile.py b/testUpdateHostsFile.py index 3326b9e29..0bb92d8a6 100644 --- a/testUpdateHostsFile.py +++ b/testUpdateHostsFile.py @@ -1268,6 +1268,7 @@ class TestFlushDnsCache(BaseStdout): ]: self.assertIn(expected, output) + class TestRemoveOldHostsFile(BaseMockDir): def setUp(self): super(TestRemoveOldHostsFile, self).setUp() From c6b54e967422280af574fb61aaa48b3c4dd402c5 Mon Sep 17 00:00:00 2001 From: Anthony Molinaro Date: Tue, 7 Apr 2020 23:26:38 -0700 Subject: [PATCH 8/9] update readme.md with new additional argument --- readme.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/readme.md b/readme.md index 4485dd374..ee3e27225 100644 --- a/readme.md +++ b/readme.md @@ -163,6 +163,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 From b87c81f7962deb50eba2296be06a3be7eb1d4adc Mon Sep 17 00:00:00 2001 From: Anthony Molinaro Date: Wed, 8 Apr 2020 11:56:50 -0700 Subject: [PATCH 9/9] update template not readme --- readme.md | 5 ----- readme_template.md | 5 +++++ 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/readme.md b/readme.md index ee3e27225..4485dd374 100644 --- a/readme.md +++ b/readme.md @@ -163,11 +163,6 @@ 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 diff --git a/readme_template.md b/readme_template.md index 10def1c4c..fb449f913 100644 --- a/readme_template.md +++ b/readme_template.md @@ -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