Browse Source

add localrc extraction

Sean Dague 2 years ago
parent
commit
a0f574ab97
2 changed files with 43 additions and 10 deletions
  1. 19
    10
      devstack/dsconf.py
  2. 24
    0
      devstack/tests/test_localconf_extract.py

+ 19
- 10
devstack/dsconf.py View File

@@ -134,8 +134,20 @@ class LocalConf(object):
134 134
         self.fname = fname
135 135
 
136 136
     def _conf(self, group, conf):
137
-        in_section = False
138 137
         current_section = ""
138
+        for line in self._section(group, conf):
139
+            m = re.match("\[([^\[\]]+)\]", line)
140
+            if m:
141
+                current_section = m.group(1)
142
+                continue
143
+            else:
144
+                m2 = re.match(r"(\w+)\s*\=\s*(.+)", line)
145
+                if m2:
146
+                    yield current_section, m2.group(1), m2.group(2)
147
+
148
+    def _section(self, group, conf):
149
+        """Yield all the lines out of a meta section."""
150
+        in_section = False
139 151
         with open(self.fname) as reader:
140 152
             for line in reader.readlines():
141 153
                 if re.match(r"\[\[%s\|%s\]\]" % (
@@ -149,18 +161,15 @@ class LocalConf(object):
149 161
                 elif re.match("\[\[.*\|.*\]\]", line):
150 162
                     in_section = False
151 163
                     continue
152
-
153 164
                 if in_section:
154
-                    m = re.match("\[([^\[\]]+)\]", line)
155
-                    if m:
156
-                        current_section = m.group(1)
157
-                        continue
158
-                    else:
159
-                        m2 = re.match(r"(\w+)\s*\=\s*(.+)", line)
160
-                        if m2:
161
-                            yield current_section, m2.group(1), m2.group(2)
165
+                    yield line
162 166
 
163 167
     def extract(self, group, conf, target):
164 168
         ini_file = IniFile(target)
165 169
         for section, name, value in self._conf(group, conf):
166 170
             ini_file.set(section, name, value)
171
+
172
+    def extract_localrc(self, target):
173
+        with open(target, "a+") as f:
174
+            for line in self._section("local", "localrc"):
175
+                f.write(line)

+ 24
- 0
devstack/tests/test_localconf_extract.py View File

@@ -36,6 +36,17 @@ global_physnet_mtu=1450
36 36
 compute = auto
37 37
 """
38 38
 
39
+LOCALRC = """a = 1
40
+c = b
41
+"""
42
+
43
+LOCALRC_RES = """a = 1
44
+c = b
45
+a = b
46
+c = d
47
+f = 1
48
+"""
49
+
39 50
 NOVA = """[upgrade_levels]
40 51
 compute = auto
41 52
 """
@@ -114,3 +125,16 @@ class TestLcExtract(testtools.TestCase):
114 125
         with open(neutron) as f:
115 126
             content = f.read()
116 127
             self.assertEqual(content, NEUTRON_BASE2_RES)
128
+
129
+    def test_extract_localrc(self):
130
+        dirname = self.useFixture(fixtures.TempDir()).path
131
+        localrc = os.path.join(dirname, "localrc")
132
+        with open(localrc, "w+") as f:
133
+            f.write(LOCALRC)
134
+
135
+        conf = dsconf.LocalConf(self._path)
136
+        conf.extract_localrc(localrc)
137
+
138
+        with open(localrc) as f:
139
+            content = f.read()
140
+            self.assertEqual(content, LOCALRC_RES)

Loading…
Cancel
Save