Coverage for python/lsst/daf/butler/script/removeCollections.py: 46%
38 statements
« prev ^ index » next coverage.py v7.3.2, created at 2023-10-25 15:14 +0000
« prev ^ index » next coverage.py v7.3.2, created at 2023-10-25 15:14 +0000
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 program is free software: you can redistribute it and/or modify
10# it under the terms of the GNU General Public License as published by
11# the Free Software Foundation, either version 3 of the License, or
12# (at your option) any later version.
13#
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17# GNU General Public License for more details.
18#
19# You should have received a copy of the GNU General Public License
20# along with this program. If not, see <http://www.gnu.org/licenses/>.
21from __future__ import annotations
23from collections.abc import Callable
24from dataclasses import dataclass
25from functools import partial
27from astropy.table import Table
29from .._butler import Butler
30from ..registry import CollectionType, MissingCollectionError
33@dataclass
34class RemoveCollectionResult:
35 """Container to return to the cli command; holds tables describing the
36 collections that will be removed, as well as any found RUN collections
37 which can not be removed by this command. Also holds the callback funciton
38 to execute the remove upon user confirmation.
39 """
41 # the callback function to do the removal
42 onConfirmation: Callable[[], None]
43 # astropy table describing data that will be removed.
44 removeCollectionsTable: Table
45 # astropy table describing any run collections that will NOT be removed.
46 runsTable: Table
49@dataclass
50class CollectionInfo:
51 """Lightweight container to hold the name and type of non-run
52 collections, as well as the names of run collections.
53 """
55 nonRunCollections: Table
56 runCollections: Table
59def _getCollectionInfo(
60 repo: str,
61 collection: str,
62) -> CollectionInfo:
63 """Get the names and types of collections that match the collection
64 string.
66 Parameters
67 ----------
68 repo : `str`
69 The URI to the repostiory.
70 collection : `str`
71 The collection string to search for. Same as the `expression`
72 argument to `registry.queryCollections`.
74 Returns
75 -------
76 collectionInfo : `CollectionInfo`
77 Contains tables with run and non-run collection info.
78 """
79 butler = Butler(repo, without_datastore=True)
80 try:
81 names = sorted(
82 butler.registry.queryCollections(
83 collectionTypes=frozenset(
84 (
85 CollectionType.RUN,
86 CollectionType.TAGGED,
87 CollectionType.CHAINED,
88 CollectionType.CALIBRATION,
89 )
90 ),
91 expression=collection,
92 includeChains=True,
93 )
94 )
95 except MissingCollectionError:
96 names = []
97 collections = Table(names=("Collection", "Collection Type"), dtype=(str, str))
98 runCollections = Table(names=("Collection",), dtype=(str,))
99 for name in names:
100 collectionType = butler.registry.getCollectionType(name).name
101 if collectionType == "RUN":
102 runCollections.add_row((name,))
103 else:
104 collections.add_row((name, collectionType))
106 return CollectionInfo(collections, runCollections)
109def removeCollections(
110 repo: str,
111 collection: str,
112) -> Table:
113 """Remove collections.
115 Parameters
116 ----------
117 repo : `str`
118 Same as the ``config`` argument to ``Butler.__init__``
119 collection : `str`
120 Same as the ``name`` argument to ``Registry.removeCollection``.
122 Returns
123 -------
124 collections : `RemoveCollectionResult`
125 Contains tables describing what will be removed, and
126 run collections that *will not* be removed.
127 """
128 collectionInfo = _getCollectionInfo(repo, collection)
130 def doRemove(collections: Table) -> None:
131 """Perform the prune collection step."""
132 butler = Butler(repo, writeable=True, without_datastore=True)
133 for name in collections["Collection"]:
134 butler.registry.removeCollection(name)
136 result = RemoveCollectionResult(
137 onConfirmation=partial(doRemove, collectionInfo.nonRunCollections),
138 removeCollectionsTable=collectionInfo.nonRunCollections,
139 runsTable=collectionInfo.runCollections,
140 )
141 return result