#! /usr/bin/env python
#
# LSST Data Management System
# Copyright 2008, 2009, 2010 LSST Corporation.
#
# This product includes software developed by the
# LSST Project (http://www.lsst.org/).
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the LSST License Statement and
# the GNU General Public License along with this program. If not,
# see <http://www.lsstcorp.org/LegalNotices/>.
#
#
import optparse
import sys
import os
from lsst.pex.policy import Policy, Dictionary
import lsst.pex.exceptions
usage = """usage: %prog [--help] <options> [policy] [dictionary]"""
desc = """
Validate a policy file against a dictionary (policy schema).
"""
class PolicyValidator:
def __init__(self):
self.verbose = False
def main(self, argv=None):
self.parseArgs(argv)
# 1. load policy
policy = self.tryThis(Policy,
"reading policy file \"" + self.policyFile + "\"",
self.policyFile)
# resolve policy file references
polLoadDir = self.options.loadPolicy
polLoadDesc = polLoadDir
55 ↛ 56line 55 didn't jump to line 56, because the condition on line 55 was never true if polLoadDir is None:
if self.verbose:
print("No policy load dir specified; using current dir.")
polLoadDir = ""
polLoadDesc = "current directory; " \
"try -l DIR or --load-policy-references=DIR"
message = "resolving references in " + self.policyFile + ",\n using " \
+ polLoadDesc
self.tryThis(policy.loadPolicyFiles, message, polLoadDir, True)
# 2. load dictionary
dictionary = self.tryThis(Dictionary,
"reading dictionary file \"" + self.dictFile + "\"",
self.dictFile)
# resolve dictionary file references
dictLoadDir = self.options.loadDict
dictLoadDesc = dictLoadDir
73 ↛ 74line 73 didn't jump to line 74, because the condition on line 73 was never true if (dictLoadDir is None):
if self.verbose:
print("No dictionary load dir specified; using policy load dir",
polLoadDesc)
if polLoadDir != "":
dictLoadDir = polLoadDir
dictLoadDesc = polLoadDesc
else:
dictLoadDir = ""
dictLoadDesc = "current directory; " \
"try -l DIR or --load-dictionary-references=DIR"
message = "resolving references in " + self.dictFile + ",\n" \
" using " + dictLoadDesc
self.tryThis(dictionary.loadPolicyFiles, message, dictLoadDir, True)
# 3. merge defaults into policy
defaults = None
defaultsFile = self.options.defaults
91 ↛ 92line 91 didn't jump to line 92, because the condition on line 91 was never true if (defaultsFile is not None):
defaults = self.tryThis(Policy,
"reading defaults from \"" + defaultsFile + "\"",
defaultsFile)
else:
defaults = dictionary # if no defaults file specified, use dictionary
self.tryThis(policy.mergeDefaults, "merging defaults into policy", defaults)
# 4. validate
self.tryThis(dictionary.validate,
"validating " + self.policyFile + "\n against " + self.dictFile,
policy)
104 ↛ 106line 104 didn't jump to line 106, because the condition on line 104 was never false if self.verbose:
print()
print("Validation passed:")
print(" policy: " + self.policyFile)
print(" is a valid instance of")
print(" dictionary: " + self.dictFile)
def tryThis(self, callableObj, explain, *args, **kwargs):
try:
113 ↛ 115line 113 didn't jump to line 115, because the condition on line 113 was never false if self.verbose:
print(explain)
result = callableObj(*args, **kwargs)
return result
except lsst.pex.exceptions.Exception as e:
print("error", explain + ":")
print(e.args[0].what())
sys.exit(2)
def parseArgs(self, argv=None):
# see http://docs.python.org/library/optparse.html
self.parser = optparse.OptionParser(usage=usage, description=desc) # parasoft-suppress W0201
self.parser.add_option("-d", "--dictionary", dest="dictionary", metavar="FILE",
help="The dictionary to validate a policy against.")
self.parser.add_option("-p", "--policy", dest="policy", metavar="FILE",
help="the policy to validate")
self.parser.add_option("-l", "--load-policy-references", dest="loadPolicy",
metavar="DIR",
help="Directory from which to load policy file "
"references (if not specified, load from "
"current directory).")
self.parser.add_option("-L", "--load-dictionary-references", dest="loadDict",
metavar="DIR",
help="Directory from which to load dictionary "
"file references (if not specified, load from "
"policy references dir).")
self.parser.add_option("-D", "--load-defaults", dest="defaults", metavar="FILE",
help="Load defaults from FILE, which can be a policy "
"or dictionary. If not specified, load defaults from "
"the dictionary being used for validation.")
self.parser.add_option("-v", "--verbose", dest="verbose",
action="store_const", const=True,
help="Print extra messages.")
147 ↛ 149line 147 didn't jump to line 149, because the condition on line 147 was never false if argv is None:
argv = sys.argv
(self.options, args) = self.parser.parse_args(argv) # parasoft-suppress W0201
# print("args =", args, len(args))
# print("options =", self.options)
152 ↛ 155line 152 didn't jump to line 155, because the condition on line 152 was never false if (self.options.verbose is not None):
self.verbose = self.options.verbose
self.policyFile = self.options.policy # parasoft-suppress W0201
self.dictFile = self.options.dictionary # parasoft-suppress W0201
del args[0] # script name
158 ↛ 163line 158 didn't jump to line 163, because the condition on line 158 was never false if (self.policyFile is None):
159 ↛ 160line 159 didn't jump to line 160, because the condition on line 159 was never true if len(args) < 1:
self.parser.error("no policy specified")
self.policyFile = args[0]
del(args[0])
163 ↛ 169line 163 didn't jump to line 169, because the condition on line 163 was never false if (self.dictFile is None):
164 ↛ 165line 164 didn't jump to line 165, because the condition on line 164 was never true if len(args) < 1:
self.parser.error("no dictionary specified")
self.dictFile = args[0]
del(args[0])
169 ↛ 170line 169 didn't jump to line 170, because the condition on line 169 was never true if not os.path.exists(self.policyFile):
self.parser.error("file not found: " + self.policyFile)
171 ↛ 172line 171 didn't jump to line 172, because the condition on line 171 was never true if not os.path.exists(self.dictFile):
self.parser.error("file not found: " + self.dictFile)
174 ↛ 175line 174 didn't jump to line 175, because the condition on line 174 was never true if len(args) != 0:
self.parser.error("too many arguments: " + str(args) + " were not parsed.")
178 ↛ exitline 178 didn't exit the module, because the condition on line 178 was never falseif __name__ == "__main__":
pv = PolicyValidator()
pv.main()
sys.exit(0)
|