From: Daan De Meyer Date: Thu, 23 Jan 2025 14:27:48 +0000 (+0100) Subject: Create zipapp for mkosi sandbox like we do in generate-zipapp.sh X-Git-Tag: v25.1~7 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4f46fb7dae749a09041935e3b180eb413a47ab11;p=thirdparty%2Fmkosi.git Create zipapp for mkosi sandbox like we do in generate-zipapp.sh 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. --- diff --git a/mkosi/__init__.py b/mkosi/__init__.py index 395de5b02..37a1c8232 100644 --- a/mkosi/__init__.py +++ b/mkosi/__init__.py @@ -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']}"}))