3
|
1 |
#
|
|
2 |
# Copyright (c) 2006-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 the License "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 |
# raptor_data module
|
|
16 |
# This module contains the classes that make up the Raptor Data Model.
|
|
17 |
#
|
|
18 |
|
|
19 |
import copy
|
|
20 |
import generic_path
|
|
21 |
import os
|
|
22 |
import hashlib
|
|
23 |
import raptor_utilities
|
|
24 |
import re
|
|
25 |
import types
|
|
26 |
import sys
|
|
27 |
import subprocess
|
|
28 |
from tempfile import gettempdir
|
|
29 |
from time import time, clock
|
5
|
30 |
import traceback
|
|
31 |
import raptor_cache
|
3
|
32 |
|
|
33 |
|
5
|
34 |
class MissingInterfaceError(Exception):
|
|
35 |
def __init__(self, s):
|
|
36 |
Exception.__init__(self,s)
|
3
|
37 |
|
|
38 |
# What host platforms we recognise
|
|
39 |
# This allows us to tie some variants to one host platform and some to another
|
|
40 |
class HostPlatform(object):
|
|
41 |
""" List the host platforms on which we can build. Allow configuration
|
|
42 |
files to specify different information based on that.
|
|
43 |
"""
|
|
44 |
hostplatforms = ["win32", "win64", "linux2"]
|
|
45 |
hostplatform = sys.platform
|
|
46 |
|
|
47 |
@classmethod
|
|
48 |
def IsKnown(cls, platformpattern):
|
|
49 |
"Does the parameter match the name of a known platform "
|
|
50 |
hpnre = re.compile(platformpattern, re.I)
|
|
51 |
for hp in cls.hostplatforms:
|
|
52 |
if hpnre.match(hp):
|
|
53 |
return True
|
|
54 |
return False
|
|
55 |
|
|
56 |
@classmethod
|
|
57 |
def IsHost(cls, platformpattern):
|
|
58 |
""" Does the parameter match the name of the
|
|
59 |
platform that we're executing on? """
|
|
60 |
ppre = re.compile(platformpattern, re.I)
|
|
61 |
if ppre.match(cls.hostplatform):
|
|
62 |
return True
|
|
63 |
return False
|
|
64 |
|
|
65 |
|
|
66 |
# Make sure not to start up on an unsupported platform
|
|
67 |
if not HostPlatform.IsKnown(HostPlatform.hostplatform):
|
|
68 |
raise Exception("raptor_data module loaded on an unrecognised platform '%s'. Expected one of %s" % (hostplatform, str(hostplatforms)))
|
|
69 |
|
|
70 |
|
|
71 |
# raptor_data module classes
|
|
72 |
|
|
73 |
class Model(object):
|
|
74 |
"Base class for data-model objects"
|
|
75 |
|
5
|
76 |
__slots__ = ('host', 'source', 'cacheID')
|
|
77 |
|
3
|
78 |
def __init__(self):
|
|
79 |
self.source = None # XML file
|
|
80 |
self.host = None
|
|
81 |
self.cacheID = "" # default set of cached objects
|
5
|
82 |
# not using the cache parameter - there to make the
|
|
83 |
# init for all Model objects "standard"
|
3
|
84 |
|
|
85 |
|
|
86 |
def SetSourceFile(self, filename):
|
|
87 |
self.source = filename
|
|
88 |
|
|
89 |
|
|
90 |
def SetProperty(self, name, value):
|
|
91 |
raise InvalidPropertyError()
|
|
92 |
|
|
93 |
|
|
94 |
def AddChild(self, child):
|
|
95 |
raise InvalidChildError()
|
|
96 |
|
|
97 |
|
|
98 |
def Valid(self):
|
|
99 |
return False
|
|
100 |
|
|
101 |
def IsApplicable(self):
|
|
102 |
"This variant may be caused to only apply when used on a particular host build platform"
|
|
103 |
if self.host is None:
|
|
104 |
return True
|
|
105 |
|
|
106 |
if HostPlatform.IsHost(self.host):
|
|
107 |
return True
|
|
108 |
|
|
109 |
return False
|
|
110 |
|
|
111 |
|
|
112 |
class InvalidPropertyError(Exception):
|
|
113 |
pass
|
|
114 |
|
|
115 |
class InvalidChildError(Exception):
|
|
116 |
pass
|
|
117 |
|
|
118 |
class BadReferenceError(Exception):
|
|
119 |
pass
|
|
120 |
|
|
121 |
|
|
122 |
class Reference(Model):
|
|
123 |
"Base class for data-model reference objects"
|
|
124 |
|
|
125 |
def __init__(self, ref = None):
|
|
126 |
Model.__init__(self)
|
|
127 |
self.ref = ref
|
|
128 |
self.modifiers = []
|
|
129 |
|
|
130 |
def SetProperty(self, name, value):
|
|
131 |
if name == "ref":
|
|
132 |
self.ref = value
|
|
133 |
elif name == "mod":
|
|
134 |
self.modifiers = value.split(".")
|
|
135 |
else:
|
|
136 |
raise InvalidPropertyError()
|
|
137 |
|
|
138 |
def Resolve(self):
|
|
139 |
raise BadReferenceError()
|
|
140 |
|
5
|
141 |
def GetModifiers(self, cache):
|
3
|
142 |
return [ cache.FindNamedVariant(m) for m in self.modifiers ]
|
|
143 |
|
|
144 |
def Valid(self):
|
|
145 |
return self.ref
|
|
146 |
|
|
147 |
|
|
148 |
class VariantContainer(Model):
|
|
149 |
|
|
150 |
def __init__(self):
|
|
151 |
Model.__init__(self) # base class constructor
|
|
152 |
self.variants = []
|
|
153 |
|
|
154 |
|
5
|
155 |
def __str__(self):
|
|
156 |
return "\n".join([str(v) for v in self.variants])
|
3
|
157 |
|
|
158 |
|
|
159 |
def AddVariant(self, variant):
|
|
160 |
if type(variant) is types.StringTypes:
|
5
|
161 |
variant = VariantRef(ref = variant)
|
3
|
162 |
|
|
163 |
|
|
164 |
# Only add the variant if it's not in the list
|
|
165 |
# already
|
|
166 |
if not variant in self.variants:
|
|
167 |
self.variants.append(variant)
|
|
168 |
|
5
|
169 |
def GetVariants(self, cache):
|
3
|
170 |
# resolve any VariantRef objects into Variant objects
|
5
|
171 |
missing_variants = []
|
3
|
172 |
for i,var in enumerate(self.variants):
|
|
173 |
if isinstance(var, Reference):
|
|
174 |
try:
|
5
|
175 |
self.variants[i] = var.Resolve(cache=cache)
|
3
|
176 |
|
|
177 |
except BadReferenceError:
|
5
|
178 |
missing_variants.append(var.ref)
|
|
179 |
|
|
180 |
if len(missing_variants) > 0:
|
|
181 |
raise MissingVariantException("Missing variants '%s'", " ".join(missing_variants))
|
3
|
182 |
|
|
183 |
return self.variants
|
|
184 |
|
|
185 |
|
|
186 |
class Interface(Model):
|
|
187 |
|
|
188 |
def __init__(self, name = None):
|
|
189 |
Model.__init__(self) # base class constructor
|
|
190 |
self.name = name
|
|
191 |
self.flm = None
|
|
192 |
self.abstract = False
|
|
193 |
self.extends = None
|
|
194 |
self.params = []
|
|
195 |
self.paramgroups = []
|
|
196 |
|
5
|
197 |
def __str__(self):
|
|
198 |
return "<interface name='%s'>" % self.name + "</interface>"
|
3
|
199 |
|
5
|
200 |
def FindParent(self, cache):
|
3
|
201 |
try:
|
5
|
202 |
return cache.FindNamedInterface(self.extends, self.cacheID)
|
3
|
203 |
except KeyError:
|
|
204 |
raise BadReferenceError("Cannot extend interface because it cannot be found: "+str(self.extends))
|
|
205 |
|
5
|
206 |
def GetParams(self, cache):
|
3
|
207 |
if self.extends != None:
|
5
|
208 |
parent = self.FindParent(cache)
|
3
|
209 |
|
|
210 |
# what parameter names do we have already?
|
|
211 |
names = set([x.name for x in self.params])
|
|
212 |
|
|
213 |
# pick up ones we don't have that are in our parent
|
|
214 |
pp = []
|
5
|
215 |
for p in parent.GetParams(cache):
|
3
|
216 |
if not p.name in names:
|
|
217 |
pp.append(p)
|
|
218 |
|
|
219 |
# list parent parameters first then ours
|
|
220 |
pp.extend(self.params)
|
|
221 |
return pp
|
|
222 |
|
|
223 |
return self.params
|
|
224 |
|
5
|
225 |
def GetParamGroups(self, cache):
|
3
|
226 |
if self.extends != None:
|
5
|
227 |
parent = self.FindParent(cache)
|
3
|
228 |
|
|
229 |
# what parameter names do we have already?
|
|
230 |
patterns = set([x.pattern for x in self.paramgroups])
|
|
231 |
|
|
232 |
# pick up ones we don't have that are in our parent
|
5
|
233 |
for g in parent.GetParamGroups(cache):
|
3
|
234 |
if not g.pattern in patterns:
|
|
235 |
self.paramgroups.append(g)
|
|
236 |
|
|
237 |
return self.paramgroups
|
|
238 |
|
|
239 |
|
5
|
240 |
def GetFLMIncludePath(self, cache):
|
3
|
241 |
"absolute path to the FLM"
|
|
242 |
|
|
243 |
if self.flm == None:
|
|
244 |
if self.extends != None:
|
5
|
245 |
parent = self.FindParent(cache)
|
3
|
246 |
|
5
|
247 |
return parent.GetFLMIncludePath(cache)
|
3
|
248 |
else:
|
|
249 |
raise InvalidPropertyError()
|
|
250 |
|
|
251 |
if not os.path.isabs(self.flm):
|
|
252 |
self.flm = os.path.join(os.path.dirname(self.source), self.flm)
|
|
253 |
|
|
254 |
return generic_path.Path(self.flm)
|
|
255 |
|
|
256 |
|
|
257 |
def SetProperty(self, name, value):
|
|
258 |
if name == "name":
|
|
259 |
self.name = value
|
|
260 |
elif name == "flm":
|
|
261 |
self.flm = value
|
|
262 |
elif name == "abstract":
|
|
263 |
self.abstract = (value == "true")
|
|
264 |
elif name == "extends":
|
|
265 |
self.extends = value
|
|
266 |
else:
|
|
267 |
raise InvalidPropertyError()
|
|
268 |
|
|
269 |
|
|
270 |
def AddChild(self, child):
|
|
271 |
if isinstance(child, Parameter):
|
|
272 |
self.AddParameter(child)
|
|
273 |
elif isinstance(child, ParameterGroup):
|
|
274 |
self.AddParameterGroup(child)
|
|
275 |
else:
|
|
276 |
raise InvalidChildError()
|
|
277 |
|
|
278 |
|
|
279 |
def AddParameter(self, parameter):
|
|
280 |
self.params.append(parameter)
|
|
281 |
|
|
282 |
def AddParameterGroup(self, parametergroup):
|
|
283 |
self.paramgroups.append(parametergroup)
|
|
284 |
|
|
285 |
def Valid(self):
|
|
286 |
return (self.name != None)
|
|
287 |
|
|
288 |
|
|
289 |
class InterfaceRef(Reference):
|
|
290 |
|
5
|
291 |
def __str__(self):
|
|
292 |
return "<interfaceRef ref='%s'/>" % self.ref
|
3
|
293 |
|
5
|
294 |
def Resolve(self, cache):
|
3
|
295 |
try:
|
5
|
296 |
return cache.FindNamedInterface(self.ref, self.cacheID)
|
3
|
297 |
except KeyError:
|
|
298 |
raise BadReferenceError()
|
|
299 |
|
|
300 |
|
|
301 |
class Specification(VariantContainer):
|
|
302 |
|
|
303 |
def __init__(self, name = "", type = ""):
|
|
304 |
VariantContainer.__init__(self) # base class constructor
|
|
305 |
self.name = name
|
|
306 |
self.type = type
|
|
307 |
self.interface = None
|
|
308 |
self.childSpecs = []
|
|
309 |
self.parentSpec = None
|
|
310 |
|
|
311 |
|
5
|
312 |
def __str__(self):
|
|
313 |
s = "<spec name='%s'>" % str(self.name)
|
|
314 |
s += VariantContainer.__str__(self)
|
3
|
315 |
for c in self.childSpecs:
|
5
|
316 |
s += str(c) + '\n'
|
|
317 |
s += "</spec>"
|
|
318 |
return s
|
3
|
319 |
|
|
320 |
|
|
321 |
def SetProperty(self, name, value):
|
|
322 |
if name == "name":
|
|
323 |
self.name = value
|
|
324 |
else:
|
|
325 |
raise InvalidPropertyError()
|
|
326 |
|
|
327 |
|
5
|
328 |
def Configure(self, config, cache):
|
3
|
329 |
# configure all the children (some may be Filters or parents of)
|
|
330 |
for spec in self.GetChildSpecs():
|
5
|
331 |
spec.Configure(config, cache = cache)
|
3
|
332 |
|
|
333 |
|
|
334 |
def HasInterface(self):
|
|
335 |
return self.interface != None
|
|
336 |
|
|
337 |
|
|
338 |
def SetInterface(self, interface):
|
|
339 |
if isinstance(interface, Interface) \
|
|
340 |
or isinstance(interface, InterfaceRef):
|
|
341 |
self.interface = interface
|
|
342 |
else:
|
5
|
343 |
self.interface = InterfaceRef(ref = interface)
|
3
|
344 |
|
|
345 |
|
5
|
346 |
def GetInterface(self, cache):
|
3
|
347 |
"""return the Interface (fetching from the cache if it was a ref)
|
|
348 |
may return None"""
|
|
349 |
|
|
350 |
if self.interface == None \
|
|
351 |
or isinstance(self.interface, Interface):
|
|
352 |
return self.interface
|
|
353 |
|
|
354 |
if isinstance(self.interface, InterfaceRef):
|
|
355 |
try:
|
5
|
356 |
self.interface = self.interface.Resolve(cache=cache)
|
3
|
357 |
return self.interface
|
|
358 |
|
|
359 |
except BadReferenceError:
|
5
|
360 |
raise MissingInterfaceError("Missing interface %s" % self.interface.ref)
|
3
|
361 |
|
|
362 |
def AddChild(self, child):
|
|
363 |
if isinstance(child, Specification):
|
|
364 |
self.AddChildSpecification(child)
|
|
365 |
elif isinstance(child, Interface) \
|
|
366 |
or isinstance(child, InterfaceRef):
|
|
367 |
self.SetInterface(child)
|
|
368 |
elif isinstance(child, Variant) \
|
|
369 |
or isinstance(child, VariantRef):
|
|
370 |
self.AddVariant(child)
|
|
371 |
else:
|
|
372 |
raise InvalidChildError()
|
|
373 |
|
|
374 |
|
|
375 |
def AddChildSpecification(self, child):
|
|
376 |
child.SetParentSpec(self)
|
|
377 |
self.childSpecs.append(child)
|
|
378 |
|
|
379 |
|
|
380 |
def SetParentSpec(self, parent):
|
|
381 |
self.parentSpec = parent
|
|
382 |
|
|
383 |
|
|
384 |
def GetChildSpecs(self):
|
|
385 |
return self.childSpecs
|
|
386 |
|
|
387 |
|
|
388 |
def Valid(self):
|
|
389 |
return True
|
|
390 |
|
|
391 |
|
5
|
392 |
def GetAllVariantsRecursively(self, cache):
|
3
|
393 |
"""Returns all variants contained in this node and in its ancestors.
|
|
394 |
|
|
395 |
The returned value is a list, the structure of which is [variants-in-parent,
|
|
396 |
variants-in-self].
|
|
397 |
|
|
398 |
Note that the function recurses through parent *Specifications*, not through
|
|
399 |
the variants themselves.
|
|
400 |
"""
|
|
401 |
if self.parentSpec:
|
5
|
402 |
variants = self.parentSpec.GetAllVariantsRecursively(cache = cache)
|
3
|
403 |
else:
|
|
404 |
variants = []
|
|
405 |
|
5
|
406 |
variants.extend( self.GetVariants(cache = cache) )
|
3
|
407 |
|
|
408 |
return variants
|
|
409 |
|
|
410 |
|
|
411 |
class Filter(Specification):
|
|
412 |
"""A Filter is two Specification nodes and a True/False switch.
|
|
413 |
|
|
414 |
Filter extends Specification to have two nodes, only one of
|
|
415 |
which can be active at any time. Which node is active is determined
|
|
416 |
when the Configure method is called after setting up a Condition.
|
|
417 |
|
|
418 |
If several Conditions are set, the test is an OR of all of them."""
|
|
419 |
|
|
420 |
def __init__(self, name = ""):
|
5
|
421 |
Specification.__init__(self, name = name) # base class constructor
|
|
422 |
self.Else = Specification(name = name) # same for Else part
|
3
|
423 |
self.isTrue = True
|
|
424 |
self.configNames = set() #
|
|
425 |
self.variableNames = set() # TO DO: Condition class
|
|
426 |
self.variableValues = {} #
|
|
427 |
|
5
|
428 |
def __str__(self, prefix = ""):
|
|
429 |
s = "<filter name='%s'>\n"% self.name
|
|
430 |
s += "<if config='%s'>\n" % " | ".join(self.configNames)
|
|
431 |
s += Specification.__str__(self)
|
|
432 |
s += "</if>\n <else>\n"
|
|
433 |
s += str(self.Else)
|
|
434 |
s += " </else>\n</filter>\n"
|
|
435 |
return s
|
3
|
436 |
|
|
437 |
|
|
438 |
def SetConfigCondition(self, configName):
|
|
439 |
self.configNames = set([configName])
|
|
440 |
|
|
441 |
def AddConfigCondition(self, configName):
|
|
442 |
self.configNames.add(configName)
|
|
443 |
|
|
444 |
|
|
445 |
def SetVariableCondition(self, variableName, variableValues):
|
|
446 |
self.variableNames = set([variableName])
|
|
447 |
if type(variableValues) == types.ListType:
|
|
448 |
self.variableValues[variableName] = set(variableValues)
|
|
449 |
else:
|
|
450 |
self.variableValues[variableName] = set([variableValues])
|
|
451 |
|
|
452 |
def AddVariableCondition(self, variableName, variableValues):
|
|
453 |
self.variableNames.add(variableName)
|
|
454 |
if type(variableValues) == types.ListType:
|
|
455 |
self.variableValues[variableName] = set(variableValues)
|
|
456 |
else:
|
|
457 |
self.variableValues[variableName] = set([variableValues])
|
|
458 |
|
|
459 |
|
5
|
460 |
def Configure(self, buildUnit, cache):
|
3
|
461 |
self.isTrue = False
|
|
462 |
|
|
463 |
if buildUnit.name in self.configNames:
|
|
464 |
self.isTrue = True
|
|
465 |
elif self.variableNames:
|
5
|
466 |
|
|
467 |
evaluator = Evaluator(self.parentSpec, buildUnit, cache=cache)
|
3
|
468 |
|
|
469 |
for variableName in self.variableNames:
|
|
470 |
variableValue = evaluator.Get(variableName)
|
|
471 |
|
|
472 |
if variableValue in self.variableValues[variableName]:
|
|
473 |
self.isTrue = True
|
|
474 |
break
|
|
475 |
|
|
476 |
# configure all the children too
|
|
477 |
for spec in self.GetChildSpecs():
|
5
|
478 |
spec.Configure(buildUnit, cache=cache)
|
3
|
479 |
|
|
480 |
|
|
481 |
def HasInterface(self):
|
|
482 |
if self.isTrue:
|
|
483 |
return Specification.HasInterface(self)
|
|
484 |
else:
|
|
485 |
return self.Else.HasInterface()
|
|
486 |
|
|
487 |
|
5
|
488 |
def GetInterface(self, cache):
|
3
|
489 |
if self.isTrue:
|
5
|
490 |
return Specification.GetInterface(self, cache = cache)
|
3
|
491 |
else:
|
5
|
492 |
return self.Else.GetInterface(cache = cache)
|
3
|
493 |
|
|
494 |
|
5
|
495 |
def GetVariants(self, cache):
|
3
|
496 |
if self.isTrue:
|
5
|
497 |
return Specification.GetVariants(self, cache = cache)
|
3
|
498 |
else:
|
5
|
499 |
return self.Else.GetVariants(cache = cache)
|
3
|
500 |
|
|
501 |
|
|
502 |
def SetParentSpec(self, parent):
|
|
503 |
# base class method
|
|
504 |
Specification.SetParentSpec(self, parent)
|
|
505 |
# same for Else part
|
|
506 |
self.Else.SetParentSpec(parent)
|
|
507 |
|
|
508 |
|
|
509 |
def GetChildSpecs(self):
|
|
510 |
if self.isTrue:
|
|
511 |
return Specification.GetChildSpecs(self)
|
|
512 |
else:
|
|
513 |
return self.Else.GetChildSpecs()
|
|
514 |
|
|
515 |
|
|
516 |
class Parameter(Model):
|
|
517 |
|
|
518 |
def __init__(self, name = None, default = None):
|
|
519 |
Model.__init__(self) # base class constructor
|
|
520 |
self.name = name
|
|
521 |
self.default = default
|
|
522 |
|
|
523 |
|
|
524 |
def SetProperty(self, name, value):
|
|
525 |
if name == "name":
|
|
526 |
self.name = value
|
|
527 |
elif name == "default":
|
|
528 |
self.default = value
|
|
529 |
else:
|
|
530 |
raise InvalidPropertyError()
|
|
531 |
|
|
532 |
|
|
533 |
def Valid(self):
|
|
534 |
return (self.name != None)
|
|
535 |
|
|
536 |
class ParameterGroup(Model):
|
|
537 |
"""A group of Parameters specified in an interface by a regexp"""
|
|
538 |
def __init__(self, pattern = None, default = None):
|
|
539 |
Model.__init__(self) # base class constructor
|
|
540 |
self.pattern = pattern
|
|
541 |
|
|
542 |
self.patternre = None
|
|
543 |
if pattern:
|
|
544 |
try:
|
|
545 |
self.patternre = re.compile(pattern)
|
|
546 |
except TypeError:
|
|
547 |
pass
|
|
548 |
self.default = default
|
|
549 |
|
|
550 |
|
|
551 |
def SetProperty(self, pattern, value):
|
|
552 |
if pattern == "pattern":
|
|
553 |
self.pattern = value
|
|
554 |
self.patternre = re.compile(value)
|
|
555 |
elif pattern == "default":
|
|
556 |
self.default = value
|
|
557 |
else:
|
|
558 |
raise InvalidPropertyError()
|
|
559 |
|
|
560 |
|
|
561 |
def Valid(self):
|
|
562 |
return (self.pattern != None and self.patternre != None)
|
|
563 |
|
|
564 |
|
|
565 |
class Operation(Model):
|
|
566 |
"Base class for variant operations"
|
5
|
567 |
__slots__ = 'type'
|
3
|
568 |
def __init__(self):
|
|
569 |
Model.__init__(self) # base class constructor
|
|
570 |
self.type = None
|
|
571 |
|
|
572 |
def Apply(self, oldValue):
|
|
573 |
pass
|
|
574 |
|
|
575 |
|
|
576 |
class Append(Operation):
|
5
|
577 |
__slots__ = ('name', 'value', 'separator')
|
3
|
578 |
def __init__(self, name = None, value = None, separator = " "):
|
|
579 |
Operation.__init__(self) # base class constructor
|
|
580 |
self.name = name
|
|
581 |
self.value = value
|
|
582 |
self.separator = separator
|
|
583 |
|
|
584 |
|
5
|
585 |
def __str__(self):
|
3
|
586 |
attributes = "name='" + self.name + "' value='" + self.value + "' separator='" + self.separator + "'"
|
5
|
587 |
return "<append %s/>" % attributes
|
3
|
588 |
|
|
589 |
|
|
590 |
def Apply(self, oldValue):
|
|
591 |
if len(oldValue) > 0:
|
|
592 |
if len(self.value) > 0:
|
|
593 |
return oldValue + self.separator + self.value
|
|
594 |
else:
|
|
595 |
return oldValue
|
|
596 |
else:
|
|
597 |
return self.value
|
|
598 |
|
|
599 |
|
|
600 |
def SetProperty(self, name, value):
|
|
601 |
if name == "name":
|
|
602 |
self.name = value
|
|
603 |
elif name == "value":
|
|
604 |
self.value = value
|
|
605 |
elif name == "separator":
|
|
606 |
self.separator = value
|
|
607 |
else:
|
|
608 |
raise InvalidPropertyError()
|
|
609 |
|
|
610 |
|
|
611 |
def Valid(self):
|
|
612 |
return (self.name != None and self.value != None)
|
|
613 |
|
|
614 |
|
|
615 |
class Prepend(Operation):
|
5
|
616 |
__slots__ = ('name', 'value', 'separator')
|
3
|
617 |
def __init__(self, name = None, value = None, separator = " "):
|
|
618 |
Operation.__init__(self) # base class constructor
|
|
619 |
self.name = name
|
|
620 |
self.value = value
|
|
621 |
self.separator = separator
|
|
622 |
|
|
623 |
|
5
|
624 |
def __str__(self, prefix = ""):
|
3
|
625 |
attributes = "name='" + self.name + "' value='" + self.value + "' separator='" + self.separator + "'"
|
5
|
626 |
return "<prepend %s/>" % prefix
|
3
|
627 |
|
|
628 |
|
|
629 |
def Apply(self, oldValue):
|
|
630 |
if len(oldValue) > 0:
|
|
631 |
if len(self.value) > 0:
|
|
632 |
return self.value + self.separator + oldValue
|
|
633 |
else:
|
|
634 |
return oldValue
|
|
635 |
else:
|
|
636 |
return self.value
|
|
637 |
|
|
638 |
|
|
639 |
def SetProperty(self, name, value):
|
|
640 |
if name == "name":
|
|
641 |
self.name = value
|
|
642 |
elif name == "value":
|
|
643 |
self.value = value
|
|
644 |
elif name == "separator":
|
|
645 |
self.separator = value
|
|
646 |
else:
|
|
647 |
raise InvalidPropertyError()
|
|
648 |
|
|
649 |
|
|
650 |
def Valid(self):
|
|
651 |
return (self.name != None and self.value != None)
|
|
652 |
|
|
653 |
|
|
654 |
class Set(Operation):
|
5
|
655 |
__slots__ = ('name', 'value', 'type', 'versionCommand', 'versionResult')
|
3
|
656 |
"""implementation of <set> operation"""
|
|
657 |
|
|
658 |
def __init__(self, name = None, value = "", type = ""):
|
|
659 |
Operation.__init__(self) # base class constructor
|
|
660 |
self.name = name
|
|
661 |
self.value = value
|
|
662 |
self.type = type
|
|
663 |
self.versionCommand = ""
|
|
664 |
self.versionResult = ""
|
|
665 |
|
|
666 |
|
5
|
667 |
def __str__(self):
|
3
|
668 |
attributes = "name='" + self.name + "' value='" + self.value + "' type='" + self.type + "'"
|
|
669 |
if type == "tool":
|
|
670 |
attributes += " versionCommand='" + self.versionCommand + "' versionResult='" + self.versionResult
|
|
671 |
|
5
|
672 |
return "<set %s/>" % attributes
|
3
|
673 |
|
|
674 |
|
|
675 |
def Apply(self, oldValue):
|
|
676 |
return self.value
|
|
677 |
|
|
678 |
|
|
679 |
def SetProperty(self, name, value):
|
|
680 |
if name == "name":
|
|
681 |
self.name = value
|
|
682 |
elif name == "value":
|
|
683 |
self.value = value
|
|
684 |
elif name == "type":
|
|
685 |
self.type = value
|
|
686 |
elif name == "versionCommand":
|
|
687 |
self.versionCommand = value
|
|
688 |
elif name == "versionResult":
|
|
689 |
self.versionResult = value
|
|
690 |
elif name == "host":
|
|
691 |
if HostPlatform.IsKnown(value):
|
|
692 |
self.host = value
|
|
693 |
else:
|
|
694 |
raise InvalidPropertyError()
|
|
695 |
|
|
696 |
|
|
697 |
def Valid(self):
|
|
698 |
return (self.name != None and self.value != None)
|
|
699 |
|
5
|
700 |
class BadToolValue(Exception):
|
|
701 |
pass
|
3
|
702 |
|
|
703 |
class Env(Set):
|
|
704 |
"""implementation of <env> operator"""
|
|
705 |
|
|
706 |
def __init__(self, name = None, default = None, type = ""):
|
|
707 |
Set.__init__(self, name, "", type) # base class constructor
|
|
708 |
self.default = default
|
|
709 |
|
|
710 |
|
5
|
711 |
def __str__(self):
|
3
|
712 |
attributes = "name='" + self.name + "' type='" + self.type + "'"
|
|
713 |
if default != None:
|
|
714 |
attributes += " default='" + self.default + "'"
|
|
715 |
|
|
716 |
if type == "tool":
|
|
717 |
attributes += " versionCommand='" + self.versionCommand + "' versionResult='" + self.versionResult + "'"
|
|
718 |
|
5
|
719 |
return "<env %s/>" % attributes
|
3
|
720 |
|
|
721 |
|
|
722 |
def Apply(self, oldValue):
|
|
723 |
try:
|
|
724 |
value = os.environ[self.name]
|
|
725 |
|
|
726 |
# if this value is a "path" or a "tool" then we need to make sure
|
|
727 |
# it is a proper absolute path in our preferred format.
|
|
728 |
if value and (self.type == "path" or self.type == "tool"):
|
|
729 |
try:
|
|
730 |
path = generic_path.Path(value)
|
|
731 |
value = str(path.Absolute())
|
|
732 |
except ValueError,e:
|
5
|
733 |
raise BadToolValue("the environment variable %s is incorrect: %s" % (self.name, str(e)))
|
3
|
734 |
except KeyError:
|
|
735 |
if self.default != None:
|
|
736 |
value = self.default
|
|
737 |
else:
|
5
|
738 |
raise BadToolValue("%s is not set in the environment and has no default" % self.name)
|
3
|
739 |
|
|
740 |
return value
|
|
741 |
|
|
742 |
|
|
743 |
def SetProperty(self, name, value):
|
|
744 |
if name == "default":
|
|
745 |
self.default = value
|
|
746 |
else:
|
|
747 |
Set.SetProperty(self, name, value)
|
|
748 |
|
|
749 |
|
|
750 |
def Valid(self):
|
|
751 |
return (self.name != None)
|
|
752 |
|
|
753 |
|
|
754 |
class BuildUnit(object):
|
|
755 |
"Represents an individual buildable unit."
|
|
756 |
|
|
757 |
def __init__(self, name, variants):
|
|
758 |
self.name = name
|
|
759 |
|
|
760 |
# A list of Variant objects.
|
|
761 |
self.variants = variants
|
|
762 |
|
|
763 |
# Cache for the variant operations implied by this BuildUnit.
|
|
764 |
self.operations = []
|
|
765 |
self.variantKey = ""
|
|
766 |
|
5
|
767 |
def GetOperations(self, cache):
|
3
|
768 |
"""Return all operations related to this BuildUnit.
|
|
769 |
|
|
770 |
The result is cached, and so will only be computed once per BuildUnit.
|
|
771 |
"""
|
|
772 |
key = '.'.join([x.name for x in self.variants])
|
|
773 |
if self.variantKey != key:
|
|
774 |
self.variantKey = key
|
|
775 |
for v in self.variants:
|
5
|
776 |
self.operations.extend( v.GetAllOperationsRecursively(cache=cache) )
|
3
|
777 |
|
|
778 |
return self.operations
|
|
779 |
|
|
780 |
class Config(object):
|
|
781 |
"""Abstract type representing an argument to the '-c' option.
|
|
782 |
|
|
783 |
The fundamental property of a Config is that it can generate one or more
|
|
784 |
BuildUnits.
|
|
785 |
"""
|
|
786 |
|
|
787 |
def __init__(self):
|
|
788 |
self.modifiers = []
|
|
789 |
|
|
790 |
def AddModifier(self, variant):
|
|
791 |
self.modifiers.append(variant)
|
|
792 |
|
|
793 |
def ClearModifiers(self):
|
|
794 |
self.modifiers = []
|
|
795 |
|
5
|
796 |
def GenerateBuildUnits(self,cache):
|
3
|
797 |
"""Returns a list of BuildUnits.
|
|
798 |
|
|
799 |
This function must be overridden by derived classes.
|
|
800 |
"""
|
|
801 |
raise NotImplementedError()
|
|
802 |
|
|
803 |
|
|
804 |
class Variant(Model, Config):
|
|
805 |
|
5
|
806 |
__slots__ = ('cache','name','host','extends','ops','variantRefs','allOperations')
|
|
807 |
|
3
|
808 |
def __init__(self, name = ""):
|
|
809 |
Model.__init__(self)
|
|
810 |
Config.__init__(self)
|
|
811 |
self.name = name
|
|
812 |
|
|
813 |
# Operations defined inside this variant.
|
|
814 |
self.ops = []
|
|
815 |
|
|
816 |
# The name of our parent variant, if any.
|
|
817 |
self.extends = ""
|
|
818 |
|
|
819 |
# Any variant references used inside this variant.
|
|
820 |
self.variantRefs = []
|
|
821 |
|
|
822 |
self.allOperations = []
|
|
823 |
|
|
824 |
def SetProperty(self, name, value):
|
|
825 |
if name == "name":
|
|
826 |
self.name = value
|
|
827 |
elif name == "host":
|
|
828 |
if HostPlatform.IsKnown(value):
|
|
829 |
self.host = value
|
|
830 |
elif name == "extends":
|
|
831 |
self.extends = value
|
|
832 |
else:
|
|
833 |
raise InvalidPropertyError()
|
|
834 |
|
|
835 |
def AddChild(self, child):
|
|
836 |
if isinstance(child, Operation):
|
|
837 |
self.ops.append(child)
|
|
838 |
elif isinstance(child, VariantRef):
|
|
839 |
self.variantRefs.append(child)
|
|
840 |
else:
|
|
841 |
raise InvalidChildError()
|
|
842 |
|
|
843 |
def Valid(self):
|
|
844 |
return self.name
|
|
845 |
|
|
846 |
def AddOperation(self, op):
|
|
847 |
self.ops.append(op)
|
|
848 |
|
5
|
849 |
def GetAllOperationsRecursively(self, cache):
|
3
|
850 |
"""Returns a list of all operations in this variant.
|
|
851 |
|
|
852 |
The list elements are themselves lists; the overall structure of the
|
|
853 |
returned value is:
|
|
854 |
|
|
855 |
[ [ops-from-parent],[ops-from-varRefs], [ops-in-self] ]
|
|
856 |
"""
|
|
857 |
|
|
858 |
if not self.allOperations:
|
|
859 |
if self.extends:
|
5
|
860 |
parent = cache.FindNamedVariant(self.extends)
|
|
861 |
self.allOperations.extend( parent.GetAllOperationsRecursively(cache = cache) )
|
3
|
862 |
for r in self.variantRefs:
|
5
|
863 |
for v in [ r.Resolve(cache = cache) ] + r.GetModifiers(cache = cache):
|
|
864 |
self.allOperations.extend( v.GetAllOperationsRecursively(cache = cache) )
|
3
|
865 |
self.allOperations.append(self.ops)
|
|
866 |
|
|
867 |
return self.allOperations
|
|
868 |
|
5
|
869 |
def GenerateBuildUnits(self,cache):
|
3
|
870 |
|
|
871 |
name = self.name
|
|
872 |
vars = [self]
|
|
873 |
|
|
874 |
for m in self.modifiers:
|
|
875 |
name = name + "." + m.name
|
|
876 |
vars.append(m)
|
5
|
877 |
return [ BuildUnit(name=name, variants=vars) ]
|
3
|
878 |
|
5
|
879 |
def __str__(self):
|
|
880 |
s = "<var name='%s' extends='%s'>\n" % (self.name, self.extends)
|
3
|
881 |
for op in self.ops:
|
5
|
882 |
s += str(op) + '\n'
|
|
883 |
s += "</var>"
|
|
884 |
return s
|
3
|
885 |
|
5
|
886 |
import traceback
|
3
|
887 |
class VariantRef(Reference):
|
|
888 |
|
|
889 |
def __init__(self, ref=None):
|
5
|
890 |
Reference.__init__(self, ref = ref)
|
3
|
891 |
|
5
|
892 |
def __str__(self):
|
|
893 |
return "<varRef ref='%s'/>" % self.ref
|
3
|
894 |
|
5
|
895 |
def Resolve(self, cache):
|
3
|
896 |
try:
|
5
|
897 |
return cache.FindNamedVariant(self.ref)
|
|
898 |
except KeyError, e:
|
3
|
899 |
raise BadReferenceError(self.ref)
|
|
900 |
|
5
|
901 |
class MissingVariantException(Exception):
|
|
902 |
pass
|
3
|
903 |
|
|
904 |
class Alias(Model, Config):
|
|
905 |
|
|
906 |
def __init__(self, name=""):
|
|
907 |
Model.__init__(self)
|
|
908 |
Config.__init__(self)
|
|
909 |
self.name = name
|
|
910 |
self.meaning = ""
|
|
911 |
self.varRefs = []
|
|
912 |
self.variants = []
|
|
913 |
|
5
|
914 |
def __str__(self):
|
|
915 |
return "<alias name='%s' meaning='%s'/>" % (self.name, self.meaning)
|
3
|
916 |
|
|
917 |
def SetProperty(self, key, val):
|
|
918 |
if key == "name":
|
|
919 |
self.name = val
|
|
920 |
elif key == "meaning":
|
|
921 |
self.meaning = val
|
|
922 |
|
|
923 |
for u in val.split("."):
|
5
|
924 |
self.varRefs.append( VariantRef(ref = u) )
|
3
|
925 |
else:
|
|
926 |
raise InvalidPropertyError()
|
|
927 |
|
|
928 |
def Valid(self):
|
|
929 |
return self.name and self.meaning
|
|
930 |
|
5
|
931 |
def GenerateBuildUnits(self, cache):
|
3
|
932 |
if not self.variants:
|
5
|
933 |
missing_variants = []
|
3
|
934 |
for r in self.varRefs:
|
|
935 |
try:
|
5
|
936 |
self.variants.append( r.Resolve(cache=cache) )
|
3
|
937 |
except BadReferenceError:
|
5
|
938 |
missing_variants.append(r.ref)
|
|
939 |
|
|
940 |
if len(missing_variants) > 0:
|
|
941 |
raise MissingVariantException("Missing variants '%s'", " ".join(missing_variants))
|
3
|
942 |
|
|
943 |
name = self.name
|
|
944 |
|
|
945 |
for v in self.modifiers:
|
|
946 |
name = name + "." + v.name
|
|
947 |
|
5
|
948 |
return [ BuildUnit(name=name, variants=self.variants + self.modifiers) ]
|
3
|
949 |
|
|
950 |
|
|
951 |
class AliasRef(Reference):
|
|
952 |
|
|
953 |
def __init__(self, ref=None):
|
|
954 |
Reference.__init__(self, ref)
|
|
955 |
|
5
|
956 |
def __str__(self):
|
|
957 |
return "<aliasRef ref='%s'/>" % self.ref
|
3
|
958 |
|
5
|
959 |
def Resolve(self, cache):
|
3
|
960 |
try:
|
5
|
961 |
return cache.FindNamedAlias(self.ref)
|
3
|
962 |
except KeyError:
|
|
963 |
raise BadReferenceError(self.ref)
|
|
964 |
|
|
965 |
|
|
966 |
class Group(Model, Config):
|
|
967 |
def __init__(self, name=""):
|
|
968 |
Model.__init__(self)
|
|
969 |
Config.__init__(self)
|
|
970 |
self.name = name
|
|
971 |
self.childRefs = []
|
|
972 |
|
|
973 |
def SetProperty(self, key, val):
|
|
974 |
if key == "name":
|
|
975 |
self.name = val
|
|
976 |
else:
|
|
977 |
raise InvalidPropertyError()
|
|
978 |
|
|
979 |
def AddChild(self, child):
|
|
980 |
if isinstance( child, (VariantRef,AliasRef,GroupRef) ):
|
|
981 |
self.childRefs.append(child)
|
|
982 |
else:
|
|
983 |
raise InvalidChildError()
|
|
984 |
|
|
985 |
def Valid(self):
|
|
986 |
return self.name and self.childRefs
|
|
987 |
|
5
|
988 |
def __str__(self):
|
|
989 |
s = "<group name='%s'>" % self.name
|
3
|
990 |
for r in self.childRefs:
|
5
|
991 |
s += str(r)
|
|
992 |
s += "</group>"
|
|
993 |
return s
|
3
|
994 |
|
5
|
995 |
def GenerateBuildUnits(self, cache):
|
3
|
996 |
units = []
|
|
997 |
|
5
|
998 |
missing_variants = []
|
3
|
999 |
for r in self.childRefs:
|
5
|
1000 |
refMods = r.GetModifiers(cache)
|
3
|
1001 |
|
|
1002 |
try:
|
5
|
1003 |
obj = r.Resolve(cache=cache)
|
3
|
1004 |
except BadReferenceError:
|
5
|
1005 |
missing_variants.append(r.ref)
|
3
|
1006 |
else:
|
|
1007 |
obj.ClearModifiers()
|
|
1008 |
|
|
1009 |
for m in refMods + self.modifiers:
|
|
1010 |
obj.AddModifier(m)
|
|
1011 |
|
5
|
1012 |
units.extend( obj.GenerateBuildUnits(cache) )
|
|
1013 |
|
|
1014 |
if len(missing_variants) > 0:
|
|
1015 |
raise MissingVariantException("Missing variants '%s'", " ".join(missing_variants))
|
3
|
1016 |
|
|
1017 |
return units
|
|
1018 |
|
|
1019 |
|
|
1020 |
class GroupRef(Reference):
|
|
1021 |
|
|
1022 |
def __init__(self, ref=None):
|
|
1023 |
Reference.__init__(self, ref)
|
|
1024 |
|
5
|
1025 |
def __str__(self):
|
|
1026 |
return "<%s /><groupRef ref='%s' mod='%s'/>" % (prefix, self.ref, ".".join(self.modifiers))
|
3
|
1027 |
|
5
|
1028 |
def Resolve(self, cache):
|
3
|
1029 |
try:
|
5
|
1030 |
return cache.FindNamedGroup(self.ref)
|
3
|
1031 |
except KeyError:
|
|
1032 |
raise BadReferenceError(self.ref)
|
|
1033 |
|
5
|
1034 |
class ToolErrorException(Exception):
|
|
1035 |
def __init__(self, s):
|
|
1036 |
Exception.__init__(self,s)
|
|
1037 |
|
3
|
1038 |
class Tool(object):
|
|
1039 |
"""Represents a tool that might be used by raptor e.g. a compiler"""
|
|
1040 |
|
5
|
1041 |
# It's difficult and expensive to give each tool a log reference but a class one
|
|
1042 |
# will facilitate debugging when that is needed without being a design flaw the
|
|
1043 |
# rest of the time.
|
|
1044 |
log = raptor_utilities.nulllog
|
|
1045 |
|
3
|
1046 |
# For use in dealing with tools that return non-ascii version strings.
|
|
1047 |
nonascii = ""
|
|
1048 |
identity_chartable = chr(0)
|
|
1049 |
for c in xrange(1,128):
|
|
1050 |
identity_chartable += chr(c)
|
|
1051 |
for c in xrange(128,256):
|
|
1052 |
nonascii += chr(c)
|
|
1053 |
identity_chartable += " "
|
|
1054 |
|
5
|
1055 |
def __init__(self, name, command, versioncommand, versionresult, id=""):
|
3
|
1056 |
self.name = name
|
|
1057 |
self.command = command
|
|
1058 |
self.versioncommand = versioncommand
|
|
1059 |
self.versionresult = versionresult
|
|
1060 |
self.id = id # what config this is from - used in debug messages
|
|
1061 |
self.date = None
|
|
1062 |
|
|
1063 |
|
|
1064 |
# Assume the tool is unavailable or the wrong
|
|
1065 |
# version until someone proves that it's OK
|
|
1066 |
self.valid = False
|
|
1067 |
|
|
1068 |
|
|
1069 |
def expand(self, toolset):
|
|
1070 |
self.versioncommand = toolset.ExpandAll(self.versioncommand)
|
|
1071 |
self.versionresult = toolset.ExpandAll(self.versionresult)
|
|
1072 |
self.command = toolset.ExpandAll(self.command)
|
|
1073 |
self.key = hashlib.md5(self.versioncommand + self.versionresult).hexdigest()
|
|
1074 |
|
|
1075 |
# We need the tool's date to find out if we should check it.
|
|
1076 |
try:
|
|
1077 |
if '/' in self.command:
|
|
1078 |
testfile = os.path.abspath(self.command.strip("\"'"))
|
|
1079 |
else:
|
|
1080 |
# The tool isn't a relative or absolute path so the could be relying on the
|
|
1081 |
# $PATH variable to make it available. We must find the tool if it's a simple
|
|
1082 |
# executable file (e.g. "armcc" rather than "python myscript.py") then get it's date.
|
|
1083 |
# We can use the date later to see if our cache is valid.
|
|
1084 |
# If it really is not a simple command then we won't be able to get a date and
|
|
1085 |
# we won't be able to tell if it is altered or updated - too bad!
|
|
1086 |
testfile = generic_path.Where(self.command)
|
5
|
1087 |
#self.log.Debug("toolcheck: tool '%s' was found on the path at '%s' ", self.command, testfile)
|
3
|
1088 |
if testfile is None:
|
|
1089 |
raise Exception("Can't be found in path")
|
|
1090 |
|
|
1091 |
if not os.path.isfile(testfile):
|
|
1092 |
raise Exception("tool %s appears to not be a file %s", self.command, testfile)
|
|
1093 |
|
|
1094 |
testfile_stat = os.stat(testfile)
|
|
1095 |
self.date = testfile_stat.st_mtime
|
|
1096 |
except Exception,e:
|
5
|
1097 |
# We really don't mind if the tool could not be dated - for any reason
|
|
1098 |
Tool.log.Debug("toolcheck: '%s=%s' cannot be dated - this is ok, but the toolcheck won't be able to tell when a new version of the tool is installed. (%s)", self.name, self.command, str(e))
|
|
1099 |
pass
|
3
|
1100 |
|
|
1101 |
|
5
|
1102 |
def check(self, shell, evaluator, log = raptor_utilities.nulllog):
|
3
|
1103 |
|
|
1104 |
self.vre = re.compile(self.versionresult)
|
|
1105 |
|
|
1106 |
try:
|
|
1107 |
self.log.Debug("Pre toolcheck: '%s' for version '%s'", self.name, self.versionresult)
|
|
1108 |
p = subprocess.Popen(args=[shell, "-c", self.versioncommand], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
|
5
|
1109 |
log.Debug("Checking tool '%s' for version '%s'", self.name, self.versionresult)
|
3
|
1110 |
versionoutput,err = p.communicate()
|
|
1111 |
except Exception,e:
|
|
1112 |
versionoutput=None
|
|
1113 |
|
|
1114 |
# Some tools return version strings with unicode characters!
|
|
1115 |
# There is no good response other than a lot of decoding and encoding.
|
|
1116 |
# Simpler to ignore it:
|
|
1117 |
versionoutput_a = versionoutput.translate(Tool.identity_chartable,"")
|
|
1118 |
|
|
1119 |
if versionoutput_a and self.vre.search(versionoutput_a) != None:
|
5
|
1120 |
log.Debug("tool '%s' returned an acceptable version '%s'", self.name, versionoutput_a)
|
3
|
1121 |
self.valid = True
|
|
1122 |
else:
|
|
1123 |
self.valid = False
|
5
|
1124 |
raise ToolErrorException("tool '%s' from config '%s' did not return version '%s' as required.\nCommand '%s' returned:\n%s\nCheck your environment and configuration.\n" % (self.name, self.id, self.versionresult, self.versioncommand, versionoutput_a))
|
3
|
1125 |
|
|
1126 |
def envhash(irrelevant_vars):
|
|
1127 |
"""Determine something unique about this environment to identify it.
|
|
1128 |
must ignore variables that change without mattering to the caller
|
|
1129 |
e.g. perhaps PATH matters but PWD and PPID don't"""
|
|
1130 |
envid = hashlib.md5()
|
|
1131 |
for k in os.environ:
|
|
1132 |
if k not in irrelevant_vars:
|
|
1133 |
envid.update(os.environ[k])
|
|
1134 |
return envid.hexdigest()[:16]
|
|
1135 |
|
|
1136 |
|
|
1137 |
class ToolSet(object):
|
|
1138 |
"""
|
|
1139 |
This class manages a bunch of tools and keeps a cache of
|
|
1140 |
all tools that it ever sees (across all configurations).
|
|
1141 |
toolset.check() is called for each config but the cache is kept across calls to
|
|
1142 |
catch the use of one tool in many configs.
|
|
1143 |
write() is used to flush the cache to disc.
|
|
1144 |
"""
|
|
1145 |
# The raptor shell - this is not mutable.
|
5
|
1146 |
if 'SBS_SHELL' in os.environ:
|
|
1147 |
shell = os.environ['SBS_SHELL']
|
|
1148 |
else:
|
|
1149 |
hostbinaries = os.path.join(os.environ['SBS_HOME'],
|
|
1150 |
os.environ['HOSTPLATFORM_DIR'])
|
3
|
1151 |
|
5
|
1152 |
if HostPlatform.IsHost('lin*'):
|
|
1153 |
shell=os.path.join(hostbinaries, 'bin/bash')
|
3
|
1154 |
else:
|
5
|
1155 |
if 'SBS_CYGWIN' in os.environ:
|
|
1156 |
shell=os.path.join(os.environ['SBS_CYGWIN'], 'bin\\bash.exe')
|
|
1157 |
else:
|
|
1158 |
shell=os.path.join(hostbinaries, 'cygwin\\bin\\bash.exe')
|
3
|
1159 |
|
|
1160 |
|
|
1161 |
irrelevant_vars = ['PWD','OLDPWD','PID','PPID', 'SHLVL' ]
|
|
1162 |
|
|
1163 |
|
|
1164 |
shell_version=".*GNU bash, version [34].*"
|
|
1165 |
shell_re = re.compile(shell_version)
|
|
1166 |
if 'SBS_BUILD_DIR' in os.environ:
|
|
1167 |
cachefile_basename = str(generic_path.Join(os.environ['SBS_BUILD_DIR'],"toolcheck_cache_"))
|
|
1168 |
elif 'EPOCROOT' in os.environ:
|
|
1169 |
cachefile_basename = str(generic_path.Join(os.environ['EPOCROOT'],"epoc32/build/toolcheck_cache_"))
|
|
1170 |
else:
|
|
1171 |
cachefile_basename = None
|
|
1172 |
|
|
1173 |
tool_env_id = envhash(irrelevant_vars)
|
|
1174 |
filemarker = "sbs_toolcache_2.8.2"
|
|
1175 |
|
|
1176 |
def __init__(self, log = raptor_utilities.nulllog, forced=False):
|
|
1177 |
self.__toolcheckcache = {}
|
|
1178 |
|
|
1179 |
self.valid = True
|
|
1180 |
self.checked = False
|
|
1181 |
self.shellok = False
|
|
1182 |
self.configname=""
|
|
1183 |
self.cache_loaded = False
|
|
1184 |
self.forced = forced
|
|
1185 |
|
|
1186 |
self.log=log
|
|
1187 |
|
|
1188 |
# Read in the tool cache
|
|
1189 |
#
|
|
1190 |
# The cache format is a hash key which identifies the
|
|
1191 |
# command and the version that we're checking for. Then
|
|
1192 |
# there are name,value pairs that record, e.g. the date
|
|
1193 |
# of the command file or the name of the variable that
|
|
1194 |
# the config uses for the tool (GNUCP or MWCC or whatever)
|
|
1195 |
|
|
1196 |
if ToolSet.cachefile_basename:
|
|
1197 |
self.cachefilename = ToolSet.cachefile_basename+".tmp"
|
|
1198 |
if not self.forced:
|
|
1199 |
try:
|
|
1200 |
f = open(self.cachefilename, "r+")
|
|
1201 |
# if this tool cache was recorded in
|
|
1202 |
# a different environment then ignore it.
|
|
1203 |
marker = f.readline().rstrip("\r\n")
|
|
1204 |
if marker == ToolSet.filemarker:
|
|
1205 |
env_id_tmp = f.readline().rstrip("\r\n")
|
|
1206 |
if env_id_tmp == ToolSet.tool_env_id:
|
|
1207 |
try:
|
|
1208 |
for l in f.readlines():
|
|
1209 |
toolhistory = l.rstrip(",\n\r").split(",")
|
|
1210 |
ce = {}
|
|
1211 |
for i in toolhistory[1:]:
|
|
1212 |
(name,val) = i.split("=")
|
|
1213 |
if name == "valid":
|
|
1214 |
val = bool(val)
|
|
1215 |
elif name == "age":
|
|
1216 |
val = int(val)
|
|
1217 |
elif name == "date":
|
|
1218 |
if val != "None":
|
|
1219 |
val = float(val)
|
|
1220 |
else:
|
|
1221 |
val= None
|
|
1222 |
|
|
1223 |
ce[name] = val
|
|
1224 |
self.__toolcheckcache[toolhistory[0]] = ce
|
|
1225 |
log.Info("Loaded toolcheck cache: %s\n", self.cachefilename)
|
|
1226 |
except Exception, e:
|
|
1227 |
log.Info("Ignoring garbled toolcheck cache: %s (%s)\n", self.cachefilename, str(e))
|
|
1228 |
self.__toolcheckcache = {}
|
|
1229 |
|
|
1230 |
else:
|
|
1231 |
log.Info("Toolcheck cache %s ignored - environment changed\n", self.cachefilename)
|
|
1232 |
else:
|
|
1233 |
log.Info("Toolcheck cache not loaded = marker missing: %s %s\n", self.cachefilename, ToolSet.filemarker)
|
|
1234 |
f.close()
|
|
1235 |
except IOError, e:
|
|
1236 |
log.Info("Failed to load toolcheck cache: %s\n", self.cachefilename)
|
|
1237 |
else:
|
|
1238 |
log.Debug("Toolcheck cachefile not created because EPOCROOT not set in environment.\n")
|
|
1239 |
|
|
1240 |
def check_shell(self):
|
|
1241 |
# The command shell is a critical tool because all the other tools run
|
|
1242 |
# within it so we must check for it first. It has to be in the path.
|
|
1243 |
# bash 4 is preferred, 3 is accepted
|
|
1244 |
try:
|
|
1245 |
p = subprocess.Popen(args=[ToolSet.shell, '--version'], bufsize=1024, shell = False, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, universal_newlines=True)
|
|
1246 |
shellversion_out, errtxt = p.communicate()
|
|
1247 |
if ToolSet.shell_re.search(shellversion_out) == None:
|
|
1248 |
self.log.Error("A critical tool, '%s', did not return the required version '%s':\n%s\nPlease check that '%s' is in the path.", ToolSet.shell, ToolSet.shell_version, shellversion_out, ToolSet.shell)
|
|
1249 |
self.valid = False
|
|
1250 |
except Exception,e:
|
|
1251 |
self.log.Error("A critical tool could not be found.\nPlease check that '%s' is in the path. (%s)", ToolSet.shell, str(e))
|
|
1252 |
self.valid = False
|
|
1253 |
|
|
1254 |
return self.valid
|
|
1255 |
|
|
1256 |
def check(self, evaluator, configname):
|
|
1257 |
"""Check the toolset for a particular config"""
|
|
1258 |
|
|
1259 |
self.checked = True # remember that we did check something
|
|
1260 |
|
|
1261 |
if not self.shellok:
|
|
1262 |
self.shellok = self.check_shell()
|
|
1263 |
self.shellok = True
|
|
1264 |
|
|
1265 |
self.valid = self.valid and self.shellok
|
|
1266 |
|
|
1267 |
cache = self.__toolcheckcache
|
|
1268 |
for tool in evaluator.tools:
|
|
1269 |
if not self.forced:
|
|
1270 |
try:
|
|
1271 |
t = cache[tool.key]
|
|
1272 |
|
|
1273 |
except KeyError,e:
|
|
1274 |
pass
|
|
1275 |
else:
|
|
1276 |
# if the cache has an entry for the tool then see if the date on
|
|
1277 |
# the tool has changed (assuming the tool is a simple executable file)
|
|
1278 |
if t.has_key('date') and (tool.date is None or (tool.date - t['date'] > 0.1)) :
|
|
1279 |
self.log.Debug("toolcheck forced: '%s' changed since the last check: %s < %s", tool.command, str(t['date']), str(tool.date))
|
|
1280 |
else:
|
|
1281 |
t['age'] = 0 # we used it so it's obviously needed
|
|
1282 |
self.valid = self.valid and t['valid']
|
|
1283 |
self.log.Debug("toolcheck saved on: '%s'", tool.name)
|
|
1284 |
continue
|
|
1285 |
|
|
1286 |
|
|
1287 |
self.log.Debug("toolcheck done: %s -key: %s" % (tool.name, tool.key))
|
|
1288 |
|
5
|
1289 |
try:
|
|
1290 |
tool.check(ToolSet.shell, evaluator, log = self.log)
|
|
1291 |
except ToolErrorException, e:
|
3
|
1292 |
self.valid = False
|
5
|
1293 |
self.log.Error("%s\n" % str(e))
|
3
|
1294 |
|
|
1295 |
# Tool failures are cached just like successes - don't want to repeat them
|
|
1296 |
cache[tool.key] = { "name" : tool.name, "valid" : tool.valid, "age" : 0 , "date" : tool.date }
|
|
1297 |
|
|
1298 |
|
|
1299 |
def write(self):
|
|
1300 |
"""Writes the tool check cache to disc.
|
|
1301 |
|
|
1302 |
toolset.write()
|
|
1303 |
"""
|
|
1304 |
cache = self.__toolcheckcache
|
|
1305 |
|
|
1306 |
# Write out the cache.
|
|
1307 |
if self.checked and ToolSet.cachefile_basename:
|
|
1308 |
self.log.Debug("Saving toolcache: %s", self.cachefilename)
|
|
1309 |
try:
|
|
1310 |
f = open(self.cachefilename, "w+")
|
|
1311 |
f.write(ToolSet.filemarker+"\n")
|
|
1312 |
f.write(ToolSet.tool_env_id+"\n")
|
|
1313 |
for k,ce in cache.iteritems():
|
|
1314 |
|
|
1315 |
# If a tool has not been used for an extraordinarily long time
|
|
1316 |
# then forget it - to prevent the cache from clogging up with old tools.
|
|
1317 |
# Only write entries for tools that were found to be ok - so that the
|
|
1318 |
# next time the ones that weren't will be re-tested
|
|
1319 |
|
|
1320 |
if ce['valid'] and ce['age'] < 100:
|
|
1321 |
ce['age'] += 1
|
|
1322 |
f.write("%s," % k)
|
|
1323 |
for n,v in ce.iteritems():
|
|
1324 |
f.write("%s=%s," % (n,str(v)))
|
|
1325 |
f.write("\n")
|
|
1326 |
f.close()
|
|
1327 |
self.log.Info("Created/Updated toolcheck cache: %s\n", self.cachefilename)
|
|
1328 |
except Exception, e:
|
|
1329 |
self.log.Info("Could not write toolcheck cache: %s", str(e))
|
|
1330 |
return self.valid
|
|
1331 |
|
5
|
1332 |
class UninitialisedVariableException(Exception):
|
|
1333 |
pass
|
3
|
1334 |
|
|
1335 |
class Evaluator(object):
|
|
1336 |
"""Determine the values of variables under different Configurations.
|
|
1337 |
Either of specification and buildUnit may be None."""
|
|
1338 |
|
|
1339 |
|
|
1340 |
refRegex = re.compile("\$\((.+?)\)")
|
|
1341 |
|
5
|
1342 |
def __init__(self, specification, buildUnit, cache, gathertools = False):
|
3
|
1343 |
self.dict = {}
|
|
1344 |
self.tools = []
|
|
1345 |
self.gathertools = gathertools
|
5
|
1346 |
self.cache = cache
|
3
|
1347 |
|
|
1348 |
specName = "none"
|
|
1349 |
configName = "none"
|
|
1350 |
|
|
1351 |
# A list of lists of operations.
|
|
1352 |
opsLists = []
|
|
1353 |
|
|
1354 |
if buildUnit:
|
5
|
1355 |
ol = buildUnit.GetOperations(cache)
|
|
1356 |
self.buildUnit = buildUnit
|
|
1357 |
|
|
1358 |
opsLists.extend( ol )
|
3
|
1359 |
|
|
1360 |
if specification:
|
5
|
1361 |
for v in specification.GetAllVariantsRecursively(cache):
|
|
1362 |
opsLists.extend( v.GetAllOperationsRecursively(cache) )
|
3
|
1363 |
|
|
1364 |
tools = {}
|
|
1365 |
|
5
|
1366 |
unfound_values = []
|
3
|
1367 |
for opsList in opsLists:
|
|
1368 |
for op in opsList:
|
|
1369 |
# applying an Operation to a non-existent variable
|
|
1370 |
# is OK. We assume that it is just an empty string.
|
|
1371 |
try:
|
|
1372 |
oldValue = self.dict[op.name]
|
|
1373 |
except KeyError:
|
|
1374 |
oldValue = ""
|
|
1375 |
|
5
|
1376 |
try:
|
|
1377 |
newValue = op.Apply(oldValue)
|
|
1378 |
except BadToolValue, e:
|
|
1379 |
unfound_values.append(str(e))
|
|
1380 |
newValue = "NO_VALUE_FOR_" + op.name
|
|
1381 |
|
3
|
1382 |
self.dict[op.name] = newValue
|
|
1383 |
|
|
1384 |
if self.gathertools:
|
|
1385 |
if op.type == "tool" and op.versionCommand and op.versionResult:
|
5
|
1386 |
tools[op.name] = Tool(op.name, newValue, op.versionCommand, op.versionResult, configName)
|
3
|
1387 |
|
5
|
1388 |
if len(unfound_values) > 0:
|
|
1389 |
raise UninitialisedVariableException("\n".join(unfound_values))
|
3
|
1390 |
|
|
1391 |
if self.gathertools:
|
|
1392 |
self.tools = tools.values()
|
|
1393 |
else:
|
|
1394 |
self.tools=[]
|
|
1395 |
|
|
1396 |
# resolve inter-variable references in the dictionary
|
|
1397 |
unresolved = True
|
|
1398 |
|
|
1399 |
for k, v in self.dict.items():
|
|
1400 |
self.dict[k] = v.replace("$$","__RAPTOR_ESCAPED_DOLLAR__")
|
|
1401 |
|
|
1402 |
while unresolved:
|
|
1403 |
unresolved = False
|
|
1404 |
for k, v in self.dict.items():
|
|
1405 |
if v.find('$(' + k + ')') != -1:
|
5
|
1406 |
raise RecursionException("Recursion Detected in variable '%s' in configuration '%s' " % (k,configName))
|
|
1407 |
expanded = "RECURSIVE_INVALID_STRING"
|
3
|
1408 |
else:
|
|
1409 |
expanded = self.ExpandAll(v, specName, configName)
|
|
1410 |
|
|
1411 |
if expanded != v: # something changed?
|
|
1412 |
self.dict[k] = expanded
|
|
1413 |
unresolved = True # maybe more to do
|
|
1414 |
|
|
1415 |
# unquote double-dollar references
|
|
1416 |
for k, v in self.dict.items():
|
|
1417 |
self.dict[k] = v.replace("__RAPTOR_ESCAPED_DOLLAR__","$")
|
|
1418 |
|
|
1419 |
for t in self.tools:
|
|
1420 |
t.expand(self)
|
|
1421 |
|
|
1422 |
|
|
1423 |
|
|
1424 |
def Get(self, name):
|
|
1425 |
"""return the value of variable 'name' or None if not found."""
|
|
1426 |
|
|
1427 |
if name in self.dict:
|
|
1428 |
return self.dict[name]
|
|
1429 |
else:
|
|
1430 |
return None
|
|
1431 |
|
|
1432 |
|
|
1433 |
def Resolve(self, name):
|
|
1434 |
"""same as Get except that env variables are expanded.
|
|
1435 |
|
|
1436 |
raises BadReferenceError if the variable 'name' exists but a
|
|
1437 |
contained environment variable does not exist."""
|
|
1438 |
return self.Get(name) # all variables are now expanded anyway
|
|
1439 |
|
|
1440 |
|
|
1441 |
def ResolveMatching(self, pattern):
|
|
1442 |
""" Return a dictionary of all variables that match the pattern """
|
|
1443 |
for k,v in self.dict.iteritems():
|
|
1444 |
if pattern.match(k):
|
|
1445 |
yield (k,v)
|
|
1446 |
|
|
1447 |
|
|
1448 |
def ExpandAll(self, value, spec = "none", config = "none"):
|
|
1449 |
"""replace all $(SOMETHING) in the string value.
|
|
1450 |
|
|
1451 |
returns the newly expanded string."""
|
|
1452 |
|
|
1453 |
refs = Evaluator.refRegex.findall(value)
|
|
1454 |
|
5
|
1455 |
# store up all the unset variables before raising an exception
|
|
1456 |
# to allow us to find them all
|
|
1457 |
unset_variables = []
|
|
1458 |
|
3
|
1459 |
for r in set(refs):
|
|
1460 |
expansion = None
|
|
1461 |
|
5
|
1462 |
if r in self.dict:
|
3
|
1463 |
expansion = self.dict[r]
|
|
1464 |
else:
|
|
1465 |
# no expansion for $(r)
|
5
|
1466 |
unset_variables.append("Unset variable '%s' used in spec '%s' with config '%s'" % (r, spec, config))
|
3
|
1467 |
if expansion != None:
|
|
1468 |
value = value.replace("$(" + r + ")", expansion)
|
|
1469 |
|
5
|
1470 |
if len(unset_variables) > 0: # raise them all
|
|
1471 |
raise UninitialisedVariableException(". ".join(unset_variables))
|
|
1472 |
|
3
|
1473 |
return value
|
|
1474 |
|
|
1475 |
|
|
1476 |
# raptor_data module functions
|
|
1477 |
|
|
1478 |
|
|
1479 |
# end of the raptor_data module
|