def compose(rc_filename, random_file=None): """Compose local.ini from the given .rc file""" rc_info = combine_rc(rc_filename) ini_sequence = rc_info.get('ini_files', None) assert ini_sequence, "Define ini_files" ini_sequence = ini_sequence.split() base = ConfigParser(interpolation=None) random_file = random_file or rc_info.get('random_file', RANDOM_FILE) for overlay in ini_sequence: if overlay == 'RC_DATA': overlay = rc_to_ini(rc_info) elif overlay.startswith('RANDOM'): templates = overlay.split(':')[1:] overlay = populate_random( random_file, templates, extract_saml_info(rc_info)) combine_ini(base, overlay) return base
def migrate(rc_filename, expected_ini, random_file=None, target_dir=None): """Create a overlay.rc file from the local.ini and a base .rc file""" expected_ini = asParser(expected_ini) rc_data = combine_rc(rc_filename) ini_files = rc_data.get('ini_files', None) random_file = random_file or rc_data.get('random_file', RANDOM_FILE) templates = extract_random_templates(ini_files) if templates: random_data = populate_random( random_file, templates, extract_saml_info(rc_data)) migrate_beaker_config(random_data, expected_ini) random_data = combine_ini(random_data, expected_ini, False) with open(random_file, 'w') as f: random_data.write(f) else: random_file = None base = compose(rc_filename, random_file) diff = diff_ini(base, expected_ini) return iniconfig_to_rc(diff, extends=rc_filename, target_dir=target_dir)
def main(): parser = ArgumentParser(prog=os.path.basename(__file__)) subparsers = parser.add_subparsers(dest='command', title="subcommands") def short_help(fn): return fn.__doc__.split('\n')[0] # ini populate parser_populate = subparsers.add_parser( 'populate', help=short_help(generate_ini_files)) parser_populate.add_argument( 'config', help='The source ini file') # ini combine parser_combine = subparsers.add_parser( 'combine', help=short_help(combine_ini)) parser_combine.add_argument('--output', '-o', default=None, help='The output file') parser_combine.add_argument('input', type=FileType('r'), nargs='+', help='Input ini files') # ini compose parser_compose = subparsers.add_parser('compose', help=short_help(compose)) parser_compose.add_argument('--output', '-o', default=None, help='The output file') parser_compose.add_argument('--random', '-r', help='random.ini file', default=None) parser_compose.add_argument('input', help='Input rc file') # ini migrate parser_migrate = subparsers.add_parser( 'migrate', help=short_help(migrate)) parser_migrate.add_argument('--output', '-o', default=None, help='The output file') parser_migrate.add_argument( '--ini', '-i', type=FileType('r'), default=None, help="INI file we're migrating, usually local.ini") parser_migrate.add_argument('--random', '-r', default=None, help='file where random values will be stored,' ' and fetched if existing',) parser_migrate.add_argument('rc', help='Base rc file') # random.ini parser_random = subparsers.add_parser( 'random', help=short_help(populate_random)) parser_random.add_argument('--random', '-r', help='random.ini file', default=None) parser_random.add_argument('--output', '-o', default=None, help='The output file') parser_random.add_argument('--template', '-t', help='random template files', action='append', default=[]) parser_random.add_argument('input', help='Input rc file (for saml)') # dump .ini parser_dump = subparsers.add_parser( 'dump', help=short_help(dump)) parser_dump.add_argument('ini', help='Input ini file') # ini diff parser_diff = subparsers.add_parser('diff', help=short_help(diff_ini)) parser_diff.add_argument('--output', '-o', default=None, help='The output file') parser_diff.add_argument( '--existing_only', '-e', action="store_true", default=False, help='Only show changes to keys existing in first file') parser_diff.add_argument('base', type=FileType('r'), help='Base ini file') parser_diff.add_argument('second', type=FileType('r'), help='Second ini file') args = parser.parse_args() if args.command == 'populate': config = ConfigParser(defaults=DEFAULTS) config.read(args.config) generate_ini_files(config, args.config) elif args.command == 'combine': base = asParser(args.input[0]) for overlay in args.input[1:]: combine_ini(base, overlay) write_parser(base, args.output) elif args.command == 'random': rc_info = combine_rc(args.input) random_file = args.random or rc_info.get('random_file', RANDOM_FILE) templates = args.template or extract_random_templates( rc_info.get('ini_files', '')) random = populate_random( random_file, templates, extract_saml_info(rc_info)) write_parser(random, args.output) elif args.command == 'compose': ini_info = compose(args.input, args.random) write_parser(ini_info, args.output) elif args.command == 'migrate': ini_file = args.ini if ini_file is None: ini_file = asParser('local.ini') else: ini_file = asParser(ini_file) target_dir = os.path.dirname(args.output) if args.output else None rc_file = migrate(args.rc, ini_file, args.random, target_dir=target_dir) if args.output: with io.open(args.output, 'w', encoding='utf-8') as output: output.write(rc_file.getvalue()) else: sys.stdout.write(rc_file.getvalue()) elif args.command == 'diff': diff = diff_ini(args.base, args.second, existing_only=args.existing_only) write_parser(diff, args.output) if args.command == 'dump': dump(args.ini)