diff -r 257003279747 -r 2223ade4f259 main.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.py Thu Apr 02 20:19:18 2009 +0300 @@ -0,0 +1,63 @@ +#!/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(data_file) + + # write out the config files + write_dhcp(options, data_module) + +if __name__ == '__main__' : + from sys import argv + + main(argv) +