]> git.ipfire.org Git - thirdparty/mkosi.git/commitdiff
Add --wipe-build-dir to allow clearing the build directory independently
authorDaan De Meyer <daan.j.demeyer@gmail.com>
Fri, 9 Aug 2024 10:15:09 +0000 (12:15 +0200)
committerJörg Behrmann <behrmann@physik.fu-berlin.de>
Fri, 9 Aug 2024 11:10:30 +0000 (13:10 +0200)
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.

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

index a83c7d19d1b077d03f8c34391b804bdfb0289fdc..9bf98afe2195738d1ac3c437dddb5cfa0190d5f6 100644 (file)
@@ -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:
index 18c6a8ae5af398a528fa3d91aad96486df725aaf..ebc769b22d2b81459494e27fcd7c447a6ddce37f 100644 (file)
@@ -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(
index 805976fefaa853a32e88c76729974485c1de2995..5820d503324eacc6af6103d985a54d540ecdada2 100644 (file)
@@ -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:
index 783790412cb37a9d12fa6a3d4cdf119d6f66b55a..bc8e7a32df91fe7af07bbdb2af60edaf6441b26b 100644 (file)
@@ -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()