]> git.ipfire.org Git - thirdparty/mkosi.git/commitdiff
Implement __bool__ and __str__ for compression enum 1500/head
authorJoerg Behrmann <behrmann@physik.fu-berlin.de>
Mon, 24 Apr 2023 12:44:01 +0000 (14:44 +0200)
committerJoerg Behrmann <behrmann@physik.fu-berlin.de>
Tue, 25 Apr 2023 12:01:04 +0000 (14:01 +0200)
mkosi/__init__.py
mkosi/config.py
mkosi/util.py
tests/test_util.py

index 32db2b312a44e05936afa83ae179bda6a8e19f06..bac62c030c39d3e527ac50d769ed00c711b56506 100644 (file)
@@ -789,7 +789,7 @@ def compress_output(config: MkosiConfig, src: Path, uid: int, gid: int) -> None:
     if not src.is_file():
         return
 
-    if config.compress_output == Compression.none:
+    if not config.compress_output:
         # If we shan't compress, then at least make the output file sparse
         with complete_step(f"Digging holes into output file {src}…"):
             run(["fallocate", "--dig-holes", src], user=uid, group=gid)
@@ -1156,7 +1156,7 @@ def print_summary(config: MkosiConfig) -> None:
           Output Signature: {none_to_na(config.output_signature if config.sign else None)}
     Output nspawn Settings: {none_to_na(config.output_nspawn_settings if config.nspawn_settings is not None else None)}
                Incremental: {yes_no(config.incremental)}
-               Compression: {config.compress_output.name}
+               Compression: {config.compress_output}
                   Bootable: {config.bootable}
        Kernel Command Line: {" ".join(config.kernel_command_line)}
            UEFI SecureBoot: {yes_no(config.secure_boot)}
index f8d9fe601d56a49a92288eb8fda73be746681b84..941e94b62d118b24f445a45049a19e5c77ed964c 100644 (file)
@@ -1687,10 +1687,10 @@ class MkosiConfig:
 
     @property
     def output_compressed(self) -> Path:
-        if self.compress_output == Compression.none:
+        if not self.compress_output:
             return self.output
 
-        return self.output.parent / f"{self.output.name}.{self.compress_output.name}"
+        return self.output.parent / f"{self.output.name}.{self.compress_output}"
 
     def output_paths(self) -> tuple[Path, ...]:
         return (
@@ -1941,7 +1941,7 @@ def load_args(args: argparse.Namespace) -> MkosiConfig:
         opname = "acquire shell" if args.verb == Verb.shell else "boot"
         if args.output_format in (OutputFormat.tar, OutputFormat.cpio):
             die(f"Sorry, can't {opname} with a {args.output_format} archive.")
-        if args.compress_output != Compression.none:
+        if args.compress_output:
             die(f"Sorry, can't {opname} with a compressed image.")
 
     if args.repo_dirs and not (
index dedf5728d9df96da402221c4915467476ab307eb..144c957954fd0c261257e1b5d0ad0a1fa3c92a3e 100644 (file)
@@ -83,8 +83,8 @@ class Distribution(enum.Enum):
         return self.name
 
 
-class Compression(str, enum.Enum):
-    none = "none"
+class Compression(enum.Enum):
+    none = None
     zst = "zst"
     xz = "xz"
     bz2 = "bz2"
@@ -92,6 +92,12 @@ class Compression(str, enum.Enum):
     lz4 = "lz4"
     lzma = "lzma"
 
+    def __str__(self) -> str:
+        return str(self.value).lower()
+
+    def __bool__(self) -> bool:
+        return bool(self.value)
+
 
 def dictify(f: Callable[..., Iterator[tuple[T, V]]]) -> Callable[..., dict[T, V]]:
     def wrapper(*args: Any, **kwargs: Any) -> dict[T, V]:
index 25df36848ebee2eb0340599d4c9ca0663cf4bad7..c8da2dbbd295c27f840ba7b07618be40153ab4fe 100644 (file)
@@ -3,6 +3,7 @@
 import os
 
 from mkosi.util import (
+    Compression,
     Distribution,
     PackageType,
     set_umask,
@@ -25,3 +26,33 @@ def test_set_umask() -> None:
     assert tmp1 == 0o767
     assert tmp2 == 0o757
     assert tmp3 == 0o777
+
+
+def test_compression_enum_creation() -> None:
+    assert Compression(None) == Compression.none
+    assert Compression("zst") == Compression.zst
+    assert Compression("xz") == Compression.xz
+    assert Compression("bz2") == Compression.bz2
+    assert Compression("gz") == Compression.gz
+    assert Compression("lz4") == Compression.lz4
+    assert Compression("lzma") == Compression.lzma
+
+
+def test_compression_enum_bool() -> None:
+    assert bool(Compression.none) == False
+    assert bool(Compression.zst)  == True
+    assert bool(Compression.xz)   == True
+    assert bool(Compression.bz2)  == True
+    assert bool(Compression.gz)   == True
+    assert bool(Compression.lz4)  == True
+    assert bool(Compression.lzma) == True
+
+
+def test_compression_enum_str() -> None:
+    assert str(Compression.none) == "none"
+    assert str(Compression.zst)  == "zst"
+    assert str(Compression.xz)   == "xz"
+    assert str(Compression.bz2)  == "bz2"
+    assert str(Compression.gz)   == "gz"
+    assert str(Compression.lz4)  == "lz4"
+    assert str(Compression.lzma) == "lzma"