From: Daan De Meyer Date: Sun, 23 Apr 2023 12:35:55 +0000 (+0200) Subject: Support matching against any of a list of distributions X-Git-Tag: v15~216 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c9f16ad0178d9371d326537474ced8ac3e6c70d5;p=thirdparty%2Fmkosi.git Support matching against any of a list of distributions --- diff --git a/mkosi.md b/mkosi.md index ddc778605..51c685f72 100644 --- a/mkosi.md +++ b/mkosi.md @@ -215,7 +215,10 @@ a boolean argument: either "1", "yes", or "true" to enable, or "0", `Distribution=` -: Matches against the configured distribution. +: Matches against the configured distribution. Multiple distributions may + be specified, separated by spaces. If multiple distributions are specified, + the condition is satisfied if the current distribution equals any of the + specified distributions. `Release=` diff --git a/mkosi/config.py b/mkosi/config.py index 8715e839b..d257a899b 100644 --- a/mkosi/config.py +++ b/mkosi/config.py @@ -234,6 +234,35 @@ def config_make_list_parser(delimiter: str, unescape: bool = False, parse: Calla return config_parse_list +def config_make_list_matcher( + delimiter: str, + unescape: bool = False, + all: bool = False, + parse: Callable[[str], Any] = str, +) -> ConfigMatchCallback: + def config_match_list(dest: str, value: str, namespace: argparse.Namespace) -> bool: + 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: + m = getattr(namespace, dest) == parse(v) + + if not all and m: + return True + if all and not m: + return False + + return all + + return config_match_list + + def make_path_parser(*, required: bool, absolute: bool = True, expanduser: bool = True, expandvars: bool = True) -> Callable[[str], Path]: return functools.partial( parse_path, @@ -358,7 +387,7 @@ class MkosiConfigParser: dest="distribution", section="Distribution", parse=config_make_enum_parser(Distribution), - match=config_make_enum_matcher(Distribution), + match=config_make_list_matcher(delimiter=" ", parse=make_enum_parser(Distribution)), default=detect_distribution()[0], ), MkosiConfigSetting(