]> git.ipfire.org Git - thirdparty/mkosi.git/commitdiff
Use shlex escaping in Environment
authorGeorges Discry <georges@discry.be>
Fri, 21 Apr 2023 19:56:10 +0000 (21:56 +0200)
committerDaan De Meyer <daan.j.demeyer@gmail.com>
Sat, 22 Apr 2023 05:18:28 +0000 (07:18 +0200)
The Environment configuration takes lists of space-separated values and
combine them in a single list, splitting the values on spaces and
newlines. However, environment variables might sometimes need a space in
their value, which is currently impossible to configure.

Instead of blindly splitting the values on spaces and newlines, they are
now split with shlex. Spaces, newlines and backslashes can be escaped
with backslashes, single quotes and double quotes.

This behavior also more closely matches the Environment directive of systemd.

mkosi/config.py

index 64681c2a67f04d69d55b2eac447c9846c87ca376..8715e839bd61e173b7dc9103607711cb099a8164 100644 (file)
@@ -6,6 +6,7 @@ import fnmatch
 import functools
 import os.path
 import platform
+import shlex
 import sys
 import textwrap
 from collections.abc import Sequence
@@ -192,7 +193,7 @@ def config_make_enum_matcher(type: Type[enum.Enum]) -> ConfigMatchCallback:
     return config_match_enum
 
 
-def config_make_list_parser(delimiter: str, parse: Callable[[str], Any] = str) -> ConfigParseCallback:
+def config_make_list_parser(delimiter: str, unescape: bool = False, parse: Callable[[str], Any] = str) -> ConfigParseCallback:
     ignore: set[str] = set()
 
     def config_parse_list(dest: str, value: Optional[str], namespace: argparse.Namespace) -> list[Any]:
@@ -205,10 +206,19 @@ def config_make_list_parser(delimiter: str, parse: Callable[[str], Any] = str) -
         if not value:
             return l # type: ignore
 
-        value = value.replace("\n", delimiter)
-        values = [v for v in value.split(delimiter) if v]
+        if unescape:
+            lex = shlex.shlex(value, posix=True)
+            lex.whitespace_split = True
+            lex.whitespace = f"\n{delimiter}"
+            lex.commenters = ""
+            values = list(lex)
+        else:
+            values = value.replace(delimiter, "\n").split("\n")
 
         for v in values:
+            if not v:
+                continue
+
             if v.startswith("!"):
                 ignore.add(v[1:])
                 continue
@@ -591,7 +601,7 @@ class MkosiConfigParser:
         MkosiConfigSetting(
             dest="environment",
             section="Content",
-            parse=config_make_list_parser(delimiter=" "),
+            parse=config_make_list_parser(delimiter=" ", unescape=True),
         ),
         MkosiConfigSetting(
             dest="build_sources",