3
|
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, logging, pickle
|
|
18 |
import time
|
4
|
19 |
import types
|
3
|
20 |
from time import strftime
|
|
21 |
from cone.public import api, exceptions, utils, plugin
|
|
22 |
from cone.confml import model
|
|
23 |
from cone.report import report_util
|
|
24 |
|
|
25 |
ROOT_PATH = os.path.dirname(os.path.abspath(__file__))
|
|
26 |
SERIALISATION_FORMAT = 'pickle'
|
|
27 |
|
4
|
28 |
def pickle_persistent_id(obj):
|
|
29 |
"""
|
|
30 |
Prepare report data for pickler.
|
|
31 |
"""
|
|
32 |
if isinstance(obj, (logging.Logger, types.ModuleType)):
|
|
33 |
return ""
|
|
34 |
elif isinstance(obj, (api.Configuration, api.ConfigurationProxy)):
|
|
35 |
try: project_path = os.path.abspath(obj.get_project().get_storage().get_path())
|
|
36 |
except: project_path = None
|
|
37 |
return "conf:" + repr(pickle.dumps((project_path, obj.path)))
|
|
38 |
else:
|
|
39 |
return None
|
|
40 |
|
|
41 |
def persistent_load(persid):
|
|
42 |
"""
|
|
43 |
Prepare pickler for unpickling report data.
|
|
44 |
"""
|
|
45 |
global loaded_project
|
|
46 |
if persid.startswith("impl:"):
|
|
47 |
type_id, ref, lineno = pickle.loads(eval(persid[5:]))
|
|
48 |
impl = plugin.ImplBase(ref, None)
|
|
49 |
impl.IMPL_TYPE_ID = type_id
|
|
50 |
impl.lineno = lineno
|
|
51 |
return impl
|
|
52 |
elif persid.startswith("conf:"):
|
|
53 |
project_path, config_path = pickle.loads(eval(persid[5:]))
|
|
54 |
configuration = api.ConfigurationProxy(config_path)
|
|
55 |
if project_path:
|
|
56 |
if not loaded_project:
|
|
57 |
loaded_project = api.Project(api.Storage.open(project_path))
|
|
58 |
configuration._set_parent(loaded_project)
|
|
59 |
return configuration
|
|
60 |
else:
|
|
61 |
return None
|
|
62 |
|
3
|
63 |
def save_report_data(rep_data, file_path):
|
|
64 |
"""
|
|
65 |
Save report data into an intermediary report data file.
|
|
66 |
"""
|
|
67 |
dir = os.path.dirname(file_path)
|
|
68 |
if dir != '' and not os.path.exists(dir):
|
|
69 |
os.makedirs(dir)
|
|
70 |
f = open(file_path, 'wb')
|
4
|
71 |
|
3
|
72 |
try:
|
|
73 |
if SERIALISATION_FORMAT == 'yaml':
|
|
74 |
yaml.dump(rep_data, f)
|
|
75 |
elif SERIALISATION_FORMAT == 'pickle':
|
4
|
76 |
pickler = pickle.Pickler(f)
|
|
77 |
pickler.persistent_id = pickle_persistent_id
|
|
78 |
pickler.dump(rep_data)
|
3
|
79 |
elif SERIALISATION_FORMAT == 'pickle/2':
|
4
|
80 |
pickler = pickle.Pickler(f, 2)
|
|
81 |
pickler.persistent_id = pickle_persistent_id
|
|
82 |
pickler.dump(rep_data)
|
3
|
83 |
finally:
|
|
84 |
f.close()
|
|
85 |
|
|
86 |
def load_report_data(file_path):
|
|
87 |
"""
|
|
88 |
Load report data from an intermediary report data file.
|
|
89 |
"""
|
|
90 |
try:
|
|
91 |
f = open(file_path, "rb")
|
4
|
92 |
unpickler = pickle.Unpickler(f)
|
|
93 |
global loaded_project
|
|
94 |
loaded_project = None
|
|
95 |
unpickler.persistent_load = persistent_load
|
|
96 |
|
3
|
97 |
if SERIALISATION_FORMAT == 'yaml':
|
|
98 |
data = yaml.load(f)
|
|
99 |
elif SERIALISATION_FORMAT == 'pickle':
|
4
|
100 |
data = unpickler.load()
|
3
|
101 |
elif SERIALISATION_FORMAT == 'pickle/2':
|
4
|
102 |
data = unpickler.load()
|
3
|
103 |
finally:
|
|
104 |
f.close()
|
|
105 |
|
|
106 |
data.label = get_generation_run_label(file_path)
|
|
107 |
return data
|
|
108 |
|
|
109 |
def get_generation_run_label(datafile_path):
|
|
110 |
filename = os.path.split(datafile_path)[1]
|
|
111 |
filename_noext = os.path.splitext(filename)[0]
|
|
112 |
return filename_noext
|
|
113 |
|
|
114 |
def _get_parent_sequence_or_self(feature):
|
|
115 |
current = feature._parent
|
|
116 |
while current is not None:
|
|
117 |
if isinstance(current, api.FeatureSequence):
|
|
118 |
return current
|
|
119 |
current = current._parent
|
|
120 |
return feature
|
|
121 |
|
|
122 |
|
|
123 |
def generate_report(rep_data, report_file_path, template_file_path=None, template_paths=[], report_options=[]):
|
|
124 |
"""
|
|
125 |
Generate a generation report based on the given report data.
|
|
126 |
@param rep_data: The report data.
|
|
127 |
@param report_file_path: Path to the report file to generate.
|
|
128 |
@param template_file_path: Path to the template file to use.
|
|
129 |
If None, the default template is used.
|
|
130 |
@param template_paths: the additional search paths for templates. The default location cone.report is
|
|
131 |
always included.
|
|
132 |
"""
|
|
133 |
# Determine the template file and directory to use
|
|
134 |
if template_file_path is None:
|
|
135 |
template_file_path = 'gen_report_template.html'
|
|
136 |
contexts = [report_data.context for report_data in rep_data]
|
4
|
137 |
merged_context = plugin.MergedContext(contexts)
|
3
|
138 |
report_data = {'rep_data' : rep_data,
|
|
139 |
'report_options' : report_options,
|
4
|
140 |
'merged_context' : merged_context}
|
3
|
141 |
report_util.generate_report(template_file_path, report_file_path, report_data, template_paths)
|
|
142 |
|
|
143 |
def normalize_slash(path):
|
|
144 |
"""
|
|
145 |
Normalize backslashes to slashes to make testing easier (no differences
|
|
146 |
between reports in linux and windows).
|
|
147 |
"""
|
|
148 |
return path.replace('\\', '/')
|
|
149 |
|
|
150 |
class ReportData(object):
|
|
151 |
"""
|
|
152 |
Data object that stores all information used in report generation.
|
|
153 |
"""
|
|
154 |
|
|
155 |
def __init__(self):
|
|
156 |
self.project = None
|
|
157 |
self.generation_timestamp = time.time()
|
|
158 |
self.generation_time = strftime("%d.%m.%Y %H:%M:%S")
|
|
159 |
self.options = None
|
|
160 |
self.duration = 0
|
|
161 |
self.output_dir = os.getcwd()
|
|
162 |
self.project_dir = ''
|
|
163 |
self.context = None
|
|
164 |
self.label = ''
|
|
165 |
|
|
166 |
def set_output_dir(self, dir):
|
|
167 |
self.output_dir = os.path.abspath(os.path.normpath(dir))
|
|
168 |
|
|
169 |
def set_duration(self, duration):
|
|
170 |
self.duration = duration
|
|
171 |
|
|
172 |
def set_options(self, options):
|
|
173 |
self.options = options
|
|
174 |
self.project_dir = os.path.abspath(options.project)
|
|
175 |
|
|
176 |
def set_report_filename(self, filename):
|
|
177 |
self.report_filename = filename
|
|
178 |
|
|
179 |
def __repr__(self):
|
|
180 |
return "ReportData(%s)" % [self.generation_timestamp,
|
|
181 |
self.generation_time,
|
|
182 |
self.options,
|
|
183 |
self.duration,
|
|
184 |
self.output_dir,
|
4
|
185 |
self.project_dir]
|
|
186 |
|
|
187 |
def __getstate__(self):
|
|
188 |
state = self.__dict__.copy()
|
|
189 |
if self.project:
|
|
190 |
state['project_path'] = os.path.abspath(self.project.get_path())
|
|
191 |
else:
|
|
192 |
state['project_path'] = None
|
|
193 |
del state['project']
|
|
194 |
return state
|
|
195 |
|
|
196 |
def __setstate__(self, dict):
|
|
197 |
project_path = dict['project_path']
|
|
198 |
if project_path is None:
|
|
199 |
self.project = None
|
|
200 |
else:
|
|
201 |
self.project = api.Project(api.Storage.open(project_path))
|
|
202 |
|
|
203 |
self.__dict__.update(dict)
|
3
|
204 |
|
|
205 |
|
|
206 |
class RefLine(object):
|
|
207 |
"""
|
|
208 |
Data object that stores information for one ref in report generation.
|
|
209 |
"""
|
|
210 |
|
|
211 |
def __init__(self, ref, type):
|
|
212 |
self.ref = ref
|
|
213 |
self.feat_type = type
|
|
214 |
self.feat_name = None
|
|
215 |
self.feat_value = None
|
|
216 |
self.config_path = None
|
|
217 |
self.impls = []
|
|
218 |
self.output = None
|
|
219 |
self.nbr_impls = 0
|
|
220 |
self.nbr_outputfiles = 0
|
|
221 |
self.datas = []
|
|
222 |
self.nbr_of_datas = 0
|
|
223 |
self.nbr_of_rows = 0
|
|
224 |
self.seq_data = []
|
|
225 |
self.is_temp_feature = False
|
|
226 |
|
|
227 |
def add_impl(self, impl_file, impl_type, outputfiles):
|
|
228 |
self.impls.append(ImplLine(impl_file, impl_type, outputfiles))
|
|
229 |
self.nbr_impls = len(self.impls)
|
|
230 |
self.nbr_outputfiles = len(outputfiles) + self.nbr_outputfiles
|
|
231 |
|
|
232 |
def add_data(self, layer, value):
|
|
233 |
self.datas.append(DataLine(layer,value))
|
|
234 |
self.nbr_of_datas = len(self.datas)
|
|
235 |
|
|
236 |
def add_sequence(self, subsetting, values):
|
|
237 |
self.seq_data.append([subsetting, values])
|
|
238 |
|
|
239 |
def set_feat_name(self, name):
|
|
240 |
self.feat_name = name
|
|
241 |
|
|
242 |
def set_feat_value(self, value):
|
|
243 |
self.feat_value = value
|
|
244 |
|
|
245 |
def set_config_path(self, filename):
|
|
246 |
self.config_path = normalize_slash(os.path.normpath(filename))
|
|
247 |
|
|
248 |
|
|
249 |
class ImplLine():
|
|
250 |
def __init__(self, impl_file, impl_type, outputfiles, generation_runs=[]):
|
|
251 |
self.name = normalize_slash(os.path.normpath(impl_file))
|
|
252 |
self.type = impl_type
|
|
253 |
files = []
|
|
254 |
|
|
255 |
for outputfile in outputfiles:
|
|
256 |
files.append(Outputfile(outputfile))
|
|
257 |
|
|
258 |
self.outputfiles = files
|
|
259 |
self.generation_runs = generation_runs
|
|
260 |
|
|
261 |
class Outputfile():
|
|
262 |
def __init__(self, filename):
|
|
263 |
self.filename = normalize_slash(os.path.normpath(filename))
|
|
264 |
self.abs_filename = normalize_slash(os.path.abspath(filename))
|
|
265 |
self.exists = os.path.isfile(self.abs_filename)
|
|
266 |
|
|
267 |
def __eq__(self, other):
|
|
268 |
if type(self) is type(other):
|
|
269 |
return self.filename == other.filename
|
|
270 |
else:
|
|
271 |
return False
|
|
272 |
|
|
273 |
class DataLine():
|
|
274 |
def __init__(self, layer, value):
|
|
275 |
self.layer = normalize_slash(os.path.normpath(layer))
|
|
276 |
self.value = value |