← Back to team overview

cloud-init-dev team mailing list archive

[Merge] ~smoser/cloud-init:feature/load_shell_content-comments into cloud-init:master

 

Scott Moser has proposed merging ~smoser/cloud-init:feature/load_shell_content-comments into cloud-init:master.

Commit message:
Support comments in content read by load_shell_content.

load_shell_content previously would not allow shell comment characters
in the content being parsed.  If comments=True is not passed then an
exception would previously be raised as the line would not be guaranteed to
have an '=' in it.

Requested reviews:
  cloud-init commiters (cloud-init-dev)

For more details, see:
https://code.launchpad.net/~smoser/cloud-init/+git/cloud-init/+merge/327525
-- 
Your team cloud-init commiters is requested to review the proposed merge of ~smoser/cloud-init:feature/load_shell_content-comments into cloud-init:master.
diff --git a/cloudinit/util.py b/cloudinit/util.py
index b486e18..f570b9d 100644
--- a/cloudinit/util.py
+++ b/cloudinit/util.py
@@ -2529,7 +2529,7 @@ def load_shell_content(content, add_empty=False, empty_val=None):
         if PY26 and isinstance(blob, six.text_type):
             # Older versions don't support unicode input
             blob = blob.encode("utf8")
-        return shlex.split(blob)
+        return shlex.split(blob, comments=True)
 
     data = {}
     for line in _shlex_split(content):
diff --git a/tests/unittests/test_util.py b/tests/unittests/test_util.py
index 65035be..f38a664 100644
--- a/tests/unittests/test_util.py
+++ b/tests/unittests/test_util.py
@@ -807,4 +807,20 @@ class TestSystemIsSnappy(helpers.FilesystemMockingTestCase):
         self.reRoot(root_d)
         self.assertTrue(util.system_is_snappy())
 
+
+class TestLoadShellContent(helpers.TestCase):
+    def test_comments_handled_correctly(self):
+        """Shell comments should be allowed in the content."""
+        self.assertEqual(
+            {'key1': 'val1', 'key2': 'val2', 'key3': 'val3 #tricky'},
+            util.load_shell_content('\n'.join([
+                "#top of file comment",
+                "key1=val1 #this is a comment",
+                "# second comment",
+                'key2="val2" # inlin comment'
+                '#badkey=wark',
+                'key3="val3 #tricky"',
+                ''])))
+
+
 # vi: ts=4 expandtab

References