Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DM-28857 sort collections before pruning in butler prune-collection #487

Merged
merged 1 commit into from
Mar 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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