main.py
changeset 1 2223ade4f259
child 2 e66102ab7048
equal deleted inserted replaced
0:257003279747 1:2223ade4f259
       
     1 #!/usr/bin/env python2.5
       
     2 
       
     3 import data, dhcp_conf, dhcp
       
     4 
       
     5 import optparse, itertools
       
     6 
       
     7 def parse_args (argv) :
       
     8     """
       
     9         Parse the command-line arguments from the given argv list, returning a (options_struct, args_list) tuple,
       
    10         as per optparse.
       
    11     """
       
    12 
       
    13     usage = "Usage: %prog [options] data-file"
       
    14 
       
    15     # define our options
       
    16     parser = optparse.OptionParser(usage=usage)
       
    17     parser.add_option('--dhcpd-conf',    dest='dhcpd_conf', metavar='PATH', help="path to dhcpd.conf", default='/etc/dhcp3/dhcpd.conf')
       
    18     
       
    19     # parse them
       
    20     options, args = parser.parse_args(args=argv[1:])
       
    21 
       
    22     # parse the positional arguments
       
    23     data_file, = args
       
    24 
       
    25     # ok
       
    26     return options, (data_file, )
       
    27 
       
    28 def write_dhcp (options, settings) :
       
    29     """
       
    30         Write the DHCP config module using the data loaded from the given module
       
    31     """
       
    32     
       
    33     # build the config f file
       
    34     config = dhcp.Config(path=options.dhcpd_conf,
       
    35         settings        = settings.dhcp_settings,
       
    36         options         = settings.dhcp_options,
       
    37         shared_network  = settings.shared_network,
       
    38         subnets         = settings.subnets,
       
    39         hosts           = itertools.chain(*(host.build_dhcp_hosts() for host in settings.hosts)),
       
    40     )
       
    41 
       
    42     # write it out
       
    43     config.write()
       
    44 
       
    45 def main (argv) :
       
    46     """
       
    47         Our app entry point, parse args, load data, write out the config files
       
    48     """
       
    49     
       
    50     # parse args
       
    51     options, (data_file, ) = parse_args(argv)
       
    52 
       
    53     # load the data
       
    54     data_module = data.load_py(data_file)
       
    55     
       
    56     # write out the config files
       
    57     write_dhcp(options, data_module)
       
    58 
       
    59 if __name__ == '__main__' :
       
    60     from sys import argv
       
    61 
       
    62     main(argv)
       
    63