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