Hide keyboard shortcuts

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 

2 

3# 

4# LSST Data Management System 

5# Copyright 2008, 2009, 2010 LSST Corporation. 

6# 

7# This product includes software developed by the 

8# LSST Project (http://www.lsst.org/). 

9# 

10# This program is free software: you can redistribute it and/or modify 

11# it under the terms of the GNU General Public License as published by 

12# the Free Software Foundation, either version 3 of the License, or 

13# (at your option) any later version. 

14# 

15# This program is distributed in the hope that it will be useful, 

16# but WITHOUT ANY WARRANTY; without even the implied warranty of 

17# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

18# GNU General Public License for more details. 

19# 

20# You should have received a copy of the LSST License Statement and 

21# the GNU General Public License along with this program. If not, 

22# see <http://www.lsstcorp.org/LegalNotices/>. 

23# 

24 

25# 

26import optparse 

27import sys 

28import os 

29 

30from lsst.pex.policy import Policy, Dictionary 

31import lsst.pex.exceptions 

32 

33usage = """usage: %prog [--help] <options> [policy] [dictionary]""" 

34 

35desc = """ 

36Validate a policy file against a dictionary (policy schema). 

37""" 

38 

39 

40class PolicyValidator: 

41 def __init__(self): 

42 self.verbose = False 

43 

44 def main(self, argv=None): 

45 self.parseArgs(argv) 

46 

47 # 1. load policy 

48 policy = self.tryThis(Policy, 

49 "reading policy file \"" + self.policyFile + "\"", 

50 self.policyFile) 

51 

52 # resolve policy file references 

53 polLoadDir = self.options.loadPolicy 

54 polLoadDesc = polLoadDir 

55 if polLoadDir is None: 55 ↛ 56line 55 didn't jump to line 56, because the condition on line 55 was never true

56 if self.verbose: 

57 print("No policy load dir specified; using current dir.") 

58 polLoadDir = "" 

59 polLoadDesc = "current directory; " \ 

60 "try -l DIR or --load-policy-references=DIR" 

61 message = "resolving references in " + self.policyFile + ",\n using " \ 

62 + polLoadDesc 

63 self.tryThis(policy.loadPolicyFiles, message, polLoadDir, True) 

64 

65 # 2. load dictionary 

66 dictionary = self.tryThis(Dictionary, 

67 "reading dictionary file \"" + self.dictFile + "\"", 

68 self.dictFile) 

69 

70 # resolve dictionary file references 

71 dictLoadDir = self.options.loadDict 

72 dictLoadDesc = dictLoadDir 

73 if (dictLoadDir is None): 73 ↛ 74line 73 didn't jump to line 74, because the condition on line 73 was never true

74 if self.verbose: 

75 print("No dictionary load dir specified; using policy load dir", 

76 polLoadDesc) 

77 if polLoadDir != "": 

78 dictLoadDir = polLoadDir 

79 dictLoadDesc = polLoadDesc 

80 else: 

81 dictLoadDir = "" 

82 dictLoadDesc = "current directory; " \ 

83 "try -l DIR or --load-dictionary-references=DIR" 

84 message = "resolving references in " + self.dictFile + ",\n" \ 

85 " using " + dictLoadDesc 

86 self.tryThis(dictionary.loadPolicyFiles, message, dictLoadDir, True) 

87 

88 # 3. merge defaults into policy 

89 defaults = None 

90 defaultsFile = self.options.defaults 

91 if (defaultsFile is not None): 91 ↛ 92line 91 didn't jump to line 92, because the condition on line 91 was never true

92 defaults = self.tryThis(Policy, 

93 "reading defaults from \"" + defaultsFile + "\"", 

94 defaultsFile) 

95 else: 

96 defaults = dictionary # if no defaults file specified, use dictionary 

97 self.tryThis(policy.mergeDefaults, "merging defaults into policy", defaults) 

98 

99 # 4. validate 

100 self.tryThis(dictionary.validate, 

101 "validating " + self.policyFile + "\n against " + self.dictFile, 

102 policy) 

103 

104 if self.verbose: 104 ↛ 106line 104 didn't jump to line 106, because the condition on line 104 was never false

105 print() 

