add localrc extraction

This commit is contained in:
Sean Dague 2017-01-16 12:45:37 -05:00
parent edd7cc1bcc
commit a0f574ab97
2 changed files with 43 additions and 10 deletions

View File

@ -134,8 +134,20 @@ class LocalConf(object):
self.fname = fname
def _conf(self, group, conf):
in_section = False
current_section = ""
for line in self._section(group, conf):
m = re.match("\[([^\[\]]+)\]", line)
if m:
current_section = m.group(1)
continue
else:
m2 = re.match(r"(\w+)\s*\=\s*(.+)", line)
if m2:
yield current_section, m2.group(1), m2.group(2)
def _section(self, group, conf):
"""Yield all the lines out of a meta section."""
in_section = False
with open(self.fname) as reader:
for line in reader.readlines():
if re.match(r"\[\[%s\|%s\]\]" % (
@ -149,18 +161,15 @@ class LocalConf(object):
elif re.match("\[\[.*\|.*\]\]", line):
in_section = False
continue
if in_section:
m = re.match("\[([^\[\]]+)\]", line)
if m:
current_section = m.group(1)
continue
else:
m2 = re.match(r"(\w+)\s*\=\s*(.+)", line)
if m2:
yield current_section, m2.group(1), m2.group(2)
yield line
def extract(self, group, conf, target):
ini_file = IniFile(target)
for section, name, value in self._conf(group, conf):
ini_file.set(section, name, value)
def extract_localrc(self, target):
with open(target, "a+") as f:
for line in self._section("local", "localrc"):
f.write(line)

View File

@ -36,6 +36,17 @@ global_physnet_mtu=1450
compute = auto
"""
LOCALRC = """a = 1
c = b
"""
LOCALRC_RES = """a = 1
c = b
a = b
c = d
f = 1
"""
NOVA = """[upgrade_levels]
compute = auto
"""
@ -114,3 +125,16 @@ class TestLcExtract(testtools.TestCase):
with open(neutron) as f:
content = f.read()
self.assertEqual(content, NEUTRON_BASE2_RES)
def test_extract_localrc(self):
dirname = self.useFixture(fixtures.TempDir()).path
localrc = os.path.join(dirname, "localrc")
with open(localrc, "w+") as f:
f.write(LOCALRC)
conf = dsconf.LocalConf(self._path)
conf.extract_localrc(localrc)
with open(localrc) as f:
content = f.read()
self.assertEqual(content, LOCALRC_RES)