Coverage for python/lsst/daf/butler/script/_pruneDatasets.py: 34%

77 statements  

« prev     ^ index     » next       coverage.py v6.4, created at 2022-05-24 02:27 -0700

1# This file is part of daf_butler. 

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 

23from enum import Enum, auto 

24 

25from .._butler import Butler 

26from ..registry import CollectionType 

27from .queryDatasets import QueryDatasets 

28 

29 

30class PruneDatasetsResult: 

31 """Contains the results of a prune-datasets action. 

32 

33 The action may not be complete if the caller requested a confirmation, in 

34 which case calling ``onConfirmation`` will perform the action. 

35 

36 Parameters 

37 ---------- 

38 tables : `list` [``astropy.table.table``], optional 

39 The astropy tables that will be or were deleted, by default None. 

40 state : ``PruneDatasetsResult.State``, optional 

41 The initial state of execution of the action, if `None` the result 

42 state is ``INIT``, by default None. 

43 

44 Attributes 

45 ---------- 

46 tables 

47 Same as in Parameters. 

48 state : ``PruneDatasetsResult.State`` 

49 The current state of the action. 

50 onConfirmation : `Callable[None, None]` 

51 The function to call to perform the action if the caller wants to 

52 confirm the tables before performing the action. 

53 """ 

54 

55 class State(Enum): 

56 INIT = auto() 

57 DRY_RUN_COMPLETE = auto() 

58 AWAITING_CONFIRMATION = auto() 

59 FINISHED = auto() 

60 ERR_PURGE_AND_DISASSOCIATE = auto() 

61 ERR_NO_COLLECTION_RESTRICTION = auto() 

62 ERR_PRUNE_ON_NOT_RUN = auto() 

63 ERR_NO_OP = auto() 

64 

65 def __init__(self, tables=None, state=None, errDict=None): 

66 self.state = state or self.State.INIT 

67 self.tables = tables 

68 self.onConfirmation = None 

69 # Action describes the removal action for dry-run, will be a dict with 

70 # keys disassociate, unstore, purge, and collections. 

71 self.action = None 

72 # errDict is a container for variables related to the error that may be 

73 # substituted into a user-visible string. 

74 self.errDict = errDict or {} 

75 

76 @property 

77 def dryRun(self): 

78 return self.state is self.State.DRY_RUN_COMPLETE 

79 

80 @property 

81 def confirm(self): 

82 return self.state is self.State.AWAITING_CONFIRMATION 

83 

84 @property 

85 def finished(self): 

86 return self.state is self.State.FINISHED 

87 

88 @property 

89 def errPurgeAndDisassociate(self): 

90 return self.state is self.State.ERR_PURGE_AND_DISASSOCIATE 

91 

92 @property 

93 def errNoCollectionRestriction(self): 

94 return self.state is self.State.ERR_NO_COLLECTION_RESTRICTION 

95 

96 @property 

97 def errPruneOnNotRun(self): 

98 return self.state is self.state.ERR_PRUNE_ON_NOT_RUN 

99 

100 @property 

101 def errNoOp(self): 

102 return self.state is self.state.ERR_NO_OP 

103 

104 

105def pruneDatasets( 

106 repo, collections, datasets, where, disassociate_tags, unstore, purge_run, dry_run, confirm, find_all 

107): 

