0
|
1 |
# Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
2 |
# All rights reserved.
|
|
3 |
# This component and the accompanying materials are made available
|
3
|
4 |
# under the terms of the License "Symbian Foundation License v1.0"
|
0
|
5 |
# which accompanies this distribution, and is available
|
3
|
6 |
# at the URL "http://www.symbianfoundation.org/legal/sfl-v10.html".
|
0
|
7 |
#
|
|
8 |
# Initial Contributors:
|
|
9 |
# Nokia Corporation - initial contribution.
|
|
10 |
#
|
|
11 |
# Contributors:
|
|
12 |
#
|
|
13 |
# Description:
|
|
14 |
#
|
|
15 |
|
|
16 |
'''
|
|
17 |
Ruleml eval extension to check is passed ref changed on given layer(range).
|
|
18 |
'''
|
|
19 |
|
3
|
20 |
from cone.public import api, plugin
|
0
|
21 |
|
|
22 |
import logging
|
3
|
23 |
import re
|
0
|
24 |
|
|
25 |
logger = logging.getLogger('cone.ruleplugin.evals.layer_utils')
|
|
26 |
|
3
|
27 |
|
|
28 |
def layers_used(config, layers_or_regex, impl_tags):
|
|
29 |
"""
|
|
30 |
Return True if matching layers are used by any implementation with given tag.
|
|
31 |
@param config: The configuration.
|
|
32 |
@param layers_or_regex: List of layer objects to check against, or regular
|
|
33 |
expression (string) for resolving the list of layers.
|
|
34 |
@param impl_tags: Implementation tags to use.
|
|
35 |
"""
|
|
36 |
# Resolve layer list
|
|
37 |
if isinstance(layers_or_regex, basestring):
|
|
38 |
layers = []
|
|
39 |
for lay in config.list_configurations():
|
|
40 |
if re.search(layers_or_regex, lay):
|
|
41 |
layers.append(config.get_configuration(lay))
|
|
42 |
else:
|
|
43 |
layers = layers_or_regex
|
|
44 |
|
|
45 |
impls = plugin.filtered_impl_set(config).filter_implementations(tags=impl_tags)
|
|
46 |
context = plugin.GenerationContext(tags=impl_tags,
|
|
47 |
configuration=config)
|
|
48 |
for impl in impls:
|
|
49 |
if impl.uses_layers(layers, context) == True:
|
|
50 |
return True
|
|
51 |
return False
|
|
52 |
|
|
53 |
|
|
54 |
def get_all_layers(feat):
|
|
55 |
"""
|
|
56 |
Returns all layers.
|
|
57 |
"""
|
|
58 |
|
|
59 |
root_conf = feat.get_root_configuration()
|
|
60 |
result = []
|
|
61 |
|
|
62 |
for i in range(0, len(root_conf.list_configurations())):
|
|
63 |
result.append(root_conf.get_configuration_by_index(i))
|
|
64 |
return result
|
|
65 |
|
0
|
66 |
def give_changed_layers(feat):
|
|
67 |
"""
|
|
68 |
Returns a list of booleans where True means that feature is changed in that layer. Index is
|
|
69 |
same than in configuration root's get_configuration_by_index().
|
|
70 |
"""
|
|
71 |
|
|
72 |
logger.debug('Checking feature: %s' % feat.fqr)
|
|
73 |
|
|
74 |
root_conf = feat.get_root_configuration()
|
|
75 |
nro_of_layers = len(root_conf.list_configurations())
|
3
|
76 |
result = []
|
0
|
77 |
for i in range(0, nro_of_layers):
|
3
|
78 |
result.append(_changed_on_layer(feat, root_conf.get_configuration_by_index(i)))
|
0
|
79 |
return result
|
|
80 |
|
|
81 |
def changed_on_last_layer(feat):
|
|
82 |
"""
|
|
83 |
Returns True if feature is changed in the last layer.
|
|
84 |
"""
|
|
85 |
|
|
86 |
root_conf = feat.get_root_configuration()
|
|
87 |
conf = root_conf.get_configuration_by_index(-2)#autoconfig layer is ignored
|
3
|
88 |
return _changed_on_layer(feat, conf)
|
0
|
89 |
|
|
90 |
def changed_on_autoconfig_layer(feat):
|
|
91 |
"""
|
|
92 |
Returns True if feature is changed in the autoconfig layer.
|
|
93 |
"""
|
|
94 |
|
|
95 |
root_conf = feat.get_root_configuration()
|
3
|
96 |
conf = root_conf.get_configuration_by_index(-1)
|
|
97 |
return _changed_on_layer(feat, conf)
|
0
|
98 |
|
|
99 |
def changed_on_layer(feat, layer):
|
|
100 |
"""
|
|
101 |
Returns True if feature is changed in the given layer.
|
|
102 |
"""
|
|
103 |
try:
|
|
104 |
return give_changed_layers(feat)[layer]
|
3
|
105 |
except IndexError:
|
0
|
106 |
logger.warning("Given layer is not found: %s" % (layer))
|
|
107 |
return False
|
|
108 |
|
3
|
109 |
def _changed_on_layer(feature, layer_obj):
|
|
110 |
"""
|
|
111 |
Return whether the given feature is changed on the given layer.
|
|
112 |
"""
|
|
113 |
# Check recursively if the layer contains any data objects with
|
|
114 |
# the same FQR as the feature
|
|
115 |
def check(node):
|
|
116 |
if isinstance(node, api.Data) and node.fqr == feature.fqr:
|
|
117 |
return True
|
|
118 |
for obj in node._objects():
|
|
119 |
if check(obj):
|
|
120 |
return True
|
|
121 |
return False
|
|
122 |
|
|
123 |
return check(layer_obj)
|
|
124 |
|
0
|
125 |
def changed_on_layers(feat, findex, tindex):
|
|
126 |
"""
|
|
127 |
Returns True if feature is changed in layer of given range.
|
|
128 |
"""
|
3
|
129 |
root_conf = feat.get_root_configuration()
|
|
130 |
nro_of_layers = len(root_conf.list_configurations())
|
0
|
131 |
|
3
|
132 |
# Convert negative indices to positive
|
|
133 |
def neg_index_to_pos(index):
|
|
134 |
if index < 0: return nro_of_layers + index
|
|
135 |
else: return index
|
|
136 |
begin = neg_index_to_pos(findex)
|
|
137 |
end = neg_index_to_pos(tindex)
|
0
|
138 |
|
3
|
139 |
if end == begin:
|
|
140 |
return changed_on_layer(feat, begin)
|
|
141 |
|
|
142 |
# Check the layers inside the range
|
|
143 |
if end > begin: index_range = xrange(begin, end)
|
|
144 |
else: index_range = xrange(end, begin)
|
|
145 |
for i in index_range:
|
|
146 |
if i < 0 or i >= nro_of_layers:
|
0
|
147 |
continue
|
3
|
148 |
|
|
149 |
layer = root_conf.get_configuration_by_index(i)
|
|
150 |
if _changed_on_layer(feat, layer):
|
0
|
151 |
return True
|
|
152 |
return False
|
|
153 |
|
3
|
154 |
def changed_on_layers_regex(feat, regex):
|
|
155 |
"""
|
|
156 |
Return whether the given feature is changed on any layer that matches
|
|
157 |
the given regular expression.
|
|
158 |
"""
|
|
159 |
pattern = re.compile(regex)
|
|
160 |
root_conf = feat.get_root_configuration()
|
|
161 |
for config_path in root_conf.list_configurations():
|
|
162 |
if pattern.search(config_path):
|
|
163 |
layer = root_conf.get_configuration(config_path)
|
|
164 |
if _changed_on_layer(feat, layer):
|
|
165 |
return True
|
|
166 |
return False
|
|
167 |
|
|
168 |
def changed_on_custvariant_layer(feat):
|
|
169 |
"""
|
|
170 |
Return whether the given feature is changed on any of the custvariant layers
|
|
171 |
(layers that match the regex '/custvariant(_.*)?/').
|
|
172 |
"""
|
|
173 |
return changed_on_layers_regex(feat, r'/custvariant(_.*)?/')
|