bin/pvl.dhcp-conf
changeset 321 e931ff718ead
child 376 31d9ae0c1dab
equal deleted inserted replaced
320:bfda3703fa0c 321:e931ff718ead
       
     1 #!/usr/bin/env python
       
     2 
       
     3 """
       
     4     Process dhcpd configs.
       
     5 
       
     6     Takes a conf file as input, and gives a conf file as output.
       
     7 """
       
     8 
       
     9 import pvl.args
       
    10 import pvl.dhcp.config
       
    11 
       
    12 import os.path
       
    13 import optparse
       
    14 import logging; log = logging.getLogger('pvl.dhcp-conf')
       
    15 
       
    16 def apply_input (options, conf) :
       
    17     """
       
    18         Parse (items, blocks) from given dhcp.conf path.
       
    19     """
       
    20 
       
    21     file = pvl.args.apply_file(conf, 'r')
       
    22     return pvl.dhcp.config.DHCPConfigParser.load(file)
       
    23 
       
    24 def process_dhcp_items (options, block, items) :
       
    25     """
       
    26         Yield items for output from given input items
       
    27     """
       
    28 
       
    29     for item in items :
       
    30         name, args = item[0], item[1:]
       
    31 
       
    32         log.debug("%s: %s: %s", block, name, args)
       
    33 
       
    34         if name == 'include' :
       
    35             include, = args
       
    36             
       
    37             if options.include_path :
       
    38                 include = os.path.join(options.include_path, include)
       
    39                 log.info("include: %s", include)
       
    40 
       
    41             yield 'include', '"{include}"'.format(include=include)
       
    42         else :
       
    43             yield item
       
    44 
       
    45 def process_dhcp_block (options, block, items, blocks) :
       
    46     """
       
    47         Yield block for output for given input block.
       
    48     """
       
    49 
       
    50     log.debug("%s -> %s %s", block, items, blocks)
       
    51 
       
    52     items = list(process_dhcp_items(options, block, items))
       
    53     blocks = [process_dhcp_block(options, subblock, subitems, subblocks) for subblock, subitems, subblocks in blocks]
       
    54 
       
    55     log.debug("%s <- %s %s", block, items, blocks)
       
    56     
       
    57     return block, items, blocks
       
    58 
       
    59 def process_dhcp_conf (options, conf) :
       
    60     """
       
    61         Yield (items, blocks) for output for given input (items, blocks).
       
    62     """
       
    63     items, blocks = conf
       
    64     
       
    65     log.debug("-> %s %s", items, blocks)
       
    66 
       
    67     _, items, blocks = process_dhcp_block(options, None, items, blocks)
       
    68     
       
    69     log.debug("<- %s %s", items, blocks)
       
    70     
       
    71     return items, blocks
       
    72 
       
    73 def process_output (options, items, blocks) :
       
    74     """
       
    75         Generate output lines from given (items, blocks).
       
    76     """
       
    77 
       
    78     for item in items :
       
    79         yield '\t'.join(item) + ';'
       
    80 
       
    81     for block, subitems, subblocks in blocks :
       
    82         yield ' '.join(block) + ' {'
       
    83         for line in process_output(options, subitems, subblocks) :
       
    84             yield '\t' + line
       
    85         yield '}'
       
    86 
       
    87 def apply_output (options, conf) :
       
    88     """
       
    89         Write output line for given (items, blocks) to --output-conf.
       
    90     """
       
    91 
       
    92     file = pvl.args.apply_file(options.output_conf, 'w')
       
    93 
       
    94     items, blocks = conf
       
    95 
       
    96     for line in process_output(options, items, blocks) :
       
    97         print >>file, line
       
    98 
       
    99 def main (argv) :
       
   100     parser = optparse.OptionParser(__doc__)
       
   101     parser.add_option_group(pvl.args.parser(parser))
       
   102 
       
   103     parser.add_option('--output-conf',          metavar='FILE',
       
   104             help="Output conf to file; default stdout")
       
   105     parser.add_option('--include-path',         metavar='PATH',
       
   106             help="Adjust includes to use given path prefix")
       
   107 
       
   108     options, args = parser.parse_args(argv[1:])
       
   109     pvl.args.apply(options)
       
   110 
       
   111     for conf in args :
       
   112         # input
       
   113         conf = apply_input(options, conf)
       
   114 
       
   115         # process
       
   116         conf = process_dhcp_conf(options, conf)
       
   117         
       
   118         # output
       
   119         apply_output(options, conf)
       
   120 
       
   121 if __name__ == '__main__':
       
   122     pvl.args.main(main)