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