pvl.dns.zone: implement --include-path
authorTero Marttila <terom@paivola.fi>
Thu, 19 Dec 2013 02:50:59 +0200
changeset 316 41bd6688b142
parent 315 3ad1822cd91d
child 317 2859d3dedcfe
pvl.dns.zone: implement --include-path
bin/pvl.dns-zone
pvl/dns/zone.py
--- 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)
 
--- a/pvl/dns/zone.py	Thu Dec 19 02:17:44 2013 +0200
+++ b/pvl/dns/zone.py	Thu Dec 19 02:50:59 2013 +0200
@@ -225,7 +225,13 @@
         self.timestamp = timestamp
         self.comment = comment
 
-    def __str__ (self) :
+    def __unicode__ (self) :
+        return u"{indent}{parts}".format(
+                indent      = u"\t" if self.indent else '',
+                parts       = u'\t'.join(self.parts),
+        )
+
+    def __repr__ (self) :
         return "{file}:{lineno}".format(file=self.file, lineno=self.lineno)
 
 class ZoneRecord (object) :