Coverage for python/lsst/ctrl/mpexec/cli/script/cleanup.py: 35%
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 ctrl_mpexec.
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/>.
23import re
25from lsst.daf.butler import Butler, CollectionType
26from lsst.daf.butler.registry import CollectionTypeError, MissingCollectionError
28from .confirmable import ConfirmableResult
31class NoSuchCollectionFailure:
32 def __init__(self, collection):
33 self.collection = collection
35 def __str__(self):
36 return f'Did not find a collection named "{self.collection}"'
39class NotChainedCollectionFailure:
40 def __init__(self, collection, type):
41 self.collection = collection
42 self.type = type
44 def __str__(self):
45 return f'COLLETION must be a CHAINED collection, "{self.collection}" is a "{self.type}" collection.'
48class CleanupResult(ConfirmableResult):
49 def __init__(self, butler_config):
50 self.butler_config = butler_config
51 self.runs_to_remove = []
52 self.others_to_remove = []
53 self.failure = None
55 def describe(self, will: bool) -> str:
56 if self.can_continue:
57 msg = "Will remove:" if will else "Removed:"
58 msg += "\n"
59 msg += f" runs: {', '.join(self.runs_to_remove)}\n"
60 msg += f" others: {', '.join(self.others_to_remove)}"
61 else:
62 msg = "Did not find any collections to remove."
63 return msg
65 def on_confirmation(self) -> None:
66 butler = Butler(self.butler_config, writeable=True)
67 for collection in self.others_to_remove:
68 butler.registry.removeCollection(collection)
69 butler.removeRuns(self.runs_to_remove)
71 @property
72 def failed(self) -> bool:
73 return self.failure is not None
75 @property
76 def describe_failure(self) -> str:
77 return str(self.failure)
79 @property
80 def can_continue(self) -> bool:
81 return self.runs_to_remove or self.others_to_remove
84def cleanup(
85 butler_config: str,
86 collection: str,
87):
88 """Remove collections that start with the same name as a CHAINED
89 collection but are not members of that collection.
91 Parameters
92 ----------
93 butler_config : str
94 The path location of the gen3 butler/registry config file.
95 collection : str
96 TODO
97 """
98 butler = Butler(butler_config)
99 result = CleanupResult(butler_config)
100 try:
101 to_keep = set(butler.registry.getCollectionChain(collection))
102 except MissingCollectionError:
103 result.failure = NoSuchCollectionFailure(collection)
104 return result
105 except CollectionTypeError:
106 result.failure = NotChainedCollectionFailure(
107 collection, butler.registry.getCollectionType(collection).name
108 )
109 return result
110 regex = re.compile(collection + ".+")
111 to_consider = set(butler.registry.queryCollections(regex))
112 to_remove = to_consider - to_keep
113 for r in to_remove:
114 if butler.registry.getCollectionType(r) == CollectionType.RUN:
115 result.runs_to_remove.append(r)
116 else:
117 result.others_to_remove.append(r)
118 return result