]> git.ipfire.org Git - thirdparty/mkosi.git/commitdiff
Add CompressLevel= setting
authorDaan De Meyer <daan.j.demeyer@gmail.com>
Thu, 25 Jan 2024 09:35:09 +0000 (10:35 +0100)
committerJörg Behrmann <behrmann@physik.fu-berlin.de>
Thu, 25 Jan 2024 10:52:54 +0000 (11:52 +0100)
Let's allow configuring the compression level.

mkosi/__init__.py
mkosi/config.py
mkosi/resources/mkosi.md
tests/test_json.py

index 1f30fd22fc3bf68bdfbe96d3e8faf25900da1fd9..acfd47421dc261164dd88fbcbc49275c83200fd4 100644 (file)
@@ -1485,6 +1485,7 @@ def build_initrd(context: Context) -> Path:
         # Note that when compress_output == Compression.none == 0 we don't pass --compress-output which means the
         # default compression will get picked. This is exactly what we want so that initrds are always compressed.
         *(["--compress-output", str(context.config.compress_output)] if context.config.compress_output else []),
+        "--compress-level", str(context.config.compress_level),
         "--with-network", str(context.config.with_network),
         "--cache-only", str(context.config.cache_only),
         "--output-dir", str(context.workspace / "initrd"),
@@ -1958,11 +1959,11 @@ def compressor_command(context: Context, compression: Compression) -> list[PathS
     """Returns a command suitable for compressing archives."""
 
     if compression == Compression.gz:
-        return [gzip_binary(context), "--fast", "--stdout", "-"]
+        return [gzip_binary(context), f"-{context.config.compress_level}" "--stdout", "-"]
     elif compression == Compression.xz:
-        return ["xz", "--check=crc32", "--fast", "-T0", "--stdout", "-"]
+        return ["xz", "--check=crc32", f"-{context.config.compress_level}", "-T0", "--stdout", "-"]
     elif compression == Compression.zstd:
-        return ["zstd", "-q", "-T0", "--stdout", "-"]
+        return ["zstd", "-q", f"-{context.config.compress_level}", "-T0", "--stdout", "-"]
     else:
         die(f"Unknown compression {compression}")
 
index bea651a8b6156a4fbd6ecb845ef7dd7af2fbbeeb..1647c5d5a6b980c9700210a89bd03f5bbf23c90d 100644 (file)
@@ -545,12 +545,29 @@ def config_parse_source_date_epoch(value: Optional[str], old: Optional[int]) ->
     try:
         timestamp = int(value)
     except ValueError:
-        raise ValueError(f"{value} is not a valid timestamp")
+        die(f"{value} is not a valid timestamp")
+
     if timestamp < 0:
-        raise ValueError(f"{value} is negative")
+        die(f"Source date epoch timestamp cannot be negative (got {value})")
+
     return timestamp
 
 
+def config_parse_compress_level(value: Optional[str], old: Optional[int]) -> Optional[int]:
+    if not value:
+        return None
+
+    try:
+        level = int(value)
+    except ValueError:
+        die(f"{value} is not a valid compression level")
+
+    if level < 0:
+        die(f"Compression level cannot be negative (got {value})")
+
+    return level
+
+
 def config_default_compression(namespace: argparse.Namespace) -> Compression:
     if namespace.output_format in (OutputFormat.tar, OutputFormat.cpio, OutputFormat.uki, OutputFormat.esp):
         if namespace.distribution.is_centos_variant() and int(namespace.release) <= 8:
@@ -1150,6 +1167,7 @@ class Config:
     manifest_format: list[ManifestFormat]
     output: str
     compress_output: Compression
+    compress_level: int
     output_dir: Optional[Path]
     workspace_dir: Optional[Path]
     cache_dir: Optional[Path]
@@ -1685,6 +1703,14 @@ SETTINGS = (
         default_factory_depends=("distribution", "release", "output_format"),
         help="Enable whole-output compression (with images or archives)",
     ),
+    ConfigSetting(
+        dest="compress_level",
+        metavar="LEVEL",
+        section="Output",
+        parse=config_parse_compress_level,
+        default=3,
+        help="Set the compression level to use",
+    ),
     ConfigSetting(
         dest="output_dir",
         short="-O",
@@ -3403,6 +3429,7 @@ def summary(config: Config) -> str:
                    Manifest Formats: {maniformats}
                              Output: {bold(config.output_with_compression)}
                         Compression: {config.compress_output}
+                  Compression Level: {config.compress_level}
                    Output Directory: {config.output_dir_or_cwd()}
                 Workspace Directory: {config.workspace_dir_or_default()}
                     Cache Directory: {none_to_none(config.cache_dir)}
index 07753bbd65670415d2152524fe869ae21d368db8..d1bc254513105f429b079a8167de31ab3595d60c 100644 (file)
@@ -669,6 +669,11 @@ boolean argument: either `1`, `yes`, or `true` to enable, or `0`, `no`,
   are not available when this option is used. Implied for `tar`, `cpio`, `uki`,
   and `esp`.
 
+`CompressLevel=`, `--compress-level=`
+
+: Configure the compression level to use. Takes an integer. The possible
+  values depend on the compression being used.
+
 `OutputDirectory=`, `--output-dir=`, `-O`
 
 : Path to a directory where to place all generated artifacts. If this is
index 461081e011c6e804471fb9595533909e191dcd0e..b6dc04a287fcb389017aa07b3d047c7ce5134dad 100644 (file)
@@ -108,6 +108,7 @@ def test_config() -> None:
             "CacheOnly": true,
             "Checksum": false,
             "CleanPackageMetadata": "auto",
+            "CompressLevel": 3,
             "CompressOutput": "bz2",
             "Credentials": {
                 "credkey": "credval"
@@ -310,6 +311,7 @@ def test_config() -> None:
         cache_only =  True,
         checksum =  False,
         clean_package_metadata = ConfigFeature.auto,
+        compress_level = 3,
         compress_output = Compression.bz2,
         credentials =  {"credkey": "credval"},
         dependencies = ("dep1",),