]> git.ipfire.org Git - thirdparty/mkosi.git/commitdiff
Remove scaffolding for DistributionInstaller possibly being None 1217/head
authorJoerg Behrmann <behrmann@physik.fu-berlin.de>
Thu, 24 Nov 2022 16:14:40 +0000 (17:14 +0100)
committerJoerg Behrmann <behrmann@physik.fu-berlin.de>
Thu, 24 Nov 2022 16:27:59 +0000 (17:27 +0100)
mkosi/__init__.py
mkosi/backend.py
mkosi/distributions/__init__.py
mkosi/install.py

index 381126db14c4ce0a02d23942203365234ef7ccb1..57a4c7f1e1d6982c60482e8e3cd2588d0e3be5ca 100644 (file)
@@ -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
 
index 8856fff2a862394bd4a45a312c9f764497b48655..6cca4dd428e2c1982ea33f111598986ae5a251ef 100644 (file)
@@ -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.
index 32e57d21ade63550d7dbf2dd9b4c93b072db7130..ea1c6316cbad2200f9201bc3d168c5d3a481e7f6 100644 (file)
@@ -24,4 +24,4 @@ class DistributionInstaller:
 
     @classmethod
     def remove_packages(cls, state: "MkosiState", remove: List[str]) -> None:
-        pass
+        raise NotImplementedError
index e6cdee6b11d60d2acf14ae9bb3da434fed6d5203..6862c9a7833b3be427dd3e4468e97108ee595bd9 100644 (file)
@@ -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…"):