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