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

46 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2023-01-19 02:07 -0800

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

21from __future__ import annotations 

22 

23__all__ = ["remove_collections"] 

24 

25from typing import Any 

26 

27import click 

28 

29from ... import script 

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

31from ..utils import ButlerCommand 

32 

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

34# tests. 

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

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

37removedCollectionsMsg = "Removed collections" 

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

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

40abortedMsg = "Aborted." 

41 

42 

43@click.command(cls=ButlerCommand) 

44@repo_argument(required=True) 

45@collection_argument( 

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

47) 

48@confirm_option() 

49@options_file_option() 

50def remove_collections(**kwargs: Any) -> None: 

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

52 

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

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

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

56 confirmation, but they will not be removed. 

57 

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

59 """ 

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

61 result = script.removeCollections(**kwargs) 

62 canRemoveCollections = len(result.removeCollectionsTable) 

63 doContinue = canRemoveCollections 

64 if confirm: 

65 if canRemoveCollections: 

66 print("\n" + willRemoveCollectionMsg) 

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

68 else: 

69 print("\n" + noNonRunCollectionsMsg) 

70 if len(result.runsTable): 

71 print("\n" + canNotRemoveFoundRuns) 

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

73 print() 

74 if canRemoveCollections: 

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

76 if doContinue: 

77 result.onConfirmation() 

78 if confirm: 

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

80 else: 

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

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

83 if len(result.runsTable): 

84 print("\n" + didNotRemoveFoundRuns) 

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

86 print() 

87 elif canRemoveCollections and not doContinue: 

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