Skip to content

Commit

Permalink
add --unlink arg to 'butler prune-collection'
Browse files Browse the repository at this point in the history
  • Loading branch information
n8pease committed Mar 25, 2021
1 parent 0df9135 commit ca2e884
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
19 changes: 18 additions & 1 deletion python/lsst/daf/butler/_butler.py
Original file line number Diff line number Diff line change
Expand Up @@ -1204,7 +1204,8 @@ def removeRuns(self, names: Iterable[str], unstore: bool = True) -> None:
# Point of no return for removing artifacts
self.datastore.emptyTrash()

def pruneCollection(self, name: str, purge: bool = False, unstore: bool = False) -> None:
def pruneCollection(self, name: str, purge: bool = False, unstore: bool = False,
unlink: Optional[List[str]] = None) -> None:
"""Remove a collection and possibly prune datasets within it.
Parameters
Expand All @@ -1224,6 +1225,9 @@ def pruneCollection(self, name: str, purge: bool = False, unstore: bool = False)
unstore: `bool`, optional
If `True`, remove all datasets in the collection from all
datastores in which they appear.
unlink: `list` [`str`], optional
Before removing the given `collection` unlink it from from these
parent collections.
Raises
------
Expand All @@ -1246,7 +1250,20 @@ def pruneCollection(self, name: str, purge: bool = False, unstore: bool = False)
if collectionType is not CollectionType.RUN and purge:
raise PurgeUnsupportedPruneCollectionsError(collectionType)

def remove(child: str, parent: str) -> None:
"""Remove a child collection from a parent collection."""
# Remove child from parent.
chain = list(self.registry.getCollectionChain(parent))
try:
chain.remove(name)
except ValueError as e:
raise RuntimeError(f"{name} is not a child of {parent}") from e
self.registry.setCollectionChain(parent, chain)

with self.registry.transaction():
if (unlink):
for parent in unlink:
remove(name, parent)
if unstore:
for ref in self.registry.queryDatasets(..., collections=name, findFirst=True):
if self.datastore.exists(ref):
Expand Down
4 changes: 4 additions & 0 deletions python/lsst/daf/butler/cli/cmd/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,10 @@ def config_validate(*args, **kwargs):
@click.option("--unstore",
help=("""Remove all datasets in the collection from all datastores in which they appear."""),
is_flag=True)
@click.option("--unlink",
help="Before removing the given `collection` unlink it from from this parent collection.",
multiple=True,
callback=split_commas)
@options_file_option()
def prune_collection(**kwargs):
"""Remove a collection and possibly prune datasets within it."""
Expand Down
6 changes: 4 additions & 2 deletions python/lsst/daf/butler/script/pruneCollection.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
)


def pruneCollection(repo, collection, purge, unstore):
def pruneCollection(repo, collection, purge, unstore, unlink):
"""Remove a collection and possibly prune datasets within it.
Parameters
Expand All @@ -40,10 +40,12 @@ def pruneCollection(repo, collection, purge, unstore):
Same as the ``purge`` argument to ``Butler.pruneCollection``.
unstore: `bool`, optional
Same as the ``unstore`` argument to ``Butler.pruneCollection``.
unlink: `list` [`str`]
Same as the ``unlink`` argument to ``Butler.pruneCollection``.
"""
butler = Butler(repo, writeable=True)
try:
butler.pruneCollection(collection, purge, unstore)
butler.pruneCollection(collection, purge, unstore, unlink)
except PurgeWithoutUnstorePruneCollectionsError as e:
raise TypeError("Cannot pass --purge without --unstore.") from e
except RunWithoutPurgePruneCollectionsError as e:
Expand Down

0 comments on commit ca2e884

Please sign in to comment.