]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
ukify: prefer compression.zstd when available
authorLukáš Zaoral <lzaoral@redhat.com>
Mon, 8 Dec 2025 14:42:40 +0000 (15:42 +0100)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Tue, 9 Dec 2025 05:37:22 +0000 (14:37 +0900)
Python 3.14 introduced support for zstd in the standard library [1].  Therefore,
let's prefer it when available to decrease the number of necessary  external
dependencies.

[1] https://docs.python.org/3/whatsnew/3.14.html#whatsnew314-zstandard

src/ukify/ukify.py

index fce36b66860435ecbd1d3f3f0e836262aa289af7..ad6560534accc79e86950dae1fea96b4d3565426 100755 (executable)
@@ -198,9 +198,14 @@ def get_zboot_kernel(f: IO[bytes]) -> bytes:
     elif comp_type.startswith(b'xzkern'):
         raise NotImplementedError('xzkern decompression not implemented')
     elif comp_type.startswith(b'zstd'):
-        zstd = try_import('zstandard')
-        data = f.read(size)
-        return cast(bytes, zstd.ZstdDecompressor().stream_reader(data).read())
+        try:
+            zstd = try_import('compression.zstd')
+            data = f.read(size)
+            return cast(bytes, zstd.zstd.ZstdDecompressor().decompress(data))
+        except ValueError:
+            zstd = try_import('zstandard')
+            data = f.read(size)
+            return cast(bytes, zstd.ZstdDecompressor().stream_reader(data).read())
 
     raise NotImplementedError(f'unknown compressed type: {comp_type!r}')
 
@@ -230,8 +235,12 @@ def maybe_decompress(filename: Union[str, Path]) -> bytes:
         return cast(bytes, gzip.open(f).read())
 
     if start.startswith(b'\x28\xb5\x2f\xfd'):
-        zstd = try_import('zstandard')
-        return cast(bytes, zstd.ZstdDecompressor().stream_reader(f.read()).read())
+        try:
+            zstd = try_import('compression.zstd')
+            return cast(bytes, zstd.zstd.ZstdDecompressor().decompress(f.read()))
+        except ValueError:
+            zstd = try_import('zstandard')
+            return cast(bytes, zstd.ZstdDecompressor().stream_reader(f.read()).read())
 
     if start.startswith(b'\x02\x21\x4c\x18'):
         lz4 = try_import('lz4.frame', 'lz4')