]> git.ipfire.org Git - thirdparty/mkosi.git/commitdiff
Revert "Add our own "which" implementation"
authorDaan De Meyer <daan.j.demeyer@gmail.com>
Mon, 17 Jul 2023 16:04:12 +0000 (18:04 +0200)
committerDaan De Meyer <daan.j.demeyer@gmail.com>
Mon, 17 Jul 2023 16:04:12 +0000 (18:04 +0200)
This reverts commit d0d2044316548123eaa9fca3745d29a653107edd.

mkosi/__init__.py
mkosi/btrfs.py
mkosi/distributions/debian.py
mkosi/distributions/fedora.py
mkosi/distributions/opensuse.py
mkosi/qemu.py
mkosi/run.py

index ba3574bda6d0a8aa00261cd4c75bcb03b53ffbcd..1e33c36dd04ff31165d2d0dab9685d7668340088 100644 (file)
@@ -45,7 +45,6 @@ from mkosi.run import (
     fork_and_wait,
     run,
     spawn,
-    which,
 )
 from mkosi.state import MkosiState
 from mkosi.types import PathString
@@ -439,7 +438,7 @@ def install_boot_loader(state: MkosiState) -> None:
     if not any(gen_kernel_images(state)) and state.config.bootable == ConfigFeature.auto:
         return
 
-    if not which("bootctl", tools=state.config.tools_tree):
+    if not shutil.which("bootctl"):
         if state.config.bootable == ConfigFeature.enabled:
             die("A bootable image was requested but bootctl was not found")
         return
