Skip to content

Commit

Permalink
xbstrap: add discard command for nuking packages
Browse files Browse the repository at this point in the history
  • Loading branch information
no92 committed Jan 23, 2023
1 parent 6d0f5f6 commit 65e2523
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
26 changes: 26 additions & 0 deletions xbstrap/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -915,6 +915,30 @@ def resolve_host_paths(x):
# ----------------------------------------------------------------------------------------


def do_discard(args):
cfg = config_for_args(args)
sel = select_pkgs(cfg, args)
plan = xbstrap.base.Plan(cfg)
handle_plan_args(cfg, plan, args)
if args.src:
plan.wanted.update(
[(xbstrap.base.Action.DISCARD_SRC, cfg.get_source(pkg.source)) for pkg in sel]
)
plan.wanted.update([(xbstrap.base.Action.DISCARD_PKG, pkg) for pkg in sel])
plan.run_plan()


do_discard.parser = main_subparsers.add_parser(
"discard",
parents=[handle_plan_args.parser, select_pkgs.parser],
description="Discard a package from your tree",
)
do_discard.parser.add_argument("--src", action="store_true")


# ----------------------------------------------------------------------------------------


def do_execute_manifest(args):
if args.c is not None:
manifest = yaml.load(args.c, Loader=xbstrap.base.global_yaml_loader)
Expand Down Expand Up @@ -990,6 +1014,8 @@ def main():
do_run_task(args)
elif args.command == "lsp":
do_lsp(args)
elif args.command == "discard":
do_discard(args)
else:
assert not "Unexpected command"
except (
Expand Down
44 changes: 44 additions & 0 deletions xbstrap/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import tempfile
import urllib.request
import zipfile
from contextlib import suppress
from enum import Enum

import colorama
Expand Down Expand Up @@ -2614,6 +2615,33 @@ def run_tool_task(cfg, task):
)


def discard_src(cfg, src):
with suppress(Exception):
shutil.rmtree(os.path.join(cfg.source_root, src.source_subdir))


def discard_pkg(cfg, pkg):
with suppress(Exception):
shutil.rmtree(os.path.join(cfg.build_root, pkg.build_subdir))
shutil.rmtree(os.path.join(cfg.build_root, pkg.collect_subdir))

if cfg.use_xbps:
output = subprocess.DEVNULL
if verbosity:
output = None
effective_arch = pkg.architecture
environ = os.environ.copy()
_util.build_environ_paths(
environ, "PATH", prepend=[os.path.join(_util.find_home(), "bin")]
)
environ["XBPS_ARCH"] = effective_arch
args = ["xbps-remove", "-Fy", "-r", cfg.sysroot_dir, pkg.name]
_util.log_info("Running {}".format(args))
subprocess.call(args, env=environ, stdout=output)
else:
raise GenericError("Package management configuration does not support discard")


# ---------------------------------------------------------------------------------------
# Build planning.
# ---------------------------------------------------------------------------------------
Expand Down Expand Up @@ -2666,6 +2694,8 @@ class Action(Enum):
WANT_PKG = 21
# xbstrap-mirror functionality.
MIRROR_SRC = 22
DISCARD_SRC = 23
DISCARD_PKG = 24


Action.strings = {
Expand All @@ -2691,6 +2721,8 @@ class Action(Enum):
Action.WANT_TOOL: "want-tool",
Action.WANT_PKG: "want-pkg",
Action.MIRROR_SRC: "mirror",
Action.DISCARD_SRC: "discard-source",
Action.DISCARD_PKG: "discard-package",
}


Expand Down Expand Up @@ -2770,6 +2802,8 @@ def _determine_state(self):
Action.WANT_TOOL: lambda s, c: s.check_if_fully_installed(c),
Action.WANT_PKG: lambda s, c: s.check_staging(c),
Action.MIRROR_SRC: lambda s, c: s.check_if_mirrord(c),
Action.DISCARD_SRC: lambda s, c: ItemState(missing=True),
Action.DISCARD_PKG: lambda s, c: ItemState(missing=True),
}
self._state = visitors[self.action](self.subject, self.settings)

Expand Down Expand Up @@ -2969,6 +3003,12 @@ def add_task_dependencies(s):
add_pkg_dependencies(subject)
add_task_dependencies(subject)

elif action == Action.DISCARD_SRC:
pass

elif action == Action.DISCARD_PKG:
pass

return item

def _do_materialization_visit(self, edges):
Expand Down Expand Up @@ -3312,6 +3352,10 @@ def emit_progress(status):
raise ExecutionFailureError(action, subject)
elif action == Action.MIRROR_SRC:
mirror_src(self._cfg, subject)
elif action == Action.DISCARD_SRC:
discard_src(self._cfg, subject)
elif action == Action.DISCARD_PKG:
discard_pkg(self._cfg, subject)
else:
raise AssertionError("Unexpected action")
item.exec_status = ExecutionStatus.SUCCESS
Expand Down

0 comments on commit 65e2523

Please sign in to comment.