Coverage for python/lsst/ctrl/mpexec/cli/script/cleanup.py: 29%
64 statements
« prev ^ index » next coverage.py v6.5.0, created at 2022-11-15 02:01 -0800
« prev ^ index » next coverage.py v6.5.0, created at 2022-11-15 02:01 -0800
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
24from typing import Any
26from lsst.daf.butler import Butler, CollectionType
27from lsst.daf.butler.registry import CollectionTypeError, MissingCollectionError
29from .confirmable import ConfirmableResult
32class NoSuchCollectionFailure:
33 def __init__(self, collection: str):
34 self.collection = collection
36 def __str__(self) -> str:
37 return f'Did not find a collection named "{self.collection}"'
40class NotChainedCollectionFailure:
41 def __init__(self, collection: str, type: str):
42 self.collection = collection
43 self.type = type
45 def __str__(self) -> str:
46 return f'COLLECTION must be a CHAINED collection, "{self.collection}" is a "{self.type}" collection.'
49class CleanupResult(ConfirmableResult):
50 def __init__(self, butler_config: str):
51 self.butler_config = butler_config
52 self.runs_to_remove: list[str] = []
53 self.others_to_remove: list[str] = []
54 self.failure: Any = None
56 def describe(self, will: bool) -> str:
57 if self.can_continue:
58 msg = "Will remove:" if will else "Removed:"
59 msg += "\n"
60 msg += f" runs: {', '.join(self.runs_to_remove)}\n"
61 msg += f" others: {', '.join(self.others_to_remove)}"
62 else:
63 msg = "Did not find any collections to remove."
64 return msg
66 def on_confirmation(self) -> None:
67 butler = Butler(self.butler_config, writeable=True)
68 with butler.transaction():
69 for collection in self.others_to_remove:
70 butler.registry.removeCollection(collection)
71 butler.removeRuns(self.runs_to_remove)
73 @property
74 def failed(self) -> bool:
75 return self.failure is not None
77 @property
78 def describe_failure(self) -> str:
79 return str(self.failure)
81 @property
82 def can_continue(self) -> bool:
83 return bool(self.runs_to_remove) or bool(self.others_to_remove)
86def cleanup(
87 butler_config: str,
88 collection: str,
89) -> CleanupResult:
90 """Remove collections that start with the same name as a CHAINED
91 collection but are not members of that collection.
93 Parameters
94 ----------
95 butler_config : str
96 The path location of the gen3 butler/registry config file.
97 collection : str
98 The name of the chained collection.
99 """
100 butler = Butler(butler_config)
101 result = CleanupResult(butler_config)
102 try:
103 to_keep = set(butler.registry.getCollectionChain(collection))
104 except MissingCollectionError:
105 result.failure = NoSuchCollectionFailure(collection)
106 return result
107 except CollectionTypeError:
108 result.failure = NotChainedCollectionFailure(
109 collection, butler.registry.getCollectionType(collection).name
110 )
111 return result
112 regex = re.compile(collection + ".+")
113 to_consider = set(butler.registry.queryCollections(regex))
114 to_remove = to_consider - to_keep
115 for r in to_remove:
116 if butler.registry.getCollectionType(r) == CollectionType.RUN:
117 result.runs_to_remove.append(r)
118 else:
119 result.others_to_remove.append(r)
120 return result