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

64 statements  

« prev     ^ index     » next       coverage.py v7.3.1, created at 2023-09-13 09:53 +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 

29import re 

30from typing import Any 

31 

32from lsst.daf.butler import Butler, CollectionType 

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

34 

35from .confirmable import ConfirmableResult 

36 

37 

38class NoSuchCollectionFailure: 

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

40 

41 def __init__(self, collection: str): 

42 self.collection = collection 

43 

44 def __str__(self) -> str: 

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

46 

47 

48class NotChainedCollectionFailure: 

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

50 

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

52 self.collection = collection 

53 self.type = type 

54 

55 def __str__(self) -> str: 

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

57 

58 

59class CleanupResult(ConfirmableResult): 

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

61 

62 def __init__(self, butler_config: str): 

63 self.butler_config = butler_config 

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

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

66 self.failure: Any = None 

67 

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

69 if self.can_continue: 

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

71 msg += "\n" 

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

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

74 else: 

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

76 return msg 

77 

78 def on_confirmation(self) -> None: 

79 butler = Butler(self.butler_config, writeable=True) 

80 with butler.transaction(): 

81 for collection in self.others_to_remove: 

82 butler.registry.removeCollection(collection) 

83 butler.removeRuns(self.runs_to_remove) 

84 

85 @property 

86 def failed(self) -> bool: 

87 return self.failure is not None 

88 

89 @property 

90 def describe_failure(self) -> str: 

91 return str(self.failure) 

92 

93 @property 

94 def can_continue(self) -> bool: 

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

96 

97 

98def cleanup( 

99 butler_config: str, 

100 collection: str, 

101) -> CleanupResult: 

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

103 collection but are not members of that collection. 

104 

105 Parameters 

106 ---------- 

107 butler_config : str 

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

109 collection : str 

110 The name of the chained collection. 

111 """ 

112 butler = Butler(butler_config) 

113 result = CleanupResult(butler_config) 

114 try: 

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

116 except MissingCollectionError: 

117 result.failure = NoSuchCollectionFailure(collection) 

118 return result 

119 except CollectionTypeError: 

120 result.failure = NotChainedCollectionFailure( 

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

122 ) 

123 return result 

124 regex = re.compile(collection + ".+") 

125 to_consider = set(butler.registry.queryCollections(regex)) 

126 to_remove = to_consider - to_keep 

127 for r in to_remove: 

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

129 result.runs_to_remove.append(r) 

130 else: 

131 result.others_to_remove.append(r) 

132 return result