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

37 statements  

« prev     ^ index     » next       coverage.py v6.4.2, created at 2022-07-27 01:57 -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 dataclasses import dataclass 

24from functools import partial 

25from typing import Callable 

26 

27from astropy.table import Table 

28 

29from .._butler import Butler 

30from ..registry import CollectionType, MissingCollectionError 

31 

32 

33@dataclass 

34class RemoveCollectionResult: 

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

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

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

38 to execute the remove upon user confirmation. 

39 """ 

40 

41 # the callback function to do the removal 

42 onConfirmation: Callable[[], None] 

43 # astropy table describing data that will be removed. 

44 removeCollectionsTable: Table 

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

46 runsTable: Table 

47 

48 

49@dataclass 

50class CollectionInfo: 

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

52 collections, as well as the names of run collections.""" 

53 

54 nonRunCollections: Table 

55 runCollections: Table 

56 

57 

58def _getCollectionInfo( 

59 repo: str, 

60 collection: str, 

61) -> CollectionInfo: 

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

63 string. 

64 

65 Parameters 

66 ---------- 

67 repo : `str` 

68 The URI to the repostiory. 

69 collection : `str` 

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

71 argument to `registry.queryCollections`. 

72 

73 Returns 

74 ------- 

75 collectionInfo : `CollectionInfo` 

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

77 """ 

78 butler = Butler(repo) 

79 try: 

80 names = sorted( 

81 butler.registry.queryCollections( 

82 collectionTypes=frozenset( 

83 ( 

84 CollectionType.RUN, 

85 CollectionType.TAGGED, 

86 CollectionType.CHAINED, 

87 CollectionType.CALIBRATION, 

88 ) 

89 ), 

90 expression=collection, 

91 includeChains=True, 

92 ) 

93 ) 

94 except MissingCollectionError: 

95 names = list() 

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

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

98 for name in names: 

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

100 if collectionType == "RUN": 

101 runCollections.add_row((name,)) 

102 else: 

103 collections.add_row((name, collectionType)) 

104 

105 return CollectionInfo(collections, runCollections) 

106 

107 

108def removeCollections( 

109 repo: str, 

110 collection: str, 

111) -> Table: 

112 """Remove collections. 

113 

114 Parameters 

115 ---------- 

116 repo : `str` 

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

118 collection : `str` 

119 Same as the ``name`` argument to ``Butler.pruneCollection``. 

120 

121 Returns 

122 ------- 

123 collections : `RemoveCollectionResult` 

124 Contains tables describing what will be removed, and 

125 run collections that *will not* be removed. 

126 """ 

127 collectionInfo = _getCollectionInfo(repo, collection) 

128 

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

130 """Perform the prune collection step.""" 

131 butler = Butler(repo, writeable=True) 

132 for name in collections["Collection"]: 

133 butler.registry.removeCollection(name) 

134 

135 result = RemoveCollectionResult( 

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

137 removeCollectionsTable=collectionInfo.nonRunCollections, 

138 runsTable=collectionInfo.runCollections, 

139 ) 

140 return result