import platform
import resource
import shutil
-import string
import subprocess
import sys
import tempfile
find_image_version(args)
- args.extra_search_paths = expand_paths(args.extra_search_paths)
-
if args.cmdline and args.verb not in MKOSI_COMMANDS_CMDLINE:
die(f"Parameters after verb are only accepted for {list_to_string(verb.name for verb in MKOSI_COMMANDS_CMDLINE)}.")
Path("mkosi.version").write_text(new_version + "\n")
-def expand_paths(paths: Sequence[str]) -> list[Path]:
- 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(f"~{sudo_user}")
-
- # No os.path.expandvars because it treats unset variables as empty.
- expanded = []
- for path in paths:
- if not sudo_user:
- path = os.path.expanduser(path)
- try:
- expanded += [Path(string.Template(str(path)).substitute(environ))]
- except KeyError:
- # Skip path if it uses a variable not defined.
- pass
- return expanded
-
-
@contextlib.contextmanager
def prepend_to_environ_path(paths: Sequence[Path]) -> Iterator[None]:
if not paths:
import enum
import fnmatch
import functools
-import os
+import os.path
import platform
import sys
import textwrap
OutputFormat,
Verb,
chdir,
+ current_user,
detect_distribution,
flatten,
)
die(f"Invalid boolean literal: {s!r}")
-def parse_path(value: str, *, required: bool, absolute: bool = True) -> Path:
+def parse_path(value: str, *, required: bool, absolute: bool = True, expanduser: bool = True, expandvars: bool = True) -> Path:
+ if expandvars:
+ value = os.path.expandvars(value)
+
path = Path(value)
+ if expanduser:
+ user = current_user()
+ if path.is_relative_to("~") and not user.is_running_user():
+ path = user.home / path.relative_to("~")
+ else:
+ path = path.expanduser()
+
if required and not path.exists():
die(f"{value} does not exist")
src, _, target = value.partition(':')
src_path = parse_path(src, required=True)
if target:
- target_path = parse_path(target, required=False, absolute=False)
+ target_path = parse_path(target, required=False, absolute=False, expanduser=False)
if not target_path.is_absolute():
die("Target path must be absolute")
else:
return config_parse_list
-def make_path_parser(*, required: bool, absolute: bool = True) -> Callable[[str], Path]:
+def make_path_parser(*, required: bool, absolute: bool = True, expanduser: bool = True, expandvars: bool = True) -> Callable[[str], Path]:
return functools.partial(
parse_path,
required=required,
absolute=absolute,
+ expanduser=expanduser,
+ expandvars=expandvars,
)
-def config_make_path_parser(*, required: bool, absolute: bool = True) -> ConfigParseCallback:
+def config_make_path_parser(*, required: bool, absolute: bool = True, expanduser: bool = True, expandvars: bool = True) -> ConfigParseCallback:
def config_parse_path(dest: str, value: Optional[str], namespace: argparse.Namespace) -> Optional[Path]:
if dest in namespace:
return getattr(namespace, dest) # type: ignore
value,
required=required,
absolute=absolute,
+ expanduser=expanduser,
+ expandvars=expandvars,
)
return None