Coverage for python/lsst/daf/butler/script/removeCollections.py: 43%
38 statements
« prev ^ index » next coverage.py v7.10.6, created at 2025-10-09 03:23 -0700
« prev ^ index » next coverage.py v7.10.6, created at 2025-10-09 03:23 -0700
1# This file is part of daf_butler.
2#
3# Developed for the LSST Data Management System.
4# This product includes software developed by the LSST Project
5# (http://www.lsst.org).
6# See the COPYRIGHT file at the top-level directory of this distribution
7# for details of code ownership.
8#
9# This software is dual licensed under the GNU General Public License and also
10# under a 3-clause BSD license. Recipients may choose which of these licenses
11# to use; please see the files gpl-3.0.txt and/or bsd_license.txt,
12# respectively. If you choose the GPL option then the following text applies
13# (but note that there is still no warranty even if you opt for BSD instead):
14#
15# This program is free software: you can redistribute it and/or modify
16# it under the terms of the GNU General Public License as published by
17# the Free Software Foundation, either version 3 of the License, or
18# (at your option) any later version.
19#
20# This program is distributed in the hope that it will be useful,
21# but WITHOUT ANY WARRANTY; without even the implied warranty of
22# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23# GNU General Public License for more details.
24#
25# You should have received a copy of the GNU General Public License
26# along with this program. If not, see <http://www.gnu.org/licenses/>.
27from __future__ import annotations
29from collections.abc import Callable
30from dataclasses import dataclass
31from functools import partial
33from astropy.table import Table
35from .._butler import Butler
36from .._collection_type import CollectionType
37from ..registry import MissingCollectionError
40@dataclass
41class RemoveCollectionResult:
42 """Container to return to the cli command; holds tables describing the
43 collections that will be removed, as well as any found RUN collections
44 which can not be removed by this command. Also holds the callback function
45 to execute the remove upon user confirmation.
46 """
48 # the callback function to do the removal
49 onConfirmation: Callable[[], None]
50 # astropy table describing data that will be removed.
51 removeCollectionsTable: Table
52 # astropy table describing any run collections that will NOT be removed.
53 runsTable: Table
56@dataclass
57class CollectionInfo:
58 """Lightweight container to hold the name and type of non-run
59 collections, as well as the names of run collections.
60 """
62 nonRunCollections: Table
63 runCollections: Table
66def _getCollectionInfo(
67 repo: str,
68 collection: str,
69) -> CollectionInfo:
70 """Get the names and types of collections that match the collection
71 string.
73 Parameters
74 ----------
75 repo : `str`
76 The URI to the repository.
77 collection : `str`
78 The collection string to search for. Same as the `expression`
79 argument to `registry.queryCollections`.
81 Returns
82 -------
83 collectionInfo : `CollectionInfo`
84 Contains tables with run and non-run collection info.
85 """
86 butler = Butler.from_config(repo, without_datastore=True)
87 try:
88 collections_info = sorted(butler.collections.query_info(collection, include_chains=True))
89 except MissingCollectionError:
90 # Hide the error and act like no collections should be removed.
91 collections_info = []
92 collections = Table(names=("Collection", "Collection Type"), dtype=(str, str))
93 runCollections = Table(names=("Collection",), dtype=(str,))
94 for collection_info in collections_info:
95 if collection_info.type == CollectionType.RUN:
96 runCollections.add_row((collection_info.name,))
97 else:
98 collections.add_row((collection_info.name, collection_info.type.name))
100 return CollectionInfo(collections, runCollections)
103def removeCollections(
104 repo: str,
105 collection: str,
106) -> Table:
107 """Remove collections.
109 Parameters
110 ----------
111 repo : `str`
112 Same as the ``config`` argument to ``Butler.__init__``.
113 collection : `str`
114 Same as the ``name`` argument to ``Registry.removeCollection``.
116 Returns
117 -------
118 collections : `RemoveCollectionResult`
119 Contains tables describing what will be removed, and
120 run collections that *will not* be removed.
121 """
122 collectionInfo = _getCollectionInfo(repo, collection)
124 def _doRemove(collections: Table) -> None:
125 """Perform the prune collection step."""
126 butler = Butler.from_config(repo, writeable=True, without_datastore=True)
127 for name in collections["Collection"]:
128 butler.collections.x_remove(name)
130 result = RemoveCollectionResult(
131 onConfirmation=partial(_doRemove, collectionInfo.nonRunCollections),
132 removeCollectionsTable=collectionInfo.nonRunCollections,
133 runsTable=collectionInfo.runCollections,
134 )
135 return result