Coverage for python/lsst/daf/butler/cli/cmd/commands.py : 70%

Hot-keys 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
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/>.
22import click
23import yaml
25from ..opt import (
26 collection_type_option,
27 collection_argument,
28 collections_option,
29 dataset_type_option,
30 datasets_option,
31 dimensions_argument,
32 directory_argument,
33 glob_argument,
34 options_file_option,
35 repo_argument,
36 transfer_option,
37 verbose_option,
38 where_option,
39)
41from ..utils import cli_handle_exception, split_commas, to_upper, typeStrAcceptsMultiple, unwrap
42from ... import script
45willCreateRepoHelp = "REPO is the URI or path to the new repository. Will be created if it does not exist."
46existingRepoHelp = "REPO is the URI or path to an existing data repository root or configuration file."
47whereHelp = unwrap("""A string expression similar to a SQL WHERE clause. May involve any column of a dimension
48 table or a dimension name as a shortcut for the primary key column of a dimension
49 table.""")
52# The conversion from the import command name to the butler_import function
53# name for subcommand lookup is implemented in the cli/butler.py, in
54# funcNameToCmdName and cmdNameToFuncName. If name changes are made here they
55# must be reflected in that location. If this becomes a common pattern a better
56# mechanism should be implemented.
57@click.command("import")
58@repo_argument(required=True, help=willCreateRepoHelp)
59@directory_argument(required=True)
60@transfer_option()
61@click.option("--export-file",
62 help="Name for the file that contains database information associated with the exported "
63 "datasets. If this is not an absolute path, does not exist in the current working "
64 "directory, and --dir is provided, it is assumed to be in that directory. Defaults "
65 "to \"export.yaml\".",
66 type=click.File("r"))
67@click.option("--skip-dimensions", "-s", type=str, multiple=True, callback=split_commas,
68 metavar=typeStrAcceptsMultiple,
69 help="Dimensions that should be skipped during import")
70@options_file_option()
71def butler_import(*args, **kwargs):
72 """Import data into a butler repository."""
73 cli_handle_exception(script.butlerImport, *args, **kwargs)
76@click.command()
77@repo_argument(required=True, help=willCreateRepoHelp)
78@click.option("--seed-config", help="Path to an existing YAML config file to apply (on top of defaults).")
79@click.option("--dimension-config", help="Path to an existing YAML config file with dimension configuration.")
80@click.option("--standalone", is_flag=True, help="Include all defaults in the config file in the repo, "
81 "insulating the repo from changes in package defaults.")
82@click.option("--override", is_flag=True, help="Allow values in the supplied config to override all "
83 "repo settings.")
84@click.option("--outfile", "-f", default=None, type=str, help="Name of output file to receive repository "
85 "configuration. Default is to write butler.yaml into the specified repo.")
86@options_file_option()
87def create(*args, **kwargs):
88 """Create an empty Gen3 Butler repository."""
89 cli_handle_exception(script.createRepo, *args, **kwargs)
92@click.command(short_help="Dump butler config to stdout.")
93@repo_argument(required=True, help=existingRepoHelp)
94@click.option("--subset", "-s", type=str,
95 help="Subset of a configuration to report. This can be any key in the hierarchy such as "
96 "'.datastore.root' where the leading '.' specified the delimiter for the hierarchy.")
97@click.option("--searchpath", "-p", type=str, multiple=True, callback=split_commas,
98 metavar=typeStrAcceptsMultiple,
99 help="Additional search paths to use for configuration overrides")
100@click.option("--file", "outfile", type=click.File("w"), default="-",
101 help="Print the (possibly-expanded) configuration for a repository to a file, or to stdout "
102 "by default.")
103@options_file_option()
104def config_dump(*args, **kwargs):
105 """Dump either a subset or full Butler configuration to standard output."""
106 cli_handle_exception(script.configDump, *args, **kwargs)
109@click.command(short_help="Validate the configuration files.")
110@repo_argument(required=True, help=existingRepoHelp)
111@click.option("--quiet", "-q", is_flag=True, help="Do not report individual failures.")
112@dataset_type_option(help="Specific DatasetType(s) to validate.", multiple=True)
113@click.option("--ignore", "-i", type=str, multiple=True, callback=split_commas,
114 metavar=typeStrAcceptsMultiple,
115 help="DatasetType(s) to ignore for validation.")
116@options_file_option()
117def config_validate(*args, **kwargs):
118 """Validate the configuration files for a Gen3 Butler repository."""
119 is_good = cli_handle_exception(script.configValidate, *args, **kwargs)
120 if not is_good:
121 raise click.exceptions.Exit(1)
124@click.command()
125@repo_argument(required=True)
126@collection_argument(help=unwrap("""COLLECTION is the Name of the collection to remove. If this is a tagged or
127 chained collection, datasets within the collection are not modified unless --unstore
128 is passed. If this is a run collection, --purge and --unstore must be passed, and
129 all datasets in it are fully removed from the data repository."""))
130@click.option("--purge",
131 help=unwrap("""Permit RUN collections to be removed, fully removing datasets within them.
132 Requires --unstore as an added precaution against accidental deletion. Must not be
133 passed if the collection is not a RUN."""),
134 is_flag=True)
135@click.option("--unstore",
136 help=("""Remove all datasets in the collection from all datastores in which they appear."""),
137 is_flag=True)
138@options_file_option()
139def prune_collection(**kwargs):
140 """Remove a collection and possibly prune datasets within it."""
141 cli_handle_exception(script.pruneCollection, **kwargs)
144@click.command(short_help="Search for collections.")
145@repo_argument(required=True)
146@glob_argument(help="GLOB is one or more glob-style expressions that fully or partially identify the "
147 "collections to return.")
148@collection_type_option()
149@click.option("--chains",
150 default="table",
151 help=unwrap("""Affects how results are presented. TABLE lists each dataset in a row with
152 chained datasets' children listed in a Definition column. TREE lists children below
153 their parent in tree form. FLATTEN lists all datasets, including child datasets in
154 one list.Defaults to TABLE. """),
155 callback=to_upper,
156 type=click.Choice(("TABLE", "TREE", "FLATTEN"), case_sensitive=False))
157@options_file_option()
158def query_collections(*args, **kwargs):
159 """Get the collections whose names match an expression."""
160 table = cli_handle_exception(script.queryCollections, *args, **kwargs)
161 # The unit test that mocks script.queryCollections does not return a table
162 # so we need the following `if`.
163 if table:
164 # When chains==TREE, the children of chained datasets are indented
165 # relative to their parents. For this to work properly the table must
166 # be left-aligned.
167 table.pprint_all(align="<")
170@click.command()
171@repo_argument(required=True)
172@glob_argument(help="GLOB is one or more glob-style expressions that fully or partially identify the "
173 "dataset types to return.")
174@verbose_option(help="Include dataset type name, dimensions, and storage class in output.")
175@click.option("--components/--no-components",
176 default=None,
177 help="For --components, apply all expression patterns to component dataset type names as well. "
178 "For --no-components, never apply patterns to components. Default (where neither is "
179 "specified) is to apply patterns to components only if their parent datasets were not "
180 "matched by the expression. Fully-specified component datasets (`str` or `DatasetType` "
181 "instances) are always included.")
182@options_file_option()
183def query_dataset_types(*args, **kwargs):
184 """Get the dataset types in a repository."""
185 print(yaml.dump(cli_handle_exception(script.queryDatasetTypes, *args, **kwargs), sort_keys=False))
188@click.command()
189@repo_argument(required=True)
190@click.argument('dataset-type-name', nargs=1)
191def remove_dataset_type(*args, **kwargs):
192 """Remove a dataset type definition from a repository."""
193 cli_handle_exception(script.removeDatasetType, *args, **kwargs)
196@click.command()
197@repo_argument(required=True)
198@glob_argument(help="GLOB is one or more glob-style expressions that fully or partially identify the "
199 "dataset types to be queried.")
200@collections_option()
201@where_option(help=whereHelp)
202@click.option("--find-first",
203 is_flag=True,
204 help=unwrap("""For each result data ID, only yield one DatasetRef of each DatasetType, from the
205 first collection in which a dataset of that dataset type appears (according to the
206 order of 'collections' passed in). If used, 'collections' must specify at least one
207 expression and must not contain wildcards."""))
208@click.option("--show-uri",
209 is_flag=True,
210 help="Show the dataset URI in results.")
211@options_file_option()
212def query_datasets(**kwargs):
213 """List the datasets in a repository."""
214 tables = cli_handle_exception(script.queryDatasets, **kwargs)
216 for table in tables:
217 print("")
218 table.pprint_all()
219 print("")
222@click.command()
223@repo_argument(required=True)
224@click.argument('input-collection')
225@click.argument('output-collection')
226@click.argument('dataset-type-name')
227@click.option("--begin-date", type=str, default=None,
228 help=unwrap("""ISO-8601 datetime (TAI) of the beginning of the validity range for the
229 certified calibrations."""))
230@click.option("--end-date", type=str, default=None,
231 help=unwrap("""ISO-8601 datetime (TAI) of the end of the validity range for the
232 certified calibrations."""))
233@click.option("--search-all-inputs", is_flag=True, default=False,
234 help=unwrap("""Search all children of the inputCollection if it is a CHAINED collection,
235 instead of just the most recent one."""))
236def certify_calibrations(*args, **kwargs):
237 """Certify calibrations in a repository.
238 """
239 cli_handle_exception(script.certifyCalibrations, *args, **kwargs)
242@click.command()
243@repo_argument(required=True)
244@dimensions_argument(help=unwrap("""DIMENSIONS are the keys of the data IDs to yield, such as exposure,
245 instrument, or tract. Will be expanded to include any dependencies."""))
246@collections_option()
247@datasets_option(help=unwrap("""An expression that fully or partially identifies dataset types that should
248 constrain the yielded data IDs. For example, including "raw" here would
249 constrain the yielded "instrument", "exposure", "detector", and
250 "physical_filter" values to only those for which at least one "raw" dataset
251 exists in "collections"."""))
252@where_option(help=whereHelp)
253@options_file_option()
254def query_data_ids(**kwargs):
255 """List the data IDs in a repository.
256 """
257 table = cli_handle_exception(script.queryDataIds, **kwargs)
258 if table:
259 table.pprint_all()
260 else:
261 if not kwargs.get("dimensions") and not kwargs.get("datasets"):
262 print("No results. Try requesting some dimensions or datasets, see --help for more information.")
263 else:
264 print("No results. Try --help for more information.")