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

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

63 statements  

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/>. 

21 

22 

23import re 

24 

25from lsst.daf.butler import Butler, CollectionType 

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

27 

28from .confirmable import ConfirmableResult 

29 

30 

31class NoSuchCollectionFailure: 

32 def __init__(self, collection): 

33 self.collection = collection 

34 

35 def __str__(self): 

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

37 

38 

39class NotChainedCollectionFailure: 

40 def __init__(self, collection, type): 

41 self.collection = collection 

42 self.type = type 

43 

44 def __str__(self): 

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

46 

47 

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 

54 

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 

64 

65 def on_confirmation(self) -> None: 

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

67 with butler.transaction(): 

68 for collection in self.others_to_remove: 

69 butler.registry.removeCollection(collection) 

70 butler.removeRuns(self.runs_to_remove) 

71 

72 @property 

73 def failed(self) -> bool: 

74 return self.failure is not None 

75 

76 @property 

77 def describe_failure(self) -> str: 

78 return str(self.failure) 

79 

80 @property 

81 def can_continue(self) -> bool: 

82 return self.runs_to_remove or self.others_to_remove 

83 

84 

85def cleanup( 

86 butler_config: str, 

87 collection: str, 

88): 

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

90 collection but are not members of that collection. 

91 

92 Parameters 

93 ---------- 

94 butler_config : str 

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

96 collection : str 

97 The name of the chained collection. 

98 """ 

99 butler = Butler(butler_config) 

100 result = CleanupResult(butler_config) 

101 try: 

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

103 except MissingCollectionError: 

104 result.failure = NoSuchCollectionFailure(collection) 

105 return result 

106 except CollectionTypeError: 

107 result.failure = NotChainedCollectionFailure( 

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

109 ) 

110 return result 

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

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

113 to_remove = to_consider - to_keep 

114 for r in to_remove: 

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

116 result.runs_to_remove.append(r) 

117 else: 

118 result.others_to_remove.append(r) 

119 return result