Coverage for python/lsst/daf/butler/script/validateButlerConfiguration.py : 17%

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#!/usr/bin/env python
3# This file is part of daf_butler.
4#
5# Developed for the LSST Data Management System.
6# This product includes software developed by the LSST Project
7# (http://www.lsst.org).
8# See the COPYRIGHT file at the top-level directory of this distribution
9# for details of code ownership.
10#
11# This program is free software: you can redistribute it and/or modify
12# it under the terms of the GNU General Public License as published by
13# the Free Software Foundation, either version 3 of the License, or
14# (at your option) any later version.
15#
16# This program is distributed in the hope that it will be useful,
17# but WITHOUT ANY WARRANTY; without even the implied warranty of
18# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19# GNU General Public License for more details.
20#
21# You should have received a copy of the GNU General Public License
22# along with this program. If not, see <http://www.gnu.org/licenses/>.
24__all__ = ("main",)
26import argparse
28from lsst.daf.butler import Butler, ValidationError
31def processCommas(arg):
32 """Given a list that might contain strings with commas, return expanded
33 list.
35 Parameters
36 ----------
37 arg : iterable of `str`
38 Values read from command line.
40 Returns
41 -------
42 expanded : `list` of `str`
43 List where any items with commas are expanded into multiple entries.
44 """
45 expanded = []
46 if arg is None:
47 return expanded
48 for item in arg:
49 expanded.extend(item.split(","))
50 return expanded
53def build_argparser():
54 """Construct an argument parser for the ``validateButlerConfiguration``
55 script.
57 Returns
58 -------
59 argparser : `argparse.ArgumentParser`
60 The argument parser that defines the script
61 command-line interface.
62 """
63 parser = argparse.ArgumentParser(description="Validate the configuration files for a "
64 "Gen3 Butler repository.")
65 parser.add_argument("root",
66 help="Filesystem path for an existing Butler repository.")
67 parser.add_argument("--collection", "-c", default="validate", type=str,
68 help="Collection to refer to in this repository.")
69 parser.add_argument("--quiet", "-q", action="store_true",
70 help="Do not report individual failures.")
71 parser.add_argument("--datasettype", "-d", action="append", type=str,
72 help="Specific DatasetType(s) to validate (can be comma-separated)")
73 parser.add_argument("--ignore", "-i", action="append", type=str,
74 help="DatasetType(s) to ignore for validation (can be comma-separated)")
76 return parser
79def validateButlerConfiguration(root, datasetTypes=None, ignore=None, quiet=False, collection="validate"):
80 """Validate a bulter configuration.
82 Parameters
83 ----------
84 root : `str`
85 Butler root to validate.
86 datasetTypes : `list` of `str`
87 Dataset types to specifically check.
88 ignore : `list` of `str`
89 Dataset types to ignore.
90 quiet : `bool`, optional
91 If `True` report pass/fail but not details.
92 collection : `str`, optional
93 Collection to use. Sometimes this is needed to ensure that butler
94 can be instantiated properly.
96 Returns
97 -------
98 validates : `bool`
99 `True` if everything looks okay, `False` if there is a problem.
100 """
101 logFailures = not quiet
103 # The collection does not matter for validation but if a run is specified
104 # in the configuration then it must be consistent with this collection
105 butler = Butler(config=root, collection=collection)
106 try:
107 butler.validateConfiguration(logFailures=logFailures, datasetTypeNames=datasetTypes,
108 ignore=ignore)
109 except ValidationError:
110 return False
112 return True
115def main():
116 args = build_argparser().parse_args()
118 # Process any commas in dataset type or ignore list
119 ignore = processCommas(args.ignore)
120 datasetTypes = processCommas(args.datasettype)
122 valid = validateButlerConfiguration(args.root, datasetTypes, ignore, args.quiet, args.collection)
124 if valid:
125 print("No problems encountered with configuration.")
126 return 0
127 else:
128 return 1