From b115c2454c29b234873a0736242217597ce9c6bc Mon Sep 17 00:00:00 2001 From: Joerg Behrmann Date: Thu, 24 Nov 2022 17:14:40 +0100 Subject: [PATCH] Remove scaffolding for DistributionInstaller possibly being None --- mkosi/__init__.py | 31 +++++++------------------------ mkosi/backend.py | 29 +++++++++++------------------ mkosi/distributions/__init__.py | 2 +- mkosi/install.py | 6 +----- 4 files changed, 20 insertions(+), 48 deletions(-) diff --git a/mkosi/__init__.py b/mkosi/__init__.py index 381126db1..57a4c7f1e 100644 --- a/mkosi/__init__.py +++ b/mkosi/__init__.py @@ -1286,10 +1286,7 @@ def configure_hostname(state: MkosiState, cached: bool) -> None: @contextlib.contextmanager def mount_cache(state: MkosiState) -> Iterator[None]: - if state.installer is not None: - cache_paths = state.installer.cache_path() - else: - cache_paths = [] + cache_paths = state.installer.cache_path() # We can't do this in mount_image() yet, as /var itself might have to be created as a subvolume first with complete_step("Mounting Package Cache", "Unmounting Package Cache"), contextlib.ExitStack() as stack: @@ -1594,15 +1591,8 @@ def install_distribution(state: MkosiState, cached: bool) -> None: if cached: return - install: Callable[[MkosiState], None] - - if state.installer is not None: - install = state.installer.install - else: - die("No Installer") - with mount_cache(state): - install(state) + state.installer.install(state) # Link /var/lib/rpm→/usr/lib/sysimage/rpm for compat with old rpm. # We do this only if the new location is used, which depends on the dnf @@ -1617,15 +1607,11 @@ def remove_packages(state: MkosiState) -> None: if not state.config.remove_packages: return - remove: Callable[[List[str]], Any] - - if state.installer is not None: - remove = lambda p: state.installer.remove_packages(state, p) # type: ignore - else: - die(f"Removing packages is not supported for {state.config.distribution}") - with complete_step(f"Removing {len(state.config.packages)} packages…"): - remove(state.config.remove_packages) + try: + state.installer.remove_packages(state, state.config.remove_packages) + except NotImplementedError: + die(f"Removing packages is not supported for {state.config.distribution}") def reset_machine_id(state: MkosiState) -> None: @@ -2507,10 +2493,7 @@ def gen_kernel_images(state: MkosiState) -> Iterator[Tuple[str, Path]]: if not kver.is_dir(): continue - if state.installer is not None: - kimg = state.installer.kernel_image(kver.name, state.config.architecture) - else: - kimg = Path("lib/modules") / kver.name / "vmlinuz" + kimg = state.installer.kernel_image(kver.name, state.config.architecture) yield kver.name, kimg diff --git a/mkosi/backend.py b/mkosi/backend.py index 8856fff2a..6cca4dd42 100644 --- a/mkosi/backend.py +++ b/mkosi/backend.py @@ -683,6 +683,7 @@ class MkosiState: machine_id: str for_cache: bool environment: Dict[str, str] = dataclasses.field(init=False) + installer: DistributionInstaller = dataclasses.field(init=False) cache_pre_inst: Optional[Path] = None cache_pre_dev: Optional[Path] = None @@ -695,6 +696,16 @@ class MkosiState: self.environment['IMAGE_ID'] = self.config.image_id if self.config.image_version is not None: self.environment['IMAGE_VERSION'] = self.config.image_version + try: + distro = str(self.config.distribution) + mod = importlib.import_module(f"mkosi.distributions.{distro}") + installer = getattr(mod, f"{distro.title().replace('_','')}Installer") + instance = installer() if issubclass(installer, DistributionInstaller) else None + except (ImportError, AttributeError): + instance = None + if instance is None: + die("No installer for this distribution.") + self.installer = instance @property def root(self) -> Path: @@ -711,24 +722,6 @@ class MkosiState: return None return self.partition_table.partitions.get(ident) - @property - def installer(self) -> Optional["DistributionInstaller"]: - try: - # we do a getattr here because we wnt to use the missing attribute - # as a sentinel, since we will use None as a sentinel further down - # for the case of distros that are not migrated to this interface - return cast(Optional["DistributionInstaller"], getattr(self, "_installer")) - except AttributeError: - try: - distro = str(self.config.distribution) - mod = importlib.import_module(f"mkosi.distributions.{distro}") - installer = getattr(mod, f"{distro.title().replace('_','')}Installer") - instance = installer() if issubclass(installer, DistributionInstaller) else None - except (ImportError, AttributeError): - instance = None - setattr(self, "_installer", instance) - return cast(Optional["DistributionInstaller"], instance) - def should_compress_fs(config: Union[argparse.Namespace, MkosiConfig]) -> Union[bool, str]: """True for the default compression, a string, or False. diff --git a/mkosi/distributions/__init__.py b/mkosi/distributions/__init__.py index 32e57d21a..ea1c6316c 100644 --- a/mkosi/distributions/__init__.py +++ b/mkosi/distributions/__init__.py @@ -24,4 +24,4 @@ class DistributionInstaller: @classmethod def remove_packages(cls, state: "MkosiState", remove: List[str]) -> None: - pass + raise NotImplementedError diff --git a/mkosi/install.py b/mkosi/install.py index e6cdee6b1..6862c9a78 100644 --- a/mkosi/install.py +++ b/mkosi/install.py @@ -140,11 +140,7 @@ def install_skeleton_trees(state: MkosiState, cached: bool, *, late: bool=False) if cached: return - if state.installer is not None: - skeletons_after_bootstrap = state.installer.needs_skeletons_after_bootstrap - else: - skeletons_after_bootstrap = False - if not late and skeletons_after_bootstrap: + if not late and state.installer.needs_skeletons_after_bootstrap: return with complete_step("Copying in skeleton file trees…"): -- 2.47.2