From: Daan De Meyer Date: Fri, 21 Jul 2023 10:19:32 +0000 (+0200) Subject: Move Verb to config.py X-Git-Tag: v15~66^2~10 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=5791d2b8fc77716cde8127d5e622d205cff2e87b;p=thirdparty%2Fmkosi.git Move Verb to config.py --- diff --git a/mkosi/__init__.py b/mkosi/__init__.py index 92783e710..520edd15b 100644 --- a/mkosi/__init__.py +++ b/mkosi/__init__.py @@ -29,6 +29,7 @@ from mkosi.config import ( MkosiConfig, MkosiConfigParser, SecureBootSignTool, + Verb, ) from mkosi.install import add_dropin_config_from_resource, copy_path from mkosi.log import Style, color_error, complete_step, die, log_step @@ -45,7 +46,6 @@ from mkosi.util import ( InvokingUser, ManifestFormat, OutputFormat, - Verb, flatten, flock, format_bytes, @@ -56,9 +56,6 @@ from mkosi.util import ( try_import, ) -MKOSI_COMMANDS_NEED_BUILD = (Verb.build, Verb.shell, Verb.boot, Verb.qemu, Verb.serve) -MKOSI_COMMANDS_SUDO = (Verb.shell, Verb.boot) - @contextlib.contextmanager def mount_image(state: MkosiState) -> Iterator[None]: @@ -2128,7 +2125,7 @@ def expand_specifier(s: str) -> str: def needs_build(args: MkosiArgs, config: MkosiConfig) -> bool: - return args.verb in MKOSI_COMMANDS_NEED_BUILD and (args.force > 0 or not config.output_dir.joinpath(config.output_with_compression).exists()) + return args.verb.needs_build() and (args.force > 0 or not config.output_dir.joinpath(config.output_with_compression).exists()) @contextlib.contextmanager @@ -2154,7 +2151,7 @@ def prepend_to_environ_path(config: MkosiConfig) -> Iterator[None]: def run_verb(args: MkosiArgs, presets: Sequence[MkosiConfig]) -> None: - if args.verb in MKOSI_COMMANDS_SUDO: + if args.verb.needs_sudo(): check_root() if args.verb == Verb.genkey: diff --git a/mkosi/config.py b/mkosi/config.py index b01608099..ab7b7dc9e 100644 --- a/mkosi/config.py +++ b/mkosi/config.py @@ -33,7 +33,6 @@ from mkosi.util import ( InvokingUser, ManifestFormat, OutputFormat, - Verb, chdir, detect_distribution, flatten, @@ -45,13 +44,38 @@ from mkosi.util import ( __version__ = "14" -MKOSI_COMMANDS_CMDLINE = (Verb.build, Verb.shell, Verb.boot, Verb.qemu, Verb.ssh) - ConfigParseCallback = Callable[[str, Optional[str], argparse.Namespace], Any] ConfigMatchCallback = Callable[[str, str, argparse.Namespace], bool] ConfigDefaultCallback = Callable[[argparse.Namespace], Any] +class Verb(enum.Enum): + build = "build" + clean = "clean" + summary = "summary" + shell = "shell" + boot = "boot" + qemu = "qemu" + ssh = "ssh" + serve = "serve" + bump = "bump" + help = "help" + genkey = "genkey" + + # Defining __str__ is required to get "print_help()" output to include the human readable (values) of Verb. + def __str__(self) -> str: + return self.value + + def supports_cmdline(self) -> bool: + return self in (Verb.build, Verb.shell, Verb.boot, Verb.qemu, Verb.ssh) + + def needs_build(self) -> bool: + return self in (Verb.build, Verb.shell, Verb.boot, Verb.qemu, Verb.serve) + + def needs_sudo(self) -> bool: + return self in (Verb.shell, Verb.boot) + + class ConfigFeature(enum.Enum): auto = "auto" enabled = "enabled" @@ -2072,8 +2096,8 @@ def load_args(args: argparse.Namespace) -> MkosiArgs: def load_config(args: argparse.Namespace) -> MkosiConfig: find_image_version(args) - if args.cmdline and args.verb not in MKOSI_COMMANDS_CMDLINE: - die(f"Parameters after verb are only accepted for {' '.join(verb.name for verb in MKOSI_COMMANDS_CMDLINE)}.") + if args.cmdline and not args.verb.supports_cmdline(): + die(f"Arguments after verb are not supported for {args.verb}.") if shutil.which("bsdtar") and args.distribution == Distribution.openmandriva and args.tar_strip_selinux_context: die("Sorry, bsdtar on OpenMandriva is incompatible with --tar-strip-selinux-context") diff --git a/mkosi/util.py b/mkosi/util.py index 3bdf0ed30..7e2e3753a 100644 --- a/mkosi/util.py +++ b/mkosi/util.py @@ -30,24 +30,6 @@ class PackageType(enum.Enum): ebuild = 5 -class Verb(enum.Enum): - build = "build" - clean = "clean" - summary = "summary" - shell = "shell" - boot = "boot" - qemu = "qemu" - ssh = "ssh" - serve = "serve" - bump = "bump" - help = "help" - genkey = "genkey" - - # Defining __str__ is required to get "print_help()" output to include the human readable (values) of Verb. - def __str__(self) -> str: - return self.value - - class Distribution(enum.Enum): package_type: PackageType diff --git a/tests/test_parse_load_args.py b/tests/test_parse_load_args.py index 9d045089f..d9f5b0de0 100644 --- a/tests/test_parse_load_args.py +++ b/tests/test_parse_load_args.py @@ -12,8 +12,8 @@ from typing import Iterator, List, Optional import pytest -from mkosi.config import MkosiArgs, MkosiConfig, MkosiConfigParser -from mkosi.util import Compression, Distribution, Verb +from mkosi.config import MkosiArgs, MkosiConfig, MkosiConfigParser, Verb +from mkosi.util import Compression, Distribution @contextmanager