Skip to content

Commit 27d6fd0

Browse files
committed
opennebula: fix shell variables parsing
Fix parsing of the empty values written as: KEY="" KEY='' Change-Id: I56ea03da5d78c01af2647444b825bf63d5146137
1 parent 28f4fbb commit 27d6fd0

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

cloudbaseinit/metadata/services/opennebulaservice.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,14 @@ def _parse_shell_variables(content):
8888
new_content = sep.join(lines)
8989
# get pairs
9090
pairs = {}
91-
pattern = (br"(?P<key>\w+)=(['\"](?P<str_value>[\s\S]+?)['\"]|"
92-
br"(?P<int_value>\d+))(?=\s+\w+=)")
91+
pattern = (br"(?P<key>\w+)=((?P<int_value>\d+)|"
92+
br"['\"](?P<str_value>[\s\S]*?)['\"])(?=\s+\w+=)")
9393
for match in re.finditer(pattern, new_content):
9494
key = encoding.get_as_string(match.group("key"))
95-
pairs[key] = (match.group("str_value") or
96-
int(match.group("int_value")))
95+
val = match.group("str_value")
96+
if match.group("int_value"):
97+
val = int(match.group("int_value"))
98+
pairs[key] = val
9799
return pairs
98100

99101
@staticmethod

cloudbaseinit/tests/metadata/services/test_opennebulaservice.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,13 +145,17 @@ def _test_parse_shell_variables(self, crlf=False, comment=False):
145145
d: e
146146
'
147147
ivar=10
148+
TESTEMPTY=''
149+
TESTEMPTY2=""
148150
""")
149151
if comment:
150152
content += "# A simple comment\n"
151153
if crlf:
152154
content = content.replace("\n", "\r\n")
153155
pairs = self._service._parse_shell_variables(content.encode())
154156
_pairs = {
157+
"TESTEMPTY": b"",
158+
"TESTEMPTY2": b"",
155159
"VAR1": b"1",
156160
"var2": b"abcdef",
157161
"VAR_VAR3": b"aaa.bbb.123.ccc",

0 commit comments

Comments
 (0)