Coverage for python/lsst/daf/butler/script/removeCollections.py: 46%

38 statements  

« prev     ^ index     » next       coverage.py v7.3.2, created at 2023-12-01 11:00 +0000

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

27from __future__ import annotations 

28 

29from collections.abc import Callable 

30from dataclasses import dataclass 

31from functools import partial 

32 

33from astropy.table import Table 

34 

35from .._butler import Butler 

36from ..registry import CollectionType, MissingCollectionError 

37 

38 

39@dataclass 

40class RemoveCollectionResult: 

41 """Container to return to the cli command; holds tables describing the 

42 collections that will be removed, as well as any found RUN collections 

43 which can not be removed by this command. Also holds the callback funciton 

44 to execute the remove upon user confirmation. 

45 """ 

46 

47 # the callback function to do the removal 

48 onConfirmation: Callable[[], None] 

49 # astropy table describing data that will be removed. 

50 removeCollectionsTable: Table 

51 # astropy table describing any run collections that will NOT be removed. 

52 runsTable: Table 

53 

54 

55@dataclass 

56class CollectionInfo: 

57 """Lightweight container to hold the name and type of non-run 

58 collections, as well as the names of run collections. 

59 """ 

60 

61 nonRunCollections: Table 

62 runCollections: Table 

63 

64 

65def _getCollectionInfo( 

66 repo: str, 

67 collection: str, 

68) -> CollectionInfo: 

69 """Get the names and types of collections that match the collection 

70 string. 

71 

72 Parameters 

73 ---------- 

74 repo : `str` 

75 The URI to the repostiory. 

76 collection : `str` 

77 The collection string to search for. Same as the `expression` 

78 argument to `registry.queryCollections`. 

79 

80 Returns 

81 ------- 

82 collectionInfo : `CollectionInfo` 

83 Contains tables with run and non-run collection info. 

84 """ 

85 butler = Butler.from_config(repo, without_datastore=True) 

86 try: 

87 names = sorted( 

88 butler.registry.queryCollections( 

89 collectionTypes=frozenset( 

90 ( 

91 CollectionType.RUN, 

92 CollectionType.TAGGED, 

93 CollectionType.CHAINED, 

94 CollectionType.CALIBRATION, 

95 ) 

96 ), 

97 expression=collection, 

98 includeChains=True, 

99 ) 

100 ) 

101 except MissingCollectionError: 

102 names = [] 

103 collections = Table(names=("Collection", "Collection Type"), dtype=(str, str)) 

104 runCollections = Table(names=("Collection",), dtype=(str,)) 

105 for name in names: 

106 collectionType = butler.registry.getCollectionType(name).name 

107 if collectionType == "RUN": 

108 runCollections.add_row((name,)) 

109 else: 

110 collections.add_row((name, collectionType)) 

111 

112 return CollectionInfo(collections, runCollections) 

113 

114 

115def removeCollections( 

116 repo: str, 

117 collection: str, 

118) -> Table: 

119 """Remove collections. 

120 

121 Parameters 

122 ---------- 

123 repo : `str` 

124 Same as the ``config`` argument to ``Butler.__init__`` 

125 collection : `str` 

126 Same as the ``name`` argument to ``Registry.removeCollection``. 

127 

128 Returns 

129 ------- 

130 collections : `RemoveCollectionResult` 

131 Contains tables describing what will be removed, and 

132 run collections that *will not* be removed. 

133 """ 

134 collectionInfo = _getCollectionInfo(repo, collection) 

135 

136 def doRemove(collections: Table) -> None: 

137 """Perform the prune collection step.""" 

138 butler = Butler.from_config(repo, writeable=True, without_datastore=True) 

139 for name in collections["Collection"]: 

140 butler.registry.removeCollection(name) 

141 

142 result = RemoveCollectionResult( 

143 onConfirmation=partial(doRemove, collectionInfo.nonRunCollections), 

144 removeCollectionsTable=collectionInfo.nonRunCollections, 

145 runsTable=collectionInfo.runCollections, 

146 ) 

147 return result