main.py
author Tero Marttila <terom@fixme.fi>
Thu, 02 Apr 2009 20:54:37 +0300
changeset 2 e66102ab7048
parent 1 2223ade4f259
child 5 86b05c0ab5cd
permissions -rwxr-xr-x
fix up data.load_py, and make conf.File be a ConfObject itself - implement this for dhcp_conf
#!/usr/bin/env python2.5

import data, dhcp_conf, dhcp

import optparse, itertools

def parse_args (argv) :
    """
        Parse the command-line arguments from the given argv list, returning a (options_struct, args_list) tuple,
        as per optparse.
    """

    usage = "Usage: %prog [options] data-file"

    # define our options
    parser = optparse.OptionParser(usage=usage)
    parser.add_option('--dhcpd-conf',    dest='dhcpd_conf', metavar='PATH', help="path to dhcpd.conf", default='/etc/dhcp3/dhcpd.conf')
    
    # parse them
    options, args = parser.parse_args(args=argv[1:])

    # parse the positional arguments
    data_file, = args

    # ok
    return options, (data_file, )

def write_dhcp (options, settings) :
    """
        Write the DHCP config module using the data loaded from the given module
    """
    
    # build the config f file
    config = dhcp.Config(path=options.dhcpd_conf,
        settings        = settings.dhcp_settings,
        options         = settings.dhcp_options,
        shared_network  = settings.shared_network,
        subnets         = settings.subnets,
        hosts           = itertools.chain(*(host.build_dhcp_hosts() for host in settings.hosts)),
    )

    # write it out
    config.write()

def main (argv) :
    """
        Our app entry point, parse args, load data, write out the config files
    """
    
    # parse args
    options, (data_file, ) = parse_args(argv)

    # load the data
    data_module = data.load_py('pvl_hosts_data', data_file)
    
    # write out the config files
    write_dhcp(options, data_module)

if __name__ == '__main__' :
    from sys import argv

    main(argv)