summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSean Dague <sean@dague.net>2017-01-17 09:03:50 -0500
committerSean Dague <sean@dague.net>2017-01-17 09:03:50 -0500
commit0366529cc9f018fdcce4b2f56e884f332278ef35 (patch)
treeb7192bb0b51f8a6ac6e36efa131fd13b610d4233
parentbe8cb7402141d9bc561f219693f1015eccaab57d (diff)
initial take at merging local.conf code
-rw-r--r--devstack/cmd.py22
-rw-r--r--devstack/dsconf.py24
2 files changed, 43 insertions, 3 deletions
diff --git a/devstack/cmd.py b/devstack/cmd.py
index da8a502..5e54219 100644
--- a/devstack/cmd.py
+++ b/devstack/cmd.py
@@ -50,6 +50,11 @@ def setlc_conf(local_conf, args):
50 local_conf.set(args.group, args.conf, args.section, args.name, args.value) 50 local_conf.set(args.group, args.conf, args.section, args.name, args.value)
51 51
52 52
53def merge(local_conf, args):
54 for source in args.sources:
55 local_conf.merge_lc(source)
56
57
53def parse_args(argv): 58def parse_args(argv):
54 parser = argparse.ArgumentParser(prog='dsconf') 59 parser = argparse.ArgumentParser(prog='dsconf')
55 subparsers = parser.add_subparsers(title='commands', 60 subparsers = parser.add_subparsers(title='commands',
@@ -120,14 +125,25 @@ def parse_args(argv):
120 parser_setlc_conf.add_argument('name') 125 parser_setlc_conf.add_argument('name')
121 parser_setlc_conf.add_argument('value') 126 parser_setlc_conf.add_argument('value')
122 127
123 return parser.parse_args() 128 parser_merge = subparsers.add_parser(
129 'merge_lc', help='merge local.conf files')
130 parser_merge.set_defaults(func=merge)
131 parser_merge.add_argument('local_conf')
132 parser_merge.add_argument('sources', nargs='+')
133
134 return parser.parse_args(), parser
124 135
125 136
126def main(argv=None): 137def main(argv=None):
127 args = parse_args(argv or sys.argv) 138 args, parser = parse_args(argv or sys.argv)
128 if hasattr(args, 'inifile'): 139 if hasattr(args, 'inifile'):
129 f = devstack.dsconf.IniFile(args.inifile) 140 f = devstack.dsconf.IniFile(args.inifile)
130 elif hasattr(args, 'local_conf'): 141 elif hasattr(args, 'local_conf'):
131 f = devstack.dsconf.LocalConf(args.local_conf) 142 f = devstack.dsconf.LocalConf(args.local_conf)
132 args.func(f, args) 143
144 if hasattr(args, 'func'):
145 args.func(f, args)
146 else:
147 parser.print_help()
148 return 1
133 return 149 return
diff --git a/devstack/dsconf.py b/devstack/dsconf.py
index 9e9ff6c..66071cd 100644
--- a/devstack/dsconf.py
+++ b/devstack/dsconf.py
@@ -145,6 +145,17 @@ class LocalConf(object):
145 if m2: 145 if m2:
146 yield current_section, m2.group(1), m2.group(2) 146 yield current_section, m2.group(1), m2.group(2)
147 147
148 def groups(self):
149 """Return a list of all groups in the local.conf"""
150 groups = []
151 with open(self.fname) as reader:
152 for line in reader.readlines():
153 m = re.match(r"\[\[([^\[\]]+)\|([^\[\]]+)\]\]", line)
154 if m:
155 group = (m.group(1), m.group(2))
156 groups.append(group)
157 return groups
158
148 def _section(self, group, conf): 159 def _section(self, group, conf):
149 """Yield all the lines out of a meta section.""" 160 """Yield all the lines out of a meta section."""
150 in_section = False 161 in_section = False
@@ -283,3 +294,16 @@ class LocalConf(object):
283 def _do_set(writer, line): 294 def _do_set(writer, line):
284 writer.write("%s = %s\n" % (name, value)) 295 writer.write("%s = %s\n" % (name, value))
285 self._at_insert_point(group, conf, section, name, _do_set) 296 self._at_insert_point(group, conf, section, name, _do_set)
297
298 def merge_lc(self, lcfile):
299 lc = LocalConf(lcfile)
300 groups = lc.groups()
301 for group, conf in groups:
302 if group == "local":
303 for line in lc._section(group, conf):
304 m = re.match(r"(\w+)\s*\=\s*(.+)", line)
305 if m:
306 self.set_local(m.group(1), m.group(2))
307 else:
308 for section, name, value in lc._conf(group, conf):
309 lc.set(group, conf, section, name, value)