summaryrefslogtreecommitdiff
path: root/cloudinit/distros/parsers
diff options
context:
space:
mode:
authorJoshua Harlow <harlowja@yahoo-inc.com>2012-10-11 21:24:30 -0700
committerJoshua Harlow <harlowja@yahoo-inc.com>2012-10-11 21:24:30 -0700
commitbbe325c902ef3a3b8845cd3c1bb8bee0c3c74a89 (patch)
treef9d104e0badb23c41c026a8badf9cf0cc3a64f48 /cloudinit/distros/parsers
parent204c635e622b52bbe2b2c2a72765e3cb886602fc (diff)
downloadvyos-cloud-init-bbe325c902ef3a3b8845cd3c1bb8bee0c3c74a89.tar.gz
vyos-cloud-init-bbe325c902ef3a3b8845cd3c1bb8bee0c3c74a89.zip
Add some more sysconfig tests + pylint cleanups.
Diffstat (limited to 'cloudinit/distros/parsers')
-rw-r--r--cloudinit/distros/parsers/sys_conf.py59
1 files changed, 38 insertions, 21 deletions
diff --git a/cloudinit/distros/parsers/sys_conf.py b/cloudinit/distros/parsers/sys_conf.py
index 1cefb8bc..5cd765fc 100644
--- a/cloudinit/distros/parsers/sys_conf.py
+++ b/cloudinit/distros/parsers/sys_conf.py
@@ -22,7 +22,7 @@ import pipes
import re
# This library is used to parse/write
-# out the various sysconfig files edited
+# out the various sysconfig files edited (best attempt effort)
#
# It has to be slightly modified though
# to ensure that all values are quoted/unquoted correctly
@@ -30,11 +30,26 @@ import re
# bash scripts...
import configobj
+# See: http://pubs.opengroup.org/onlinepubs/000095399/basedefs/xbd_chap08.html
+# or look at the 'param_expand()' function in the subst.c file in the bash
+# source tarball...
+SHELL_VAR_RULE = r'[a-zA-Z_]+[a-zA-Z0-9_]*'
+SHELL_VAR_REGEXES = [
+ # Basic variables
+ re.compile(r"\$" + SHELL_VAR_RULE),
+ # Things like $?, $0, $-, $@
+ re.compile(r"\$[0-9#\?\-@\*]"),
+ # Things like ${blah:1} - but this one
+ # gets very complex so just try the
+ # simple path
+ re.compile(r"\$\{.+\}"),
+]
+
def _contains_shell_variable(text):
- if (re.search(r"\$\{.+\}", text) or
- re.search(r"\$[a-zA-Z_]+[a-zA-Z0-9_]*", text)):
- return True
+ for r in SHELL_VAR_REGEXES:
+ if r.search(text):
+ return True
return False
@@ -58,33 +73,36 @@ class SysConf(configobj.ConfigObj):
raise ValueError('Value "%s" is not a string' % (value))
if len(value) == 0:
return ''
- quot_func = (lambda x: str(x))
+ quot_func = None
if value[0] in ['"', "'"] and value[-1] in ['"', "'"]:
if len(value) == 1:
- quot_func = (lambda x: self._get_single_quote(x) % x)
+ quot_func = (lambda x:
+ self._get_single_quote(x) % x)
else:
# Quote whitespace if it isn't the start + end of a shell command
- white_space_ok = False
if value.strip().startswith("$(") and value.strip().endswith(")"):
- white_space_ok = True
- if re.search(r"[\t\r\n ]", value) and not white_space_ok:
- if _contains_shell_variable(value):
- # If it contains shell variables then we likely want to
- # leave it alone since the pipes.quote function likes to
- # use single quotes which won't get expanded...
- if re.search(r"[\n\"']", value):
- quot_func = (lambda x: self._get_triple_quote(x) % x)
+ pass
+ else:
+ if re.search(r"[\t\r\n ]", value):
+ if _contains_shell_variable(value):
+ # If it contains shell variables then we likely want to
+ # leave it alone since the pipes.quote function likes
+ # to use single quotes which won't get expanded...
+ if re.search(r"[\n\"']", value):
+ quot_func = (lambda x:
+ self._get_triple_quote(x) % x)
+ else:
+ quot_func = (lambda x:
+ self._get_single_quote(x) % x)
else:
- quot_func = (lambda x: self._get_single_quote(x) % x)
- else:
- quot_func = pipes.quote
+ quot_func = pipes.quote
+ if not quot_func:
+ return value
return quot_func(value)
def _write_line(self, indent_string, entry, this_entry, comment):
# Ensure it is formatted fine for
# how these sysconfig scripts are used
- if this_entry.startswith("'") or this_entry.startswith('"'):
- val = this_entry
val = self._decode_element(self._quote(this_entry))
key = self._decode_element(self._quote(entry))
cmnt = self._decode_element(comment)
@@ -93,4 +111,3 @@ class SysConf(configobj.ConfigObj):
self._a_to_u('='),
val,
cmnt)
-