author | Richard Taylor <richard.i.taylor@nokia.com> |
Tue, 04 May 2010 14:01:37 +0100 | |
branch | wip |
changeset 509 | a337b022f8a8 |
parent 507 | 54a88b895bcd |
permissions | -rw-r--r-- |
498 | 1 |
# |
2 |
# Copyright (c) 2010 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 |
# |
|
16 |
# raptor_api module |
|
17 |
# |
|
18 |
# Python API for Raptor. External code should interact with Raptor via this |
|
19 |
# module only, as it is the only programatic interface considered public. The |
|
20 |
# command line --query option is also implemented using this module. |
|
21 |
||
507
54a88b895bcd
apply review comments
Richard Taylor <richard.i.taylor@nokia.com>
parents:
504
diff
changeset
|
22 |
# constants |
54a88b895bcd
apply review comments
Richard Taylor <richard.i.taylor@nokia.com>
parents:
504
diff
changeset
|
23 |
ALL = 1 |
54a88b895bcd
apply review comments
Richard Taylor <richard.i.taylor@nokia.com>
parents:
504
diff
changeset
|
24 |
|
54a88b895bcd
apply review comments
Richard Taylor <richard.i.taylor@nokia.com>
parents:
504
diff
changeset
|
25 |
# objects |
54a88b895bcd
apply review comments
Richard Taylor <richard.i.taylor@nokia.com>
parents:
504
diff
changeset
|
26 |
|
498 | 27 |
class Reply(object): |
28 |
"""object to return values from API calls. |
|
29 |
""" |
|
30 |
def __init__(self, text=""): |
|
499
cad3b96a4fb1
refactoring for --query
Richard Taylor <richard.i.taylor@nokia.com>
parents:
498
diff
changeset
|
31 |
self.text = text |
498 | 32 |
|
33 |
def __str__(self): |
|
34 |
name = type(self).__name__.lower() |
|
35 |
||
499
cad3b96a4fb1
refactoring for --query
Richard Taylor <richard.i.taylor@nokia.com>
parents:
498
diff
changeset
|
36 |
string = "<" + name |
cad3b96a4fb1
refactoring for --query
Richard Taylor <richard.i.taylor@nokia.com>
parents:
498
diff
changeset
|
37 |
children = [] |
cad3b96a4fb1
refactoring for --query
Richard Taylor <richard.i.taylor@nokia.com>
parents:
498
diff
changeset
|
38 |
longend = False |
498 | 39 |
|
40 |
for attribute,value in self.__dict__.items(): |
|
499
cad3b96a4fb1
refactoring for --query
Richard Taylor <richard.i.taylor@nokia.com>
parents:
498
diff
changeset
|
41 |
if attribute != "text": |
cad3b96a4fb1
refactoring for --query
Richard Taylor <richard.i.taylor@nokia.com>
parents:
498
diff
changeset
|
42 |
if isinstance(value, Reply): |
cad3b96a4fb1
refactoring for --query
Richard Taylor <richard.i.taylor@nokia.com>
parents:
498
diff
changeset
|
43 |
children.append(value) |
cad3b96a4fb1
refactoring for --query
Richard Taylor <richard.i.taylor@nokia.com>
parents:
498
diff
changeset
|
44 |
else: |
cad3b96a4fb1
refactoring for --query
Richard Taylor <richard.i.taylor@nokia.com>
parents:
498
diff
changeset
|
45 |
string += " %s='%s'" % (attribute, value) |
cad3b96a4fb1
refactoring for --query
Richard Taylor <richard.i.taylor@nokia.com>
parents:
498
diff
changeset
|
46 |
|
cad3b96a4fb1
refactoring for --query
Richard Taylor <richard.i.taylor@nokia.com>
parents:
498
diff
changeset
|
47 |
if children or self.text: |
cad3b96a4fb1
refactoring for --query
Richard Taylor <richard.i.taylor@nokia.com>
parents:
498
diff
changeset
|
48 |
string += ">" |
cad3b96a4fb1
refactoring for --query
Richard Taylor <richard.i.taylor@nokia.com>
parents:
498
diff
changeset
|
49 |
longend = True |
cad3b96a4fb1
refactoring for --query
Richard Taylor <richard.i.taylor@nokia.com>
parents:
498
diff
changeset
|
50 |
|
cad3b96a4fb1
refactoring for --query
Richard Taylor <richard.i.taylor@nokia.com>
parents:
498
diff
changeset
|
51 |
if self.text: |
cad3b96a4fb1
refactoring for --query
Richard Taylor <richard.i.taylor@nokia.com>
parents:
498
diff
changeset
|
52 |
string += self.text |
498 | 53 |
|
499
cad3b96a4fb1
refactoring for --query
Richard Taylor <richard.i.taylor@nokia.com>
parents:
498
diff
changeset
|
54 |
if children: |
cad3b96a4fb1
refactoring for --query
Richard Taylor <richard.i.taylor@nokia.com>
parents:
498
diff
changeset
|
55 |
string += "\n" |
cad3b96a4fb1
refactoring for --query
Richard Taylor <richard.i.taylor@nokia.com>
parents:
498
diff
changeset
|
56 |
|
cad3b96a4fb1
refactoring for --query
Richard Taylor <richard.i.taylor@nokia.com>
parents:
498
diff
changeset
|
57 |
for c in children: |
cad3b96a4fb1
refactoring for --query
Richard Taylor <richard.i.taylor@nokia.com>
parents:
498
diff
changeset
|
58 |
string += str(c) |
498 | 59 |
|
499
cad3b96a4fb1
refactoring for --query
Richard Taylor <richard.i.taylor@nokia.com>
parents:
498
diff
changeset
|
60 |
if longend: |
cad3b96a4fb1
refactoring for --query
Richard Taylor <richard.i.taylor@nokia.com>
parents:
498
diff
changeset
|
61 |
string += "</%s>\n" % name |
cad3b96a4fb1
refactoring for --query
Richard Taylor <richard.i.taylor@nokia.com>
parents:
498
diff
changeset
|
62 |
else: |
cad3b96a4fb1
refactoring for --query
Richard Taylor <richard.i.taylor@nokia.com>
parents:
498
diff
changeset
|
63 |
string += "/>\n" |
498 | 64 |
|
499
cad3b96a4fb1
refactoring for --query
Richard Taylor <richard.i.taylor@nokia.com>
parents:
498
diff
changeset
|
65 |
return string |
498 | 66 |
|
67 |
class Alias(Reply): |
|
504
ea75c3a64a17
change to lowercase method names
Richard Taylor <richard.i.taylor@nokia.com>
parents:
503
diff
changeset
|
68 |
def __init__(self, name, meaning): |
ea75c3a64a17
change to lowercase method names
Richard Taylor <richard.i.taylor@nokia.com>
parents:
503
diff
changeset
|
69 |
super(Alias,self).__init__() |
ea75c3a64a17
change to lowercase method names
Richard Taylor <richard.i.taylor@nokia.com>
parents:
503
diff
changeset
|
70 |
self.name = name |
ea75c3a64a17
change to lowercase method names
Richard Taylor <richard.i.taylor@nokia.com>
parents:
503
diff
changeset
|
71 |
self.meaning = meaning |
498 | 72 |
|
73 |
class Config(Reply): |
|
504
ea75c3a64a17
change to lowercase method names
Richard Taylor <richard.i.taylor@nokia.com>
parents:
503
diff
changeset
|
74 |
def __init__(self, fullname, outputpath): |
ea75c3a64a17
change to lowercase method names
Richard Taylor <richard.i.taylor@nokia.com>
parents:
503
diff
changeset
|
75 |
super(Config,self).__init__() |
ea75c3a64a17
change to lowercase method names
Richard Taylor <richard.i.taylor@nokia.com>
parents:
503
diff
changeset
|
76 |
self.fullname = fullname |
ea75c3a64a17
change to lowercase method names
Richard Taylor <richard.i.taylor@nokia.com>
parents:
503
diff
changeset
|
77 |
self.outputpath = outputpath |
498 | 78 |
|
79 |
class Product(Reply): |
|
504
ea75c3a64a17
change to lowercase method names
Richard Taylor <richard.i.taylor@nokia.com>
parents:
503
diff
changeset
|
80 |
def __init__(self, name): |
ea75c3a64a17
change to lowercase method names
Richard Taylor <richard.i.taylor@nokia.com>
parents:
503
diff
changeset
|
81 |
super(Product,self).__init__() |
ea75c3a64a17
change to lowercase method names
Richard Taylor <richard.i.taylor@nokia.com>
parents:
503
diff
changeset
|
82 |
self.name = name |
498 | 83 |
|
84 |
import generic_path |
|
85 |
import raptor |
|
86 |
import raptor_data |
|
87 |
import re |
|
88 |
||
89 |
class Context(object): |
|
90 |
"""object to contain state information for API calls. |
|
91 |
||
92 |
For example, |
|
93 |
||
94 |
api = raptor_api.Context() |
|
504
ea75c3a64a17
change to lowercase method names
Richard Taylor <richard.i.taylor@nokia.com>
parents:
503
diff
changeset
|
95 |
val = api.getaliases("X") |
498 | 96 |
""" |
97 |
def __init__(self, initialiser=None): |
|
98 |
# this object has a private Raptor object that can either be |
|
99 |
# passed in or created internally. |
|
100 |
||
101 |
if initialiser == None: |
|
102 |
self.__raptor = raptor.Raptor() |
|
103 |
else: |
|
104 |
self.__raptor = initialiser |
|
105 |
||
504
ea75c3a64a17
change to lowercase method names
Richard Taylor <richard.i.taylor@nokia.com>
parents:
503
diff
changeset
|
106 |
def stringquery(self, query): |
498 | 107 |
"""turn a string into an API call and execute it. |
108 |
||
109 |
This is a convenience method for "lazy" callers. |
|
110 |
||
507
54a88b895bcd
apply review comments
Richard Taylor <richard.i.taylor@nokia.com>
parents:
504
diff
changeset
|
111 |
The return value is also converted into a well-formed XML string. |
498 | 112 |
""" |
113 |
||
114 |
if query == "aliases": |
|
504
ea75c3a64a17
change to lowercase method names
Richard Taylor <richard.i.taylor@nokia.com>
parents:
503
diff
changeset
|
115 |
aliases = self.getaliases() |
498 | 116 |
return "".join(map(str, aliases)).strip() |
117 |
||
118 |
elif query == "products": |
|
504
ea75c3a64a17
change to lowercase method names
Richard Taylor <richard.i.taylor@nokia.com>
parents:
503
diff
changeset
|
119 |
variants = self.getproducts() |
498 | 120 |
return "".join(map(str, variants)).strip() |
121 |
||
122 |
elif query.startswith("config"): |
|
123 |
match = re.match("config\[(.*)\]", query) |
|
124 |
if match: |
|
504
ea75c3a64a17
change to lowercase method names
Richard Taylor <richard.i.taylor@nokia.com>
parents:
503
diff
changeset
|
125 |
config = self.getconfig(match.group(1)) |
498 | 126 |
return str(config).strip() |
127 |
else: |
|
128 |
raise BadQuery("syntax error") |
|
129 |
||
130 |
raise BadQuery("unknown query") |
|
131 |
||
504
ea75c3a64a17
change to lowercase method names
Richard Taylor <richard.i.taylor@nokia.com>
parents:
503
diff
changeset
|
132 |
def getaliases(self, type=""): |
498 | 133 |
"""extract all aliases of a given type. |
134 |
||
135 |
the default type is "". |
|
507
54a88b895bcd
apply review comments
Richard Taylor <richard.i.taylor@nokia.com>
parents:
504
diff
changeset
|
136 |
to get all aliases pass type=ALL |
498 | 137 |
""" |
138 |
aliases = [] |
|
139 |
||
140 |
for a in self.__raptor.cache.aliases.values(): |
|
507
54a88b895bcd
apply review comments
Richard Taylor <richard.i.taylor@nokia.com>
parents:
504
diff
changeset
|
141 |
if type == ALL or a.type == type: |
498 | 142 |
# copy the members we want to expose |
504
ea75c3a64a17
change to lowercase method names
Richard Taylor <richard.i.taylor@nokia.com>
parents:
503
diff
changeset
|
143 |
aliases.append( Alias(a.name, a.meaning) ) |
498 | 144 |
|
145 |
return aliases |
|
146 |
||
504
ea75c3a64a17
change to lowercase method names
Richard Taylor <richard.i.taylor@nokia.com>
parents:
503
diff
changeset
|
147 |
def getconfig(self, name): |
498 | 148 |
"""extract the values for a given configuration. |
149 |
||
150 |
'name' should be an alias or variant followed optionally by a |
|
151 |
dot-separated list of variants. For example "armv5_urel" or |
|
152 |
"armv5_urel.savespace.vasco". |
|
153 |
""" |
|
154 |
names = name.split(".") |
|
155 |
if names[0] in self.__raptor.cache.aliases: |
|
156 |
x = self.__raptor.cache.FindNamedAlias(names[0]) |
|
157 |
||
158 |
if len(names) > 1: |
|
499
cad3b96a4fb1
refactoring for --query
Richard Taylor <richard.i.taylor@nokia.com>
parents:
498
diff
changeset
|
159 |
fullname = x.meaning + "." + ".".join(names[1:]) |
498 | 160 |
else: |
499
cad3b96a4fb1
refactoring for --query
Richard Taylor <richard.i.taylor@nokia.com>
parents:
498
diff
changeset
|
161 |
fullname = x.meaning |
498 | 162 |
|
163 |
elif names[0] in self.__raptor.cache.variants: |
|
499
cad3b96a4fb1
refactoring for --query
Richard Taylor <richard.i.taylor@nokia.com>
parents:
498
diff
changeset
|
164 |
fullname = name |
498 | 165 |
|
166 |
else: |
|
167 |
raise BadQuery("'%s' is not an alias or a variant" % names[0]) |
|
168 |
||
499
cad3b96a4fb1
refactoring for --query
Richard Taylor <richard.i.taylor@nokia.com>
parents:
498
diff
changeset
|
169 |
# create an evaluator for the named configuration |
498 | 170 |
tmp = raptor_data.Alias("tmp") |
499
cad3b96a4fb1
refactoring for --query
Richard Taylor <richard.i.taylor@nokia.com>
parents:
498
diff
changeset
|
171 |
tmp.SetProperty("meaning", fullname) |
498 | 172 |
|
173 |
units = tmp.GenerateBuildUnits(self.__raptor.cache) |
|
174 |
evaluator = self.__raptor.GetEvaluator(None, units[0]) |
|
175 |
||
499
cad3b96a4fb1
refactoring for --query
Richard Taylor <richard.i.taylor@nokia.com>
parents:
498
diff
changeset
|
176 |
# get the outputpath |
cad3b96a4fb1
refactoring for --query
Richard Taylor <richard.i.taylor@nokia.com>
parents:
498
diff
changeset
|
177 |
# this is messy as some configs construct the path inside the FLM |
cad3b96a4fb1
refactoring for --query
Richard Taylor <richard.i.taylor@nokia.com>
parents:
498
diff
changeset
|
178 |
# rather than talking it from the XML: usually because of some |
cad3b96a4fb1
refactoring for --query
Richard Taylor <richard.i.taylor@nokia.com>
parents:
498
diff
changeset
|
179 |
# conditional logic... but maybe some refactoring could avoid that. |
498 | 180 |
releasepath = evaluator.Get("RELEASEPATH") |
499
cad3b96a4fb1
refactoring for --query
Richard Taylor <richard.i.taylor@nokia.com>
parents:
498
diff
changeset
|
181 |
if not releasepath: |
cad3b96a4fb1
refactoring for --query
Richard Taylor <richard.i.taylor@nokia.com>
parents:
498
diff
changeset
|
182 |
raise BadQuery("could not get RELEASEPATH for config '%s'" % name) |
cad3b96a4fb1
refactoring for --query
Richard Taylor <richard.i.taylor@nokia.com>
parents:
498
diff
changeset
|
183 |
|
cad3b96a4fb1
refactoring for --query
Richard Taylor <richard.i.taylor@nokia.com>
parents:
498
diff
changeset
|
184 |
variantplatform = evaluator.Get("VARIANTPLATFORM") |
cad3b96a4fb1
refactoring for --query
Richard Taylor <richard.i.taylor@nokia.com>
parents:
498
diff
changeset
|
185 |
varianttype = evaluator.Get("VARIANTTYPE") |
cad3b96a4fb1
refactoring for --query
Richard Taylor <richard.i.taylor@nokia.com>
parents:
498
diff
changeset
|
186 |
featurevariantname = evaluator.Get("FEATUREVARIANTNAME") |
cad3b96a4fb1
refactoring for --query
Richard Taylor <richard.i.taylor@nokia.com>
parents:
498
diff
changeset
|
187 |
|
cad3b96a4fb1
refactoring for --query
Richard Taylor <richard.i.taylor@nokia.com>
parents:
498
diff
changeset
|
188 |
platform = evaluator.Get("TRADITIONAL_PLATFORM") |
498 | 189 |
|
499
cad3b96a4fb1
refactoring for --query
Richard Taylor <richard.i.taylor@nokia.com>
parents:
498
diff
changeset
|
190 |
if platform == "TOOLS2": |
503 | 191 |
outputpath = releasepath |
498 | 192 |
else: |
499
cad3b96a4fb1
refactoring for --query
Richard Taylor <richard.i.taylor@nokia.com>
parents:
498
diff
changeset
|
193 |
if not variantplatform: |
cad3b96a4fb1
refactoring for --query
Richard Taylor <richard.i.taylor@nokia.com>
parents:
498
diff
changeset
|
194 |
raise BadQuery("could not get VARIANTPLATFORM for config '%s'" % name) |
cad3b96a4fb1
refactoring for --query
Richard Taylor <richard.i.taylor@nokia.com>
parents:
498
diff
changeset
|
195 |
|
cad3b96a4fb1
refactoring for --query
Richard Taylor <richard.i.taylor@nokia.com>
parents:
498
diff
changeset
|
196 |
if featurevariantname: |
cad3b96a4fb1
refactoring for --query
Richard Taylor <richard.i.taylor@nokia.com>
parents:
498
diff
changeset
|
197 |
variantplatform += featurevariantname |
cad3b96a4fb1
refactoring for --query
Richard Taylor <richard.i.taylor@nokia.com>
parents:
498
diff
changeset
|
198 |
|
cad3b96a4fb1
refactoring for --query
Richard Taylor <richard.i.taylor@nokia.com>
parents:
498
diff
changeset
|
199 |
if not varianttype: |
cad3b96a4fb1
refactoring for --query
Richard Taylor <richard.i.taylor@nokia.com>
parents:
498
diff
changeset
|
200 |
raise BadQuery("could not get VARIANTTYPE for config '%s'" % name) |
cad3b96a4fb1
refactoring for --query
Richard Taylor <richard.i.taylor@nokia.com>
parents:
498
diff
changeset
|
201 |
|
cad3b96a4fb1
refactoring for --query
Richard Taylor <richard.i.taylor@nokia.com>
parents:
498
diff
changeset
|
202 |
outputpath = str(generic_path.Join(releasepath, variantplatform, varianttype)) |
498 | 203 |
|
504
ea75c3a64a17
change to lowercase method names
Richard Taylor <richard.i.taylor@nokia.com>
parents:
503
diff
changeset
|
204 |
return Config(fullname, outputpath) |
498 | 205 |
|
504
ea75c3a64a17
change to lowercase method names
Richard Taylor <richard.i.taylor@nokia.com>
parents:
503
diff
changeset
|
206 |
def getproducts(self): |
498 | 207 |
"""extract all product variants.""" |
208 |
||
209 |
variants = [] |
|
210 |
||
211 |
for v in self.__raptor.cache.variants.values(): |
|
212 |
if v.type == "product": |
|
213 |
# copy the members we want to expose |
|
504
ea75c3a64a17
change to lowercase method names
Richard Taylor <richard.i.taylor@nokia.com>
parents:
503
diff
changeset
|
214 |
variants.append( Product(v.name) ) |
498 | 215 |
|
216 |
return variants |
|
217 |
||
218 |
class BadQuery(Exception): |
|
219 |
pass |
|
220 |
||
221 |
# end of the raptor_api module |