From: Georges Discry Date: Fri, 21 Apr 2023 19:56:10 +0000 (+0200) Subject: Use shlex escaping in Environment X-Git-Tag: v15~219 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=3154a16984628e0a7ea24669f0825f46e72f6c3c;p=thirdparty%2Fmkosi.git Use shlex escaping in Environment 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. --- diff --git a/mkosi/config.py b/mkosi/config.py index 64681c2a6..8715e839b 100644 --- a/mkosi/config.py +++ b/mkosi/config.py @@ -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",