587
|
1 |
#============================================================================
|
|
2 |
#Name : configuration_model.py
|
|
3 |
#Part of : Helium
|
|
4 |
|
|
5 |
#Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
6 |
#All rights reserved.
|
|
7 |
#This component and the accompanying materials are made available
|
|
8 |
#under the terms of the License "Eclipse Public License v1.0"
|
|
9 |
#which accompanies this distribution, and is available
|
|
10 |
#at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
|
11 |
#
|
|
12 |
#Initial Contributors:
|
|
13 |
#Nokia Corporation - initial contribution.
|
|
14 |
#
|
|
15 |
#Contributors:
|
|
16 |
#
|
|
17 |
#Description:
|
|
18 |
#===============================================================================
|
|
19 |
""" the model definition of property, group of property and configuration of property"""
|
|
20 |
|
|
21 |
import amara
|
|
22 |
import logging
|
|
23 |
import copy
|
|
24 |
|
|
25 |
_logger = logging.getLogger('configurationmodel')
|
|
26 |
#logging.basicConfig(level=logging.DEBUG)
|
|
27 |
_handler = logging.StreamHandler()
|
|
28 |
_handler.setFormatter(logging.Formatter('%(levelname)s: %(message)s'))
|
|
29 |
_logger.addHandler(_handler)
|
|
30 |
|
|
31 |
|
|
32 |
class PropertyDef(object):
|
|
33 |
""" The model definition of a property. """
|
|
34 |
|
|
35 |
def __init__(self, property_node):
|
|
36 |
""" Initialization. """
|
|
37 |
if not hasattr(property_node, 'name'):
|
|
38 |
raise Exception("Name is not defined for '" + str(property_node) + "'")
|
|
39 |
if not hasattr(property_node, 'type'):
|
|
40 |
raise Exception("Type is not defined for '" + str(property_node.name) + "'")
|
|
41 |
if not hasattr(property_node, 'description'):
|
|
42 |
raise Exception("Description is not defined for '" + str(property_node.name) + "'")
|
|
43 |
|
|
44 |
self.name = str(property_node.name)
|
|
45 |
self.editStatus = str(property_node.editStatus)
|
|
46 |
self.type = str(property_node.type)
|
|
47 |
self.description = str(property_node.description).strip()
|
|
48 |
|
|
49 |
if len(self.description) == 0:
|
|
50 |
_logger.log(logging.ERROR, "Description has no content for '" + str(property_node.name) + "'")
|
|
51 |
|
|
52 |
if hasattr(property_node, 'deprecated'):
|
|
53 |
self.deprecated = str(property_node.deprecated)
|
|
54 |
|
|
55 |
def __repr__(self):
|
|
56 |
return self.name
|
|
57 |
|
|
58 |
def __str__(self):
|
|
59 |
return self.name
|
|
60 |
|
|
61 |
|
|
62 |
class GroupDef(object):
|
|
63 |
""" The model definition of a group of properties. """
|
|
64 |
def __init__(self, group_node):
|
|
65 |
""" Initialization. """
|
|
66 |
self.name = str(group_node.name)
|
|
67 |
self.description = str(group_node.description)
|
|
68 |
self.properties = {}
|
|
69 |
self.propref = []
|
|
70 |
for property_ in group_node.propertyRef:
|
|
71 |
self.properties[str(property_)] = property_.usage
|
|
72 |
|
|
73 |
def check_config(self, config, items):
|
|
74 |
""" Checks that the set of properties in a group are properly defined. """
|
|
75 |
defined_props = [p for p in self.properties.keys() if config.has_key(p)]
|
|
76 |
if len(defined_props) > 0:
|
|
77 |
required_props = [p for p in self.properties.keys() if self.properties[p] == 'required']
|
|
78 |
required_not_defined_props = set(required_props).difference(set(defined_props))
|
|
79 |
for undefined_property in required_not_defined_props:
|
|
80 |
items.append(UndefinedRequiredInGroupItem((self.name, undefined_property)))
|
|
81 |
|
|
82 |
|
|
83 |
class DataModel(object):
|
|
84 |
""" A model of the configuration properties. """
|
|
85 |
def __init__(self, modelpath):
|
|
86 |
""" Initialization. """
|
|
87 |
doc = amara.parse(open(modelpath, 'r'))
|
|
88 |
|
|
89 |
self.properties = {}
|
|
90 |
self.required_props = []
|
|
91 |
for property_ in doc.heliumDataModel.property:
|
|
92 |
self.properties[str(property_.name)] = PropertyDef(property_)
|
|
93 |
|
|
94 |
self.nongroupedproperties = copy.copy(self.properties)
|
|
95 |
self.groups = {}
|
|
96 |
for group in doc.heliumDataModel.group:
|
|
97 |
groupobj = GroupDef(group)
|
|
98 |
self.groups[str(group.name)] = groupobj
|
|
99 |
|
|
100 |
for prop in groupobj.properties:
|
|
101 |
groupobj.propref.append(self.properties[prop])
|
|
102 |
if prop in self.nongroupedproperties:
|
|
103 |
del self.nongroupedproperties[prop]
|
|
104 |
|
|
105 |
groupobj.propref.sort()
|
|
106 |
|
|
107 |
required_props_in_group = [p for p in group.propertyRef if p.usage == 'required']
|
|
108 |
|
|
109 |
for required_prop in required_props_in_group:
|
|
110 |
self.required_props.append(required_prop)
|
|
111 |
if not self.properties.has_key(str(required_prop)):
|
|
112 |
raise Exception("Required property " + str(required_prop) + " is not defined!")
|
|
113 |
|
|
114 |
def validate_config(self, config):
|
|
115 |
""" Validate the Ant configuration against the model. """
|
|
116 |
items = []
|
|
117 |
self._check_deprecated_properties(config, items)
|
|
118 |
self._check_undefined_properties(config, items)
|
|
119 |
self._check_undefined_properties_in_groups(config, items)
|
|
120 |
self._check_type_validation(config, items)
|
|
121 |
self._check_defined_properties_not_in_groups()
|
|
122 |
return items
|
|
123 |
|
|
124 |
def validate_config_at_startup(self, config):
|
|
125 |
""" Validate the Ant configuration against the model at build startup. """
|
|
126 |
items = []
|
|
127 |
|
|
128 |
for p_props in self.required_props:
|
|
129 |
if not config.has_key(str(p_props)):
|
|
130 |
print "Required property " + str(p_props) + " is not defined!"
|
|
131 |
|
|
132 |
return items
|
|
133 |
|
|
134 |
def _check_deprecated_properties(self, config, items):
|
|
135 |
""" Check that deprecated properties not being used. """
|
|
136 |
deprecated_props = [p for p in self.properties.values() if hasattr(p, 'deprecated')]
|
|
137 |
for deprecated_prop in deprecated_props:
|
|
138 |
_logger.debug('Checking deprecated property: ' + str(deprecated_prop))
|
|
139 |
if config.has_key(str(deprecated_prop)):
|
|
140 |
items.append(UsingDeprecatedItem(deprecated_prop))
|
|
141 |
|
|
142 |
def _check_undefined_properties(self, config, items):
|
|
143 |
""" Check for any defined properties that are not in the model. """
|
|
144 |
undefined_properties = [p_key for p_key in config.keys() if p_key not in self.properties]
|
|
145 |
undefined_properties.sort()
|
|
146 |
for undefined_property in undefined_properties:
|
|
147 |
items.append(MissingFromDataModelItem(undefined_property))
|
|
148 |
|
|
149 |
def _check_undefined_properties_in_groups(self, config, items):
|
|
150 |
""" Check for any undefined properties that are in the group. """
|
|
151 |
for group in self.groups.values():
|
|
152 |
_logger.debug('Checking group: %s' % group.name)
|
|
153 |
group.check_config(config, items)
|
|
154 |
|
|
155 |
def _check_defined_properties_not_in_groups(self):
|
|
156 |
""" Check for any defined properties that are not in the group. """
|
|
157 |
g_props = []
|
|
158 |
for group in self.groups.values():
|
|
159 |
g_props = g_props + group.properties.keys()
|
|
160 |
for p_props in self.properties.values():
|
|
161 |
if not str(p_props) in g_props:
|
|
162 |
raise Exception(str(p_props) + ' not in a group')
|
|
163 |
|
|
164 |
def _check_type_validation(self, config, items):
|
|
165 |
""" Check the type validation. """
|
|
166 |
prop_string = [p_props for p_props in self.properties.values() if p_props.type == 'string']
|
|
167 |
prop_integer = [p_props for p_props in self.properties.values() if p_props.type == 'integer']
|
|
168 |
prop_boolean = [p_props for p_props in self.properties.values() if p_props.type == 'boolean']
|
|
169 |
#prop_flag = [p_props for p_props in self.properties.values() if p_props.type == 'flag']
|
|
170 |
|
|
171 |
for prop in prop_integer:
|
|
172 |
if config.has_key(str(prop)):
|
|
173 |
if not config[str(prop)].isdigit():
|
|
174 |
items.append(WrongTypeItem(("integer", prop)))
|
|
175 |
|
|
176 |
for prop in prop_boolean:
|
|
177 |
if config.has_key(str(prop)) :
|
|
178 |
if not (config[str(prop)] == 'false' or 'true'):
|
|
179 |
items.append(WrongTypeItem(("boolean", prop)))
|
|
180 |
|
|
181 |
for prop in prop_string:
|
|
182 |
if config.has_key(str(prop)):
|
|
183 |
if len(config[str(prop)]) == 0:
|
|
184 |
items.append(WrongTypeItem(("string", prop)))
|
|
185 |
|
|
186 |
|
|
187 |
class Item(object):
|
|
188 |
""" Base class item """
|
|
189 |
level = logging.INFO
|
|
190 |
message = ''
|
|
191 |
|
|
192 |
def __init__(self, values):
|
|
193 |
""" Initialization. """
|
|
194 |
self.values = values
|
|
195 |
|
|
196 |
def log(self, logger):
|
|
197 |
"""log an error"""
|
|
198 |
logger.log(self.level, str(self))
|
|
199 |
|
|
200 |
def __str__(self):
|
|
201 |
"""string conversion"""
|
|
202 |
return self.message % self.values
|
|
203 |
|
|
204 |
|
|
205 |
class MissingFromDataModelItem(Item):
|
|
206 |
""" info message """
|
|
207 |
level = logging.INFO
|
|
208 |
message = 'Property not in data model: %s'
|
|
209 |
|
|
210 |
|
|
211 |
class UsingDeprecatedItem(Item):
|
|
212 |
""" warning msg """
|
|
213 |
level = logging.WARNING
|
|
214 |
message = 'Deprecated property used: %s'
|
|
215 |
|
|
216 |
|
|
217 |
class UndefinedRequiredInGroupItem(Item):
|
|
218 |
""" warning msg """
|
|
219 |
level = logging.WARNING
|
|
220 |
message = 'Required property in %s group is not defined: %s'
|
|
221 |
|
|
222 |
|
|
223 |
class WrongTypeItem(Item):
|
|
224 |
""" warning msg """
|
|
225 |
level = logging.WARNING
|
|
226 |
message = 'Invalid %s value: %s' |