108 """Prune datasets from a repository. 

109 

110 Parameters 

111 ---------- 

112 repo : `str` 

113 URI to the location of the repo or URI to a config file describing the 

114 repo and its location. 

115 collections : iterable [`str`] 

116 A list of glob-style search string that identify the collections to 

117 search for. 

118 datasets : iterable [`str`] 

119 A list of glob-style search string that identify the dataset type names 

120 to search for. 

121 where : `str` 

122 A string expression similar to a SQL WHERE clause. May involve any 

123 column of a dimension table or (as a shortcut for the primary key 

124 column of a dimension table) dimension name. 

125 find_all : `bool` 

126 If False, for each result data ID, will only delete the dataset from 

127 the first collection in which a dataset of that dataset type appears 

128 (according to the order of ``collections`` passed in). If used, 

129 ``collections`` must specify at least one expression and must not 

130 contain wildcards. This is the inverse of ``QueryDataset``'s find_first 

131 option. 

132 disassociate_tags : `list` [`str`] 

133 TAGGED collections to disassociate the datasets from. If not `None` 

134 then ``purge_run`` must be `None`. 

135 unstore : `bool` 

136 Same as the unstore argument to ``Butler.pruneDatasets``. 

137 purge_run : `str` 

138 Completely remove the dataset from this run in the ``Registry``. 

139 dry_run : `bool` 

140 Get results for what would be removed but do not remove. 

141 confirm : `bool` 

142 Get results for what would be removed and return the results for 

143 display & confirmation, with a completion function to run after 

144 confirmation. 

145 

146 The matrix of legal & illegal combinations of purge, unstore, and 

147 disassociate is this: 

148 - none of (purge, unstore, disassociate): error, nothing to do 

149 - purge only: ok 

150 - unstore only: ok 

151 - disassociate only: ok 

152 - purge+unstore: ok, just ignore unstore (purge effectively implies 

153 unstore) 

154 - purge+disassociate: this is an error (instead of ignoring disassociate), 

155 because that comes with a collection argument that we can't respect, and 

156 that might be confusing (purge will disassociate from all TAGGED 

157 collections, not just the one given) 

158 - purge+unstore+disassociate: an error, for the same reason as just 

159 purge+disassociate 

160 - unstore+disassociate: ok; these operations are unrelated to each other 

161 

162 Returns 

163 ------- 

164 results : ``PruneDatasetsResult`` 

165 A data structure that contains information about datasets for removal, 

166 removal status, and options to continue in some cases. 

167 """ 

168 if not disassociate_tags and not unstore and not purge_run: 

169 return PruneDatasetsResult(state=PruneDatasetsResult.State.ERR_NO_OP) 

170 

171 if disassociate_tags and purge_run: 

172 return PruneDatasetsResult(state=PruneDatasetsResult.State.ERR_PURGE_AND_DISASSOCIATE) 

173 

174 # If collections is not specified and a purge_run is, use the purge_run for 

175 # collections, or if disassociate_tags is then use that. 

176 if not collections: 

177 if purge_run: 

178 collections = (purge_run,) 

179 elif disassociate_tags: 

180 collections = disassociate_tags 

181 

182 if not collections: 

183 return PruneDatasetsResult(state=PruneDatasetsResult.State.ERR_NO_COLLECTION_RESTRICTION) 

184 

185 butler = Butler(repo) 

186 

187 # If purging, verify that all the collections to purge are RUN type 

188 # collections: 

189 if purge_run: 

190 collectionType = butler.registry.getCollectionType(purge_run) 

191 if collectionType is not CollectionType.RUN: 

192 return PruneDatasetsResult( 

193 state=PruneDatasetsResult.State.ERR_PRUNE_ON_NOT_RUN, errDict=dict(collection=purge_run) 

194 ) 

195 

196 datasets = QueryDatasets( 

197 repo=repo, 

198 glob=datasets, 

199 collections=collections, 

200 where=where, 

201 # By default we want find_first to be True if collections are provided 

202 # (else False) (find_first requires collections to be provided). 

203 # But the user may specify that they want to find all (thus forcing 

204 # find_first to be False) 

205 find_first=not find_all, 

206 show_uri=False, 

207 ) 

208 

209 result = PruneDatasetsResult(datasets.getTables()) 

210 

211 disassociate = bool(disassociate_tags) or bool(purge_run) 

212 purge = bool(purge_run) 

213 unstore = unstore or bool(purge_run) 

214 

215 if dry_run: 

216 result.state = PruneDatasetsResult.State.DRY_RUN_COMPLETE 

217 result.action = dict(disassociate=disassociate, purge=purge, unstore=unstore, collections=collections) 

218 return result 

219 

220 def doPruneDatasets(): 

221 butler = Butler(repo, writeable=True) 

222 butler.pruneDatasets( 

223 refs=datasets.getDatasets(), 

224 disassociate=disassociate, 

225 tags=disassociate_tags or (), 

226 purge=purge, 

227 run=purge_run or None, 

228 unstore=unstore, 

229 ) 

230 result.state = PruneDatasetsResult.State.FINISHED 

231 return result 

232 

233 if confirm: 

234 result.state = PruneDatasetsResult.State.AWAITING_CONFIRMATION 

235 result.onConfirmation = doPruneDatasets 

236 return result 

237 

238 return doPruneDatasets()