]> git.ipfire.org Git - thirdparty/mkosi.git/commitdiff
Create zipapp for mkosi sandbox like we do in generate-zipapp.sh
authorDaan De Meyer <daan.j.demeyer@gmail.com>
Thu, 23 Jan 2025 14:27:48 +0000 (15:27 +0100)
committerJörg Behrmann <behrmann@physik.fu-berlin.de>
Thu, 23 Jan 2025 16:11:22 +0000 (17:11 +0100)
Otherwise we run into ModuleNotFoundError trying to run a zipapp
created from a packaged version of mkosi. This is the same
workaround that's already used in generate-zipapp.sh.

mkosi/__init__.py

index 395de5b025ae1c28cba7c999833837a853639750..37a1c8232732ec94e5092b69bbd04d897f0ecac4 100644 (file)
@@ -3896,11 +3896,25 @@ def run_sandbox(args: Args, config: Config) -> None:
     with contextlib.ExitStack() as stack:
         if config.tools() != Path("/"):
             d = stack.enter_context(tempfile.TemporaryDirectory(prefix="mkosi-path-"))
-            zipapp.create_archive(
-                source=Path(__file__).parent,
-                target=Path(d) / "mkosi",
-                interpreter="/usr/bin/env python3",
-            )
+
+            # We have to point zipapp to a directory containing the mkosi module and set the entrypoint
+            # manually instead of directly at the mkosi package, otherwise we get ModuleNotFoundError when
+            # trying to run a zipapp created from a packaged version of mkosi. While zipapp.create_archive()
+            # supports a filter= argument, trying to use this within a site-packages directory is rather slow
+            # so we copy the mkosi package to a temporary directory instead which is much faster.
+            with tempfile.TemporaryDirectory(prefix="mkosi-zipapp-") as tmp:
+                copy_tree(
+                    Path(__file__).parent,
+                    Path(tmp) / Path(__file__).parent.name,
+                    sandbox=config.sandbox,
+                )
+                zipapp.create_archive(
+                    source=tmp,
+                    target=Path(d) / "mkosi",
+                    main="mkosi.__main__:main",
+                    interpreter="/usr/bin/env python3",
+                )
+
             make_executable(Path(d) / "mkosi")
             mounts += ["--ro-bind", d, "/mkosi"]
             stack.enter_context(scopedenv({"PATH": f"/mkosi:{os.environ['PATH']}"}))