]> git.ipfire.org Git - thirdparty/mkosi.git/commitdiff
Support matching against any of a list of distributions
authorDaan De Meyer <daan.j.demeyer@gmail.com>
Sun, 23 Apr 2023 12:35:55 +0000 (14:35 +0200)
committerDaan De Meyer <daan.j.demeyer@gmail.com>
Sun, 23 Apr 2023 12:50:27 +0000 (14:50 +0200)
mkosi.md
mkosi/config.py

index ddc778605c083b9f2471c7304aaaa06ceddef5c2..51c685f7228213d91d01394066f9f945248f7dde 100644 (file)
--- 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=`
 
index 8715e839bd61e173b7dc9103607711cb099a8164..d257a899be62aeb4d75c718c93e805215bcdd2fe 100644 (file)
@@ -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(