From: Daan De Meyer Date: Fri, 9 Aug 2024 10:15:09 +0000 (+0200) Subject: Add --wipe-build-dir to allow clearing the build directory independently X-Git-Tag: v25~363 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=acff160395dc4294abaf38f64989ac78feb95c26;p=thirdparty%2Fmkosi.git Add --wipe-build-dir to allow clearing the build directory independently Currently, to clear the build directory, -ff has to be used which also clears the image cache. Let's add --wipe-build-dir (-w) to allow clearing only the build directory without clearing the image cache. --- diff --git a/mkosi/__init__.py b/mkosi/__init__.py index a83c7d19d..9bf98afe2 100644 --- a/mkosi/__init__.py +++ b/mkosi/__init__.py @@ -4587,13 +4587,17 @@ def run_clean(args: Args, config: Config, *, resources: Path) -> None: # "--force". if args.verb == Verb.clean: - remove_build_cache = args.force > 0 + remove_output_dir = config.output_format != OutputFormat.none + remove_build_cache = args.force > 0 or args.wipe_build_dir + remove_image_cache = args.force > 0 remove_package_cache = args.force > 1 else: - remove_build_cache = args.force > 1 + remove_output_dir = config.output_format != OutputFormat.none or args.force > 0 + remove_build_cache = args.force > 1 or args.wipe_build_dir + remove_image_cache = args.force > 1 remove_package_cache = args.force > 2 - if config.output_format != OutputFormat.none or args.force: + if remove_output_dir: outputs = { config.output_dir_or_cwd() / output for output in config.outputs @@ -4613,26 +4617,22 @@ def run_clean(args: Args, config: Config, *, resources: Path) -> None: ): rmtree(*outputs) - if remove_build_cache: - if config.cache_dir: - initrd = ( - cache_tree_paths(finalize_default_initrd(args, config, resources=resources)) - if config.distribution != Distribution.custom - else [] - ) + if remove_build_cache and config.build_dir and config.build_dir.exists() and any(config.build_dir.iterdir()): + with complete_step(f"Clearing out build directory of {config.name()} image…"): + rmtree(*config.build_dir.iterdir()) - if any(p.exists() for p in itertools.chain(cache_tree_paths(config), initrd)): - with complete_step(f"Removing cache entries of {config.name()} image…"): - rmtree(*(p for p in itertools.chain(cache_tree_paths(config), initrd) if p.exists())) + if remove_image_cache and config.cache_dir: + initrd = ( + cache_tree_paths(finalize_default_initrd(args, config, resources=resources)) + if config.distribution != Distribution.custom + else [] + ) - if config.build_dir and config.build_dir.exists() and any(config.build_dir.iterdir()): - with complete_step(f"Clearing out build directory of {config.name()} image…"): - rmtree(*config.build_dir.iterdir()) + if any(p.exists() for p in itertools.chain(cache_tree_paths(config), initrd)): + with complete_step(f"Removing cache entries of {config.name()} image…"): + rmtree(*(p for p in itertools.chain(cache_tree_paths(config), initrd) if p.exists())) - if ( - remove_package_cache and - any(config.package_cache_dir_or_default().glob("*")) - ): + if remove_package_cache and any(config.package_cache_dir_or_default().glob("*")): subdir = config.distribution.package_manager(config).subdir(config) with ( @@ -4882,7 +4882,7 @@ def run_verb(args: Args, images: Sequence[Config], *, resources: Path) -> None: # First, process all directory removals because otherwise if different images share directories a later # image build could end up deleting the output generated by an earlier image build. for config in images: - if needs_clean(args, config): + if needs_clean(args, config) or args.wipe_build_dir: fork_and_wait(run_clean, args, config, resources=resources) if args.verb == Verb.clean: diff --git a/mkosi/config.py b/mkosi/config.py index 18c6a8ae5..ebc769b22 100644 --- a/mkosi/config.py +++ b/mkosi/config.py @@ -1340,6 +1340,7 @@ class Args: auto_bump: bool doc_format: DocFormat json: bool + wipe_build_dir: bool @classmethod def default(cls) -> "Args": @@ -3320,6 +3321,12 @@ def create_argument_parser(chdir: bool = True) -> argparse.ArgumentParser: action="store_true", default=False, ) + parser.add_argument( + "-w", "--wipe-build-dir", + help="Remove the build directory before building the image", + action="store_true", + default=False, + ) # These can be removed once mkosi v15 is available in LTS distros and compatibility with <= v14 # is no longer needed in build infrastructure (e.g.: OBS). parser.add_argument( diff --git a/mkosi/resources/mkosi.md b/mkosi/resources/mkosi.md index 805976fef..5820d5033 100644 --- a/mkosi/resources/mkosi.md +++ b/mkosi/resources/mkosi.md @@ -249,6 +249,9 @@ Those settings cannot be configured in the configuration files. `--json` : Show the summary output as JSON-SEQ. +`--wipe-build-dir`, `-w` +: Wipe the build directory if one is configured before building the image. + ## Supported output formats The following output formats are supported: diff --git a/tests/test_json.py b/tests/test_json.py index 783790412..bc8e7a32d 100644 --- a/tests/test_json.py +++ b/tests/test_json.py @@ -55,25 +55,27 @@ def test_args(path: Optional[Path]) -> None: "GenkeyValidDays": "100", "Json": false, "Pager": true, - "Verb": "build" + "Verb": "build", + "WipeBuildDir": true }} """ ) args = Args( - auto_bump = False, - cmdline = ["foo", "bar"], - debug = False, - debug_shell = False, - debug_workspace = False, - directory = Path(path) if path is not None else None, - doc_format = DocFormat.auto, - force = 9001, - genkey_common_name = "test", - genkey_valid_days = "100", - json = False, - pager = True, - verb = Verb.build, + auto_bump=False, + cmdline=["foo", "bar"], + debug=False, + debug_shell=False, + debug_workspace=False, + directory=Path(path) if path is not None else None, + doc_format=DocFormat.auto, + force=9001, + genkey_common_name="test", + genkey_valid_days="100", + json=False, + pager=True, + verb=Verb.build, + wipe_build_dir=True, ) assert args.to_json(indent=4, sort_keys=True) == dump.rstrip()