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

63 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-04-14 23:48 +0000

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 with Butler.from_config(self.butler_config, writeable=True) as butler, butler.transaction(): 

99 for collection in self.others_to_remove: 

100 butler.registry.removeCollection(collection) 

101 butler.removeRuns(self.runs_to_remove) 

102 

103 @property 

104 def failed(self) -> bool: 

105 return self.failure is not None 

106 

107 @property 

108 def describe_failure(self) -> str: 

109 return str(self.failure) 

110 

111 @property 

112 def can_continue(self) -> bool: 

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

114 

115 

116def cleanup( 

117 butler_config: str, 

118 collection: str, 

119) -> CleanupResult: 

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

121 collection but are not members of that collection. 

122 

123 Parameters 

124 ---------- 

125 butler_config : str 

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

127 collection : str 

128 The name of the chained collection. 

129 """ 

130 with Butler.from_config(butler_config) as butler: 

131 result = CleanupResult(butler_config) 

132 try: 

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

134 except MissingCollectionError: 

135 result.failure = NoSuchCollectionFailure(collection) 

136 return result 

137 except CollectionTypeError: 

138 result.failure = NotChainedCollectionFailure( 

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

140 ) 

141 return result 

142 to_keep.add(collection) 

143 glob = collection + "*" 

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

145 to_remove = to_consider - to_keep 

146 for r in to_remove: 

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

148 result.runs_to_remove.append(r) 

149 else: 

150 result.others_to_remove.append(r) 

151 return result