def test_remove_all_matching_failure(tmpdir): """ Test removal of multiple entries with a common alias """ with pytest.raises(ValueError): hosts_file = tmpdir.mkdir("etc").join("hosts") hosts_file.write("1.2.3.4\tfoo-1 foo\n" "2.3.4.5\tfoo-2 foo\n") hosts = Hosts(path=hosts_file.strpath) hosts.remove_all_matching() hosts.write()
def test_remove_all_matching_multiple(tmpdir): """ Test removal of multiple entries with a common alias """ hosts_file = tmpdir.mkdir("etc").join("hosts") hosts_file.write("1.2.3.4\tfoo-1 foo\n" "2.3.4.5\tfoo-2 foo\n") hosts = Hosts(path=hosts_file.strpath) hosts.remove_all_matching(name="foo") hosts.write() assert not hosts_file.read()
def test_remove_all_matching_failure(tmpdir): """ Test removal of multiple entries with a common alias """ with pytest.raises(ValueError): hosts_file = tmpdir.mkdir("etc").join("hosts") hosts_file.write("1.2.3.4\tfoo-1 foo\n" "2.3.4.5\tfoo-2 foo\n") hosts = Hosts(path=hosts_file.strpath) hosts.remove_all_matching() hosts.write()
def test_remove_all_matching_multiple(tmpdir): """ Test removal of multiple entries with a common alias """ hosts_file = tmpdir.mkdir("etc").join("hosts") hosts_file.write("1.2.3.4\tfoo-1 foo\n" "2.3.4.5\tfoo-2 foo\n") hosts = Hosts(path=hosts_file.strpath) hosts.remove_all_matching(name="foo") hosts.write() assert not hosts_file.read()
def write_hosts(ip_address, domain_name, alias_name): if platform.system() == 'Windows': hosts_path="C:\\Windows\\System32\\drivers\\etc\\hosts" else: hosts_path="/etc/hosts" hosts = Hosts(path=hosts_path) hosts.remove_all_matching(name=domain_name) new_entry = HostsEntry(entry_type='ipv4', address=ip_address, names=[domain_name, alias_name]) hosts.add([new_entry]) hosts.write()
def test_remove_existing_entry_using_address_only(tmpdir): """ Test removal of an existing entry using ip4 address only """ entries = '1.2.3.4 example.com example\n# this is a comment' hosts_file = tmpdir.mkdir("etc").join("hosts") hosts_file.write(entries) hosts_entries = Hosts(path=hosts_file.strpath) assert hosts_entries.exists(address='1.2.3.4') assert hosts_entries.exists(names=['example.com']) hosts_entries.remove_all_matching(address='1.2.3.4') assert not hosts_entries.exists(address='1.2.3.4')
def test_remove_existing_entry_using_address_only(tmpdir): """ Test removal of an existing entry using ip4 address only """ entries = '1.2.3.4 example.com example\n# this is a comment' hosts_file = tmpdir.mkdir("etc").join("hosts") hosts_file.write(entries) hosts_entries = Hosts(path=hosts_file.strpath) assert hosts_entries.exists(address='1.2.3.4') assert hosts_entries.exists(names=['example.com']) hosts_entries.remove_all_matching(address='1.2.3.4') assert not hosts_entries.exists(address='1.2.3.4')
def test_remove_existing_ipv4_address_using_hostsentry(tmpdir): """ Test removal of an existing ip4 address """ ipv4_line = '1.2.3.4 example.com example' hosts_file = tmpdir.mkdir("etc").join("hosts") hosts_file.write(ipv4_line) hosts_entries = Hosts(path=hosts_file.strpath) assert hosts_entries.exists(address='1.2.3.4') assert hosts_entries.exists(names=['example.com']) hosts_entries.remove_all_matching(address='1.2.3.4', name='example.com') assert not hosts_entries.exists(address='1.2.3.4') assert not hosts_entries.exists(names=['example.com'])
def test_remove_existing_ipv4_address_using_hostsentry(tmpdir): """ Test removal of an existing ip4 address """ ipv4_line = '1.2.3.4 example.com example' hosts_file = tmpdir.mkdir("etc").join("hosts") hosts_file.write(ipv4_line) hosts_entries = Hosts(path=hosts_file.strpath) assert hosts_entries.exists(address='1.2.3.4') assert hosts_entries.exists(names=['example.com']) hosts_entries.remove_all_matching(address='1.2.3.4', name='example.com') assert not hosts_entries.exists(address='1.2.3.4') assert not hosts_entries.exists(names=['example.com'])
def test_remove_existing_entry_using_name_only(tmpdir): """ Test removal of an existing entry using name only """ entries = '1.2.3.4 example.com example\n# this is a comment\n\n3.4.5.6 random.com' # two newlines intentionally follow, see issue #11 hosts_file = tmpdir.mkdir("etc").join("hosts") hosts_file.write(entries) hosts_entries = Hosts(path=hosts_file.strpath) assert hosts_entries.exists(address='1.2.3.4') assert hosts_entries.exists(names=['example.com']) hosts_entries.remove_all_matching(name='example.com') assert not hosts_entries.exists(names=['example.com']) hosts_entries.write() assert '# this is a comment\n' in open(hosts_file.strpath).read() assert '3.4.5.6\trandom.com' in open(hosts_file.strpath).read()
def test_remove_existing_entry_using_name_only(tmpdir): """ Test removal of an existing entry using name only """ entries = '1.2.3.4 example.com example\n# this is a comment\n\n3.4.5.6 random.com' # two newlines intentionally follow, see issue #11 hosts_file = tmpdir.mkdir("etc").join("hosts") hosts_file.write(entries) hosts_entries = Hosts(path=hosts_file.strpath) assert hosts_entries.exists(address='1.2.3.4') assert hosts_entries.exists(names=['example.com']) hosts_entries.remove_all_matching(name='example.com') assert not hosts_entries.exists(names=['example.com']) hosts_entries.write() assert '# this is a comment\n' in open(hosts_file.strpath).read() assert '3.4.5.6\trandom.com' in open(hosts_file.strpath).read()
dest='unset', action='store_true', help= 'Unset development host (without this parameter the dev host will be set).' ) parser.add_argument('-p', '--path_to_hosts_file', dest='path_to_hosts_file', help='Path to hosts file, defaults to autodetect.') # Get cli args args = parser.parse_args() args_dict = vars(args) ############################################################################### # SET HOSTS ############################################################################### hosts = Hosts(path=args_dict['path_to_hosts_file']) if args_dict['unset']: hosts.remove_all_matching(name='{{ FS_DOMAIN }}') print('Removed development host.') else: new_entry = HostsEntry(entry_type='ipv4', address='0.0.0.0', names=['www.{{ FS_DOMAIN }}', '{{ FS_DOMAIN }}']) hosts.add([new_entry]) print('Added development host.') hosts.write()