Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support alternative raw without 0.0.0.0 #2756

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions testUpdateHostsFile.py
Original file line number Diff line number Diff line change
Expand Up @@ -968,6 +968,42 @@ def test_with_comments_raw(self):

sys.stdout = StringIO()

def test_no_comment_alt_raw(self):
for rule in (
"twitter.com",
"google.com",
"foo.bar.edu",
"www.example-foo.bar.edu",
"www.example-3045.foobar.com",
"www.example.xn--p1ai",
):
expected = (rule, rule + "\n")

actual = normalize_rule(rule, target_ip=None, keep_domain_comments=False)
self.assertEqual(actual, expected)

# Nothing gets printed if there's a match.
output = sys.stdout.getvalue()
self.assertEqual(output, "")

sys.stdout = StringIO()

def test_with_comments_alt_raw(self):
for comment in ("foo", "bar", "baz"):
rule = "1.google.co.uk " + comment
expected = (
"1.google.co.uk",
("1.google.co.uk # " + comment + "\n"),
)

actual = normalize_rule(rule, target_ip=None, keep_domain_comments=True)
self.assertEqual(actual, expected)

# Nothing gets printed if there's a match.
output = sys.stdout.getvalue()
self.assertEqual(output, "")

sys.stdout = StringIO()

class TestStripRule(Base):
def test_strip_exactly_two(self):
Expand Down
23 changes: 19 additions & 4 deletions updateHostsFile.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,10 @@ def main():
source_data_filename = settings["sourcedatafilename"]
no_unified_hosts = settings["nounifiedhosts"]

settings["targetip"] = (
None if settings["targetip"] == "None" else settings["targetip"]
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I preferred to go with an explicit None instead of ""

)

update_sources = prompt_for_update(freshen=settings["freshen"], update_auto=auto)
if update_sources:
update_all_sources(source_data_filename, settings["hostfilename"])
Expand Down Expand Up @@ -853,12 +857,16 @@ def compress_file(input_file, target_ip, output_file):
----------
input_file : file
The file object that contains the hostnames that we are reducing.
target_ip : str
target_ip : str | None
The target IP address.
output_file : file
The file object that will contain the reduced hostnames.
"""

if target_ip is None:
print("Compress file is not supported with targetip to None")
return

input_file.seek(0) # reset file pointer
write_data(output_file, "\n")

Expand Down Expand Up @@ -893,12 +901,16 @@ def minimise_file(input_file, target_ip, output_file):
----------
input_file : file
The file object that contains the hostnames that we are reducing.
target_ip : str
target_ip : str | None
The target IP address.
output_file : file
The file object that will contain the reduced hostnames.
"""

if target_ip is None:
print("Minimise file is not supported with targetip to None")
return

input_file.seek(0) # reset file pointer
write_data(output_file, "\n")

Expand Down Expand Up @@ -1014,7 +1026,7 @@ def normalize_rule(rule, target_ip, keep_domain_comments):
----------
rule : str
The rule whose spelling and spacing we are standardizing.
target_ip : str
target_ip : str | None
The target IP address for the rule.
keep_domain_comments : bool
Whether or not to keep comments regarding these domains in
Expand Down Expand Up @@ -1048,7 +1060,10 @@ def normalize_response(
and spacing reformatted.
"""

rule = "%s %s" % (target_ip, extracted_hostname)
if target_ip is None:
rule = extracted_hostname
else:
rule = "%s %s" % (target_ip, extracted_hostname)

if keep_domain_comments and extracted_suffix:
if not extracted_suffix.strip().startswith("#"):
Expand Down