]> git.ipfire.org Git - thirdparty/mkosi.git/commitdiff
Move unlink_try_hard and dependents into mkosi.remove
authorJoerg Behrmann <behrmann@physik.fu-berlin.de>
Thu, 24 Nov 2022 11:50:24 +0000 (12:50 +0100)
committerJoerg Behrmann <behrmann@physik.fu-berlin.de>
Thu, 24 Nov 2022 14:51:33 +0000 (15:51 +0100)
Similar to mkosi.install but for removing things.

mkosi/__init__.py
mkosi/distributions/gentoo.py
mkosi/remove.py [new file with mode: 0644]

index c5b2eb2cf68882f7ae085a6b7487f2dc5a9226aa..aa05629b6e17c55161cc9a69dcd10c089c5007df 100644 (file)
@@ -113,6 +113,7 @@ from mkosi.install import (
 )
 from mkosi.manifest import Manifest
 from mkosi.mounts import mount, mount_api_vfs, mount_bind, mount_overlay, mount_tmpfs
+from mkosi.remove import unlink_try_hard
 from mkosi.syscall import blkpg_add_partition, blkpg_del_partition
 
 complete_step = MkosiPrinter.complete_step
@@ -561,27 +562,6 @@ def btrfs_subvol_create(path: Path, mode: int = 0o755) -> None:
         run(["btrfs", "subvol", "create", path])
 
 
-def btrfs_subvol_delete(path: Path) -> None:
-    # Extract the path of the subvolume relative to the filesystem
-    c = run(["btrfs", "subvol", "show", path],
-            stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, text=True)
-    subvol_path = c.stdout.splitlines()[0]
-    # Make the subvolume RW again if it was set RO by btrfs_subvol_delete
-    run(["btrfs", "property", "set", path, "ro", "false"])
-    # Recursively delete the direct children of the subvolume
-    c = run(["btrfs", "subvol", "list", "-o", path],
-            stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, text=True)
-    for line in c.stdout.splitlines():
-        if not line:
-            continue
-        child_subvol_path = line.split(" ", 8)[-1]
-        child_path = path / cast(str, os.path.relpath(child_subvol_path, subvol_path))
-        btrfs_subvol_delete(child_path)
-    # Delete the subvolume now that all its descendants have been deleted
-    run(["btrfs", "subvol", "delete", path],
-        stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
-
-
 def btrfs_subvol_make_ro(path: Path, b: bool = True) -> None:
     run(["btrfs", "property", "set", path, "ro", "true" if b else "false"])
 
@@ -5262,29 +5242,6 @@ def detect_distribution() -> Tuple[Optional[Distribution], Optional[str]]:
     return d, version_id
 
 
-def unlink_try_hard(path: Optional[PathString]) -> None:
-    if path is None:
-        return
-
-    path = Path(path)
-    try:
-        path.unlink()
-        return
-    except FileNotFoundError:
-        return
-    except Exception:
-        pass
-
-    if shutil.which("btrfs"):
-        try:
-            btrfs_subvol_delete(path)
-            return
-        except Exception:
-            pass
-
-    shutil.rmtree(path)
-
-
 def remove_glob(*patterns: PathString) -> None:
     pathgen = (glob.glob(str(pattern)) for pattern in patterns)
     paths: Set[str] = set(sum(pathgen, []))  # uniquify
index bb262cb8b93098e97dc105d21c39159e17c2878f..a5964a219b8c8838f4f3783701dc00fb5f664847 100644 (file)
@@ -11,7 +11,6 @@ from pathlib import Path
 from textwrap import dedent
 from typing import Dict, Generator, List, Sequence
 
-from mkosi import unlink_try_hard
 from mkosi.backend import (
     ARG_DEBUG,
     MkosiConfig,
@@ -28,6 +27,7 @@ from mkosi.backend import (
 )
 from mkosi.distributions import DistributionInstaller
 from mkosi.install import copy_path, open_close
+from mkosi.remove import unlink_try_hard
 
 ARCHITECTURES = {
     "x86_64": ("amd64", "arch/x86/boot/bzImage"),
diff --git a/mkosi/remove.py b/mkosi/remove.py
new file mode 100644 (file)
index 0000000..36a66a2
--- /dev/null
@@ -0,0 +1,53 @@
+# SPDX-License-Identifier: LGPL-2.1+
+
+import os
+import shutil
+import subprocess
+from pathlib import Path
+from typing import Optional, cast
+
+from mkosi.backend import PathString, run
+
+
+def btrfs_subvol_delete(path: Path) -> None:
+    # Extract the path of the subvolume relative to the filesystem
+    c = run(["btrfs", "subvol", "show", path],
+            stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, text=True)
+    subvol_path = c.stdout.splitlines()[0]
+    # Make the subvolume RW again if it was set RO by btrfs_subvol_delete
+    run(["btrfs", "property", "set", path, "ro", "false"])
+    # Recursively delete the direct children of the subvolume
+    c = run(["btrfs", "subvol", "list", "-o", path],
+            stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, text=True)
+    for line in c.stdout.splitlines():
+        if not line:
+            continue
+        child_subvol_path = line.split(" ", 8)[-1]
+        child_path = path / cast(str, os.path.relpath(child_subvol_path, subvol_path))
+        btrfs_subvol_delete(child_path)
+    # Delete the subvolume now that all its descendants have been deleted
+    run(["btrfs", "subvol", "delete", path],
+        stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
+
+
+def unlink_try_hard(path: Optional[PathString]) -> None:
+    if path is None:
+        return
+
+    path = Path(path)
+    try:
+        path.unlink()
+        return
+    except FileNotFoundError:
+        return
+    except Exception:
+        pass
+
+    if shutil.which("btrfs"):
+        try:
+            btrfs_subvol_delete(path)
+            return
+        except Exception:
+            pass
+
+    shutil.rmtree(path)