]> git.ipfire.org Git - thirdparty/mkosi.git/commitdiff
config: allow globs when matching image IDs
authorJoerg Behrmann <behrmann@physik.fu-berlin.de>
Tue, 25 Apr 2023 12:39:56 +0000 (14:39 +0200)
committerJoerg Behrmann <behrmann@physik.fu-berlin.de>
Tue, 25 Apr 2023 13:31:39 +0000 (15:31 +0200)
mkosi.md
mkosi/config.py
tests/test_parse_load_args.py

index 14978356c112759fd3c821a325e4900b3202cb64..86830e5a4e2667889229ed9610e10963ec533b22 100644 (file)
--- a/mkosi.md
+++ b/mkosi.md
@@ -236,11 +236,11 @@ a boolean argument: either "1", "yes", or "true" to enable, or "0",
 
 `ImageId=`
 
-: Matches against the configured image ID. If this condition is used and no
-  image ID has been explicitly configured yet, this condition fails. Multiple
-  image IDs may be specified, separated by spaces. If multiple image IDs are
-  specified, the condition is satisfied if the configured image ID equals any of
-  the specified image IDs.
+: Matches against the configured image ID, supporting globs. If this condition
+  is used and no image ID has been explicitly configured yet, this condition
+  fails. Multiple image IDs may be specified, separated by spaces. If multiple
+  image IDs are specified, the condition is satisfied if the configured image ID
+  equals any of the specified image IDs.
 
 `ImageVersion=`
 
index 75fdacc7405ccb6ed33ebe1f843821ad2c510bda..9557cba733da7d63ee5196d58cca0bf53ab7f2be 100644 (file)
@@ -248,6 +248,7 @@ def config_make_list_matcher(
     delimiter: str,
     *,
     unescape: bool = False,
+    allow_globs: bool = False,
     all: bool = False,
     parse: Callable[[str], Any] = str,
 ) -> ConfigMatchCallback:
@@ -262,7 +263,16 @@ def config_make_list_matcher(
             values = value.replace(delimiter, "\n").split("\n")
 
         for v in values:
-            m = getattr(namespace, dest) == parse(v)
+            current_value = getattr(namespace, dest)
+            comparison_value = parse(v)
+            if allow_globs:
+                # check if the option has been set, since fnmatch wants strings
+                if isinstance(current_value, str):
+                    m = fnmatch.fnmatchcase(current_value, comparison_value)
+                else:
+                    m = False
+            else:
+                m = current_value == comparison_value
 
             if not all and m:
                 return True
@@ -563,7 +573,7 @@ class MkosiConfigParser:
         ),
         MkosiConfigSetting(
             dest="image_id",
-            match=config_make_list_matcher(delimiter=" "),
+            match=config_make_list_matcher(delimiter=" ", allow_globs=True),
             section="Output",
         ),
         MkosiConfigSetting(
index c0bdf9a32c825989b5a8359174e550c792464497..33254efb2948a45878c83740b26662ff35ce90ad 100644 (file)
@@ -270,13 +270,25 @@ def test_match_imageid(image1: str, image2: str) -> None:
                 """
             )
         )
+        child4 = Path("mkosi.conf.d/child4.conf")
+        child4.write_text(
+            dedent(
+                """\
+                [Match]
+                ImageId=image*
+
+                [Content]
+                Packages=testpkg4
+                """
+            )
+        )
 
         conf = parse([])
         assert "testpkg1" in conf.packages
         if image1 == image2:
             assert "testpkg2" in conf.packages
         assert "testpkg3" in conf.packages
-
+        assert "testpkg4" in conf.packages
 
 
 @pytest.mark.parametrize(