codes_configurator.py 2.71 KB
Newer Older
1 2 3 4
#!/usr/bin/python2.7

import os
import argparse
5
import configurator as conf
6 7 8 9 10 11 12 13

def main():
    args = parse_args()
    
    # load the template file
    tstr = open(args.template).read()

    # load the module
14
    mod = conf.import_from(args.substitute_py)
15

16 17
    # intitialize the configuration object 
    cfg = conf.configurator(mod, args.token_pairs)
18

19 20 21 22 23
    # print the header to the log
    if args.log != None:
        flog = open(args.log, "w")
    else:
        flog = open(os.devnull, "w")
24 25
    
    cfg.write_header(flog)
26

27 28 29 30 31
    # main loop (cfg iterator returns nothing, eval'd for side effects)
    for i,_ in enumerate(cfg):
        new_config = conf.replace_many(tstr, cfg.replace_map)
        fname = template+"."+str(i) if args.output_prefix==None else \
            args.output_prefix+"."+str(i)
32 33
        with open(fname, "w") as fout:
            fout.write(new_config)
34 35 36
        
        # write this config to the log 
        cfg.write_config_line(i, flog)
37

38
    flog.close()
39

40 41 42 43 44 45 46 47 48 49 50 51
sub_help = \
'python file defining "cfields" variable consisting of a sequence of <name, ' \
'value sequence> pairs. The following variables may optionally appear. ' \
'"exceptions" - a sequence of dictionaries. Each configuration generated by ' \
'"cfields" is checked against each dictionary in "exceptions"; if each value ' \
'in the dict matches that of the configuration, then that configuration is ' \
'skipped. "cfields_derived_labels", "cfields_derived" - the former is a ' \
'sequence of strings identifying replace tokens that will be dynamically set ' \
'based on the input configuration generated by cfields. The latter is a ' \
'function that adds all <name,value> pairs for names in ' \
'"cfields_derived_labels".'

52
def parse_args():
53 54 55 56
    parser = argparse.ArgumentParser(\
            description="generate set of configuration files based on template "
                        "file and replacement tokens")
    parser.add_argument("template",
57
            help="template file with replace tokens")
58
    parser.add_argument("substitute_py",
59
            help=sub_help)
60
    parser.add_argument("token_pairs", nargs='*',
61 62 63 64 65 66 67
            help="a list of whitespace-separated token, replace pairs for "
                 "command-line-driven replacement (useful in shell scripts "
                 "for generating sets of configuration files with a distinct "
                 "parameter or for special casing fields)")
    parser.add_argument("-l", "--log",
            help="log file to write parameterizations to")
    parser.add_argument("-o", "--output_prefix",
68 69 70 71 72 73
            help="prefix to output generated configuration files to "
                 "(default: the configuration index appended to the template name)")
    return parser.parse_args()

if __name__ == "__main__":
    main()