pvl.dhcp-conf: simplistic --include-path processing for dhcp.conf files
authorTero Marttila <terom@paivola.fi>
Thu, 19 Dec 2013 18:50:59 +0200
changeset 321 e931ff718ead
parent 320 bfda3703fa0c
child 322 0c3bb9d2f13b
pvl.dhcp-conf: simplistic --include-path processing for dhcp.conf files
bin/pvl.dhcp-conf
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/bin/pvl.dhcp-conf	Thu Dec 19 18:50:59 2013 +0200
@@ -0,0 +1,122 @@
+#!/usr/bin/env python
+
+"""
+    Process dhcpd configs.
+
+    Takes a conf file as input, and gives a conf file as output.
+"""
+
+import pvl.args
+import pvl.dhcp.config
+
+import os.path
+import optparse
+import logging; log = logging.getLogger('pvl.dhcp-conf')
+
+def apply_input (options, conf) :
+    """
+        Parse (items, blocks) from given dhcp.conf path.
+    """
+
+    file = pvl.args.apply_file(conf, 'r')
+    return pvl.dhcp.config.DHCPConfigParser.load(file)
+
+def process_dhcp_items (options, block, items) :
+    """
+        Yield items for output from given input items
+    """
+
+    for item in items :
+        name, args = item[0], item[1:]
+
+        log.debug("%s: %s: %s", block, name, args)
+
+        if name == 'include' :
+            include, = args
+            
+            if options.include_path :
+                include = os.path.join(options.include_path, include)
+                log.info("include: %s", include)
+
+            yield 'include', '"{include}"'.format(include=include)
+        else :
+            yield item
+
+def process_dhcp_block (options, block, items, blocks) :
+    """
+        Yield block for output for given input block.
+    """
+
+    log.debug("%s -> %s %s", block, items, blocks)
+
+    items = list(process_dhcp_items(options, block, items))
+    blocks = [process_dhcp_block(options, subblock, subitems, subblocks) for subblock, subitems, subblocks in blocks]
+
+    log.debug("%s <- %s %s", block, items, blocks)
+    
+    return block, items, blocks
+
+def process_dhcp_conf (options, conf) :
+    """
+        Yield (items, blocks) for output for given input (items, blocks).
+    """
+    items, blocks = conf
+    
+    log.debug("-> %s %s", items, blocks)
+
+    _, items, blocks = process_dhcp_block(options, None, items, blocks)
+    
+    log.debug("<- %s %s", items, blocks)
+    
+    return items, blocks
+
+def process_output (options, items, blocks) :
+    """
+        Generate output lines from given (items, blocks).
+    """
+
+    for item in items :
+        yield '\t'.join(item) + ';'
+
+    for block, subitems, subblocks in blocks :
+        yield ' '.join(block) + ' {'
+        for line in process_output(options, subitems, subblocks) :
+            yield '\t' + line
+        yield '}'
+
+def apply_output (options, conf) :
+    """
+        Write output line for given (items, blocks) to --output-conf.
+    """
+
+    file = pvl.args.apply_file(options.output_conf, 'w')
+
+    items, blocks = conf
+
+    for line in process_output(options, items, blocks) :
+        print >>file, line
+
+def main (argv) :
+    parser = optparse.OptionParser(__doc__)
+    parser.add_option_group(pvl.args.parser(parser))
+
+    parser.add_option('--output-conf',          metavar='FILE',
+            help="Output conf to file; default stdout")
+    parser.add_option('--include-path',         metavar='PATH',
+            help="Adjust includes to use given path prefix")
+
+    options, args = parser.parse_args(argv[1:])
+    pvl.args.apply(options)
+
+    for conf in args :
+        # input
+        conf = apply_input(options, conf)
+
+        # process
+        conf = process_dhcp_conf(options, conf)
+        
+        # output
+        apply_output(options, conf)
+
+if __name__ == '__main__':
+    pvl.args.main(main)