pvl.hosts-import: also import --zone-unused=ufc hosts, and output them as special comments
authorTero Marttila <terom@paivola.fi>
Thu, 19 Dec 2013 21:44:17 +0200
changeset 328 9b4ee4bad3d1
parent 327 9d7d40206a74
child 329 b3778c190aa5
pvl.hosts-import: also import --zone-unused=ufc hosts, and output them as special comments
bin/pvl.hosts-import
--- a/bin/pvl.hosts-import	Thu Dec 19 21:12:07 2013 +0200
+++ b/bin/pvl.hosts-import	Thu Dec 19 21:44:17 2013 +0200
@@ -146,16 +146,16 @@
 
         host, domain = import_zone_host_name(options, rr.name, rr.origin)
 
-        if options.zone_unused and rr.name == options.zone_unused :
-            log.debug("%s: skip %s", rr.name, rr)
-            continue
-
-        elif rr.type in ('A', 'AAAA') :
+        if rr.type in ('A', 'AAAA') :
             ip, = rr.data
+            ip = ipaddr.IPAddress(ip)
 
             type = { 'A': 'ip', 'AAAA': 'ip6' }[rr.type]
 
-            yield (host, domain), type, ipaddr.IPAddress(ip)
+            if options.zone_unused and rr.name == options.zone_unused :
+                yield (str(ip), domain), 'ip.unused', ip
+            else :
+                yield (host, domain), type, ip,
 
             if rr.comment :
                 yield (host, domain), 'comment', rr.comment
@@ -496,6 +496,7 @@
     for (domain, host), fields in hosts.iteritems() :
         SINGLE_FIELDS = (
                 'ip',
+                'ip.unused',
                 'ip6',
                 'comment.owner',
                 'owner',
@@ -629,8 +630,10 @@
 
         if ip in by_ip :
             log.warn("%s: duplicate ip %s: %s", host, ip, by_ip[ip])
+        elif ip :
+            by_ip[ip] = host
         else :
-            by_ip[ip] = host
+            log.warn("%s: no ip", host)
 
         yield (host, domain), fields
 
@@ -650,7 +653,7 @@
         select_domain = None
 
     for (host, domain), fields in hosts :
-        ip = fields.get(('ip', ))
+        ip = fields.get(('ip', )) or fields.get(('ip', 'unused'))
 
         log.debug("%s@%s: ip=%s", host, domain, ip)
         
@@ -700,6 +703,16 @@
             yield u"[{domain}]".format(domain=domain)
             output_domain = domain
 
+        # special handling for "unused" hosts
+        if ('ip', 'unused') in fields :
+            yield u"{indent}# {unused} {ip}".format(
+                    indent  = '\t' if output_domain else '',
+                    unused  = options.zone_unused,
+                    ip      = fields[('ip', 'unused')],
+            )
+            yield u""
+            continue
+
         # optional host-comments
         for comment in fields.get(('comment', 'host'), ()):
             yield u"{indent}# {comment}".format(