@@ -461,7 +460,7 @@ def install_boot_loader(state: MkosiState) -> None:
 
                 if (state.config.secure_boot_sign_tool == SecureBootSignTool.sbsign or
                     state.config.secure_boot_sign_tool == SecureBootSignTool.auto and
-                    which("sbsign", state.config.tools_tree) is not None):
+                    shutil.which("sbsign") is not None):
                     bwrap(["sbsign",
                            "--key", state.config.secure_boot_key,
                            "--cert", state.config.secure_boot_certificate,
@@ -470,7 +469,7 @@ def install_boot_loader(state: MkosiState) -> None:
                           tools=state.config.tools_tree)
                 elif (state.config.secure_boot_sign_tool == SecureBootSignTool.pesign or
                       state.config.secure_boot_sign_tool == SecureBootSignTool.auto and
-                      which("pesign", tools=state.config.tools_tree) is not None):
+                      shutil.which("pesign") is not None):
                     pesign_prepare(state)
                     bwrap(["pesign",
                            "--certdir", state.workspace / "pesign",
@@ -601,11 +600,11 @@ def install_build_dest(state: MkosiState) -> None:
         copy_path(state.install_dir, state.root, tools=state.config.tools_tree)
 
 
-def gzip_binary(config: MkosiConfig) -> str:
-    return "pigz" if which("pigz", tools=config.tools_tree) else "gzip"
+def gzip_binary() -> str:
+    return "pigz" if shutil.which("pigz") else "gzip"
 
 
-def tar_binary(config: MkosiConfig) -> str:
+def tar_binary() -> str:
     # Some distros (Mandriva) install BSD tar as "tar", hence prefer
     # "gtar" if it exists, which should be GNU tar wherever it exists.
     # We are interested in exposing same behaviour everywhere hence
@@ -613,7 +612,7 @@ def tar_binary(config: MkosiConfig) -> str:
     # everywhere. In particular given the limited/different SELinux
     # support in BSD tar and the different command line syntax
     # compared to GNU tar.
-    return "gtar" if which("gtar", tools=config.tools_tree) else "tar"
+    return "gtar" if shutil.which("gtar") else "tar"
 
 
 def make_tar(state: MkosiState) -> None:
@@ -621,7 +620,7 @@ def make_tar(state: MkosiState) -> None:
         return
 
     cmd: list[PathString] = [
-        tar_binary(state.config),
+        tar_binary(),
         "-C", state.root,
         "-c", "--xattrs",
         "--xattrs-include=*",
@@ -953,7 +952,7 @@ def install_unified_kernel(state: MkosiState, roothash: Optional[str]) -> None:
                 die(f"sd-stub not found at /{stub.relative_to(state.root)} in the image")
 
             cmd: list[PathString] = [
-                which("ukify", tools=state.config.tools_tree) or "/usr/lib/systemd/ukify",
+                shutil.which("ukify") or "/usr/lib/systemd/ukify",
                 "--cmdline", f"@{state.workspace / 'cmdline'}",
                 "--os-release", f"@{state.root / 'usr/lib/os-release'}",
                 "--stub", stub,
@@ -990,7 +989,7 @@ def install_unified_kernel(state: MkosiState, roothash: Optional[str]) -> None:
 
                 sign_expected_pcr = (state.config.sign_expected_pcr == ConfigFeature.enabled or
                                     (state.config.sign_expected_pcr == ConfigFeature.auto and
-                                     which("systemd-measure", tools=state.config.tools_tree) is not None))
+                                     shutil.which("systemd-measure") is not None))
 
                 if sign_expected_pcr:
                     cmd += [
@@ -1014,11 +1013,11 @@ def install_unified_kernel(state: MkosiState, roothash: Optional[str]) -> None:
         die("A bootable image was requested but no kernel was found")
 
 
-def compressor_command(config: MkosiConfig, compression: Compression) -> list[PathString]:
+def compressor_command(compression: Compression) -> list[PathString]:
     """Returns a command suitable for compressing archives."""
 
     if compression == Compression.gz:
-        return [gzip_binary(config), "--fast", "--stdout", "-"]
+        return [gzip_binary(), "--fast", "--stdout", "-"]
     elif compression == Compression.xz:
         return ["xz", "--check=crc32", "--fast", "-T0", "--stdout", "-"]
     elif compression == Compression.zst:
@@ -1041,7 +1040,7 @@ def maybe_compress(state: MkosiState, compression: Compression, src: Path, dst:
             src.unlink() # if src == dst, make sure dst doesn't truncate the src file but creates a new file.
 
             with dst.open("wb") as o:
-                bwrap(compressor_command(state.config, compression), stdin=i, stdout=o, tools=state.config.tools_tree)
+                bwrap(compressor_command(compression), stdin=i, stdout=o, tools=state.config.tools_tree)
                 os.chown(dst, uid=state.uid, gid=state.gid)
 
 
index 56140a738fb8f6cfe9969587bfcaee917471c2c6..5b18a2f7a34367756785aced56640c01c1cde6c4 100644 (file)
@@ -1,5 +1,6 @@
 # SPDX-License-Identifier: LGPL-2.1+
 
+import shutil
 import subprocess
 from pathlib import Path
 from typing import cast
@@ -7,7 +8,7 @@ from typing import cast
 from mkosi.config import ConfigFeature, MkosiConfig
 from mkosi.install import copy_path
 from mkosi.log import die
-from mkosi.run import bwrap, which
+from mkosi.run import bwrap
 
 
 def statfs(config: MkosiConfig, path: Path) -> str:
@@ -16,7 +17,7 @@ def statfs(config: MkosiConfig, path: Path) -> str:
 
 
 def btrfs_maybe_make_subvolume(config: MkosiConfig, path: Path, mode: int) -> None:
-    if config.use_subvolumes == ConfigFeature.enabled and not which("btrfs", tools=config.tools_tree):
+    if config.use_subvolumes == ConfigFeature.enabled and not shutil.which("btrfs"):
         die("Subvolumes requested but the btrfs command was not found")
 
     if statfs(config, path.parent) != "btrfs":
@@ -26,7 +27,7 @@ def btrfs_maybe_make_subvolume(config: MkosiConfig, path: Path, mode: int) -> No
         path.mkdir(mode)
         return
 
-    if config.use_subvolumes != ConfigFeature.disabled and which("btrfs", tools=config.tools_tree) is not None:
+    if config.use_subvolumes != ConfigFeature.disabled and shutil.which("btrfs") is not None:
         result = bwrap(["btrfs", "subvolume", "create", path],
                        check=config.use_subvolumes == ConfigFeature.enabled,
                        tools=config.tools_tree).returncode
@@ -41,9 +42,9 @@ def btrfs_maybe_make_subvolume(config: MkosiConfig, path: Path, mode: int) -> No
 
 def btrfs_maybe_snapshot_subvolume(config: MkosiConfig, src: Path, dst: Path) -> None:
     subvolume = (config.use_subvolumes == ConfigFeature.enabled or
-                 config.use_subvolumes == ConfigFeature.auto and which("btrfs", tools=config.tools_tree) is not None)
+                 config.use_subvolumes == ConfigFeature.auto and shutil.which("btrfs") is not None)
 
-    if config.use_subvolumes == ConfigFeature.enabled and not which("btrfs", tools=config.tools_tree):
+    if config.use_subvolumes == ConfigFeature.enabled and not shutil.which("btrfs"):
         die("Subvolumes requested but the btrfs command was not found")
 
     # Subvolumes always have inode 256 so we can use that to check if a directory is a subvolume.
@@ -54,7 +55,7 @@ def btrfs_maybe_snapshot_subvolume(config: MkosiConfig, src: Path, dst: Path) ->
     if dst.exists():
         dst.rmdir()
 
-    if which("btrfs", config.tools_tree):
+    if shutil.which("btrfs"):
         result = bwrap(["btrfs", "subvolume", "snapshot", src, dst],
                        check=config.use_subvolumes == ConfigFeature.enabled,
                        tools=config.tools_tree).returncode
index 5938f83bf59d32225d78923568af3fb9cbe9910a..36ede4b6497f6d27f1988e2d5e488c092b92eb0e 100644 (file)
@@ -1,5 +1,6 @@
 # SPDX-License-Identifier: LGPL-2.1+
 
+import shutil
 import tempfile
 from collections.abc import Sequence
 from pathlib import Path
@@ -8,7 +9,7 @@ from textwrap import dedent
 from mkosi.architecture import Architecture
 from mkosi.distributions import DistributionInstaller
 from mkosi.log import die
-from mkosi.run import bwrap, which
+from mkosi.run import bwrap
 from mkosi.state import MkosiState
 from mkosi.types import CompletedProcess, PathString
 
@@ -231,7 +232,7 @@ def invoke_apt(
         "-o", f"Dir::Etc::trusted={trustedkeys}",
         "-o", f"Dir::Etc::trustedparts={trustedkeys_dir}",
         "-o", f"Dir::Log={state.pkgmngr / 'var/log/apt'}",
-        "-o", f"Dir::Bin::dpkg={which('dpkg', tools=state.config.tools_tree)}",
+        "-o", f"Dir::Bin::dpkg={shutil.which('dpkg')}",
         "-o", "Debug::NoLocking=true",
         "-o", f"DPkg::Options::=--root={state.root}",
         "-o", f"DPkg::Options::=--log={state.pkgmngr / 'var/log/apt/dpkg.log'}",
index caa76e82bbc6dbf0b9a6fd41bcf042ad21611000..1b2cdea780853d1dbcf93ea60e81e6eac62f8329 100644 (file)
@@ -12,7 +12,7 @@ from mkosi.architecture import Architecture
 from mkosi.distributions import DistributionInstaller
 from mkosi.log import die
 from mkosi.remove import unlink_try_hard
-from mkosi.run import bwrap, which
+from mkosi.run import bwrap
 from mkosi.state import MkosiState
 from mkosi.util import Distribution, detect_distribution, sort_packages
 
@@ -173,8 +173,8 @@ def invoke_dnf(
     state.pkgmngr.joinpath("var/lib/dnf").mkdir(exist_ok=True, parents=True)
 
     # dnf5 does not support building for foreign architectures yet (missing --forcearch)
-    dnf = which("dnf5", tools=state.config.tools_tree) if state.config.architecture.is_native() else None
-    dnf = dnf or which("dnf", tools=state.config.tools_tree) or "yum"
+    dnf = shutil.which("dnf5") if state.config.architecture.is_native() else None
+    dnf = dnf or shutil.which("dnf") or "yum"
 
     cmdline = [
         dnf,
index e0c7ecd770aad44cdb809f8572caaf504d657ab2..6b4ff8a82d462a28e08a79feac9519b8d7499ef7 100644 (file)
@@ -1,5 +1,6 @@
 # SPDX-License-Identifier: LGPL-2.1+
 
+import shutil
 import textwrap
 import urllib.request
 import xml.etree.ElementTree as ElementTree
@@ -9,7 +10,7 @@ from mkosi.architecture import Architecture
 from mkosi.distributions import DistributionInstaller
 from mkosi.distributions.fedora import Repo, fixup_rpmdb_location, invoke_dnf, setup_dnf
 from mkosi.log import die
-from mkosi.run import bwrap, which
+from mkosi.run import bwrap
 from mkosi.state import MkosiState
 
 
@@ -43,7 +44,7 @@ class OpensuseInstaller(DistributionInstaller):
             release_url = f"{state.config.mirror}/distribution/leap/{release}/repo/oss/"
             updates_url = f"{state.config.mirror}/update/leap/{release}/oss/"
 
-        zypper = which("zypper", tools=state.config.tools_tree)
+        zypper = shutil.which("zypper")
 
         # If we need to use a local mirror, create a temporary repository definition
         # that doesn't get in the image, as it is valid only at image build time.
@@ -63,7 +64,7 @@ class OpensuseInstaller(DistributionInstaller):
 
     @classmethod
     def remove_packages(cls, state: MkosiState, packages: Sequence[str]) -> None:
-        if which("zypper", tools=state.config.tools_tree):
+        if shutil.which("zypper"):
             invoke_zypper(state, "remove", packages, ["--clean-deps"])
         else:
             invoke_dnf(state, "remove", packages)
index b3900b2c4dcf1e08c2c3be351ac5ff4a7bfed2ce..0780f19ecb5e59c2416317d73d6b5be511d8afe1 100644 (file)
@@ -20,7 +20,7 @@ from mkosi.btrfs import btrfs_maybe_snapshot_subvolume
 from mkosi.config import ConfigFeature, MkosiArgs, MkosiConfig
 from mkosi.log import die
 from mkosi.remove import unlink_try_hard
-from mkosi.run import MkosiAsyncioThread, bwrap, bwrap_cmd, spawn, which
+from mkosi.run import MkosiAsyncioThread, bwrap, bwrap_cmd, spawn
 from mkosi.types import PathString
 from mkosi.util import (
     Distribution,
@@ -41,7 +41,7 @@ def machine_cid(config: MkosiConfig) -> int:
 def find_qemu_binary(config: MkosiConfig) -> str:
     binaries = ["qemu", "qemu-kvm", f"qemu-system-{config.architecture.to_qemu()}"]
     for binary in binaries:
-        if which(binary, tools=config.tools_tree) is not None:
+        if shutil.which(binary) is not None:
             return binary
 
     die("Couldn't find QEMU/KVM binary")
@@ -294,7 +294,7 @@ def run_qemu(args: MkosiArgs, config: MkosiConfig) -> None:
                         "-device", "virtio-scsi-pci,id=scsi",
                         "-device", "scsi-hd,drive=hd,bootindex=1"]
 
-        if config.qemu_swtpm != ConfigFeature.disabled and which("swtpm", tools=config.tools_tree) is not None:
+        if config.qemu_swtpm != ConfigFeature.disabled and shutil.which("swtpm") is not None:
             sock = stack.enter_context(start_swtpm(config))
             cmdline += ["-chardev", f"socket,id=chrtpm,path={sock}",
                         "-tpmdev", "emulator,id=tpm0,chardev=chrtpm"]
index 0287450beaec3ab94e8b05d760427250008d1808..2a50f19c98cc3e75b945ac35c8d25310d7dc1d48 100644 (file)
@@ -11,7 +11,6 @@ import os
 import pwd
 import queue
 import shlex
-import shutil
 import signal
 import subprocess
 import sys
@@ -487,10 +486,6 @@ def chroot_cmd(root: Path, *, options: Sequence[PathString] = (), network: bool
     return cmdline
 
 
-def which(program: str, tools: Optional[Path]) -> Optional[str]:
-    return shutil.which(program, path=f"{tools}/usr/bin:{tools}/usr/sbin" if tools else None)
-
-
 class MkosiAsyncioThread(threading.Thread):
     """
     The default threading.Thread() is not interruptable, so we make our own version by using the concurrency