bin/pvl.dns-zone
changeset 316 41bd6688b142
parent 294 29720bbc5379
child 332 bb8a18cffe8a
--- a/bin/pvl.dns-zone	Thu Dec 19 02:17:44 2013 +0200
+++ b/bin/pvl.dns-zone	Thu Dec 19 02:50:59 2013 +0200
@@ -6,13 +6,13 @@
     Takes a zonefile as input, and gives a zonefile as output.
 """
 
-import optparse
-
 import pvl.args
 import pvl.dns.zone
 from pvl.dns import __version__
 from pvl.dns.zone import ZoneRecord, reverse_ipv4, reverse_ipv6, fqdn
 
+import optparse
+import os.path
 import logging; log = logging.getLogger('main')
 
 def parse_options (argv) :
@@ -82,6 +82,9 @@
     parser.add_option('--serial',               metavar='YYMMDDXX',
             help="Set serial for SOA record")
 
+    parser.add_option('--include-path',         metavar='PATH',
+            help="Rewrite includes to given absolute path")
+
     # defaults
     parser.set_defaults(
         # XXX: combine
@@ -259,6 +262,26 @@
 
         yield line, ZoneRecord.PTR(ptr, host_fqdn)
 
+def process_zone_includes (options, zone, path) :
+    """
+        Rewrite include paths in zones.
+    """
+
+    for line, rr in zone :
+        if line.parts[0] == '$INCLUDE' :
+            _, include = line.parts
+
+            yield pvl.dns.zone.ZoneLine(
+                    line.file,
+                    line.lineno, 
+                    line.line,
+                    line.indent,
+                    ['$INCLUDE', '"{path}"'.format(path=os.path.join(path, include))],
+            ), rr
+        else :
+            yield line, rr
+
+
 def apply_zone_output (options, zone) :
     """
         Write out the resulting zonefile.
@@ -270,7 +293,7 @@
         if r :
             file.write(unicode(r))
         else :
-            file.write(line.line)
+            file.write(unicode(line))
         file.write('\n')
 
 def main (argv) :
@@ -322,6 +345,9 @@
 
         zone = list(process_zone_reverse(zone, origin=origin, domain=domain))
     
+    if options.include_path :
+        zone = list(process_zone_includes(options, zone, options.include_path))
+
     # output
     apply_zone_output(options, zone)