Coverage for python/lsst/ctrl/mpexec/cli/script/cleanup.py: 29%

64 statements  

« prev     ^ index     » next       coverage.py v7.11.0, created at 2025-11-21 01:24 -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 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/>. 

27 

28 

29from typing import Any 

30 

31from lsst.daf.butler import Butler, CollectionType 

32from lsst.daf.butler.registry import CollectionTypeError, MissingCollectionError 

33 

34from .confirmable import ConfirmableResult 

35 

36 

37class NoSuchCollectionFailure: 

38 """Failure when there is no such collection. 

39 

40 Parameters 

41 ---------- 

42 collection : `str` 

43 Name of collection. 

44 """ 

45 

46 def __init__(self, collection: str): 

47 self.collection = collection 

48 

49 def __str__(self) -> str: 

50 return f'Did not find a collection named "{self.collection}"' 

51 

52 

53class NotChainedCollectionFailure: 

54 """Failure when this is not a chained collection. 

55 

56 Parameters 

57 ---------- 

58 collection : `str` 

59 Name of collection. 

60 type : `str` 

61 Type of collection. 

62 """ 

63 

64 def __init__(self, collection: str, type: str): 

65 self.collection = collection 

66 self.type = type 

67 

68 def __str__(self) -> str: 

69 return f'COLLECTION must be a CHAINED collection, "{self.collection}" is a "{self.type}" collection.' 

70 

71 

72class CleanupResult(ConfirmableResult): 

73 """Information containing the result of the cleanup request. 

74 

75 Parameters 

76 ---------- 

77 butler_config : `str` 

78 Butler configuration URI. 

79 """ 

80 

81 def __init__(self, butler_config: str): 

82 self.butler_config = butler_config 

83 self.runs_to_remove: list[str] = [] 

84 self.others_to_remove: list[str] = [] 

85 self.failure: Any = None 

86 

87 def describe(self, will: bool) -> str: 

88 if self.can_continue: 

89 msg = "Will remove:" if will else "Removed:" 

90 msg += "\n" 

91 msg += f" runs: {', '.join(self.runs_to_remove)}\n" 

92 msg += f" others: {', '.join(self.others_to_remove)}" 

93 else: 

94 msg = "Did not find any collections to remove." 

95 return msg 

96 

97 def on_confirmation(self) -> None: 

98 butler = Butler.from_config(self.butler_config, writeable=True) 

99 with butler.transaction(): 

100 for collection in self.others_to_remove: 

101 butler.registry.removeCollection(collection) 

102 butler.removeRuns(self.runs_to_remove) 

103 

104 @property 

105 def failed(self) -> bool: 

106 return self.failure is not None 

107 

108 @property 

109 def describe_failure(self) -> str: 

110 return str(self.failure) 

111 

112 @property 

113 def can_continue(self) -> bool: 

114 return bool(self.runs_to_remove) or bool(self.others_to_remove) 

115 

116 

117def cleanup( 

118 butler_config: str, 

119 collection: str, 

120) -> CleanupResult: 

121 """Remove collections that start with the same name as a CHAINED 

122 collection but are not members of that collection. 

123 

124 Parameters 

125 ---------- 

126 butler_config : str 

127 The path location of the gen3 butler/registry config file. 

128 collection : str 

129 The name of the chained collection. 

130 """ 

131 butler = Butler.from_config(butler_config) 

132 result = CleanupResult(butler_config) 

133 try: 

134 to_keep = set(butler.registry.getCollectionChain(collection)) 

135 except MissingCollectionError: 

136 result.failure = NoSuchCollectionFailure(collection) 

137 return result 

138 except CollectionTypeError: 

139 result.failure = NotChainedCollectionFailure( 

140 collection, butler.registry.getCollectionType(collection).name 

141 ) 

142 return result 

143 to_keep.add(collection) 

144 glob = collection + "*" 

145 to_consider = set(butler.registry.queryCollections(glob)) 

146 to_remove = to_consider - to_keep 

147 for r in to_remove: 

148 if butler.registry.getCollectionType(r) == CollectionType.RUN: 

149 result.runs_to_remove.append(r) 

150 else: 

151 result.others_to_remove.append(r) 

152 return result