106 print("Validation passed:") 

107 print(" policy: " + self.policyFile) 

108 print(" is a valid instance of") 

109 print(" dictionary: " + self.dictFile) 

110 

111 def tryThis(self, callableObj, explain, *args, **kwargs): 

112 try: 

113 if self.verbose: 113 ↛ 115line 113 didn't jump to line 115, because the condition on line 113 was never false

114 print(explain) 

115 result = callableObj(*args, **kwargs) 

116 return result 

117 except lsst.pex.exceptions.Exception as e: 

118 print("error", explain + ":") 

119 print(e.args[0].what()) 

120 sys.exit(2) 

121 

122 def parseArgs(self, argv=None): 

123 # see http://docs.python.org/library/optparse.html 

124 self.parser = optparse.OptionParser(usage=usage, description=desc) # parasoft-suppress W0201 

125 self.parser.add_option("-d", "--dictionary", dest="dictionary", metavar="FILE", 

126 help="The dictionary to validate a policy against.") 

127 self.parser.add_option("-p", "--policy", dest="policy", metavar="FILE", 

128 help="the policy to validate") 

129 self.parser.add_option("-l", "--load-policy-references", dest="loadPolicy", 

130 metavar="DIR", 

131 help="Directory from which to load policy file " 

132 "references (if not specified, load from " 

133 "current directory).") 

134 self.parser.add_option("-L", "--load-dictionary-references", dest="loadDict", 

135 metavar="DIR", 

136 help="Directory from which to load dictionary " 

137 "file references (if not specified, load from " 

138 "policy references dir).") 

139 self.parser.add_option("-D", "--load-defaults", dest="defaults", metavar="FILE", 

140 help="Load defaults from FILE, which can be a policy " 

141 "or dictionary. If not specified, load defaults from " 

142 "the dictionary being used for validation.") 

143 self.parser.add_option("-v", "--verbose", dest="verbose", 

144 action="store_const", const=True, 

145 help="Print extra messages.") 

146 

147 if argv is None: 147 ↛ 149line 147 didn't jump to line 149, because the condition on line 147 was never false

148 argv = sys.argv 

149 (self.options, args) = self.parser.parse_args(argv) # parasoft-suppress W0201 

150 # print("args =", args, len(args)) 

151 # print("options =", self.options) 

152 if (self.options.verbose is not None): 152 ↛ 155line 152 didn't jump to line 155, because the condition on line 152 was never false

153 self.verbose = self.options.verbose 

154 

155 self.policyFile = self.options.policy # parasoft-suppress W0201 

156 self.dictFile = self.options.dictionary # parasoft-suppress W0201 

157 del args[0] # script name 

158 if (self.policyFile is None): 158 ↛ 163line 158 didn't jump to line 163, because the condition on line 158 was never false

159 if len(args) < 1: 159 ↛ 160line 159 didn't jump to line 160, because the condition on line 159 was never true

160 self.parser.error("no policy specified") 

161 self.policyFile = args[0] 

162 del(args[0]) 

163 if (self.dictFile is None): 163 ↛ 169line 163 didn't jump to line 169, because the condition on line 163 was never false

164 if len(args) < 1: 164 ↛ 165line 164 didn't jump to line 165, because the condition on line 164 was never true

165 self.parser.error("no dictionary specified") 

166 self.dictFile = args[0] 

167 del(args[0]) 

168 

169 if not os.path.exists(self.policyFile): 169 ↛ 170line 169 didn't jump to line 170, because the condition on line 169 was never true

170 self.parser.error("file not found: " + self.policyFile) 

171 if not os.path.exists(self.dictFile): 171 ↛ 172line 171 didn't jump to line 172, because the condition on line 171 was never true

172 self.parser.error("file not found: " + self.dictFile) 

173 

174 if len(args) != 0: 174 ↛ 175line 174 didn't jump to line 175, because the condition on line 174 was never true

175 self.parser.error("too many arguments: " + str(args) + " were not parsed.") 

176 

177 

178if __name__ == "__main__": 178 ↛ exitline 178 didn't exit the module, because the condition on line 178 was never false

179 pv = PolicyValidator() 

180 pv.main() 

181 sys.exit(0)