0
|
1 |
#
|
|
2 |
# Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
3 |
# All rights reserved.
|
|
4 |
# This component and the accompanying materials are made available
|
|
5 |
# under the terms of "Eclipse Public License v1.0"
|
|
6 |
# which accompanies this distribution, and is available
|
|
7 |
# at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
|
8 |
#
|
|
9 |
# Initial Contributors:
|
|
10 |
# Nokia Corporation - initial contribution.
|
|
11 |
#
|
|
12 |
# Contributors:
|
|
13 |
#
|
|
14 |
# Description:
|
|
15 |
#
|
|
16 |
|
|
17 |
import os
|
|
18 |
import sys
|
|
19 |
import logging
|
|
20 |
from optparse import OptionParser, OptionGroup
|
|
21 |
import codecs
|
|
22 |
|
|
23 |
import cone_common
|
|
24 |
import time
|
|
25 |
|
|
26 |
from cone.public import api, plugin, utils, exceptions
|
|
27 |
from time import gmtime, strftime
|
3
|
28 |
from cone.report import report_util
|
0
|
29 |
ROOT_PATH = os.path.dirname(os.path.abspath(__file__))
|
|
30 |
|
|
31 |
VERSION = '1.0'
|
|
32 |
|
|
33 |
log = logging.getLogger('cone')
|
|
34 |
|
|
35 |
REPORT_SHORTCUTS = {
|
|
36 |
'api': report_util.ReportShortcut(
|
|
37 |
os.path.join(ROOT_PATH, 'compare_api_report_template.html'),
|
|
38 |
'api_comparison.html',
|
|
39 |
'Report changes in feature definitions'),
|
|
40 |
|
3
|
41 |
'ci': report_util.ReportShortcut(
|
|
42 |
os.path.join(ROOT_PATH, 'compare_ci_report_template.html'),
|
|
43 |
'ci_comparison.html',
|
|
44 |
'Report changes in CustomisationInterface definitions'),
|
|
45 |
|
0
|
46 |
'data': report_util.ReportShortcut(
|
|
47 |
os.path.join(ROOT_PATH, 'compare_data_report_template.html'),
|
|
48 |
'data_comparison.html',
|
|
49 |
'Report changes in data values'),
|
|
50 |
|
|
51 |
'crml_dc': report_util.ReportShortcut(
|
|
52 |
os.path.join(ROOT_PATH, 'crml_dc_report_template.html'),
|
|
53 |
'crml_dc_report.html',
|
|
54 |
'Report CRML data compatibility issues'),
|
|
55 |
|
|
56 |
'crml_dc_csv': report_util.ReportShortcut(
|
|
57 |
os.path.join(ROOT_PATH, 'crml_dc_report_template.csv'),
|
|
58 |
'crml_dc_report.csv',
|
|
59 |
'Report CRML data compatibility issues (CSV format)'),
|
|
60 |
}
|
|
61 |
DEFAULT_SHORTCUT = 'data'
|
|
62 |
|
|
63 |
def main():
|
3
|
64 |
""" Compare two configurations """
|
0
|
65 |
shortcut_container = report_util.ReportShortcutContainer(REPORT_SHORTCUTS,
|
|
66 |
DEFAULT_SHORTCUT)
|
|
67 |
|
|
68 |
gset = cone_common.get_settings([os.path.join(ROOT_PATH,'conesub_compare.cfg')])
|
|
69 |
|
|
70 |
parser = OptionParser(version="%%prog %s" % VERSION)
|
|
71 |
|
|
72 |
parser.add_options(cone_common.COMMON_OPTIONS)
|
|
73 |
|
|
74 |
parser.add_option("-p", "--project",\
|
|
75 |
dest="project",\
|
|
76 |
help="defines the location of current project. Default is the current working directory.",\
|
|
77 |
default=".",\
|
|
78 |
metavar="STORAGE")
|
|
79 |
|
|
80 |
group = OptionGroup(parser, 'Compare options',
|
|
81 |
'The generate function will create target files from a specific configuration.'\
|
|
82 |
'The generate will always work with read-only mode of the project, so no changes are saved to project')
|
|
83 |
|
|
84 |
group.add_option("-s", "--sourceconfiguration",\
|
|
85 |
dest="sourceconfiguration",\
|
|
86 |
help="defines the name of the sourceconfiguration for the compare action. "\
|
|
87 |
"The configuration is expected to be located current storage.",\
|
|
88 |
metavar="CONFIG")
|
|
89 |
|
|
90 |
group.add_option("-t", "--targetconfiguration",\
|
|
91 |
dest="targetconfiguration",\
|
|
92 |
help="defines the name of the target configuration for the compare action. "\
|
|
93 |
"The configuration can be located in the current storage or it the configuration"\
|
|
94 |
"definition can contain a path to a storage. The storage definition is given as a path"\
|
|
95 |
"before semicolon. e.g. x:\data\configproject;productx.confml, test.cpf;root.confml",\
|
|
96 |
metavar="CONFIG")
|
|
97 |
|
|
98 |
# group.add_option("--compare-dict",\
|
|
99 |
# dest="compare_dict",\
|
|
100 |
# action="store",
|
|
101 |
# type="string",
|
|
102 |
# help="Compare elements as a dictionary",
|
|
103 |
# metavar="DICT",\
|
|
104 |
# default=None)
|
|
105 |
|
|
106 |
group.add_option("--report",\
|
|
107 |
dest="report_file",\
|
|
108 |
action="store",
|
|
109 |
type="string",
|
|
110 |
help="The file where the comparison report is written."\
|
|
111 |
"By default this value is determined by the used "\
|
|
112 |
"report type. Example: --report report.html.",
|
|
113 |
metavar="FILE",\
|
|
114 |
default=None)
|
|
115 |
|
|
116 |
group.add_option("--template",\
|
|
117 |
dest="template",\
|
|
118 |
action="store",
|
|
119 |
type="string",
|
|
120 |
help="Template used in a report generation. By default "\
|
|
121 |
"this value is determined by the used report type. "\
|
|
122 |
"Example: --template report_template.html.",
|
|
123 |
metavar="FILE",\
|
|
124 |
default=None)
|
|
125 |
|
|
126 |
group.add_option("--report-type",
|
|
127 |
dest="report_type",
|
|
128 |
action="store",
|
|
129 |
type="string",
|
|
130 |
help="The type of the report to generate. This is a convenience "\
|
|
131 |
"switch for setting the used template. "\
|
|
132 |
"Possible values:\n "\
|
|
133 |
+ shortcut_container.get_shortcut_help_text(),
|
|
134 |
metavar="TYPE",\
|
|
135 |
default=None)
|
|
136 |
|
|
137 |
group.add_option("--impl-filter",\
|
|
138 |
dest="impl_filter",\
|
|
139 |
action="store",
|
|
140 |
type="string",
|
|
141 |
help="The pattern used for filtering implementations for the "\
|
|
142 |
"comparison. See the switch --impl in action generate for "\
|
|
143 |
"more info. ",
|
|
144 |
metavar="PATTERN",\
|
|
145 |
default=None)
|
|
146 |
|
|
147 |
start_time = time.time()
|
|
148 |
|
|
149 |
parser.add_option_group(group)
|
|
150 |
(options, args) = parser.parse_args()
|
|
151 |
|
|
152 |
cone_common.handle_common_options(options, settings=gset)
|
|
153 |
|
|
154 |
if not options.sourceconfiguration: parser.error("sourceconfiguration must be given")
|
|
155 |
if not options.targetconfiguration: parser.error("targetconfiguration must be given")
|
|
156 |
if options.report_type and options.template:
|
|
157 |
parser.error("both --report-type and --template supplied; use only one of them")
|
|
158 |
if not shortcut_container.is_valid_shortcut(options.report_type):
|
|
159 |
parser.error("Invalid report type: %s" % options.report_type)
|
|
160 |
|
|
161 |
template_file, report_file = shortcut_container.determine_template_and_report(
|
|
162 |
options.report_type,
|
|
163 |
options.template,
|
|
164 |
options.report_file,
|
|
165 |
'comparison')
|
|
166 |
|
|
167 |
action = CompareAction(options.project,
|
|
168 |
options.sourceconfiguration,
|
|
169 |
options.targetconfiguration,
|
|
170 |
template = template_file,
|
|
171 |
report_file = report_file,
|
|
172 |
impl_filter = options.impl_filter)
|
|
173 |
result = action.run_action()
|
|
174 |
|
|
175 |
resmap = {True: 0, False: 1}
|
|
176 |
sys.exit(resmap[result])
|
|
177 |
|
|
178 |
|
|
179 |
|
|
180 |
class CompareAction(object):
|
|
181 |
|
|
182 |
def __init__(self, current, sourceconfig, targetconfig, **kwargs):
|
|
183 |
self.current = current
|
|
184 |
self.sourceconfig = sourceconfig
|
|
185 |
self.targetconfig = targetconfig
|
|
186 |
self.reportfile = kwargs.get("report_file", 'compare.html')
|
|
187 |
self.reporttemplate = kwargs.get('template', '')
|
|
188 |
self.columns = kwargs.get('columns', None)
|
|
189 |
self.impl_filter = kwargs.get('impl_filter',)
|
|
190 |
|
|
191 |
def run_action(self):
|
|
192 |
"""
|
|
193 |
Run the action.
|
|
194 |
@return: True if successful, False if not.
|
|
195 |
"""
|
|
196 |
|
|
197 |
currentprj = api.Project(api.Storage.open(self.current,"r"))
|
|
198 |
targetprj = None
|
|
199 |
(targetproject,targetconf) = self.parse_target_configuration(self.targetconfig)
|
|
200 |
print "Compare %s <> %s in %s" % (self.sourceconfig, targetconf, targetproject)
|
|
201 |
if targetproject != '':
|
|
202 |
targetprj = api.Project(api.Storage.open(targetproject,"r"))
|
|
203 |
else:
|
|
204 |
# The comparison doesn't seem to work if the same project
|
|
205 |
# object is used
|
|
206 |
#targetprj = currentprj
|
|
207 |
targetprj = api.Project(api.Storage.open(self.current,"r"))
|
|
208 |
|
|
209 |
source = currentprj.get_configuration(self.sourceconfig)
|
|
210 |
target = targetprj.get_configuration(targetconf)
|
|
211 |
|
|
212 |
print "Writing report to %s" % self.reportfile
|
|
213 |
sourcedata = {}
|
|
214 |
targetdata = {}
|
|
215 |
sourcedata['name'] = self.sourceconfig
|
|
216 |
targetdata['name'] = self.targetconfig
|
|
217 |
sourcedata['features'] = self.get_feature_api_data(source)
|
|
218 |
targetdata['features'] = self.get_feature_api_data(target)
|
|
219 |
|
|
220 |
impl_comp_data_proxy = ImplComparisonDataProxy(source,
|
|
221 |
target,
|
|
222 |
self.impl_filter)
|
|
223 |
|
|
224 |
template_data = {'sourcedata': sourcedata,
|
|
225 |
'targetdata': targetdata,
|
|
226 |
'impl_data': impl_comp_data_proxy}
|
|
227 |
|
|
228 |
result = report_util.generate_report(self.reporttemplate,
|
|
229 |
self.reportfile,
|
|
230 |
{'data': template_data})
|
|
231 |
print "Done."
|
|
232 |
return result
|
|
233 |
|
|
234 |
def parse_target_configuration(self,configpath):
|
|
235 |
"""
|
|
236 |
return tuple (storage, configpath) from storagepath;root.confml.
|
|
237 |
returns ('', 'root.confml') from root.confml
|
|
238 |
"""
|
|
239 |
elems = configpath.rsplit(';',2)
|
|
240 |
if len(elems) > 1:
|
|
241 |
return (elems[0],elems[1])
|
|
242 |
else:
|
|
243 |
return ('',elems[0])
|
|
244 |
|
|
245 |
def get_feature_api_data(self,config):
|
|
246 |
# Traverse through all features in the api
|
|
247 |
# and construct the data rows
|
|
248 |
data = {}
|
|
249 |
for elem in config.get_default_view().get_features('**'):
|
|
250 |
data[elem.fqr] = elem._obj
|
|
251 |
return data
|
|
252 |
|
|
253 |
class ImplComparisonDataProxy(object):
|
|
254 |
"""
|
|
255 |
Proxy object for loading implementation comparison data on demand.
|
|
256 |
"""
|
|
257 |
def __init__(self, sourceconfig, targetconfig, impl_filter):
|
|
258 |
self.sourceconfig = sourceconfig
|
|
259 |
self.targetconfig = targetconfig
|
|
260 |
if impl_filter is None: self.impl_filter = '.*'
|
|
261 |
else: self.impl_filter = impl_filter
|
|
262 |
|
|
263 |
self._flat_data = None
|
|
264 |
|
|
265 |
@property
|
|
266 |
def flat(self):
|
|
267 |
try:
|
|
268 |
if self._flat_data is None:
|
|
269 |
self._flat_data = self._get_flat_comparison_data()
|
|
270 |
return self._flat_data
|
|
271 |
except Exception, e:
|
|
272 |
utils.log_exception(log, 'Error retrieving ImplComparisonDataProxy.flat!')
|
|
273 |
raise
|
|
274 |
|
|
275 |
def _get_flat_comparison_data(self):
|
|
276 |
log.debug("Loading implementations for comparison (impl filter = '%s')..." % (self.impl_filter))
|
|
277 |
|
|
278 |
try:
|
|
279 |
source_impls = plugin.get_impl_set(self.sourceconfig, self.impl_filter)
|
|
280 |
target_impls = plugin.get_impl_set(self.targetconfig, self.impl_filter)
|
|
281 |
except Exception, e:
|
|
282 |
utils.log_exception(log, 'Failed to load implementations!')
|
|
283 |
raise
|
|
284 |
|
|
285 |
log.debug("%d impl(s) in source." % len(source_impls))
|
|
286 |
log.debug("%d impl(s) in target." % len(target_impls))
|
|
287 |
|
|
288 |
log.debug("Generating flat comparison results...")
|
|
289 |
result = source_impls.flat_compare(target_impls)
|
|
290 |
log.debug("Generated %d result row(s)" % len(result))
|
|
291 |
return result
|
|
292 |
|
|
293 |
if __name__ == "__main__":
|
|
294 |
main()
|