]> git.ipfire.org Git - thirdparty/mkosi.git/commitdiff
Update pathlib call sites to use Python 3.9 features
authorJoerg Behrmann <behrmann@physik.fu-berlin.de>
Tue, 29 Nov 2022 13:17:59 +0000 (14:17 +0100)
committerJoerg Behrmann <behrmann@physik.fu-berlin.de>
Fri, 13 Jan 2023 14:19:03 +0000 (15:19 +0100)
These are:
- the unlink method gained the parameter missing_ok=
- Path objects now have a readlink method

mkosi/__init__.py
mkosi/distributions/debian.py
mkosi/install.py

index 1aa14d4d2cb0f1819767ecef2a357c831dfd6ef8..a71d547240ed3362736ca0c805ce45e9dfcedbc4 100644 (file)
@@ -291,10 +291,7 @@ def configure_locale(root: Path, cached: bool) -> None:
 
     etc_locale = root / "etc/locale.conf"
 
-    try:
-        etc_locale.unlink()
-    except FileNotFoundError:
-        pass
+    etc_locale.unlink(missing_ok=True)
 
     # Let's ensure we use a UTF-8 locale everywhere.
     etc_locale.write_text("LANG=C.UTF-8\n")
@@ -310,10 +307,7 @@ def configure_hostname(state: MkosiState, cached: bool) -> None:
     # symlink or suchlike. Also if no hostname is configured we really
     # don't want the file to exist, so that systemd's implicit
     # hostname logic can take effect.
-    try:
-        os.unlink(etc_hostname)
-    except FileNotFoundError:
-        pass
+    etc_hostname.unlink(missing_ok=True)
 
     if state.config.hostname:
         with complete_step("Assigning hostname"):
@@ -553,10 +547,7 @@ def reset_machine_id(state: MkosiState) -> None:
     with complete_step("Resetting machine ID"):
         if not state.config.machine_id:
             machine_id = state.root / "etc/machine-id"
-            try:
-                machine_id.unlink()
-            except FileNotFoundError:
-                pass
+            machine_id.unlink(missing_ok=True)
             machine_id.write_text("uninitialized\n")
 
         dbus_machine_id = state.root / "var/lib/dbus/machine-id"
index 21c28ef478294bab4e314a5e6917a0a25c0ed28f..649784ab068f366db0d167f1195375ebf7451191 100644 (file)
@@ -42,9 +42,7 @@ class DebianInstaller(DistributionInstaller):
         if "systemd" in extra_packages and "systemd-resolved" not in extra_packages:
             # The default resolv.conf points to 127.0.0.1, and resolved is disabled, fix it in
             # the base image.
-            # TODO: use missing_ok=True when we drop Python << 3.8
-            if state.root.joinpath("etc/resolv.conf").exists():
-                state.root.joinpath("etc/resolv.conf").unlink()
+            state.root.joinpath("etc/resolv.conf").unlink(missing_ok=True)
             state.root.joinpath("etc/resolv.conf").symlink_to("../run/systemd/resolve/resolv.conf")
             run(["systemctl", "--root", state.root, "enable", "systemd-resolved"])
 
@@ -188,10 +186,7 @@ class DebianInstaller(DistributionInstaller):
 
         # Debian/Ubuntu use a different path to store the locale so let's make sure that path is a symlink to
         # etc/locale.conf.
-        try:
-            state.root.joinpath("etc/default/locale").unlink()
-        except FileNotFoundError:
-            pass
+        state.root.joinpath("etc/default/locale").unlink(missing_ok=True)
         state.root.joinpath("etc/default/locale").symlink_to("../locale.conf")
 
     @classmethod
index eb9a74bc94434b46ff2019c0d2a3b37a3175ce16..9a4e00caec93f586864ea70623b67857411b5f33 100644 (file)
@@ -86,7 +86,7 @@ def copy_file(oldpath: PathString, newpath: PathString) -> None:
     newpath = Path(newpath)
 
     if oldpath.is_symlink():
-        src = os.readlink(oldpath)  # TODO: use oldpath.readlink() with python3.9+
+        src = oldpath.readlink()
         newpath.symlink_to(src)
         return