From: Daan De Meyer Date: Sun, 10 Dec 2023 12:17:45 +0000 (+0100) Subject: Allow specifying minimum mkosi version X-Git-Tag: v20~87^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=refs%2Fpull%2F2153%2Fhead;p=thirdparty%2Fmkosi.git Allow specifying minimum mkosi version Currently, users often get a confusing message about some property not existing when they try to use an older version of mkosi to build a configuration that requires a newer version. Let's improve on this by allowing configurations to declare the minimum version required to build the configuration. --- diff --git a/mkosi/__init__.py b/mkosi/__init__.py index ea5a527d1..2aa02a26f 100644 --- a/mkosi/__init__.py +++ b/mkosi/__init__.py @@ -38,6 +38,7 @@ from mkosi.config import ( SecureBootSignTool, ShimBootloader, Verb, + __version__, format_bytes, format_tree, parse_config, @@ -3004,6 +3005,12 @@ def run_verb(args: MkosiArgs, images: Sequence[MkosiConfig]) -> None: page(text, args.pager) return + for config in images: + if not config.minimum_version or config.minimum_version <= __version__: + continue + + die(f"mkosi {config.minimum_version} or newer is required to build this configuration (found {__version__})") + for config in images: check_workspace_directory(config) diff --git a/mkosi/config.py b/mkosi/config.py index cbac37842..3882028b3 100644 --- a/mkosi/config.py +++ b/mkosi/config.py @@ -671,6 +671,18 @@ def config_parse_vsock_cid(value: Optional[str], old: Optional[int]) -> Optional return cid +def config_parse_minimum_version(value: Optional[str], old: Optional[GenericVersion]) -> Optional[GenericVersion]: + if not value: + return old + + new = GenericVersion(value) + + if not old: + return new + + return max(old, new) + + @dataclasses.dataclass(frozen=True) class MkosiConfigSetting: dest: str @@ -873,6 +885,7 @@ class MkosiConfig: include: tuple[str, ...] images: tuple[str, ...] dependencies: tuple[str, ...] + minimum_version: Optional[GenericVersion] distribution: Distribution release: str @@ -1245,6 +1258,12 @@ SETTINGS = ( parse=config_make_list_parser(delimiter=","), help="Specify other images that this image depends on", ), + MkosiConfigSetting( + dest="minimum_version", + section="Config", + parse=config_parse_minimum_version, + help="Specify the minimum required mkosi version", + ), MkosiConfigSetting( dest="distribution", short="-d", @@ -2952,6 +2971,7 @@ def summary(config: MkosiConfig) -> str: Include: {line_join_list(config.include)} Images: {line_join_list(config.images)} Dependencies: {line_join_list(config.dependencies)} + Minimum Version: {none_to_none(config.minimum_version)} {bold("DISTRIBUTION")}: Distribution: {bold(config.distribution)} @@ -3093,6 +3113,8 @@ class MkosiJsonEncoder(json.JSONEncoder): def default(self, o: Any) -> Any: if isinstance(o, StrEnum): return str(o) + elif isinstance(o, GenericVersion): + return str(o) elif isinstance(o, os.PathLike): return os.fspath(o) elif isinstance(o, uuid.UUID): @@ -3172,6 +3194,12 @@ def json_type_transformer(refcls: Union[type[MkosiArgs], type[MkosiConfig]]) -> ) return ret + def generic_version_transformer( + version: Optional[str], + fieldtype: type[Optional[GenericVersion]], + ) -> Optional[GenericVersion]: + return GenericVersion(version) if version is not None else None + transformers = { Path: path_transformer, Optional[Path]: optional_path_transformer, @@ -3195,6 +3223,7 @@ def json_type_transformer(refcls: Union[type[MkosiArgs], type[MkosiConfig]]) -> Verb: enum_transformer, DocFormat: enum_transformer, list[QemuDrive]: config_drive_transformer, + GenericVersion: generic_version_transformer, } def json_transformer(key: str, val: Any) -> Any: diff --git a/mkosi/resources/mkosi.md b/mkosi/resources/mkosi.md index 6efc52750..6ae670dbc 100644 --- a/mkosi/resources/mkosi.md +++ b/mkosi/resources/mkosi.md @@ -464,6 +464,11 @@ boolean argument: either `1`, `yes`, or `true` to enable, or `0`, `no`, image and will be pulled in as dependencies of this image when `Images=` is used. +`MinimumVersion=`, `--minimum-version=` + +: The minimum mkosi version required to build this configuration. If + specified multiple times, the highest specified version is used. + ### [Distribution] Section `Distribution=`, `--distribution=`, `-d` diff --git a/tests/test_json.py b/tests/test_json.py index 2b13e6a7b..a759b31ad 100644 --- a/tests/test_json.py +++ b/tests/test_json.py @@ -28,6 +28,7 @@ from mkosi.config import ( Verb, ) from mkosi.distributions import Distribution +from mkosi.versioncomp import GenericVersion @pytest.mark.parametrize("path", [None, "/baz/qux"]) @@ -169,6 +170,7 @@ def test_config() -> None: "json", "changelog" ], + "MinimumVersion": "123", "Mirror": null, "NSpawnSettings": null, "Output": "outfile", @@ -329,6 +331,7 @@ def test_config() -> None: locale_messages = "", make_initrd = False, manifest_format = [ManifestFormat.json, ManifestFormat.changelog], + minimum_version = GenericVersion("123"), mirror = None, nspawn_settings = None, output = "outfile",