From: Joerg Behrmann Date: Tue, 25 Apr 2023 12:39:56 +0000 (+0200) Subject: config: allow globs when matching image IDs X-Git-Tag: v15~198^2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b537512ed8d25febcf0a800be919529778423a63;p=thirdparty%2Fmkosi.git config: allow globs when matching image IDs --- diff --git a/mkosi.md b/mkosi.md index 14978356c..86830e5a4 100644 --- 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=` diff --git a/mkosi/config.py b/mkosi/config.py index 75fdacc74..9557cba73 100644 --- a/mkosi/config.py +++ b/mkosi/config.py @@ -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( diff --git a/tests/test_parse_load_args.py b/tests/test_parse_load_args.py index c0bdf9a32..33254efb2 100644 --- a/tests/test_parse_load_args.py +++ b/tests/test_parse_load_args.py @@ -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(