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
This commit is contained in:
Sean Dague 2017-02-10 16:57:34 -05:00
parent e0098fc212
commit de46354ed3
3 changed files with 26 additions and 42 deletions

View File

@ -199,36 +199,16 @@ class LocalConf(object):
if re.match(re.escape("[[local|localrc]]"), line):
in_meta = True
writer.write(line)
elif re.match("\[\[.*\|.*\]\]", line):
# if we're not done yet, we
if in_meta:
func(writer, None)
writer.write(line)
done = True
in_meta = False
continue
elif re.match("\s*%s\s*\=" % re.escape(name), line):
# we found our key, pass to the writer func
func(writer, line)
elif in_meta and re.match(re.escape("[["), line):
func(writer, None)
done = True
else:
# write out whatever we find
writer.write(line)
in_meta = False
# otherwise, just write what we found
writer.write(line)
if not done:
func(writer, None)
def set_local(self, name, value):
if not os.path.exists(self.fname):
with open(self.fname, "w+") as writer:
writer.write("[[local|localrc]]\n")
writer.write("%s=%s\n" % (name, value))
return
def _do_set(writer, line):
writer.write("%s=%s\n" % (name, value))
self._at_insert_point_local(name, _do_set)
def set_local_raw(self, line):
if not os.path.exists(self.fname):
with open(self.fname, "w+") as writer:
@ -312,19 +292,20 @@ class LocalConf(object):
for group, conf in groups:
if group == "local":
for line in lc._section(group, conf):
if line.startswith('#'):
continue
m = re.match(r"([^#=\s]+)\s*\=\s*(.+)", line)
self.set_local_raw(line)
# if line.startswith('#'):
# continue
# m = re.match(r"([^#=\s]+)\s*\=\s*(.+)", line)
if m:
self.set_local(m.group(1), m.group(2))
elif re.match("(enable|disable)", line):
# special case appending enable* disable*
# function lines
self.set_local_raw(line)
else:
print("SKIPPING ``%s`` from '%s'" %
(line.lstrip(), lcfile))
# if m:
# self.set_local(m.group(1), m.group(2))
# elif re.match("(enable|disable)", line):
# # special case appending enable* disable*
# # function lines
# self.set_local_raw(line)
# else:
# print("SKIPPING ``%s`` from '%s'" %
# (line.lstrip(), lcfile))
else:
for section, name, value in lc._conf(group, conf):
self.set(group, conf, section, name, value)

View File

@ -57,9 +57,10 @@ TEMPEST_PLUGINS+=" /opt/stack/new/ironic"
RESULT1 = """
[[local|localrc]]
a=5
a=b
c=d
f=1
a=5
g=2
[[post-config|$NEUTRON_CONF]]
[DEFAULT]
@ -77,6 +78,7 @@ RESULT2 = """
a=b
c=d
f=1
# some other comment
enable_plugin ironic https://github.com/openstack/ironic
TEMPEST_PLUGINS+=" /opt/stack/new/ironic"
[[post-config|$NEUTRON_CONF]]

View File

@ -51,9 +51,10 @@ compute = auto
RESULT2 = """
[[local|localrc]]
a=2
a=b
c=d
f=1
a=2
[[post-config|$NEUTRON_CONF]]
[DEFAULT]
global_physnet_mtu=1450
@ -89,14 +90,14 @@ class TestLcSet(testtools.TestCase):
def test_set_new(self):
conf = dsconf.LocalConf(self._path)
conf.set_local("g", "2")
conf.set_local_raw("g=2")
with open(self._path) as f:
content = f.read()
self.assertEqual(content, RESULT1)
def test_set_existing(self):
conf = dsconf.LocalConf(self._path)
conf.set_local("a", "2")
conf.set_local_raw("a=2")
with open(self._path) as f:
content = f.read()
self.assertEqual(content, RESULT2)