From: Joerg Behrmann Date: Tue, 29 Nov 2022 13:17:59 +0000 (+0100) Subject: Update pathlib call sites to use Python 3.9 features X-Git-Tag: v15~366^2~4 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=21ace2ad9f1ac5491cc79ffbfbb274c4af465556;p=thirdparty%2Fmkosi.git Update pathlib call sites to use Python 3.9 features These are: - the unlink method gained the parameter missing_ok= - Path objects now have a readlink method --- diff --git a/mkosi/__init__.py b/mkosi/__init__.py index 1aa14d4d2..a71d54724 100644 --- a/mkosi/__init__.py +++ b/mkosi/__init__.py @@ -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" diff --git a/mkosi/distributions/debian.py b/mkosi/distributions/debian.py index 21c28ef47..649784ab0 100644 --- a/mkosi/distributions/debian.py +++ b/mkosi/distributions/debian.py @@ -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 diff --git a/mkosi/install.py b/mkosi/install.py index eb9a74bc9..9a4e00cae 100644 --- a/mkosi/install.py +++ b/mkosi/install.py @@ -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