]> git.ipfire.org Git - thirdparty/mkosi.git/commitdiff
Fix ruff warnings
authorJoerg Behrmann <behrmann@physik.fu-berlin.de>
Thu, 21 Sep 2023 10:54:51 +0000 (12:54 +0200)
committerJoerg Behrmann <behrmann@physik.fu-berlin.de>
Wed, 4 Oct 2023 07:45:00 +0000 (09:45 +0200)
- reformat overly long lines
- remove typing.Type in favour of type
- import Iterablefrom collections.abc instead of typing
- compare singletons with is
- don't use "ambiguous variable name: l"

mkosi/__init__.py
mkosi/config.py
mkosi/distributions/centos.py
mkosi/distributions/rhel_ubi.py
mkosi/qemu.py
mkosi/state.py
tests/test_config.py

index 03ec0f65abd325efa9c261d4e4785b010596b1fb..9beab92b21e69de1d71fa5a1eddfabd6d6608cec 100644 (file)
@@ -182,8 +182,13 @@ def check_root_populated(state: MkosiState) -> None:
     """Check that the root was populated by looking for a os-release file."""
     osrelease = state.root / "usr/lib/os-release"
     if not osrelease.exists():
-        die(f"{osrelease} not found.",
-            hint="The root must be populated by the distribution, or from base trees, skeleton trees, and prepare scripts.")
+        die(
+            f"{osrelease} not found.",
+            hint=(
+                "The root must be populated by the distribution, or from base trees, "
+                "skeleton trees, and prepare scripts."
+            )
+        )
 
 
 def configure_os_release(state: MkosiState) -> None:
@@ -237,7 +242,7 @@ def configure_autologin(state: MkosiState) -> None:
                 ExecStart=-/sbin/agetty -o '-f -p -- \\\\u' --autologin root --noclear --keep-baud console 115200,38400,9600 $TERM
                 StandardInput=tty
                 StandardOutput=tty
-                """
+                """  # noqa: E501
             )
 
         dropin = state.root / "usr/lib/systemd/system/getty@tty1.service.d/autologin.conf"
@@ -265,7 +270,7 @@ def configure_autologin(state: MkosiState) -> None:
                 ExecStart=-/sbin/agetty -o '-f -p -- \\\\u' --autologin root --keep-baud 115200,57600,38400,9600 - $TERM
                 StandardInput=tty
                 StandardOutput=tty
-                """
+                """  # noqa: E501
             )
 
 
@@ -816,7 +821,9 @@ def prepare_grub_bios(state: MkosiState, partitions: Sequence[Partition]) -> Non
                 distribution = state.config.distribution
                 image = Path("/") / kimg.relative_to(state.root / "efi")
                 cmdline = " ".join(state.config.kernel_command_line)
