Coverage for python/lsst/daf/butler/script/removeRuns.py: 39%
Shortcuts on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
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/>.
23from collections import defaultdict
24from dataclasses import dataclass
25from functools import partial
26from typing import Callable, Dict, List, Mapping, Sequence, Tuple
28from .. import Butler
29from ..registry import CollectionType, MissingCollectionError
30from ..registry.queries import DatasetQueryResults
33@dataclass
34class RemoveRunsResult:
35 """Container to return to the cli command.
37 Contains the names of runs that will be deleted, and a map of dataset type
38 to how many of that dataset will be deleted. Also contains the callback
39 function to execute the remove upon user confirmation.
40 """
41 # the callback function to do the removal
42 onConfirmation: Callable[[], None]
43 # list of the run collections that will be removed
44 runs: Sequence[str]
45 # mapping of dataset type name to how many will be removed.
46 datasets: Mapping[str, int]
49def _getCollectionInfo(
50 repo: str,
51 collection: str,
52) -> Tuple[List[str], Mapping[str, int]]:
53 """Get the names and types of collections that match the collection
54 string.
56 Parameters
57 ----------
58 repo : `str`
59 The URI to the repostiory.
60 collection : `str`
61 The collection string to search for. Same as the `expression`
62 argument to `registry.queryCollections`.
64 Returns
65 -------
66 runs : `list` of `str`
67 The runs that will be removed.
68 datasets : `dict` [`str`, `int`]
69 The dataset types and and how many will be removed.
70 """
71 butler = Butler(repo)
72 try:
73 collectionNames = list(
74 butler.registry.queryCollections(
75 collectionTypes=frozenset((CollectionType.RUN,)),
76 expression=collection,
77 includeChains=False,
78 )
79 )
80 except MissingCollectionError:
81 collectionNames = list()
82 runs = []
83 datasets: Dict[str, int] = defaultdict(int)
84 for collectionName in collectionNames:
85 assert butler.registry.getCollectionType(collectionName).name == "RUN"
86 runs.append(collectionName)
87 all_results = butler.registry.queryDatasets(..., collections=collectionName)
88 assert isinstance(all_results, DatasetQueryResults)
89 for r in all_results.byParentDatasetType():
90 datasets[r.parentDatasetType.name] += r.count(exact=False)
91 return runs, datasets
94def removeRuns(
95 repo: str,
96 collection: str,
97) -> RemoveRunsResult:
98 """Remove collections.
100 Parameters
101 ----------
102 repo : `str`
103 Same as the ``config`` argument to ``Butler.__init__``
104 collection : `str`
105 Same as the ``name`` argument to ``Butler.pruneCollection``.
107 Returns
108 -------
109 collections : `RemoveRunsResult`
110 Contains information describing what will be removed.
111 """
112 runs, datasets = _getCollectionInfo(repo, collection)
114 def doRemove(runs: Sequence[str]) -> None:
115 """Perform the remove step."""
116 butler = Butler(repo, writeable=True)
117 butler.removeRuns(runs, unstore=True)
119 result = RemoveRunsResult(
120 onConfirmation=partial(doRemove, runs),
121 runs=runs,
122 datasets=datasets,
123 )
124 return result