pvl.hosts.config: revert apply_hosts_directory to treat directory contents as a flat namespace without any parent=basname(directory) magic
authorTero Marttila <tero.marttila@aalto.fi>
Thu, 26 Feb 2015 18:32:32 +0200
changeset 513 3b45b4fd5102
parent 512 dafac309813e
child 514 f19d86b20f24
pvl.hosts.config: revert apply_hosts_directory to treat directory contents as a flat namespace without any parent=basname(directory) magic
etc/hosts/test/asdf.test
etc/hosts/test/quux.test
etc/hosts/test/test
etc/hosts/test/test.d/bar
etc/hosts/test/test.d/foo
pvl/hosts/config.py
pvl/hosts/tests.py
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/etc/hosts/test/asdf.test	Thu Feb 26 18:32:32 2015 +0200
@@ -0,0 +1,2 @@
+[quux]
+    ip  = 192.0.2.5
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/etc/hosts/test/test	Thu Feb 26 18:32:32 2015 +0200
@@ -0,0 +1,1 @@
+include = test.d/
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/etc/hosts/test/test.d/bar	Thu Feb 26 18:32:32 2015 +0200
@@ -0,0 +1,1 @@
+ip = 192.0.2.2
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/etc/hosts/test/test.d/foo	Thu Feb 26 18:32:32 2015 +0200
@@ -0,0 +1,2 @@
+ip = 192.0.2.1
+
--- a/pvl/hosts/config.py	Thu Feb 26 18:22:58 2015 +0200
+++ b/pvl/hosts/config.py	Thu Feb 26 18:32:32 2015 +0200
@@ -308,26 +308,25 @@
     for host in apply_hosts_config(options, file, **opts):
         yield host
 
-def apply_hosts_directory (options, path, parent=None, **opts):
+def apply_hosts_directory (options, root, **opts):
     """
         Load Hosts from a directory, loading each file within the directory.
 
         Skips .dotfiles.
     """
 
-    if parent is None:
-        # use directory name
-        parent = os.path.basename(path.rstrip('/'))
-
-        log.info("%s: @%s", path, parent)
-
-    for name in os.listdir(path):
-        file_path = os.path.join(path, name)
+    for name in os.listdir(root):
+        path = os.path.join(root, name)
 
         if name.startswith('.'):
+            log.debug("%s: skip dotfile: %s", root, name)
             continue
 
-        for host in apply_hosts_file(options, file_path, parent=parent, **opts):
+        if os.path.isdir(path):
+            log.debug("%s: skip directory: %s", root, name)
+            continue
+
+        for host in apply_hosts_file(options, path, **opts):
             yield host
 
 def apply_hosts_files (options, files, **opts):
--- a/pvl/hosts/tests.py	Thu Feb 26 18:22:58 2015 +0200
+++ b/pvl/hosts/tests.py	Thu Feb 26 18:32:32 2015 +0200
@@ -130,39 +130,36 @@
         ])
 
     def testApplyIncludes(self):
-        self.assertHostsEqual(config.apply_hosts_files(self.options, ['etc/hosts/includes.test']), [
-                ('foo@includes.test', dict(
+        self.assertHostsEqual(config.apply_hosts_files(self.options, ['etc/hosts/test/test']), [
+                ('foo@test', dict(
                     ip          = ipaddr.IPAddress('192.0.2.1'),
                 )),
-                ('bar@includes.test', dict(
+                ('bar@test', dict(
                     ip          = ipaddr.IPAddress('192.0.2.2'),
                 )),
-                ('quux@includes.test', dict(
-                    ip          = ipaddr.IPAddress('192.0.2.3'),
-                )),
         ])
 
     def testApplyDirectory(self):
-        self.assertHostsEqual(config.apply_hosts_files(self.options, ['etc/hosts/included.test/']), [
-                ('quux@asdf.included.test', dict(
+        self.assertHostsEqual(config.apply_hosts_files(self.options, ['etc/hosts/test/']), [
+                ('quux@asdf.test', dict(
                     ip          = ipaddr.IPAddress('192.0.2.5'),
                 )),
-                ('foo@included.test', dict(
+                ('foo@test', dict(
                     ip          = ipaddr.IPAddress('192.0.2.1'),
                 )),
-                ('bar@included.test', dict(
+                ('bar@test', dict(
                     ip          = ipaddr.IPAddress('192.0.2.2'),
                 )),
         ])
 
     def testApply(self):
-        self.assertHostsEqual(config.apply(self.options, ['etc/hosts/test']), [
-                ('foo@test', dict(
-                    ip          = ipaddr.IPAddress('127.0.0.1'),
+        self.assertHostsEqual(config.apply(self.options, ['etc/hosts/example.com']), [
+                ('foo@example.com', dict(
+                    ip          = ipaddr.IPAddress('192.0.2.1'),
                     ethernet    = {None: '00:11:22:33:44:55'},
                 )),
-                ('bar@test', dict(
-                    ip          = ipaddr.IPAddress('127.0.0.2'),
+                ('bar@example.com', dict(
+                    ip          = ipaddr.IPAddress('192.0.2.2'),
                     ethernet    = {None: '01:23:45:67:89:ab'},
                 )),
         ])