Browse Source

new localrc set strategy

the localrc is really just executable shell, which means there are
tons of edgecases where you can't treat it the same as simple
assignment a=b.

Instead of trying to be smart about things and reduce duplication in
the localrc files, just do the naive thing and stack up all the shell
declarations in order. When evaluated in shell they will end up
stacking up as expected.

Change-Id: I231d130b24b02cdd79618f85472cee21905884e0
Sean Dague 2 years ago
parent
commit
de46354ed3

+ 20
- 39
devstack/dsconf.py View File

@@ -199,36 +199,16 @@ class LocalConf(object):
199 199
 
200 200
                     if re.match(re.escape("[[local|localrc]]"), line):
201 201
                         in_meta = True
202
-                        writer.write(line)
203
-                    elif re.match("\[\[.*\|.*\]\]", line):
204
-                        # if we're not done yet, we
205
-                        if in_meta:
206
-                            func(writer, None)
207
-                            writer.write(line)
208
-                            done = True
209
-                        in_meta = False
210
-                        continue
211
-                    elif re.match("\s*%s\s*\=" % re.escape(name), line):
212
-                        # we found our key, pass to the writer func
213
-                        func(writer, line)
202
+                    elif in_meta and re.match(re.escape("[["), line):
203
+                        func(writer, None)
214 204
                         done = True
215
-                    else:
216
-                        # write out whatever we find
217
-                        writer.write(line)
205
+                        in_meta = False
206
+
207
+                    # otherwise, just write what we found
208
+                    writer.write(line)
218 209
             if not done:
219 210
                 func(writer, None)
220 211
 
221
-    def set_local(self, name, value):
222
-        if not os.path.exists(self.fname):
223
-            with open(self.fname, "w+") as writer:
224
-                writer.write("[[local|localrc]]\n")
225
-                writer.write("%s=%s\n" % (name, value))
226
-                return
227
-
228
-        def _do_set(writer, line):
229
-            writer.write("%s=%s\n" % (name, value))
230
-        self._at_insert_point_local(name, _do_set)
231
-
232 212
     def set_local_raw(self, line):
233 213
         if not os.path.exists(self.fname):
234 214
             with open(self.fname, "w+") as writer:
@@ -312,19 +292,20 @@ class LocalConf(object):
312 292
         for group, conf in groups:
313 293
             if group == "local":
314 294
                 for line in lc._section(group, conf):
315
-                    if line.startswith('#'):
316
-                        continue
317
-                    m = re.match(r"([^#=\s]+)\s*\=\s*(.+)", line)
318
-
319
-                    if m:
320
-                        self.set_local(m.group(1), m.group(2))
321
-                    elif re.match("(enable|disable)", line):
322
-                        # special case appending enable* disable*
323
-                        # function lines
324
-                        self.set_local_raw(line)
325
-                    else:
326
-                        print("SKIPPING ``%s`` from '%s'" %
327
-                              (line.lstrip(), lcfile))
295
+                    self.set_local_raw(line)
296
+                    # if line.startswith('#'):
297
+                    #     continue
298
+                    # m = re.match(r"([^#=\s]+)\s*\=\s*(.+)", line)
299
+
300
+                    # if m:
301
+                    #     self.set_local(m.group(1), m.group(2))
302
+                    # elif re.match("(enable|disable)", line):
303
+                    #     # special case appending enable* disable*
304
+                    #     # function lines
305
+                    #     self.set_local_raw(line)
306
+                    # else:
307
+                    #     print("SKIPPING ``%s`` from '%s'" %
308
+                    #           (line.lstrip(), lcfile))
328 309
             else:
329 310
                 for section, name, value in lc._conf(group, conf):
330 311
                     self.set(group, conf, section, name, value)

+ 3
- 1
devstack/tests/test_localconf_merge.py View File

@@ -57,9 +57,10 @@ TEMPEST_PLUGINS+=" /opt/stack/new/ironic"
57 57
 
58 58
 RESULT1 = """
59 59
 [[local|localrc]]
60
-a=5
60
+a=b
61 61
 c=d
62 62
 f=1
63
+a=5
63 64
 g=2
64 65
 [[post-config|$NEUTRON_CONF]]
65 66
 [DEFAULT]
@@ -77,6 +78,7 @@ RESULT2 = """
77 78
 a=b
78 79
 c=d
79 80
 f=1
81
+# some other comment
80 82
 enable_plugin ironic https://github.com/openstack/ironic
81 83
 TEMPEST_PLUGINS+=" /opt/stack/new/ironic"
82 84
 [[post-config|$NEUTRON_CONF]]

+ 4
- 3
devstack/tests/test_localconf_set_local.py View File

@@ -51,9 +51,10 @@ compute = auto
51 51
 
52 52
 RESULT2 = """
53 53
 [[local|localrc]]
54
-a=2
54
+a=b
55 55
 c=d
56 56
 f=1
57
+a=2
57 58
 [[post-config|$NEUTRON_CONF]]
58 59
 [DEFAULT]
59 60
 global_physnet_mtu=1450
@@ -89,14 +90,14 @@ class TestLcSet(testtools.TestCase):
89 90
 
90 91
     def test_set_new(self):
91 92
         conf = dsconf.LocalConf(self._path)
92
-        conf.set_local("g", "2")
93
+        conf.set_local_raw("g=2")
93 94
         with open(self._path) as f:
94 95
             content = f.read()
95 96
             self.assertEqual(content, RESULT1)
96 97
 
97 98
     def test_set_existing(self):
98 99
         conf = dsconf.LocalConf(self._path)
99
-        conf.set_local("a", "2")
100
+        conf.set_local_raw("a=2")
100 101
         with open(self._path) as f:
101 102
             content = f.read()
102 103
             self.assertEqual(content, RESULT2)

Loading…
Cancel
Save