Skip to content

Commit

Permalink
Simplify --remove-files
Browse files Browse the repository at this point in the history
  • Loading branch information
DaanDeMeyer committed Feb 24, 2023
1 parent e46bffa commit 657eb17
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 24 deletions.
26 changes: 3 additions & 23 deletions mkosi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import dataclasses
import datetime
import errno
import glob
import hashlib
import http.server
import itertools
Expand Down Expand Up @@ -358,10 +357,9 @@ def remove_files(state: MkosiState) -> None:
return

with complete_step("Removing files…"):
# Note: Path('/foo') / '/bar' == '/bar'. We need to strip the slash.
# https://bugs.python.org/issue44452
paths = [state.root / str(p).lstrip("/") for p in state.config.remove_files]
remove_glob(*paths)
for pattern in state.config.remove_files:
for p in state.root.glob(pattern.lstrip("/")):
unlink_try_hard(p)


def install_distribution(state: MkosiState, cached: bool) -> None:
Expand Down Expand Up @@ -1375,16 +1373,6 @@ def parse_base_packages(value: str) -> Union[str, bool]:
return parse_boolean(value)


def parse_remove_files(value: str) -> list[str]:
"""Normalize paths as relative to / to ensure we don't go outside of our root."""

# os.path.normpath() leaves leading '//' untouched, even though it normalizes '///'.
# This follows POSIX specification, see
# https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap04.html#tag_04_13.
# Let's use lstrip() to handle zero or more leading slashes correctly.
return ["/" + os.path.normpath(p).lstrip("/") for p in value.split(",") if p]


def parse_ssh_agent(value: str) -> Optional[Path]:
"""Will return None or a path to a socket."""

Expand Down Expand Up @@ -1721,7 +1709,6 @@ def create_parser() -> ArgumentParserMkosi:
action=CommaDelimitedListAction,
default=[],
help="Remove files from built image",
type=parse_remove_files,
metavar="GLOB",
)
group.add_argument(
Expand Down Expand Up @@ -2113,13 +2100,6 @@ def parse_args_file_group(
return create_parser().parse_args(config_files + argv)


def remove_glob(*patterns: Path) -> None:
pathgen = (glob.glob(str(pattern)) for pattern in patterns)
paths: set[str] = set(sum(pathgen, [])) # uniquify
for path in paths:
unlink_try_hard(Path(path))


def empty_directory(path: Path) -> None:
try:
for f in os.listdir(path):
Expand Down
2 changes: 1 addition & 1 deletion mkosi/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ class MkosiConfig:
extra_trees: list[Path]
skeleton_trees: list[Path]
clean_package_metadata: Union[bool, str]
remove_files: list[Path]
remove_files: list[str]
environment: dict[str, str]
build_sources: Path
build_dir: Optional[Path]
Expand Down

0 comments on commit 657eb17

Please sign in to comment.