]> git.ipfire.org Git - thirdparty/mkosi.git/commitdiff
Allow single files to be used with --extra-search-paths
authorDaan De Meyer <daan.j.demeyer@gmail.com>
Wed, 21 Sep 2022 07:25:26 +0000 (09:25 +0200)
committerDaan De Meyer <daan.j.demeyer@gmail.com>
Wed, 21 Sep 2022 08:15:12 +0000 (10:15 +0200)
Sometimes we just want to use one file from a directory of files
with --extra-search-paths. Let's make sure we support this by putting
a symlink to the file in a temporary directory and adding that directory
to PATH.

mkosi/__init__.py

index 3e6fcc289f675627f7ea67f5b3a9eae310f2308f..dbf81e46c27a274c2a858b7183b638d14e6e94e6 100644 (file)
@@ -5562,6 +5562,7 @@ def create_parser() -> ArgumentParserMkosi:
         dest="extra_search_paths",
         action=ColonDelimitedListAction,
         default=[],
+        type=Path,
         help="List of colon-separated paths to look for programs before looking in PATH",
     )
     group.add_argument(
@@ -7987,7 +7988,7 @@ def bump_image_version(config: MkosiConfig) -> None:
     open("mkosi.version", "w").write(new_version + "\n")
 
 
-def expand_paths(paths: List[str]) -> List[str]:
+def expand_paths(paths: Sequence[str]) -> List[Path]:
     if not paths:
         return []
 
@@ -8002,20 +8003,32 @@ def expand_paths(paths: List[str]) -> List[str]:
     expanded = []
     for path in paths:
         try:
-            expanded += [string.Template(path).substitute(environ)]
+            expanded += [Path(string.Template(path).substitute(environ))]
         except KeyError:
             # Skip path if it uses a variable not defined.
             pass
     return expanded
 
 
-def prepend_to_environ_path(paths: List[Path]) -> None:
+@contextlib.contextmanager
+def prepend_to_environ_path(paths: Sequence[Path]) -> Iterator[None]:
     if not paths:
+        yield
         return
 
-    news = [os.fspath(path) for path in paths]
-    olds = os.getenv("PATH", "").split(":")
-    os.environ["PATH"] = ":".join(news + olds)
+    with tempfile.TemporaryDirectory(prefix="mkosi.path", dir=tmp_dir()) as d:
+
+        for path in paths:
+            if not path.is_dir():
+                Path(d).joinpath(path.name).symlink_to(path.absolute())
+
+        paths = [Path(d), *paths]
+
+        news = [os.fspath(path) for path in paths if path.is_dir()]
+        olds = os.getenv("PATH", "").split(":")
+        os.environ["PATH"] = ":".join(news + olds)
+
+        yield
 
 
 def expand_specifier(s: str) -> str:
@@ -8031,48 +8044,47 @@ def needs_build(config: Union[argparse.Namespace, MkosiConfig]) -> bool:
 def run_verb(raw: argparse.Namespace) -> None:
     config: MkosiConfig = load_args(raw)
 
-    prepend_to_environ_path(config.extra_search_paths)
+    with prepend_to_environ_path(config.extra_search_paths):
+        if config.verb == Verb.genkey:
+            generate_secure_boot_key(config)
 
-    if config.verb == Verb.genkey:
-        generate_secure_boot_key(config)
-
-    if config.verb == Verb.bump:
-        bump_image_version(config)
+        if config.verb == Verb.bump:
+            bump_image_version(config)
 
-    if config.verb in MKOSI_COMMANDS_SUDO:
-        check_root()
+        if config.verb in MKOSI_COMMANDS_SUDO:
+            check_root()
 
-    if config.verb == Verb.build and not config.force:
-        check_output(config)
+        if config.verb == Verb.build and not config.force:
+            check_output(config)
 
-    if needs_build(config) or config.verb == Verb.clean:
-        check_root()
-        unlink_output(config)
+        if needs_build(config) or config.verb == Verb.clean:
+            check_root()
+            unlink_output(config)
 
-    if config.verb == Verb.summary:
-        print_summary(config)
+        if config.verb == Verb.summary:
+            print_summary(config)
 
-    if needs_build(config):
-        check_native(config)
-        init_namespace()
-        manifest = build_stuff(config)
+        if needs_build(config):
+            check_native(config)
+            init_namespace()
+            manifest = build_stuff(config)
 
-        if config.auto_bump:
-            bump_image_version(config)
+            if config.auto_bump:
+                bump_image_version(config)
 
-        save_manifest(config, manifest)
+            save_manifest(config, manifest)
 
-        print_output_size(config)
+            print_output_size(config)
 
-    with suppress_stacktrace():
-        if config.verb in (Verb.shell, Verb.boot):
-            run_shell(config)
+        with suppress_stacktrace():
+            if config.verb in (Verb.shell, Verb.boot):
+                run_shell(config)
 
-        if config.verb == Verb.qemu:
-            run_qemu(config)
+            if config.verb == Verb.qemu:
+                run_qemu(config)
 
-        if config.verb == Verb.ssh:
-            run_ssh(config)
+            if config.verb == Verb.ssh:
+                run_ssh(config)
 
-    if config.verb == Verb.serve:
-        run_serve(config)
+        if config.verb == Verb.serve:
+            run_serve(config)