Skip to content

Commit d8df5f1

Browse files
committed
Use example files to allow proper git-ignoring
The following files: * myhosts * blacklist * whitelist can be all be modified by the user for personal usage. However, git is tracking these files since they exist in the repository, which makes it difficult to do so without accidentally pushing one's own customizations. This commit converts those examples to ".example" files, which serve as the defaults if one of the files listed above does not exist. Closes gh-144.
1 parent fbbd071 commit d8df5f1

5 files changed

+40
-49
lines changed
File renamed without changes.

myhosts renamed to myhosts.example

File renamed without changes.

testUpdateHostsFile.py

Lines changed: 9 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -904,44 +904,10 @@ def test_extensions(self):
904904
):
905905
self.assertNotIn(expected, contents)
906906

907-
def test_no_preamble(self):
908-
# We should not even attempt to read this, as it is a directory.
909-
hosts_dir = os.path.join(self.test_dir, "myhosts")
910-
os.mkdir(hosts_dir)
911-
912-
kwargs = dict(extensions="", outputsubfolder="",
913-
numberofrules=5, skipstatichosts=True)
914-
915-
with self.mock_property("updateHostsFile.BASEDIR_PATH"):
916-
updateHostsFile.BASEDIR_PATH = self.test_dir
917-
write_opening_header(self.final_file, **kwargs)
918-
919-
contents = self.final_file.getvalue()
920-
contents = contents.decode("UTF-8")
921-
922-
# Expected contents.
923-
for expected in (
924-
"# This hosts file is a merged collection",
925-
"# with a dash of crowd sourcing via Github",
926-
"# Number of unique domains: {count}".format(
927-
count=kwargs["numberofrules"]),
928-
"Fetch the latest version of this file:",
929-
"Project home page: https://github.com/StevenBlack/hosts",
930-
):
931-
self.assertIn(expected, contents)
932-
933-
# Expected non-contents.
934-
for expected in (
935-
"# Extensions added to this file:",
936-
"127.0.0.1 localhost",
937-
"127.0.0.1 local",
938-
"127.0.0.53",
939-
"127.0.1.1",
940-
):
941-
self.assertNotIn(expected, contents)
942-
943-
def test_preamble(self):
907+
def _check_preamble(self, check_copy):
944908
hosts_file = os.path.join(self.test_dir, "myhosts")
909+
hosts_file += ".example" if check_copy else ""
910+
945911
with open(hosts_file, "w") as f:
946912
f.write("peter-piper-picked-a-pepper")
947913

@@ -977,6 +943,12 @@ def test_preamble(self):
977943
):
978944
self.assertNotIn(expected, contents)
979945

946+
def test_preamble_exists(self):
947+
self._check_preamble(True)
948+
949+
def test_preamble_copy(self):
950+
self._check_preamble(False)
951+
980952
def tearDown(self):
981953
super(TestWriteOpeningHeader, self).tearDown()
982954
self.final_file.close()

updateHostsFile.py

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -641,9 +641,10 @@ def create_initial_file():
641641
with open(filename, "r") as curFile:
642642
write_data(merge_file, curFile.read())
643643

644-
if os.path.isfile(settings["blacklistfile"]):
645-
with open(settings["blacklistfile"], "r") as curFile:
646-
write_data(merge_file, curFile.read())
644+
maybe_copy_example_file(settings["blacklistfile"])
645+
646+
with open(settings["blacklistfile"], "r") as curFile:
647+
write_data(merge_file, curFile.read())
647648

648649
return merge_file
649650

@@ -739,12 +740,13 @@ def remove_dups_and_excl(merge_file, exclusion_regexes, output_file=None):
739740
"""
740741

741742
number_of_rules = settings["numberofrules"]
742-
if os.path.isfile(settings["whitelistfile"]):
743-
with open(settings["whitelistfile"], "r") as ins:
744-
for line in ins:
745-
line = line.strip(" \t\n\r")
746-
if line and not line.startswith("#"):
747-
settings["exclusions"].append(line)
743+
maybe_copy_example_file(settings["whitelistfile"])
744+
745+
with open(settings["whitelistfile"], "r") as ins:
746+
for line in ins:
747+
line = line.strip(" \t\n\r")
748+
if line and not line.startswith("#"):
749+
settings["exclusions"].append(line)
748750

749751
if not os.path.exists(settings["outputpath"]):
750752
os.makedirs(settings["outputpath"])
@@ -956,10 +958,10 @@ def write_opening_header(final_file, **header_params):
956958
write_data(final_file, "\n")
957959

958960
preamble = path_join_robust(BASEDIR_PATH, "myhosts")
961+
maybe_copy_example_file(preamble)
959962

960-
if os.path.isfile(preamble):
961-
with open(preamble, "r") as f:
962-
write_data(final_file, f.read())
963+
with open(preamble, "r") as f:
964+
write_data(final_file, f.read())
963965

964966
final_file.write(file_contents)
965967

@@ -1213,6 +1215,23 @@ def domain_to_idna(line):
12131215

12141216

12151217
# Helper Functions
1218+
def maybe_copy_example_file(file_path):
1219+
"""
1220+
Given a file path, copy over its ".example" if the path doesn't exist.
1221+
1222+
If the path does exist, nothing happens in this function.
1223+
1224+
Parameters
1225+
----------
1226+
file_path : str
1227+
The full file path to check.
1228+
"""
1229+
1230+
if not os.path.isfile(file_path):
1231+
example_file_path = file_path + ".example"
1232+
shutil.copyfile(example_file_path, file_path)
1233+
1234+
12161235
def get_file_by_url(url):
12171236
"""
12181237
Get a file data located at a particular URL.
File renamed without changes.

0 commit comments

Comments
 (0)