-                initrds = " ".join([os.fspath(Path("/") / initrd.relative_to(state.root / "efi")) for initrd in initrds])
+                initrds = " ".join(
+                    [os.fspath(Path("/") / initrd.relative_to(state.root / "efi")) for initrd in initrds]
+                )
                 kmods = Path("/") / kmods.relative_to(state.root / "efi")
 
                 f.write(
@@ -1582,7 +1589,11 @@ def configure_ssh(state: MkosiState) -> None:
 
 
 def configure_initrd(state: MkosiState) -> None:
-    if not (state.root / "init").exists() and not (state.root / "init").is_symlink() and (state.root / "usr/lib/systemd/systemd").exists():
+    if (
+        not (state.root / "init").exists() and
+        not (state.root / "init").is_symlink() and
+        (state.root / "usr/lib/systemd/systemd").exists()
+    ):
         (state.root / "init").symlink_to("/usr/lib/systemd/systemd")
 
     if not state.config.make_initrd:
index 736150fc8e8c629f5c99629a74552c8077fc7275..37678fade6a189028523ede0e5825eb1bdbf9fad 100644 (file)
@@ -22,7 +22,7 @@ import textwrap
 import uuid
 from collections.abc import Collection, Iterable, Iterator, Sequence
 from pathlib import Path
-from typing import Any, Callable, Optional, Type, Union, cast
+from typing import Any, Callable, Optional, TypeVar, Union, cast
 
 from mkosi.architecture import Architecture
 from mkosi.distributions import Distribution, detect_distribution
@@ -294,7 +294,10 @@ def config_default_distribution(namespace: argparse.Namespace) -> Distribution:
     detected = detect_distribution()[0]
 
     if not detected:
-        logging.info("Distribution of your host can't be detected or isn't a supported target. Defaulting to Distribution=none.")
+        logging.info(
+            "Distribution of your host can't be detected or isn't a supported target. "
+            "Defaulting to Distribution=none."
+        )
         return Distribution.none
 
     return detected
@@ -471,7 +474,10 @@ def config_parse_filename(value: Optional[str], old: Optional[str]) -> Optional[
         die(". and .. are not valid filenames")
 
     if "/" in value:
-        die(f"{value!r} is not a valid filename. (Output= requires a filename with no path components, relative to output directory.)")
+        die(
+            f"{value!r} is not a valid filename. "
+            "(Output= requires a filename with no path components, relative to output directory.)"
+        )
 
     return value
 
@@ -1782,7 +1788,7 @@ MATCHES = (
 )
 
 
-def create_argument_parser(action: Type[argparse.Action]) -> argparse.ArgumentParser:
+def create_argument_parser(action: type[argparse.Action]) -> argparse.ArgumentParser:
     parser = argparse.ArgumentParser(
         prog="mkosi",
         description="Build Bespoke OS Images",
@@ -1978,13 +1984,13 @@ def parse_config(argv: Sequence[str] = ()) -> tuple[MkosiArgs, tuple[MkosiConfig
         namespace: argparse.Namespace,
         defaults: argparse.Namespace,
     ) -> Iterator[None]:
-        l = len(getattr(namespace, "include", []))
+        current_num_of_includes = len(getattr(namespace, "include", []))
 
         try:
             yield
         finally:
             # Parse any includes that were added after yielding.
-            for p in getattr(namespace, "include", [])[l:]:
+            for p in getattr(namespace, "include", [])[current_num_of_includes:]:
                 st = p.stat()
 
                 if (st.st_dev, st.st_ino) in parsed_includes:
index 59665cbba3554bce4867eb39b8c7aa470f81997b..181edba444a8112c4ecf0c00db26ccb9e96ce248 100644 (file)
@@ -3,9 +3,8 @@
 import os
 import shutil
 import urllib.parse
-from collections.abc import Sequence
+from collections.abc import Iterable, Sequence
 from pathlib import Path
-from typing import Iterable
 
 from mkosi.architecture import Architecture
 from mkosi.config import MkosiConfig
index 4751fe24445ed6b5418d57881c969ee64510e2a8..eb1884f7de88aaf9c511b0ee3b5609aeabf1a035 100644 (file)
@@ -1,6 +1,6 @@
 # SPDX-License-Identifier: LGPL-2.1+
 
-from typing import Iterable
+from collections.abc import Iterable
 
 from mkosi.config import MkosiConfig
 from mkosi.distributions import centos
index 7b14d30ade5421d6d88aa3789b79ce968f2e01a5..7f7646d5faf37f46274b9448f53f340af47118be 100644 (file)
@@ -362,8 +362,9 @@ def run_qemu(args: MkosiArgs, config: MkosiConfig, uid: int, gid: int) -> None:
 
         if config.architecture.supports_smbios():
             for k, v in config.credentials.items():
+                payload = base64.b64encode(v.encode()).decode()
                 cmdline += [
-                    "-smbios", f"type=11,value=io.systemd.credential.binary:{k}={base64.b64encode(v.encode()).decode()}"
+                    "-smbios", f"type=11,value=io.systemd.credential.binary:{k}={payload}"
                 ]
 
             cmdline += [
@@ -409,7 +410,10 @@ def run_qemu(args: MkosiArgs, config: MkosiConfig, uid: int, gid: int) -> None:
                  "--offline=yes",
                  fname])
 
-        if firmware == QemuFirmware.linux or config.output_format in (OutputFormat.cpio, OutputFormat.uki, OutputFormat.directory):
+        if (
+            firmware == QemuFirmware.linux or
+            config.output_format in (OutputFormat.cpio, OutputFormat.uki, OutputFormat.directory)
+        ):
             if config.output_format == OutputFormat.uki:
                 kernel = fname if firmware == QemuFirmware.uefi else config.output_dir / config.output_split_kernel
             elif config.qemu_kernel:
index e46bd4c9081d1246f87fd626b6bd3d92163adcfc..01b41e1269e2a5da66df8fc8515ec277f74f05cc 100644 (file)
@@ -45,7 +45,10 @@ class MkosiState:
 
     @property
     def cache_dir(self) -> Path:
-        return self.config.cache_dir or self.workspace / f"cache/{self.config.distribution}~{self.config.release}~{self.config.architecture}"
+        return (
+            self.config.cache_dir or
+            self.workspace / f"cache/{self.config.distribution}~{self.config.release}~{self.config.architecture}"
+        )
 
     @property
     def install_dir(self) -> Path:
index acd1afb8e9189ebf8f82c453c1889893d173c3fd..dfabdada3f923f87ff2174cc04cb42d6ab780f22 100644 (file)
@@ -200,19 +200,19 @@ def test_parse_config(tmp_path: Path) -> None:
     with chdir(tmp_path):
         _, [config] = parse_config()
         assert config.bootable == ConfigFeature.auto
-        assert config.split_artifacts == False
+        assert config.split_artifacts is False
 
         # Passing the directory should include both the main config file and the dropin.
         _, [config] = parse_config(["--include", os.fspath(tmp_path / "abc")] * 2)
         assert config.bootable == ConfigFeature.enabled
-        assert config.split_artifacts == True
+        assert config.split_artifacts is True
         # The same extra config should not be parsed more than once.
         assert config.build_packages == ["abc"]
 
         # Passing the main config file should not include the dropin.
         _, [config] = parse_config(["--include", os.fspath(tmp_path / "abc/mkosi.conf")])
         assert config.bootable == ConfigFeature.enabled
-        assert config.split_artifacts == False
+        assert config.split_artifacts is False
 
 
 def test_parse_load_verb(tmp_path: Path) -> None: