initial take at merging local.conf code

This commit is contained in:
Sean Dague 2017-01-17 09:03:50 -05:00
parent be8cb74021
commit 0366529cc9
2 changed files with 43 additions and 3 deletions

View File

@ -50,6 +50,11 @@ def setlc_conf(local_conf, args):
local_conf.set(args.group, args.conf, args.section, args.name, args.value)
def merge(local_conf, args):
for source in args.sources:
local_conf.merge_lc(source)
def parse_args(argv):
parser = argparse.ArgumentParser(prog='dsconf')
subparsers = parser.add_subparsers(title='commands',
@ -120,14 +125,25 @@ def parse_args(argv):
parser_setlc_conf.add_argument('name')
parser_setlc_conf.add_argument('value')
return parser.parse_args()
parser_merge = subparsers.add_parser(
'merge_lc', help='merge local.conf files')
parser_merge.set_defaults(func=merge)
parser_merge.add_argument('local_conf')
parser_merge.add_argument('sources', nargs='+')
return parser.parse_args(), parser
def main(argv=None):
args = parse_args(argv or sys.argv)
args, parser = parse_args(argv or sys.argv)
if hasattr(args, 'inifile'):
f = devstack.dsconf.IniFile(args.inifile)
elif hasattr(args, 'local_conf'):
f = devstack.dsconf.LocalConf(args.local_conf)
args.func(f, args)
if hasattr(args, 'func'):
args.func(f, args)
else:
parser.print_help()
return 1
return

View File

@ -145,6 +145,17 @@ class LocalConf(object):
if m2:
yield current_section, m2.group(1), m2.group(2)
def groups(self):
"""Return a list of all groups in the local.conf"""
groups = []
with open(self.fname) as reader:
for line in reader.readlines():
m = re.match(r"\[\[([^\[\]]+)\|([^\[\]]+)\]\]", line)
if m:
group = (m.group(1), m.group(2))
groups.append(group)
return groups
def _section(self, group, conf):
"""Yield all the lines out of a meta section."""
in_section = False
@ -283,3 +294,16 @@ class LocalConf(object):
def _do_set(writer, line):
writer.write("%s = %s\n" % (name, value))
self._at_insert_point(group, conf, section, name, _do_set)
def merge_lc(self, lcfile):
lc = LocalConf(lcfile)
groups = lc.groups()
for group, conf in groups:
if group == "local":
for line in lc._section(group, conf):
m = re.match(r"(\w+)\s*\=\s*(.+)", line)
if m:
self.set_local(m.group(1), m.group(2))
else:
for section, name, value in lc._conf(group, conf):
lc.set(group, conf, section, name, value)