bin/process-zone
changeset 25 3d35d0eef197
parent 24 4ccc31fdc047
child 35 840092ee4d97
--- a/bin/process-zone	Mon Mar 19 10:58:50 2012 +0200
+++ b/bin/process-zone	Mon Mar 19 11:40:06 2012 +0200
@@ -51,17 +51,23 @@
     parser.add_option('--output-charset',       metavar='CHARSET',  default='utf-8', 
             help="Encoding used for output files")
 
-    # read line mtimes?
-    parser.add_option('--input-line-date',      action='store_true',
-            help="Parse timestamp prefix from each input line (e.g. `hg blame | ...`)")
-
     # check stage
     parser.add_option('--check-hosts',          action='store_true',
             help="Check that host/IPs are unique. Use --quiet to silence warnings, and test exit status")
 
-    parser.add_option('--check-exempt',         action='append',
+    parser.add_option('--check-exempt',         metavar='HOST', action='append',
             help="Allow given names to have multiple records")
 
+    # meta stage
+    parser.add_option('--meta-zone',            action='store_true',
+            help="Generate host metadata zone; requires --input-line-date")
+
+    parser.add_option('--meta-ignore',          metavar='HOST', action='append',
+            help="Ignore given hostnames in metadata output")
+
+    parser.add_option('--input-line-date',      action='store_true',
+            help="Parse timestamp prefix from each input line (e.g. `hg blame | ...`)")
+
     # forward stage
     parser.add_option('--forward-zone',         action='store_true', 
             help="Generate forward zone")
@@ -83,7 +89,9 @@
     parser.set_defaults(
         loglevel            = logging.WARN,
 
+        # XXX: combine
         check_exempt        = [],
+        meta_ignore         = [],
     )
     
     # parse
@@ -368,8 +376,6 @@
         Process zone data -> forward zone data.
     """
 
-    TIMESTAMP_FORMAT='%Y/%m/%d'
-
     for r in zone :
         yield r
 
@@ -381,18 +387,34 @@
                 if comment :
                     yield TXTRecord(None, comment, ttl=r.ttl)
 
-                # timestamp?
-                timestamp = r.line.timestamp
-
-                if timestamp :
-                    yield TXTRecord(None, timestamp.strftime(TIMESTAMP_FORMAT), ttl=r.ttl)
-            
+           
             # XXX: RP, do we need it?
 
             if mx :
                 # XXX: is this a good idea?
                 yield ZoneRecord(None, 'MX', [10, mx], ttl=r.ttl)
 
+def process_zone_meta (zone, ignore=None) :
+    """
+        Process zone metadata -> output.
+    """
+    
+    TIMESTAMP_FORMAT='%Y/%m/%d'
+    
+    for r in zone :
+        if ignore and r.name in ignore :
+            # skip
+            log.debug("Ignore record: %s", r)
+            continue
+
+        # for hosts..
+        if r.type == 'A' :
+            # timestamp?
+            timestamp = r.line.timestamp
+
+            if timestamp :
+                yield TXTRecord(r.name, timestamp.strftime(TIMESTAMP_FORMAT), ttl=r.ttl)
+     
 def reverse_addr (ip) :
     """
         Return in-addr.arpa reverse for given IPv4 IP.
@@ -505,6 +527,15 @@
 
         zone = list(process_zone_forwards(zone, txt=options.forward_txt, mx=options.forward_mx))
 
+    elif options.meta_zone :
+        log.info("Write metadata zone: %s", output)
+
+        if not options.input_line_date :
+            log.error("--meta-zone requires --input-line-date")
+            return 1
+
+        zone = list(process_zone_meta(zone, ignore=set(options.meta_ignore)))
+
     elif options.reverse_zone :
         origin = reverse_addr(options.reverse_zone)
         domain = options.reverse_domain