|
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 |
|
22 class Reply(object): |
|
23 """object to return values from API calls. |
|
24 """ |
|
25 def __init__(self, text=""): |
|
26 self.reply_text = text |
|
27 |
|
28 def __str__(self): |
|
29 name = type(self).__name__.lower() |
|
30 |
|
31 str = "<" + name |
|
32 end = "/>\n" |
|
33 |
|
34 for attribute,value in self.__dict__.items(): |
|
35 if attribute != "reply_text": |
|
36 str += " %s='%s'" % (attribute, value) |
|
37 |
|
38 if self.reply_text: |
|
39 str += ">" + self.reply_text |
|
40 end = "</%s>\n" % name |
|
41 |
|
42 str += end |
|
43 |
|
44 return str |
|
45 |
|
46 class Alias(Reply): |
|
47 pass |
|
48 |
|
49 class Config(Reply): |
|
50 pass |
|
51 |
|
52 class Product(Reply): |
|
53 pass |
|
54 |
|
55 import generic_path |
|
56 import raptor |
|
57 import raptor_data |
|
58 import re |
|
59 |
|
60 class Context(object): |
|
61 """object to contain state information for API calls. |
|
62 |
|
63 For example, |
|
64 |
|
65 api = raptor_api.Context() |
|
66 val = api.get(X) |
|
67 """ |
|
68 def __init__(self, initialiser=None): |
|
69 # this object has a private Raptor object that can either be |
|
70 # passed in or created internally. |
|
71 |
|
72 if initialiser == None: |
|
73 self.__raptor = raptor.Raptor() |
|
74 else: |
|
75 self.__raptor = initialiser |
|
76 |
|
77 def StringQuery(self, query): |
|
78 """turn a string into an API call and execute it. |
|
79 |
|
80 This is a convenience method for "lazy" callers. |
|
81 |
|
82 The return value is also converted into a string. |
|
83 """ |
|
84 |
|
85 if query == "aliases": |
|
86 aliases = self.GetAliases() |
|
87 return "".join(map(str, aliases)).strip() |
|
88 |
|
89 elif query == "products": |
|
90 variants = self.GetProducts() |
|
91 return "".join(map(str, variants)).strip() |
|
92 |
|
93 elif query.startswith("config"): |
|
94 match = re.match("config\[(.*)\]", query) |
|
95 if match: |
|
96 config = self.GetConfig(match.group(1)) |
|
97 return str(config).strip() |
|
98 else: |
|
99 raise BadQuery("syntax error") |
|
100 |
|
101 raise BadQuery("unknown query") |
|
102 |
|
103 def GetAliases(self, type=""): |
|
104 """extract all aliases of a given type. |
|
105 |
|
106 the default type is "". |
|
107 to get all aliases pass type=None |
|
108 """ |
|
109 aliases = [] |
|
110 |
|
111 for a in self.__raptor.cache.aliases.values(): |
|
112 if a.type == type or type == None: |
|
113 r = Alias() |
|
114 # copy the members we want to expose |
|
115 r.name = a.name |
|
116 r.meaning = a.meaning |
|
117 aliases.append(r) |
|
118 |
|
119 return aliases |
|
120 |
|
121 def GetConfig(self, name): |
|
122 """extract the values for a given configuration. |
|
123 |
|
124 'name' should be an alias or variant followed optionally by a |
|
125 dot-separated list of variants. For example "armv5_urel" or |
|
126 "armv5_urel.savespace.vasco". |
|
127 """ |
|
128 |
|
129 r = Config() |
|
130 |
|
131 names = name.split(".") |
|
132 if names[0] in self.__raptor.cache.aliases: |
|
133 x = self.__raptor.cache.FindNamedAlias(names[0]) |
|
134 |
|
135 if len(names) > 1: |
|
136 r.fullname = x.meaning + "." + ".".join(names[1:]) |
|
137 else: |
|
138 r.fullname = x.meaning |
|
139 |
|
140 elif names[0] in self.__raptor.cache.variants: |
|
141 r.fullname = name |
|
142 |
|
143 else: |
|
144 raise BadQuery("'%s' is not an alias or a variant" % names[0]) |
|
145 |
|
146 tmp = raptor_data.Alias("tmp") |
|
147 tmp.SetProperty("meaning", r.fullname) |
|
148 |
|
149 units = tmp.GenerateBuildUnits(self.__raptor.cache) |
|
150 evaluator = self.__raptor.GetEvaluator(None, units[0]) |
|
151 |
|
152 releasepath = evaluator.Get("RELEASEPATH") |
|
153 fullvariantpath = evaluator.Get("FULLVARIANTPATH") |
|
154 |
|
155 if releasepath and fullvariantpath: |
|
156 r.outputpath = str(generic_path.Join(releasepath, fullvariantpath)) |
|
157 else: |
|
158 raise BadQuery("could not get outputpath for config '%s'" % name) |
|
159 |
|
160 return r |
|
161 |
|
162 def GetProducts(self): |
|
163 """extract all product variants.""" |
|
164 |
|
165 variants = [] |
|
166 |
|
167 for v in self.__raptor.cache.variants.values(): |
|
168 if v.type == "product": |
|
169 r = Product() |
|
170 # copy the members we want to expose |
|
171 r.name = v.name |
|
172 variants.append(r) |
|
173 |
|
174 return variants |
|
175 |
|
176 class BadQuery(Exception): |
|
177 pass |
|
178 |
|
179 # end of the raptor_api module |