Browse Source

initial take at merging local.conf code

Sean Dague 2 years ago
parent
commit
0366529cc9
2 changed files with 43 additions and 3 deletions
  1. 19
    3
      devstack/cmd.py
  2. 24
    0
      devstack/dsconf.py

+ 19
- 3
devstack/cmd.py View File

@@ -50,6 +50,11 @@ def setlc_conf(local_conf, args):
50 50
     local_conf.set(args.group, args.conf, args.section, args.name, args.value)
51 51
 
52 52
 
53
+def merge(local_conf, args):
54
+    for source in args.sources:
55
+        local_conf.merge_lc(source)
56
+
57
+
53 58
 def parse_args(argv):
54 59
     parser = argparse.ArgumentParser(prog='dsconf')
55 60
     subparsers = parser.add_subparsers(title='commands',
@@ -120,14 +125,25 @@ def parse_args(argv):
120 125
     parser_setlc_conf.add_argument('name')
121 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
 
126 137
 def main(argv=None):
127
-    args = parse_args(argv or sys.argv)
138
+    args, parser = parse_args(argv or sys.argv)
128 139
     if hasattr(args, 'inifile'):
129 140
         f = devstack.dsconf.IniFile(args.inifile)
130 141
     elif hasattr(args, 'local_conf'):
131 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 149
     return

+ 24
- 0
devstack/dsconf.py View File

@@ -145,6 +145,17 @@ class LocalConf(object):
145 145
                 if m2:
146 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 159
     def _section(self, group, conf):
149 160
         """Yield all the lines out of a meta section."""
150 161
         in_section = False
@@ -283,3 +294,16 @@ class LocalConf(object):
283 294
         def _do_set(writer, line):
284 295
             writer.write("%s = %s\n" % (name, value))
285 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)

Loading…
Cancel
Save