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

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

37 statements  

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 astropy.table import Table 

24from dataclasses import dataclass 

25from functools import partial 

26from typing import Callable 

27from .. import Butler 

28from ..registry import CollectionType, MissingCollectionError 

29 

30 

31@dataclass 

32class RemoveCollectionResult: 

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

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

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

36 to execute the remove upon user confirmation. 

37 """ 

38 # the callback function to do the removal 

39 onConfirmation: Callable[[], None] 

40 # astropy table describing data that will be removed. 

41 removeCollectionsTable: Table 

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

43 runsTable: Table 

44 

45 

46@dataclass 

47class CollectionInfo: 

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

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

50 nonRunCollections: Table 

51 runCollections: Table 

52 

53 

54def _getCollectionInfo( 

55 repo: str, 

56 collection: str, 

57) -> CollectionInfo: 

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

59 string. 

60 

61 Parameters 

62 ---------- 

63 repo : `str` 

64 The URI to the repostiory. 

65 collection : `str` 

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

67 argument to `registry.queryCollections`. 

68 

69 Returns 

70 ------- 

71 collectionInfo : `CollectionInfo` 

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

73 """ 

74 butler = Butler(repo) 

75 try: 

76 names = list( 

77 butler.registry.queryCollections( 

78 collectionTypes=frozenset(( 

79 CollectionType.RUN, 

80 CollectionType.TAGGED, 

81 CollectionType.CHAINED, 

82 CollectionType.CALIBRATION, 

83 )), 

84 expression=collection, 

85 includeChains=True, 

86 ) 

87 ) 

88 except MissingCollectionError: 

89 names = list() 

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

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

92 for name in names: 

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

94 if collectionType == "RUN": 

95 runCollections.add_row((name,)) 

96 else: 

97 collections.add_row((name, collectionType)) 

98 

99 return CollectionInfo(collections, runCollections) 

100 

101 

102def removeCollections( 

103 repo: str, 

104 collection: str, 

105) -> Table: 

106 """Remove collections. 

107 

108 Parameters 

109 ---------- 

110 repo : `str` 

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

112 collection : `str` 

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

114 

115 Returns 

116 ------- 

117 collections : `RemoveCollectionResult` 

118 Contains tables describing what will be removed, and 

119 run collections that *will not* be removed. 

120 """ 

121 collectionInfo = _getCollectionInfo(repo, collection) 

122 

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

124 """Perform the prune collection step.""" 

125 butler = Butler(repo, writeable=True) 

126 for name in collections["Collection"]: 

127 butler.registry.removeCollection(name) 

128 

129 result = RemoveCollectionResult( 

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

131 removeCollectionsTable=collectionInfo.nonRunCollections, 

132 runsTable=collectionInfo.runCollections, 

133 ) 

134 return result