From 01abedd6f1102c342f6c205842f9769893a142b1 Mon Sep 17 00:00:00 2001 From: James Ennis Date: Tue, 5 Mar 2019 16:02:19 +0000 Subject: cli.py: Add artifact delete command This command provides a --no-prune option because or a large cache, pruning can be an expensive operation. If a developer wishes to quicky rebuild an artifact, they may consider using this option. --- buildstream/_stream.py | 42 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) (limited to 'buildstream/_stream.py') diff --git a/buildstream/_stream.py b/buildstream/_stream.py index b0fce3817..5c880427c 100644 --- a/buildstream/_stream.py +++ b/buildstream/_stream.py @@ -30,11 +30,12 @@ from contextlib import contextmanager, suppress from fnmatch import fnmatch from ._artifactelement import verify_artifact_ref -from ._exceptions import StreamError, ImplError, BstError, ArtifactElementError, set_last_task_error +from ._exceptions import StreamError, ImplError, BstError, ArtifactElementError, CASCacheError, set_last_task_error from ._message import Message, MessageType from ._scheduler import Scheduler, SchedStatus, TrackQueue, FetchQueue, BuildQueue, PullQueue, PushQueue from ._pipeline import Pipeline, PipelineSelection from ._profile import Topics, profile_start, profile_end +from .types import _KeyStrength from . import utils, _yaml, _site from . import Scope, Consistency @@ -520,6 +521,45 @@ class Stream(): return logsdirs + # artifact_delete() + # + # Remove artifacts from the local cache + # + # Args: + # targets (str): Targets to remove + # no_prune (bool): Whether to prune the unreachable refs, default False + # + def artifact_delete(self, targets, no_prune): + # Return list of Element and/or ArtifactElement objects + target_objects = self.load_selection(targets, selection=PipelineSelection.NONE, load_refs=True) + + # Some of the targets may refer to the same key, so first obtain a + # set of the refs to be removed. + remove_refs = set() + for obj in target_objects: + for key_strength in [_KeyStrength.STRONG, _KeyStrength.WEAK]: + key = obj._get_cache_key(strength=key_strength) + remove_refs.add(obj.get_artifact_name(key=key)) + + ref_removed = False + for ref in remove_refs: + try: + self._artifacts.remove(ref, defer_prune=True) + except CASCacheError as e: + self._message(MessageType.WARN, "{}".format(e)) + continue + + self._message(MessageType.INFO, "Removed: {}".format(ref)) + ref_removed = True + + # Prune the artifact cache + if ref_removed and not no_prune: + with self._context.timed_activity("Pruning artifact cache"): + self._artifacts.prune() + + if not ref_removed: + self._message(MessageType.INFO, "No artifacts were removed") + # source_checkout() # # Checkout sources of the target element to the specified location -- cgit v1.2.1