author | Richard Taylor <richard.i.taylor@nokia.com> |
Thu, 18 Mar 2010 14:00:53 +0000 | |
branch | fix |
changeset 379 | 59ec73480ed3 |
parent 374 | 96629a6f26e4 |
child 400 | 554cc189839f |
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: |
28ee654acf42
Deal with backslashes at the end of <env/> values
Jon Chatten <>
parents:
5
diff
changeset
|
733 |
# if this value is a "path" or a "tool" then we need to make sure |
28ee654acf42
Deal with backslashes at the end of <env/> values
Jon Chatten <>
parents:
5
diff
changeset
|
734 |
# it is a proper absolute path in our preferred format. |
28ee654acf42
Deal with backslashes at the end of <env/> values
Jon Chatten <>
parents:
5
diff
changeset
|
735 |
if self.type == "path" or self.type == "tool": |
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: |
61
f520dfd22025
Correct backslash escaping to only operate on non-'path' and 'tool' types.
Jon Chatten
parents:
60
diff
changeset
|
740 |
raise BadToolValue("the environment variable %s is incorrect: %s" % (self.name, str(e))) |
f520dfd22025
Correct backslash escaping to only operate on non-'path' and 'tool' types.
Jon Chatten
parents:
60
diff
changeset
|
741 |
# if this value ends in an un-escaped backslash, then it will be treated as a line continuation character |
f520dfd22025
Correct backslash escaping to only operate on non-'path' and 'tool' types.
Jon Chatten
parents:
60
diff
changeset
|
742 |
# in makefile parsing - un-escaped backslashes at the end of values are therefore escaped |
f520dfd22025
Correct backslash escaping to only operate on non-'path' and 'tool' types.
Jon Chatten
parents:
60
diff
changeset
|
743 |
elif value.endswith('\\'): |
f520dfd22025
Correct backslash escaping to only operate on non-'path' and 'tool' types.
Jon Chatten
parents:
60
diff
changeset
|
744 |
# an odd number of backslashes means there's one to escape |
f520dfd22025
Correct backslash escaping to only operate on non-'path' and 'tool' types.
Jon Chatten
parents:
60
diff
changeset
|
745 |
count = len(value) - len(value.rstrip('\\')) |
f520dfd22025
Correct backslash escaping to only operate on non-'path' and 'tool' types.
Jon Chatten
parents:
60
diff
changeset
|
746 |
if count % 2: |
f520dfd22025
Correct backslash escaping to only operate on non-'path' and 'tool' types.
Jon Chatten
parents:
60
diff
changeset
|
747 |
value += '\\' |
3 | 748 |
except KeyError: |
749 |
if self.default != None: |
|
750 |
value = self.default |
|
751 |
else: |
|
5 | 752 |
raise BadToolValue("%s is not set in the environment and has no default" % self.name) |
3 | 753 |
|
754 |
return value |
|
755 |
||
756 |
||
757 |
def SetProperty(self, name, value): |
|
758 |
if name == "default": |
|
759 |
self.default = value |
|
760 |
else: |
|
761 |
Set.SetProperty(self, name, value) |
|
762 |
||
763 |
||
764 |
def Valid(self): |
|
765 |
return (self.name != None) |
|
766 |
||
767 |
||
768 |
class BuildUnit(object): |
|
769 |
"Represents an individual buildable unit." |
|
770 |
||
771 |
def __init__(self, name, variants): |
|
772 |
self.name = name |
|
773 |
||
774 |
# A list of Variant objects. |
|
775 |
self.variants = variants |
|
776 |
||
777 |
# Cache for the variant operations implied by this BuildUnit. |
|
778 |
self.operations = [] |
|
779 |
self.variantKey = "" |
|
780 |
||
5 | 781 |
def GetOperations(self, cache): |
3 | 782 |
"""Return all operations related to this BuildUnit. |
783 |
||
784 |
The result is cached, and so will only be computed once per BuildUnit. |
|
785 |
""" |
|
786 |
key = '.'.join([x.name for x in self.variants]) |
|
787 |
if self.variantKey != key: |
|
788 |
self.variantKey = key |
|
789 |
for v in self.variants: |
|
5 | 790 |
self.operations.extend( v.GetAllOperationsRecursively(cache=cache) ) |
3 | 791 |
|
792 |
return self.operations |
|
793 |
||
794 |
class Config(object): |
|
795 |
"""Abstract type representing an argument to the '-c' option. |
|
796 |
||
797 |
The fundamental property of a Config is that it can generate one or more |
|
798 |
BuildUnits. |
|
799 |
""" |
|
800 |
||
801 |
def __init__(self): |
|
802 |
self.modifiers = [] |
|
803 |
||
804 |
def AddModifier(self, variant): |
|
805 |
self.modifiers.append(variant) |
|
806 |
||
807 |
def ClearModifiers(self): |
|
808 |
self.modifiers = [] |
|
809 |
||
5 | 810 |
def GenerateBuildUnits(self,cache): |
3 | 811 |
"""Returns a list of BuildUnits. |
812 |
||
813 |
This function must be overridden by derived classes. |
|
814 |
""" |
|
815 |
raise NotImplementedError() |
|
816 |
||
817 |
||
818 |
class Variant(Model, Config): |
|
819 |
||
5 | 820 |
__slots__ = ('cache','name','host','extends','ops','variantRefs','allOperations') |
821 |
||
3 | 822 |
def __init__(self, name = ""): |
823 |
Model.__init__(self) |
|
824 |
Config.__init__(self) |
|
825 |
self.name = name |
|
826 |
||
827 |
# Operations defined inside this variant. |
|
828 |
self.ops = [] |
|
829 |
||
830 |
# The name of our parent variant, if any. |
|
831 |
self.extends = "" |
|
832 |
||
833 |
# Any variant references used inside this variant. |
|
834 |
self.variantRefs = [] |
|
835 |
||
836 |
self.allOperations = [] |
|
837 |
||
838 |
def SetProperty(self, name, value): |
|
839 |
if name == "name": |
|
840 |
self.name = value |
|
841 |
elif name == "host": |
|
842 |
if HostPlatform.IsKnown(value): |
|
843 |
self.host = value |
|
844 |
elif name == "extends": |
|
845 |
self.extends = value |
|
846 |
else: |
|
847 |
raise InvalidPropertyError() |
|
848 |
||
849 |
def AddChild(self, child): |
|
850 |
if isinstance(child, Operation): |
|
851 |
self.ops.append(child) |
|
852 |
elif isinstance(child, VariantRef): |
|
853 |
self.variantRefs.append(child) |
|
854 |
else: |
|
855 |
raise InvalidChildError() |
|
856 |
||
857 |
def Valid(self): |
|
858 |
return self.name |
|
859 |
||
860 |
def AddOperation(self, op): |
|
861 |
self.ops.append(op) |
|
862 |
||
5 | 863 |
def GetAllOperationsRecursively(self, cache): |
3 | 864 |
"""Returns a list of all operations in this variant. |
865 |
||
866 |
The list elements are themselves lists; the overall structure of the |
|
867 |
returned value is: |
|
868 |
||
869 |
[ [ops-from-parent],[ops-from-varRefs], [ops-in-self] ] |
|
870 |
""" |
|
871 |
||
872 |
if not self.allOperations: |
|
873 |
if self.extends: |
|
5 | 874 |
parent = cache.FindNamedVariant(self.extends) |
875 |
self.allOperations.extend( parent.GetAllOperationsRecursively(cache = cache) ) |
|
3 | 876 |
for r in self.variantRefs: |
5 | 877 |
for v in [ r.Resolve(cache = cache) ] + r.GetModifiers(cache = cache): |
878 |
self.allOperations.extend( v.GetAllOperationsRecursively(cache = cache) ) |
|
3 | 879 |
self.allOperations.append(self.ops) |
880 |
||
881 |
return self.allOperations |
|
882 |
||
5 | 883 |
def GenerateBuildUnits(self,cache): |
3 | 884 |
|
885 |
name = self.name |
|
886 |
vars = [self] |
|
887 |
||
888 |
for m in self.modifiers: |
|
889 |
name = name + "." + m.name |
|
890 |
vars.append(m) |
|
5 | 891 |
return [ BuildUnit(name=name, variants=vars) ] |
3 | 892 |
|
192
76300483f6fd
fix: get make engine name validation working with aliases.
timothy.murphy@nokia.com
parents:
191
diff
changeset
|
893 |
def isDerivedFrom(self, progenitor, cache): |
76300483f6fd
fix: get make engine name validation working with aliases.
timothy.murphy@nokia.com
parents:
191
diff
changeset
|
894 |
if self.name == progenitor: |
76300483f6fd
fix: get make engine name validation working with aliases.
timothy.murphy@nokia.com
parents:
191
diff
changeset
|
895 |
return True |
76300483f6fd
fix: get make engine name validation working with aliases.
timothy.murphy@nokia.com
parents:
191
diff
changeset
|
896 |
|
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
|
897 |
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
|
898 |
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
|
899 |
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
|
900 |
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
|
901 |
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
|
902 |
if parent.name == progenitor: |
192
76300483f6fd
fix: get make engine name validation working with aliases.
timothy.murphy@nokia.com
parents:
191
diff
changeset
|
903 |
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
|
904 |
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
|
905 |
|
192
76300483f6fd
fix: get make engine name validation working with aliases.
timothy.murphy@nokia.com
parents:
191
diff
changeset
|
906 |
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
|
907 |
|
5 | 908 |
def __str__(self): |
909 |
s = "<var name='%s' extends='%s'>\n" % (self.name, self.extends) |
|
3 | 910 |
for op in self.ops: |
5 | 911 |
s += str(op) + '\n' |
912 |
s += "</var>" |
|
913 |
return s |
|
3 | 914 |
|
915 |
class VariantRef(Reference): |
|
916 |
||
917 |
def __init__(self, ref=None): |
|
5 | 918 |
Reference.__init__(self, ref = ref) |
3 | 919 |
|
5 | 920 |
def __str__(self): |
921 |
return "<varRef ref='%s'/>" % self.ref |
|
3 | 922 |
|
5 | 923 |
def Resolve(self, cache): |
3 | 924 |
try: |
5 | 925 |
return cache.FindNamedVariant(self.ref) |
374
96629a6f26e4
fault tolerant XML groups
Richard Taylor <richard.i.taylor@nokia.com>
parents:
371
diff
changeset
|
926 |
except KeyError: |
3 | 927 |
raise BadReferenceError(self.ref) |
928 |
||
5 | 929 |
class MissingVariantException(Exception): |
930 |
pass |
|
3 | 931 |
|
932 |
class Alias(Model, Config): |
|
933 |
||
934 |
def __init__(self, name=""): |
|
935 |
Model.__init__(self) |
|
936 |
Config.__init__(self) |
|
937 |
self.name = name |
|
938 |
self.meaning = "" |
|
939 |
self.varRefs = [] |
|
940 |
self.variants = [] |
|
941 |
||
5 | 942 |
def __str__(self): |
943 |
return "<alias name='%s' meaning='%s'/>" % (self.name, self.meaning) |
|
3 | 944 |
|
945 |
def SetProperty(self, key, val): |
|
946 |
if key == "name": |
|
947 |
self.name = val |
|
948 |
elif key == "meaning": |
|
949 |
self.meaning = val |
|
950 |
||
951 |
for u in val.split("."): |
|
5 | 952 |
self.varRefs.append( VariantRef(ref = u) ) |
3 | 953 |
else: |
954 |
raise InvalidPropertyError() |
|
955 |
||
956 |
def Valid(self): |
|
957 |
return self.name and self.meaning |
|
958 |
||
192
76300483f6fd
fix: get make engine name validation working with aliases.
timothy.murphy@nokia.com
parents:
191
diff
changeset
|
959 |
def Resolve(self, cache): |
3 | 960 |
if not self.variants: |
5 | 961 |
missing_variants = [] |
3 | 962 |
for r in self.varRefs: |
963 |
try: |
|
5 | 964 |
self.variants.append( r.Resolve(cache=cache) ) |
3 | 965 |
except BadReferenceError: |
5 | 966 |
missing_variants.append(r.ref) |
967 |
||
968 |
if len(missing_variants) > 0: |
|
374
96629a6f26e4
fault tolerant XML groups
Richard Taylor <richard.i.taylor@nokia.com>
parents:
371
diff
changeset
|
969 |
raise MissingVariantException("Missing variants '%s'" % " ".join(missing_variants)) |
3 | 970 |
|
192
76300483f6fd
fix: get make engine name validation working with aliases.
timothy.murphy@nokia.com
parents:
191
diff
changeset
|
971 |
def GenerateBuildUnits(self, cache): |
76300483f6fd
fix: get make engine name validation working with aliases.
timothy.murphy@nokia.com
parents:
191
diff
changeset
|
972 |
self.Resolve(cache) |
76300483f6fd
fix: get make engine name validation working with aliases.
timothy.murphy@nokia.com
parents:
191
diff
changeset
|
973 |
|
3 | 974 |
name = self.name |
975 |
||
976 |
for v in self.modifiers: |
|
977 |
name = name + "." + v.name |
|
978 |
||
5 | 979 |
return [ BuildUnit(name=name, variants=self.variants + self.modifiers) ] |
3 | 980 |
|
192
76300483f6fd
fix: get make engine name validation working with aliases.
timothy.murphy@nokia.com
parents:
191
diff
changeset
|
981 |
def isDerivedFrom(self, progenitor, cache): |
76300483f6fd
fix: get make engine name validation working with aliases.
timothy.murphy@nokia.com
parents:
191
diff
changeset
|
982 |
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
|
983 |
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
|
984 |
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
|
985 |
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
|
986 |
return False |
3 | 987 |
|
988 |
class AliasRef(Reference): |
|
989 |
||
990 |
def __init__(self, ref=None): |
|
991 |
Reference.__init__(self, ref) |
|
992 |
||
5 | 993 |
def __str__(self): |
994 |
return "<aliasRef ref='%s'/>" % self.ref |
|
3 | 995 |
|
5 | 996 |
def Resolve(self, cache): |
3 | 997 |
try: |
5 | 998 |
return cache.FindNamedAlias(self.ref) |
3 | 999 |
except KeyError: |
1000 |
raise BadReferenceError(self.ref) |
|
1001 |
||
1002 |
||
1003 |
class Group(Model, Config): |
|
1004 |
def __init__(self, name=""): |
|
1005 |
Model.__init__(self) |
|
1006 |
Config.__init__(self) |
|
1007 |
self.name = name |
|
1008 |
self.childRefs = [] |
|
1009 |
||
1010 |
def SetProperty(self, key, val): |
|
1011 |
if key == "name": |
|
1012 |
self.name = val |
|
1013 |
else: |
|
1014 |
raise InvalidPropertyError() |
|
1015 |
||
1016 |
def AddChild(self, child): |
|
1017 |
if isinstance( child, (VariantRef,AliasRef,GroupRef) ): |
|
1018 |
self.childRefs.append(child) |
|
1019 |
else: |
|
1020 |
raise InvalidChildError() |
|
1021 |
||
1022 |
def Valid(self): |
|
1023 |
return self.name and self.childRefs |
|
1024 |
||
5 | 1025 |
def __str__(self): |
1026 |
s = "<group name='%s'>" % self.name |
|
3 | 1027 |
for r in self.childRefs: |
5 | 1028 |
s += str(r) |
1029 |
s += "</group>" |
|
1030 |
return s |
|
3 | 1031 |
|
5 | 1032 |
def GenerateBuildUnits(self, cache): |
3 | 1033 |
units = [] |
371
2db10eac415a
test data for bad groups
Richard Taylor <richard.i.taylor@nokia.com>
parents:
204
diff
changeset
|
1034 |
|
5 | 1035 |
missing_variants = [] |
3 | 1036 |
for r in self.childRefs: |
1037 |
try: |
|
5 | 1038 |
obj = r.Resolve(cache=cache) |
3 | 1039 |
except BadReferenceError: |
5 | 1040 |
missing_variants.append(r.ref) |
3 | 1041 |
else: |
1042 |
obj.ClearModifiers() |
|
374
96629a6f26e4
fault tolerant XML groups
Richard Taylor <richard.i.taylor@nokia.com>
parents:
371
diff
changeset
|
1043 |
try: |
96629a6f26e4
fault tolerant XML groups
Richard Taylor <richard.i.taylor@nokia.com>
parents:
371
diff
changeset
|
1044 |
refMods = r.GetModifiers(cache) |
96629a6f26e4
fault tolerant XML groups
Richard Taylor <richard.i.taylor@nokia.com>
parents:
371
diff
changeset
|
1045 |
except BadReferenceError,e: |
96629a6f26e4
fault tolerant XML groups
Richard Taylor <richard.i.taylor@nokia.com>
parents:
371
diff
changeset
|
1046 |
missing_variants.append(str(e)) |
96629a6f26e4
fault tolerant XML groups
Richard Taylor <richard.i.taylor@nokia.com>
parents:
371
diff
changeset
|
1047 |
else: |
96629a6f26e4
fault tolerant XML groups
Richard Taylor <richard.i.taylor@nokia.com>
parents:
371
diff
changeset
|
1048 |
for m in refMods + self.modifiers: |
96629a6f26e4
fault tolerant XML groups
Richard Taylor <richard.i.taylor@nokia.com>
parents:
371
diff
changeset
|
1049 |
obj.AddModifier(m) |
3 | 1050 |
|
374
96629a6f26e4
fault tolerant XML groups
Richard Taylor <richard.i.taylor@nokia.com>
parents:
371
diff
changeset
|
1051 |
units.extend( obj.GenerateBuildUnits(cache) ) |
5 | 1052 |
|
1053 |
if len(missing_variants) > 0: |
|
374
96629a6f26e4
fault tolerant XML groups
Richard Taylor <richard.i.taylor@nokia.com>
parents:
371
diff
changeset
|
1054 |
raise MissingVariantException("Missing variants '%s'" % " ".join(missing_variants)) |
3 | 1055 |
|
1056 |
return units |
|
1057 |
||
1058 |
||
1059 |
class GroupRef(Reference): |
|
1060 |
||
1061 |
def __init__(self, ref=None): |
|
1062 |
Reference.__init__(self, ref) |
|
1063 |
||
5 | 1064 |
def __str__(self): |
371
2db10eac415a
test data for bad groups
Richard Taylor <richard.i.taylor@nokia.com>
parents:
204
diff
changeset
|
1065 |
return "<groupRef ref='%s' mod='%s'/>" % (self.ref, ".".join(self.modifiers)) |
3 | 1066 |
|
5 | 1067 |
def Resolve(self, cache): |
3 | 1068 |
try: |
5 | 1069 |
return cache.FindNamedGroup(self.ref) |
3 | 1070 |
except KeyError: |
1071 |
raise BadReferenceError(self.ref) |
|
1072 |
||
374
96629a6f26e4
fault tolerant XML groups
Richard Taylor <richard.i.taylor@nokia.com>
parents:
371
diff
changeset
|
1073 |
def GetBuildUnits(configNames, cache, logger): |
96629a6f26e4
fault tolerant XML groups
Richard Taylor <richard.i.taylor@nokia.com>
parents:
371
diff
changeset
|
1074 |
"""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
|
1075 |
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
|
1076 |
|
96629a6f26e4
fault tolerant XML groups
Richard Taylor <richard.i.taylor@nokia.com>
parents:
371
diff
changeset
|
1077 |
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
|
1078 |
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
|
1079 |
""" |
96629a6f26e4
fault tolerant XML groups
Richard Taylor <richard.i.taylor@nokia.com>
parents:
371
diff
changeset
|
1080 |
|
96629a6f26e4
fault tolerant XML groups
Richard Taylor <richard.i.taylor@nokia.com>
parents:
371
diff
changeset
|
1081 |
# 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
|
1082 |
models = [] |
96629a6f26e4
fault tolerant XML groups
Richard Taylor <richard.i.taylor@nokia.com>
parents:
371
diff
changeset
|
1083 |
|
96629a6f26e4
fault tolerant XML groups
Richard Taylor <richard.i.taylor@nokia.com>
parents:
371
diff
changeset
|
1084 |
for c in set(configNames): |
96629a6f26e4
fault tolerant XML groups
Richard Taylor <richard.i.taylor@nokia.com>
parents:
371
diff
changeset
|
1085 |
ok = True |
96629a6f26e4
fault tolerant XML groups
Richard Taylor <richard.i.taylor@nokia.com>
parents:
371
diff
changeset
|
1086 |
names = c.split(".") |
96629a6f26e4
fault tolerant XML groups
Richard Taylor <richard.i.taylor@nokia.com>
parents:
371
diff
changeset
|
1087 |
|
96629a6f26e4
fault tolerant XML groups
Richard Taylor <richard.i.taylor@nokia.com>
parents:
371
diff
changeset
|
1088 |
base = names[0] |
96629a6f26e4
fault tolerant XML groups
Richard Taylor <richard.i.taylor@nokia.com>
parents:
371
diff
changeset
|
1089 |
mods = names[1:] |
96629a6f26e4
fault tolerant XML groups
Richard Taylor <richard.i.taylor@nokia.com>
parents:
371
diff
changeset
|
1090 |
|
96629a6f26e4
fault tolerant XML groups
Richard Taylor <richard.i.taylor@nokia.com>
parents:
371
diff
changeset
|
1091 |
if base in cache.groups: |
96629a6f26e4
fault tolerant XML groups
Richard Taylor <richard.i.taylor@nokia.com>
parents:
371
diff
changeset
|
1092 |
x = cache.FindNamedGroup(base) |
96629a6f26e4
fault tolerant XML groups
Richard Taylor <richard.i.taylor@nokia.com>
parents:
371
diff
changeset
|
1093 |
elif base in cache.aliases: |
96629a6f26e4
fault tolerant XML groups
Richard Taylor <richard.i.taylor@nokia.com>
parents:
371
diff
changeset
|
1094 |
x = cache.FindNamedAlias(base) |
96629a6f26e4
fault tolerant XML groups
Richard Taylor <richard.i.taylor@nokia.com>
parents:
371
diff
changeset
|
1095 |
elif base in cache.variants: |
96629a6f26e4
fault tolerant XML groups
Richard Taylor <richard.i.taylor@nokia.com>
parents:
371
diff
changeset
|
1096 |
x = cache.FindNamedVariant(base) |
96629a6f26e4
fault tolerant XML groups
Richard Taylor <richard.i.taylor@nokia.com>
parents:
371
diff
changeset
|
1097 |
else: |
96629a6f26e4
fault tolerant XML groups
Richard Taylor <richard.i.taylor@nokia.com>
parents:
371
diff
changeset
|
1098 |
logger.Error("Unknown build configuration '%s'" % base) |
96629a6f26e4
fault tolerant XML groups
Richard Taylor <richard.i.taylor@nokia.com>
parents:
371
diff
changeset
|
1099 |
continue |
96629a6f26e4
fault tolerant XML groups
Richard Taylor <richard.i.taylor@nokia.com>
parents:
371
diff
changeset
|
1100 |
|
96629a6f26e4
fault tolerant XML groups
Richard Taylor <richard.i.taylor@nokia.com>
parents:
371
diff
changeset
|
1101 |
x.ClearModifiers() |
96629a6f26e4
fault tolerant XML groups
Richard Taylor <richard.i.taylor@nokia.com>
parents:
371
diff
changeset
|
1102 |
|
96629a6f26e4
fault tolerant XML groups
Richard Taylor <richard.i.taylor@nokia.com>
parents:
371
diff
changeset
|
1103 |
for m in mods: |
96629a6f26e4
fault tolerant XML groups
Richard Taylor <richard.i.taylor@nokia.com>
parents:
371
diff
changeset
|
1104 |
if m in cache.variants: |
96629a6f26e4
fault tolerant XML groups
Richard Taylor <richard.i.taylor@nokia.com>
parents:
371
diff
changeset
|
1105 |
x.AddModifier( cache.FindNamedVariant(m) ) |
96629a6f26e4
fault tolerant XML groups
Richard Taylor <richard.i.taylor@nokia.com>
parents:
371
diff
changeset
|
1106 |
else: |
96629a6f26e4
fault tolerant XML groups
Richard Taylor <richard.i.taylor@nokia.com>
parents:
371
diff
changeset
|
1107 |
logger.Error("Unknown build variant '%s'" % m) |
96629a6f26e4
fault tolerant XML groups
Richard Taylor <richard.i.taylor@nokia.com>
parents:
371
diff
changeset
|
1108 |
ok = False |
96629a6f26e4
fault tolerant XML groups
Richard Taylor <richard.i.taylor@nokia.com>
parents:
371
diff
changeset
|
1109 |
|
96629a6f26e4
fault tolerant XML groups
Richard Taylor <richard.i.taylor@nokia.com>
parents:
371
diff
changeset
|
1110 |
if ok: |
96629a6f26e4
fault tolerant XML groups
Richard Taylor <richard.i.taylor@nokia.com>
parents:
371
diff
changeset
|
1111 |
models.append(copy.copy(x)) |
96629a6f26e4
fault tolerant XML groups
Richard Taylor <richard.i.taylor@nokia.com>
parents:
371
diff
changeset
|
1112 |
|
96629a6f26e4
fault tolerant XML groups
Richard Taylor <richard.i.taylor@nokia.com>
parents:
371
diff
changeset
|
1113 |
# turn Model objects into BuildUnit objects |
96629a6f26e4
fault tolerant XML groups
Richard Taylor <richard.i.taylor@nokia.com>
parents:
371
diff
changeset
|
1114 |
# |
96629a6f26e4
fault tolerant XML groups
Richard Taylor <richard.i.taylor@nokia.com>
parents:
371
diff
changeset
|
1115 |
# 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
|
1116 |
# 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
|
1117 |
# 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
|
1118 |
# abandoned) |
96629a6f26e4
fault tolerant XML groups
Richard Taylor <richard.i.taylor@nokia.com>
parents:
371
diff
changeset
|
1119 |
units = [] |
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 |
while len(models) > 0: |
96629a6f26e4
fault tolerant XML groups
Richard Taylor <richard.i.taylor@nokia.com>
parents:
371
diff
changeset
|
1122 |
x = models.pop() |
96629a6f26e4
fault tolerant XML groups
Richard Taylor <richard.i.taylor@nokia.com>
parents:
371
diff
changeset
|
1123 |
try: |
96629a6f26e4
fault tolerant XML groups
Richard Taylor <richard.i.taylor@nokia.com>
parents:
371
diff
changeset
|
1124 |
if isinstance(x, (Alias, Variant)): |
96629a6f26e4
fault tolerant XML groups
Richard Taylor <richard.i.taylor@nokia.com>
parents:
371
diff
changeset
|
1125 |
# these we just turn straight into BuildUnits |
96629a6f26e4
fault tolerant XML groups
Richard Taylor <richard.i.taylor@nokia.com>
parents:
371
diff
changeset
|
1126 |
units.extend(x.GenerateBuildUnits(cache)) |
96629a6f26e4
fault tolerant XML groups
Richard Taylor <richard.i.taylor@nokia.com>
parents:
371
diff
changeset
|
1127 |
elif isinstance(x, Group): |
96629a6f26e4
fault tolerant XML groups
Richard Taylor <richard.i.taylor@nokia.com>
parents:
371
diff
changeset
|
1128 |
# 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
|
1129 |
for child in x.childRefs: |
96629a6f26e4
fault tolerant XML groups
Richard Taylor <richard.i.taylor@nokia.com>
parents:
371
diff
changeset
|
1130 |
modChild = copy.copy(child) |
96629a6f26e4
fault tolerant XML groups
Richard Taylor <richard.i.taylor@nokia.com>
parents:
371
diff
changeset
|
1131 |
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
|
1132 |
models.append(modChild) |
96629a6f26e4
fault tolerant XML groups
Richard Taylor <richard.i.taylor@nokia.com>
parents:
371
diff
changeset
|
1133 |
elif isinstance(x, Reference): |
96629a6f26e4
fault tolerant XML groups
Richard Taylor <richard.i.taylor@nokia.com>
parents:
371
diff
changeset
|
1134 |
# resolve references and their modifiers |
96629a6f26e4
fault tolerant XML groups
Richard Taylor <richard.i.taylor@nokia.com>
parents:
371
diff
changeset
|
1135 |
try: |
96629a6f26e4
fault tolerant XML groups
Richard Taylor <richard.i.taylor@nokia.com>
parents:
371
diff
changeset
|
1136 |
obj = x.Resolve(cache) |
96629a6f26e4
fault tolerant XML groups
Richard Taylor <richard.i.taylor@nokia.com>
parents:
371
diff
changeset
|
1137 |
modObj = copy.copy(obj) |
96629a6f26e4
fault tolerant XML groups
Richard Taylor <richard.i.taylor@nokia.com>
parents:
371
diff
changeset
|
1138 |
modObj.modifiers = x.GetModifiers(cache) |
96629a6f26e4
fault tolerant XML groups
Richard Taylor <richard.i.taylor@nokia.com>
parents:
371
diff
changeset
|
1139 |
except BadReferenceError,e: |
96629a6f26e4
fault tolerant XML groups
Richard Taylor <richard.i.taylor@nokia.com>
parents:
371
diff
changeset
|
1140 |
logger.Error("Unknown reference '%s'" % str(e)) |
96629a6f26e4
fault tolerant XML groups
Richard Taylor <richard.i.taylor@nokia.com>
parents:
371
diff
changeset
|
1141 |
else: |
96629a6f26e4
fault tolerant XML groups
Richard Taylor <richard.i.taylor@nokia.com>
parents:
371
diff
changeset
|
1142 |
models.append(modObj) |
96629a6f26e4
fault tolerant XML groups
Richard Taylor <richard.i.taylor@nokia.com>
parents:
371
diff
changeset
|
1143 |
except Exception, e: |
96629a6f26e4
fault tolerant XML groups
Richard Taylor <richard.i.taylor@nokia.com>
parents:
371
diff
changeset
|
1144 |
logger.Error(str(e)) |
96629a6f26e4
fault tolerant XML groups
Richard Taylor <richard.i.taylor@nokia.com>
parents:
371
diff
changeset
|
1145 |
|
96629a6f26e4
fault tolerant XML groups
Richard Taylor <richard.i.taylor@nokia.com>
parents:
371
diff
changeset
|
1146 |
return units |
96629a6f26e4
fault tolerant XML groups
Richard Taylor <richard.i.taylor@nokia.com>
parents:
371
diff
changeset
|
1147 |
|
5 | 1148 |
class ToolErrorException(Exception): |
1149 |
def __init__(self, s): |
|
1150 |
Exception.__init__(self,s) |
|
1151 |
||
3 | 1152 |
class Tool(object): |
1153 |
"""Represents a tool that might be used by raptor e.g. a compiler""" |
|
1154 |
||
5 | 1155 |
# It's difficult and expensive to give each tool a log reference but a class one |
1156 |
# will facilitate debugging when that is needed without being a design flaw the |
|
1157 |
# rest of the time. |
|
1158 |
log = raptor_utilities.nulllog |
|
1159 |
||
3 | 1160 |
# For use in dealing with tools that return non-ascii version strings. |
1161 |
nonascii = "" |
|
1162 |
identity_chartable = chr(0) |
|
1163 |
for c in xrange(1,128): |
|
1164 |
identity_chartable += chr(c) |
|
1165 |
for c in xrange(128,256): |
|
1166 |
nonascii += chr(c) |
|
1167 |
identity_chartable += " " |
|
1168 |
||
5 | 1169 |
def __init__(self, name, command, versioncommand, versionresult, id=""): |
3 | 1170 |
self.name = name |
1171 |
self.command = command |
|
1172 |
self.versioncommand = versioncommand |
|
1173 |
self.versionresult = versionresult |
|
1174 |
self.id = id # what config this is from - used in debug messages |
|
1175 |
self.date = None |
|
1176 |
||
1177 |
||
1178 |
# Assume the tool is unavailable or the wrong |
|
1179 |
# version until someone proves that it's OK |
|
1180 |
self.valid = False |
|
1181 |
||
1182 |
||
1183 |
def expand(self, toolset): |
|
1184 |
self.versioncommand = toolset.ExpandAll(self.versioncommand) |
|
1185 |
self.versionresult = toolset.ExpandAll(self.versionresult) |
|
1186 |
self.command = toolset.ExpandAll(self.command) |
|
1187 |
self.key = hashlib.md5(self.versioncommand + self.versionresult).hexdigest() |
|
1188 |
||
1189 |
# We need the tool's date to find out if we should check it. |
|
1190 |
try: |
|
1191 |
if '/' in self.command: |
|
1192 |
testfile = os.path.abspath(self.command.strip("\"'")) |
|
1193 |
else: |
|
1194 |
# The tool isn't a relative or absolute path so the could be relying on the |
|
1195 |
# $PATH variable to make it available. We must find the tool if it's a simple |
|
1196 |
# executable file (e.g. "armcc" rather than "python myscript.py") then get it's date. |
|
1197 |
# We can use the date later to see if our cache is valid. |
|
1198 |
# If it really is not a simple command then we won't be able to get a date and |
|
1199 |
# we won't be able to tell if it is altered or updated - too bad! |
|
1200 |
testfile = generic_path.Where(self.command) |
|
5 | 1201 |
#self.log.Debug("toolcheck: tool '%s' was found on the path at '%s' ", self.command, testfile) |
3 | 1202 |
if testfile is None: |
1203 |
raise Exception("Can't be found in path") |
|
1204 |
||
1205 |
if not os.path.isfile(testfile): |
|
1206 |
raise Exception("tool %s appears to not be a file %s", self.command, testfile) |
|
1207 |
||
1208 |
testfile_stat = os.stat(testfile) |
|
1209 |
self.date = testfile_stat.st_mtime |
|
1210 |
except Exception,e: |
|
5 | 1211 |
# We really don't mind if the tool could not be dated - for any reason |
1212 |
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)) |
|
1213 |
pass |
|
3 | 1214 |
|
1215 |
||
5 | 1216 |
def check(self, shell, evaluator, log = raptor_utilities.nulllog): |
3 | 1217 |
|
1218 |
self.vre = re.compile(self.versionresult) |
|
1219 |
||
1220 |
try: |
|
1221 |
self.log.Debug("Pre toolcheck: '%s' for version '%s'", self.name, self.versionresult) |
|
1222 |
p = subprocess.Popen(args=[shell, "-c", self.versioncommand], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) |
|
5 | 1223 |
log.Debug("Checking tool '%s' for version '%s'", self.name, self.versionresult) |
3 | 1224 |
versionoutput,err = p.communicate() |
1225 |
except Exception,e: |
|
1226 |
versionoutput=None |
|
1227 |
||
1228 |
# Some tools return version strings with unicode characters! |
|
1229 |
# There is no good response other than a lot of decoding and encoding. |
|
1230 |
# Simpler to ignore it: |
|
1231 |
versionoutput_a = versionoutput.translate(Tool.identity_chartable,"") |
|
1232 |
||
1233 |
if versionoutput_a and self.vre.search(versionoutput_a) != None: |
|
5 | 1234 |
log.Debug("tool '%s' returned an acceptable version '%s'", self.name, versionoutput_a) |
3 | 1235 |
self.valid = True |
1236 |
else: |
|
1237 |
self.valid = False |
|
5 | 1238 |
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 | 1239 |
|
1240 |
def envhash(irrelevant_vars): |
|
1241 |
"""Determine something unique about this environment to identify it. |
|
1242 |
must ignore variables that change without mattering to the caller |
|
1243 |
e.g. perhaps PATH matters but PWD and PPID don't""" |
|
1244 |
envid = hashlib.md5() |
|
1245 |
for k in os.environ: |
|
1246 |
if k not in irrelevant_vars: |
|
1247 |
envid.update(os.environ[k]) |
|
1248 |
return envid.hexdigest()[:16] |
|
1249 |
||
1250 |
||
1251 |
class ToolSet(object): |
|
1252 |
""" |
|
1253 |
This class manages a bunch of tools and keeps a cache of |
|
1254 |
all tools that it ever sees (across all configurations). |
|
1255 |
toolset.check() is called for each config but the cache is kept across calls to |
|
1256 |
catch the use of one tool in many configs. |
|
1257 |
write() is used to flush the cache to disc. |
|
1258 |
""" |
|
1259 |
# The raptor shell - this is not mutable. |
|
5 | 1260 |
if 'SBS_SHELL' in os.environ: |
1261 |
shell = os.environ['SBS_SHELL'] |
|
1262 |
else: |
|
1263 |
hostbinaries = os.path.join(os.environ['SBS_HOME'], |
|
1264 |
os.environ['HOSTPLATFORM_DIR']) |
|
3 | 1265 |
|
5 | 1266 |
if HostPlatform.IsHost('lin*'): |
1267 |
shell=os.path.join(hostbinaries, 'bin/bash') |
|
3 | 1268 |
else: |
5 | 1269 |
if 'SBS_CYGWIN' in os.environ: |
1270 |
shell=os.path.join(os.environ['SBS_CYGWIN'], 'bin\\bash.exe') |
|
1271 |
else: |
|
1272 |
shell=os.path.join(hostbinaries, 'cygwin\\bin\\bash.exe') |
|
3 | 1273 |
|
1274 |
||
1275 |
irrelevant_vars = ['PWD','OLDPWD','PID','PPID', 'SHLVL' ] |
|
1276 |
||
1277 |
||
1278 |
shell_version=".*GNU bash, version [34].*" |
|
1279 |
shell_re = re.compile(shell_version) |
|
1280 |
if 'SBS_BUILD_DIR' in os.environ: |
|
1281 |
cachefile_basename = str(generic_path.Join(os.environ['SBS_BUILD_DIR'],"toolcheck_cache_")) |
|
1282 |
elif 'EPOCROOT' in os.environ: |
|
1283 |
cachefile_basename = str(generic_path.Join(os.environ['EPOCROOT'],"epoc32/build/toolcheck_cache_")) |
|
1284 |
else: |
|
1285 |
cachefile_basename = None |
|
1286 |
||
1287 |
tool_env_id = envhash(irrelevant_vars) |
|
1288 |
filemarker = "sbs_toolcache_2.8.2" |
|
1289 |
||
1290 |
def __init__(self, log = raptor_utilities.nulllog, forced=False): |
|
1291 |
self.__toolcheckcache = {} |
|
1292 |
||
1293 |
self.valid = True |
|
1294 |
self.checked = False |
|
1295 |
self.shellok = False |
|
1296 |
self.configname="" |
|
1297 |
self.cache_loaded = False |
|
1298 |
self.forced = forced |
|
1299 |
||
1300 |
self.log=log |
|
1301 |
||
1302 |
# Read in the tool cache |
|
1303 |
# |
|
1304 |
# The cache format is a hash key which identifies the |
|
1305 |
# command and the version that we're checking for. Then |
|
1306 |
# there are name,value pairs that record, e.g. the date |
|
1307 |
# of the command file or the name of the variable that |
|
1308 |
# the config uses for the tool (GNUCP or MWCC or whatever) |
|
1309 |
||
1310 |
if ToolSet.cachefile_basename: |
|
1311 |
self.cachefilename = ToolSet.cachefile_basename+".tmp" |
|
1312 |
if not self.forced: |
|
1313 |
try: |
|
1314 |
f = open(self.cachefilename, "r+") |
|
1315 |
# if this tool cache was recorded in |
|
1316 |
# a different environment then ignore it. |
|
1317 |
marker = f.readline().rstrip("\r\n") |
|
1318 |
if marker == ToolSet.filemarker: |
|
1319 |
env_id_tmp = f.readline().rstrip("\r\n") |
|
1320 |
if env_id_tmp == ToolSet.tool_env_id: |
|
1321 |
try: |
|
1322 |
for l in f.readlines(): |
|
1323 |
toolhistory = l.rstrip(",\n\r").split(",") |
|
1324 |
ce = {} |
|
1325 |
for i in toolhistory[1:]: |
|
1326 |
(name,val) = i.split("=") |
|
1327 |
if name == "valid": |
|
1328 |
val = bool(val) |
|
1329 |
elif name == "age": |
|
1330 |
val = int(val) |
|
1331 |
elif name == "date": |
|
1332 |
if val != "None": |
|
1333 |
val = float(val) |
|
1334 |
else: |
|
1335 |
val= None |
|
1336 |
||
1337 |
ce[name] = val |
|
1338 |
self.__toolcheckcache[toolhistory[0]] = ce |
|
1339 |
log.Info("Loaded toolcheck cache: %s\n", self.cachefilename) |
|
1340 |
except Exception, e: |
|
1341 |
log.Info("Ignoring garbled toolcheck cache: %s (%s)\n", self.cachefilename, str(e)) |
|
1342 |
self.__toolcheckcache = {} |
|
1343 |
||
1344 |
else: |
|
1345 |
log.Info("Toolcheck cache %s ignored - environment changed\n", self.cachefilename) |
|
1346 |
else: |
|
1347 |
log.Info("Toolcheck cache not loaded = marker missing: %s %s\n", self.cachefilename, ToolSet.filemarker) |
|
1348 |
f.close() |
|
1349 |
except IOError, e: |
|
1350 |
log.Info("Failed to load toolcheck cache: %s\n", self.cachefilename) |
|
1351 |
else: |
|
1352 |
log.Debug("Toolcheck cachefile not created because EPOCROOT not set in environment.\n") |
|
1353 |
||
1354 |
def check_shell(self): |
|
1355 |
# The command shell is a critical tool because all the other tools run |
|
1356 |
# within it so we must check for it first. It has to be in the path. |
|
1357 |
# bash 4 is preferred, 3 is accepted |
|
1358 |
try: |
|
1359 |
p = subprocess.Popen(args=[ToolSet.shell, '--version'], bufsize=1024, shell = False, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, universal_newlines=True) |
|
1360 |
shellversion_out, errtxt = p.communicate() |
|
1361 |
if ToolSet.shell_re.search(shellversion_out) == None: |
|
1362 |
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) |
|
1363 |
self.valid = False |
|
1364 |
except Exception,e: |
|
1365 |
self.log.Error("A critical tool could not be found.\nPlease check that '%s' is in the path. (%s)", ToolSet.shell, str(e)) |
|
1366 |
self.valid = False |
|
1367 |
||
1368 |
return self.valid |
|
1369 |
||
1370 |
def check(self, evaluator, configname): |
|
1371 |
"""Check the toolset for a particular config""" |
|
1372 |
||
1373 |
self.checked = True # remember that we did check something |
|
1374 |
||
1375 |
if not self.shellok: |
|
1376 |
self.shellok = self.check_shell() |
|
1377 |
self.shellok = True |
|
1378 |
||
1379 |
self.valid = self.valid and self.shellok |
|
1380 |
||
1381 |
cache = self.__toolcheckcache |
|
1382 |
for tool in evaluator.tools: |
|
1383 |
if not self.forced: |
|
1384 |
try: |
|
1385 |
t = cache[tool.key] |
|
1386 |
||
1387 |
except KeyError,e: |
|
1388 |
pass |
|
1389 |
else: |
|
1390 |
# if the cache has an entry for the tool then see if the date on |
|
1391 |
# the tool has changed (assuming the tool is a simple executable file) |
|
1392 |
if t.has_key('date') and (tool.date is None or (tool.date - t['date'] > 0.1)) : |
|
1393 |
self.log.Debug("toolcheck forced: '%s' changed since the last check: %s < %s", tool.command, str(t['date']), str(tool.date)) |
|
1394 |
else: |
|
1395 |
t['age'] = 0 # we used it so it's obviously needed |
|
1396 |
self.valid = self.valid and t['valid'] |
|
1397 |
self.log.Debug("toolcheck saved on: '%s'", tool.name) |
|
1398 |
continue |
|
1399 |
||
1400 |
||
1401 |
self.log.Debug("toolcheck done: %s -key: %s" % (tool.name, tool.key)) |
|
1402 |
||
5 | 1403 |
try: |
1404 |
tool.check(ToolSet.shell, evaluator, log = self.log) |
|
1405 |
except ToolErrorException, e: |
|
3 | 1406 |
self.valid = False |
5 | 1407 |
self.log.Error("%s\n" % str(e)) |
3 | 1408 |
|
1409 |
# Tool failures are cached just like successes - don't want to repeat them |
|
1410 |
cache[tool.key] = { "name" : tool.name, "valid" : tool.valid, "age" : 0 , "date" : tool.date } |
|
1411 |
||
1412 |
||
1413 |
def write(self): |
|
1414 |
"""Writes the tool check cache to disc. |
|
1415 |
||
1416 |
toolset.write() |
|
1417 |
""" |
|
1418 |
cache = self.__toolcheckcache |
|
1419 |
||
1420 |
# Write out the cache. |
|
1421 |
if self.checked and ToolSet.cachefile_basename: |
|
1422 |
self.log.Debug("Saving toolcache: %s", self.cachefilename) |
|
1423 |
try: |
|
1424 |
f = open(self.cachefilename, "w+") |
|
1425 |
f.write(ToolSet.filemarker+"\n") |
|
1426 |
f.write(ToolSet.tool_env_id+"\n") |
|
1427 |
for k,ce in cache.iteritems(): |
|
1428 |
||
1429 |
# If a tool has not been used for an extraordinarily long time |
|
1430 |
# then forget it - to prevent the cache from clogging up with old tools. |
|
1431 |
# Only write entries for tools that were found to be ok - so that the |
|
1432 |
# next time the ones that weren't will be re-tested |
|
1433 |
||
1434 |
if ce['valid'] and ce['age'] < 100: |
|
1435 |
ce['age'] += 1 |
|
1436 |
f.write("%s," % k) |
|
1437 |
for n,v in ce.iteritems(): |
|
1438 |
f.write("%s=%s," % (n,str(v))) |
|
1439 |
f.write("\n") |
|
1440 |
f.close() |
|
1441 |
self.log.Info("Created/Updated toolcheck cache: %s\n", self.cachefilename) |
|
1442 |
except Exception, e: |
|
1443 |
self.log.Info("Could not write toolcheck cache: %s", str(e)) |
|
1444 |
return self.valid |
|
1445 |
||
5 | 1446 |
class UninitialisedVariableException(Exception): |
1447 |
pass |
|
3 | 1448 |
|
374
96629a6f26e4
fault tolerant XML groups
Richard Taylor <richard.i.taylor@nokia.com>
parents:
371
diff
changeset
|
1449 |
class RecursionException(Exception): |
96629a6f26e4
fault tolerant XML groups
Richard Taylor <richard.i.taylor@nokia.com>
parents:
371
diff
changeset
|
1450 |
pass |
96629a6f26e4
fault tolerant XML groups
Richard Taylor <richard.i.taylor@nokia.com>
parents:
371
diff
changeset
|
1451 |
|
3 | 1452 |
class Evaluator(object): |
1453 |
"""Determine the values of variables under different Configurations. |
|
1454 |
Either of specification and buildUnit may be None.""" |
|
1455 |
||
1456 |
||
1457 |
refRegex = re.compile("\$\((.+?)\)") |
|
1458 |
||
5 | 1459 |
def __init__(self, specification, buildUnit, cache, gathertools = False): |
3 | 1460 |
self.dict = {} |
1461 |
self.tools = [] |
|
1462 |
self.gathertools = gathertools |
|
5 | 1463 |
self.cache = cache |
3 | 1464 |
|
1465 |
specName = "none" |
|
1466 |
configName = "none" |
|
1467 |
||
1468 |
# A list of lists of operations. |
|
1469 |
opsLists = [] |
|
1470 |
||
1471 |
if buildUnit: |
|
5 | 1472 |
ol = buildUnit.GetOperations(cache) |
1473 |
self.buildUnit = buildUnit |
|
1474 |
||
1475 |
opsLists.extend( ol ) |
|
3 | 1476 |
|
1477 |
if specification: |
|
5 | 1478 |
for v in specification.GetAllVariantsRecursively(cache): |
1479 |
opsLists.extend( v.GetAllOperationsRecursively(cache) ) |
|
3 | 1480 |
|
1481 |
tools = {} |
|
1482 |
||
5 | 1483 |
unfound_values = [] |
3 | 1484 |
for opsList in opsLists: |
1485 |
for op in opsList: |
|
1486 |
# applying an Operation to a non-existent variable |
|
1487 |
# is OK. We assume that it is just an empty string. |
|
1488 |
try: |
|
1489 |
oldValue = self.dict[op.name] |
|
1490 |
except KeyError: |
|
1491 |
oldValue = "" |
|
1492 |
||
5 | 1493 |
try: |
1494 |
newValue = op.Apply(oldValue) |
|
1495 |
except BadToolValue, e: |
|
1496 |
unfound_values.append(str(e)) |
|
1497 |
newValue = "NO_VALUE_FOR_" + op.name |
|
1498 |
||
3 | 1499 |
self.dict[op.name] = newValue |
1500 |
||
1501 |
if self.gathertools: |
|
1502 |
if op.type == "tool" and op.versionCommand and op.versionResult: |
|
5 | 1503 |
tools[op.name] = Tool(op.name, newValue, op.versionCommand, op.versionResult, configName) |
3 | 1504 |
|
5 | 1505 |
if len(unfound_values) > 0: |
1506 |
raise UninitialisedVariableException("\n".join(unfound_values)) |
|
3 | 1507 |
|
1508 |
if self.gathertools: |
|
1509 |
self.tools = tools.values() |
|
1510 |
else: |
|
1511 |
self.tools=[] |
|
1512 |
||
1513 |
# resolve inter-variable references in the dictionary |
|
1514 |
unresolved = True |
|
1515 |
||
1516 |
for k, v in self.dict.items(): |
|
1517 |
self.dict[k] = v.replace("$$","__RAPTOR_ESCAPED_DOLLAR__") |
|
1518 |
||
1519 |
while unresolved: |
|
1520 |
unresolved = False |
|
1521 |
for k, v in self.dict.items(): |
|
1522 |
if v.find('$(' + k + ')') != -1: |
|
5 | 1523 |
raise RecursionException("Recursion Detected in variable '%s' in configuration '%s' " % (k,configName)) |
3 | 1524 |
else: |
1525 |
expanded = self.ExpandAll(v, specName, configName) |
|
1526 |
||
1527 |
if expanded != v: # something changed? |
|
1528 |
self.dict[k] = expanded |
|
1529 |
unresolved = True # maybe more to do |
|
1530 |
||
1531 |
# unquote double-dollar references |
|
1532 |
for k, v in self.dict.items(): |
|
1533 |
self.dict[k] = v.replace("__RAPTOR_ESCAPED_DOLLAR__","$") |
|
1534 |
||
1535 |
for t in self.tools: |
|
1536 |
t.expand(self) |
|
1537 |
||
1538 |
||
1539 |
||
1540 |
def Get(self, name): |
|
1541 |
"""return the value of variable 'name' or None if not found.""" |
|
1542 |
||
1543 |
if name in self.dict: |
|
1544 |
return self.dict[name] |
|
1545 |
else: |
|
1546 |
return None |
|
1547 |
||
1548 |
||
1549 |
def Resolve(self, name): |
|
1550 |
"""same as Get except that env variables are expanded. |
|
1551 |
||
1552 |
raises BadReferenceError if the variable 'name' exists but a |
|
1553 |
contained environment variable does not exist.""" |
|
1554 |
return self.Get(name) # all variables are now expanded anyway |
|
1555 |
||
1556 |
||
1557 |
def ResolveMatching(self, pattern): |
|
1558 |
""" Return a dictionary of all variables that match the pattern """ |
|
1559 |
for k,v in self.dict.iteritems(): |
|
1560 |
if pattern.match(k): |
|
1561 |
yield (k,v) |
|
1562 |
||
1563 |
||
1564 |
def ExpandAll(self, value, spec = "none", config = "none"): |
|
1565 |
"""replace all $(SOMETHING) in the string value. |
|
1566 |
||
1567 |
returns the newly expanded string.""" |
|
1568 |
||
1569 |
refs = Evaluator.refRegex.findall(value) |
|
1570 |
||
5 | 1571 |
# store up all the unset variables before raising an exception |
1572 |
# to allow us to find them all |
|
1573 |
unset_variables = [] |
|
1574 |
||
3 | 1575 |
for r in set(refs): |
1576 |
expansion = None |
|
1577 |
||
5 | 1578 |
if r in self.dict: |
3 | 1579 |
expansion = self.dict[r] |
1580 |
else: |
|
1581 |
# no expansion for $(r) |
|
5 | 1582 |
unset_variables.append("Unset variable '%s' used in spec '%s' with config '%s'" % (r, spec, config)) |
3 | 1583 |
if expansion != None: |
1584 |
value = value.replace("$(" + r + ")", expansion) |
|
1585 |
||
5 | 1586 |
if len(unset_variables) > 0: # raise them all |
1587 |
raise UninitialisedVariableException(". ".join(unset_variables)) |
|
1588 |
||
3 | 1589 |
return value |
1590 |
||
1591 |
||
1592 |
# raptor_data module functions |
|
1593 |
||
1594 |
||
1595 |
# end of the raptor_data module |