From: Daan De Meyer Date: Thu, 25 Jan 2024 09:35:09 +0000 (+0100) Subject: Add CompressLevel= setting X-Git-Tag: v21~85 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=cb0e5f5796ac050478011b0a6f91e52f73ba84fd;p=thirdparty%2Fmkosi.git Add CompressLevel= setting Let's allow configuring the compression level. --- diff --git a/mkosi/__init__.py b/mkosi/__init__.py index 1f30fd22f..acfd47421 100644 --- a/mkosi/__init__.py +++ b/mkosi/__init__.py @@ -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}") diff --git a/mkosi/config.py b/mkosi/config.py index bea651a8b..1647c5d5a 100644 --- a/mkosi/config.py +++ b/mkosi/config.py @@ -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)} diff --git a/mkosi/resources/mkosi.md b/mkosi/resources/mkosi.md index 07753bbd6..d1bc25451 100644 --- a/mkosi/resources/mkosi.md +++ b/mkosi/resources/mkosi.md @@ -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 diff --git a/tests/test_json.py b/tests/test_json.py index 461081e01..b6dc04a28 100644 --- a/tests/test_json.py +++ b/tests/test_json.py @@ -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",),