Coverage for python/lsst/daf/butler/cli/cmd/_remove_collections.py: 42%
46 statements
« prev ^ index » next coverage.py v7.3.2, created at 2023-12-01 11:00 +0000
« 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
29__all__ = ["remove_collections"]
31from typing import Any
33import click
35from ... import script
36from ..opt import collection_argument, confirm_option, options_file_option, repo_argument
37from ..utils import ButlerCommand
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"
44canNotRemoveFoundRuns = "The following RUN collections were found but can NOT be removed by this command:"
45didNotRemoveFoundRuns = "Found RUN collections but they can NOT be removed by this command:"
46abortedMsg = "Aborted."
49@click.command(cls=ButlerCommand)
50@repo_argument(required=True)
51@collection_argument(
52 help="COLLECTION is a glob-style expression that identifies the collection(s) to remove."
53)
54@confirm_option()
55@options_file_option()
56def remove_collections(**kwargs: Any) -> None:
57 """Remove one or more non-RUN collections.
59 This command can be used to remove only non-RUN collections. If RUN
60 collections are found when searching for collections (and the --no-confirm
61 flag is not used), then they will be shown in a separate table during
62 confirmation, but they will not be removed.
64 Use the remove-runs subcommand to remove RUN collections.
65 """
66 confirm: bool = kwargs.pop("confirm")
67 result = script.removeCollections(**kwargs)
68 canRemoveCollections = len(result.removeCollectionsTable)
69 doContinue = canRemoveCollections
70 if confirm:
71 if canRemoveCollections:
72 print("\n" + willRemoveCollectionMsg)
73 result.removeCollectionsTable.pprint_all(align="<")
74 else:
75 print("\n" + noNonRunCollectionsMsg)
76 if len(result.runsTable):
77 print("\n" + canNotRemoveFoundRuns)
78 result.runsTable.pprint_all(align="<")
79 print()
80 if canRemoveCollections:
81 doContinue = click.confirm(text="Continue?", default=False)
82 if doContinue:
83 result.onConfirmation()
84 if confirm:
85 print("\n" + removedCollectionsMsg + ".\n")
86 else:
87 print("\n" + removedCollectionsMsg + ":\n")
88 result.removeCollectionsTable.pprint_all(align="<")
89 if len(result.runsTable):
90 print("\n" + didNotRemoveFoundRuns)
91 result.runsTable.pprint_all(align="<")
92 print()
93 elif canRemoveCollections and not doContinue:
94 print("\n" + abortedMsg + "\n")