From: Caio Marcelo de Oliveira Filho Date: Wed, 31 Jan 2018 22:33:29 +0000 (-0800) Subject: Add "ExtraSearchPaths=" and --extra-search-paths X-Git-Tag: v4~2^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=refs%2Fpull%2F229%2Fhead;p=thirdparty%2Fmkosi.git Add "ExtraSearchPaths=" and --extra-search-paths This variable keeps a colon-delimited list of paths to be prepended to PATH in the context of mkosi execution. Unlike shell variable expansion, if any path refers to an unset variable, that path will be ignored. Besides the environment variables, the variable SUDO_HOME can be used to refer to the home directory of the user calling mkosi with sudo. This allows settings like ExtraSearchPaths=$SUDO_HOME/go/bin --- diff --git a/mkosi b/mkosi index ff6545681..7fc595301 100755 --- a/mkosi +++ b/mkosi @@ -17,6 +17,7 @@ import pathlib import platform import shutil import stat +import string import sys import tempfile import urllib.request @@ -2273,6 +2274,9 @@ class ListAction(argparse.Action): class CommaDelimitedListAction(ListAction): delimiter = "," +class ColonDelimitedListAction(ListAction): + delimiter = ":" + def parse_args(): parser = argparse.ArgumentParser(description='Build Legacy-Free OS Images', add_help=False) @@ -2338,6 +2342,9 @@ def parse_args(): group.add_argument("--bmap", action='store_true', help='Write block map file (.bmap) for bmaptool usage (only raw_gpt, raw_btrfs)') group.add_argument("--password", help='Set the root password') + group = parser.add_argument_group("Host configuration") + group.add_argument("--extra-search-paths", action=ColonDelimitedListAction, default=[], help="List of colon-separated paths to look for programs before looking in PATH") + group = parser.add_argument_group("Additional Configuration") group.add_argument('-C', "--directory", help='Change to specified directory before doing anything', metavar='PATH') group.add_argument("--default", dest='default_path', help='Read configuration data from file', metavar='PATH') @@ -2648,6 +2655,11 @@ def process_setting(args, section, key, value): return True else: return False + elif section == "Host": + if key == "ExtraSearchPaths": + list_value = value if type(value) == list else value.split() + for v in list_value: + args.extra_search_paths.extend(v.split(":")) else: return False @@ -2861,6 +2873,8 @@ def load_args(): find_passphrase(args) find_secure_boot(args) + args.extra_search_paths = expand_paths(args.extra_search_paths) + if args.cmdline and args.verb not in ('shell', 'boot', 'qemu'): die("Additional parameters only accepted for 'shell', 'boot', 'qemu' invocations.") @@ -3182,6 +3196,9 @@ def print_summary(args): sys.stderr.write(" GPG Key: " + ("default" if args.key is None else args.key) + "\n") sys.stderr.write(" Password: " + ("default" if args.password is None else "set") + "\n") + sys.stderr.write("\nHOST CONFIGURATION:\n") + sys.stderr.write(" Extra search paths: " + line_join_list(args.extra_search_paths) + "\n") + def reuse_cache_tree(args, workspace, run_build_script, for_cache, cached): """If there's a cached version of this tree around, use it and initialize our new root directly from it. Returns a boolean indicating @@ -3501,6 +3518,40 @@ def run_qemu(args): os.execvp(cmdline[0], cmdline) +def expand_paths(paths): + if not paths: + return [] + + environ = os.environ.copy() + # Add a fake SUDO_HOME variable to allow non-root users specify + # paths in their home when using mkosi via sudo. + sudo_user = os.getenv("SUDO_USER") + if sudo_user and "SUDO_HOME" not in environ: + environ["SUDO_HOME"] = os.path.expanduser("~{}".format(sudo_user)) + + # No os.path.expandvars because it treats unset variables as empty. + expanded = [] + for path in paths: + try: + path = string.Template(path).substitute(environ) + expanded.append(path) + except KeyError: + # Skip path if it uses a variable not defined. + pass + return expanded + +def prepend_to_environ_path(paths): + if not paths: + return + + original_path = os.getenv("PATH", None) + new_path = ":".join(paths) + + if original_path is None: + os.environ["PATH"] = new_path + else: + os.environ["PATH"] = new_path + ":" + original_path + def main(): args = load_args() @@ -3516,6 +3567,8 @@ def main(): if args.verb == "summary" or needs_build: print_summary(args) + prepend_to_environ_path(args.extra_search_paths) + if needs_build: check_root() init_namespace(args)