Coverage for python / lsst / daf / butler / cli / cmd / _remove_collections.py: 29%

57 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-04-14 23:37 +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 

29__all__ = ["remove_collections"] 

30 

31from typing import Any 

32 

33import click 

34 

35from ... import script 

36from ..opt import collection_argument, confirm_option, options_file_option, repo_argument 

37from ..utils import ButlerCommand 

38 

39# messages emitted by remove-collections, defined separately for use in unit 

40# tests. 

41noNonRunCollectionsMsg = "No non-RUN collections were found." 

42willRemoveCollectionMsg = "The following collections will be removed:" 

43removedCollectionsMsg = "Removed collections" 

44willRemoveCollectionChainsMsg = "Collections to be removed from their parent collection chains:" 

45removedCollectionChainsMsg = "Removed collections from their parent collection chains:" 

46canNotRemoveFoundRuns = "The following RUN collections were found but can NOT be removed by this command:" 

47didNotRemoveFoundRuns = "Found RUN collections but they can NOT be removed by this command:" 

48abortedMsg = "Aborted." 

49 

50 

51@click.command(cls=ButlerCommand) 

52@repo_argument(required=True) 

53@collection_argument( 

54 help="COLLECTION is a glob-style expression that identifies the collection(s) to remove." 

55) 

56@confirm_option() 

57@options_file_option() 

58@click.option( 

59 "--remove-from-parents", 

60 is_flag=True, 

61 help="Forcibly remove the collection even if it is still referenced from collection chains.", 

62) 

63def remove_collections(**kwargs: Any) -> None: # numpydoc ignore=PR01 

64 """Remove one or more non-RUN collections. 

65 

66 This command can be used to remove only non-RUN collections. If RUN 

67 collections are found when searching for collections (and the --no-confirm 

68 flag is not used), then they will be shown in a separate table during 

69 confirmation, but they will not be removed. 

70 

71 Use the remove-runs subcommand to remove RUN collections. 

72 """ 

73 confirm: bool = kwargs.pop("confirm") 

74 result = script.removeCollections(**kwargs) 

75 canRemoveCollections = len(result.removeCollectionsTable) 

76 doContinue = canRemoveCollections 

77 if confirm: 

78 if canRemoveCollections: 

79 print("\n" + willRemoveCollectionMsg) 

80 result.removeCollectionsTable.pprint_all(align="<") 

81 else: 

82 print("\n" + noNonRunCollectionsMsg) 

83 if len(result.removeChainsTable): 

84 print("\n" + willRemoveCollectionChainsMsg) 

85 result.removeChainsTable.pprint_all(align="<") 

86 print() 

87 if len(result.runsTable): 

88 print("\n" + canNotRemoveFoundRuns) 

89 result.runsTable.pprint_all(align="<") 

90 print() 

91 if canRemoveCollections: 

92 doContinue = click.confirm(text="Continue?", default=False) 

93 if doContinue: 

94 result.onConfirmation() 

95 if confirm: 

96 print("\n" + removedCollectionsMsg + ".\n") 

97 else: 

98 print("\n" + removedCollectionsMsg + ":\n") 

99 result.removeCollectionsTable.pprint_all(align="<") 

100 if len(result.removeChainsTable): 

101 print("\n" + removedCollectionChainsMsg) 

102 result.removeChainsTable.pprint_all(align="<") 

103 print() 

104 if len(result.runsTable): 

105 print("\n" + didNotRemoveFoundRuns) 

106 result.runsTable.pprint_all(align="<") 

107 print() 

108 elif canRemoveCollections and not doContinue: 

109 print("\n" + abortedMsg + "\n")