]> git.ipfire.org Git - thirdparty/mkosi.git/commitdiff
pacman: Don't fail if packages to remove aren't installed
authorDaanDeMeyer <daan.j.demeyer@gmail.com>
Sat, 5 Jul 2025 19:08:32 +0000 (21:08 +0200)
committerDaanDeMeyer <daan.j.demeyer@gmail.com>
Sat, 5 Jul 2025 20:30:42 +0000 (22:30 +0200)
We already do this for other package managers, let's do it for pacman
as well.

mkosi/installer/pacman.py
mkosi/manifest.py

index 55b34f0f9d1f8428f88b65ead6624862524c8cb2..9d6343b1cb8625f0e969bc7f3857ad15790bbe5f 100644 (file)
@@ -198,7 +198,12 @@ class Pacman(PackageManager):
 
     @classmethod
     def remove(cls, context: Context, packages: Sequence[str]) -> None:
-        cls.invoke(context, "--remove", ["--nosave", "--recursive", *packages], apivfs=True)
+        installed = {
+            cls.parse_pkg_desc(i)[0] for i in (context.root / "var/lib/pacman/local").glob("*/desc")
+        }
+        remove = [p for p in packages if p in installed]
+        if remove:
+            cls.invoke(context, "--remove", ["--nosave", "--recursive", *remove], apivfs=True)
 
     @classmethod
     def keyring(cls, context: Context) -> None:
@@ -262,3 +267,22 @@ class Pacman(PackageManager):
 
         # pacman can't sync a single repository, so we go behind its back and do it ourselves.
         shutil.move(context.repository / "mkosi.db.tar", context.metadata_dir / "lib/pacman/sync/mkosi.db")
+
+    @classmethod
+    def parse_pkg_desc(cls, path: Path) -> tuple[str, str, str, str]:
+        name = version = base = arch = ""
+
+        with path.open() as desc:
+            for line in desc:
+                line = line.strip()
+                if line == "%NAME%":
+                    name = next(desc).strip()
+                elif line == "%VERSION%":
+                    version = next(desc).strip()
+                elif line == "%BASE%":
+                    base = next(desc).strip()
+                elif line == "%ARCH%":
+                    arch = next(desc).strip()
+                    break
+
+        return name, version, base, arch
index 90c485c2560e569c63814c92afed0bb478607459..56bdd16d82adb576c7db2ef26a4d401bfc8e6f43 100644 (file)
@@ -5,13 +5,13 @@ import datetime
 import json
 import subprocess
 import textwrap
-from pathlib import Path
 from typing import IO, Any, Optional
 
 from mkosi.config import ManifestFormat
 from mkosi.context import Context
 from mkosi.distributions import PackageType
 from mkosi.installer.apt import Apt
+from mkosi.installer.pacman import Pacman
 from mkosi.log import complete_step
 from mkosi.run import run
 
@@ -63,23 +63,6 @@ class SourcePackageManifest:
         return t
 
 
-def parse_pkg_desc(f: Path) -> tuple[str, str, str, str]:
-    name = version = base = arch = ""
-    with f.open() as desc:
-        for line in desc:
-            line = line.strip()
-            if line == "%NAME%":
-                name = next(desc).strip()
-            elif line == "%VERSION%":
-                version = next(desc).strip()
-            elif line == "%BASE%":
-                base = next(desc).strip()
-            elif line == "%ARCH%":
-                arch = next(desc).strip()
-                break
-    return name, version, base, arch
-
-
 @dataclasses.dataclass
 class Manifest:
     context: Context
@@ -221,7 +204,7 @@ class Manifest:
         packages = sorted((self.context.root / "var/lib/pacman/local").glob("*/desc"))
 
         for desc in packages:
-            name, version, source, arch = parse_pkg_desc(desc)
+            name, version, source, arch = Pacman.parse_pkg_desc(desc)
             package = PackageManifest("pkg", name, version, arch, 0)
             self.packages.append(package)