summaryrefslogtreecommitdiff
path: root/cloudinit/distros/parsers/sys_conf.py
diff options
context:
space:
mode:
authorJoshua Harlow <harlowja@yahoo-inc.com>2012-10-11 12:49:45 -0700
committerJoshua Harlow <harlowja@yahoo-inc.com>2012-10-11 12:49:45 -0700
commit0f1a2cbe434cba243ce65ff43a88722c2bcf6f2c (patch)
tree4f78abcde4e023878558cb066fc939ae8a12a3da /cloudinit/distros/parsers/sys_conf.py
parentfe1ec4d4cbb682731e8f65be5dab60f4593ed9d6 (diff)
downloadvyos-cloud-init-0f1a2cbe434cba243ce65ff43a88722c2bcf6f2c.tar.gz
vyos-cloud-init-0f1a2cbe434cba243ce65ff43a88722c2bcf6f2c.zip
More adjustments/cleanups for the system configuration
helper objects. 1. Add in a parser for the /etc/hostname file that can be shared 2. Adjust the sysconfig configobj parser to not always quote fields that it does not need to quote + add in tests around this to ensure that we don't go nuts with quoting again.
Diffstat (limited to 'cloudinit/distros/parsers/sys_conf.py')
-rw-r--r--cloudinit/distros/parsers/sys_conf.py85
1 files changed, 85 insertions, 0 deletions
diff --git a/cloudinit/distros/parsers/sys_conf.py b/cloudinit/distros/parsers/sys_conf.py
new file mode 100644
index 00000000..3d8802b8
--- /dev/null
+++ b/cloudinit/distros/parsers/sys_conf.py
@@ -0,0 +1,85 @@
+# vi: ts=4 expandtab
+#
+# Copyright (C) 2012 Yahoo! Inc.
+#
+# Author: Joshua Harlow <harlowja@yahoo-inc.com>
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License version 3, as
+# published by the Free Software Foundation.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+from StringIO import StringIO
+
+import re
+
+# This library is used to parse/write
+# out the various sysconfig files edited
+#
+# It has to be slightly modified though
+# to ensure that all values are quoted/unquoted correctly
+# since these configs are usually sourced into
+# bash scripts...
+import configobj
+
+
+class SysConf(configobj.ConfigObj):
+ def __init__(self, contents):
+ configobj.ConfigObj.__init__(self, contents,
+ interpolation=False,
+ write_empty_values=True)
+
+ def __str__(self):
+ contents = self.write()
+ out_contents = StringIO()
+ if isinstance(contents, (list, tuple)):
+ out_contents.write("\n".join(contents))
+ else:
+ out_contents.write(str(contents))
+ return out_contents.getvalue()
+
+ def _quote(self, value, multiline=False):
+ if not isinstance(value, (str, basestring)):
+ raise ValueError('Value "%s" is not a string' % (value))
+ if len(value) == 0:
+ return ''
+ if re.search(r"[\n\r]", value):
+ raise ValueError('Value "%s" cannot be safely quoted.' % (value))
+ quot = "%s"
+ if '#' in value:
+ quot = self._get_single_quote(value)
+ elif value[0] in ['"', "'"] and value[-1] in ['"', "'"]:
+ # Already quoted, leave it be
+ pass
+ elif "'" in value and '"' in value:
+ quot = self._get_triple_quote(value)
+ 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 ]", value) and not white_space_ok:
+ quot = self._get_single_quote(value)
+ return quot % (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)
+ return '%s%s%s%s%s' % (indent_string,
+ key,
+ self._a_to_u('='),
+ val,
+ cmnt)
+