.\b\b
[vuplus_bitbake] / bin / oe / parse / OEHandler.py
1 """class for handling .oe files
2
3    Reads the file and obtains its metadata"""
4
5 import re, oe, os, sys
6 import oe.fetch
7 from oe import debug, data, fetch, fatal
8
9 from oe.parse.ConfHandler import include, localpath, obtain, init
10
11 __func_start_regexp__    = re.compile( r"(((?P<py>python)|(?P<fr>fakeroot))\s*)*(?P<func>\w+)?\s*\(\s*\)\s*{$" )
12 __inherit_regexp__       = re.compile( r"inherit\s+(.+)" )
13 __export_func_regexp__   = re.compile( r"EXPORT_FUNCTIONS\s+(.+)" )
14 __addtask_regexp__       = re.compile("addtask\s+(?P<func>\w+)\s*((before\s*(?P<before>((.*(?=after))|(.*))))|(after\s*(?P<after>((.*(?=before))|(.*)))))*")
15 __addhandler_regexp__       = re.compile( r"addhandler\s+(.+)" )
16 __word__ = re.compile(r"\S+")
17
18 __infunc__ = ""
19 __body__   = []
20 __oepath_found__ = 0
21 __classname__ = ""
22 classes = [ None, ]
23
24 def supports(fn, d):
25         localfn = localpath(fn, d)
26         return localfn[-3:] == ".oe" or localfn[-8:] == ".oeclass"
27
28 def inherit(files, d):
29         __inherit_cache = data.getVar('__inherit_cache', d) or ""
30         fn = ""
31         lineno = 0
32         for f in files:
33                 file = data.expand(f, d)
34                 if file[0] != "/" and file[-8:] != ".oeclass":
35                         file = "classes/%s.oeclass" % file
36
37                 if not file in __inherit_cache.split():
38                         debug(2, "OE %s:%d: inheriting %s" % (fn, lineno, file))
39                         __inherit_cache += " %s" % file
40                         include(fn, file, d)
41         data.setVar('__inherit_cache', __inherit_cache, d)
42
43
44 def handle(fn, d = {}, include = 0):
45         global __func_start_regexp__, __inherit_regexp__, __export_func_regexp__, __addtask_regexp__, __addhandler_regexp__, __infunc__, __body__, __oepath_found__
46         __body__ = []
47         __oepath_found__ = 0
48         __infunc__ = ""
49         __classname__ = ""
50
51         if include == 0:
52                 debug(2, "OE " + fn + ": handle(data)")
53         else:
54                 debug(2, "OE " + fn + ": handle(data, include)")
55
56         (root, ext) = os.path.splitext(os.path.basename(fn))
57         if ext == ".oeclass":
58                 __classname__ = root
59                 classes.append(__classname__)
60
61         init(d)
62
63         oldfile = data.getVar('FILE', d)
64
65         fn = obtain(fn, d)
66         oepath = ['.']
67         if not os.path.isabs(fn):
68                 f = None
69                 voepath = data.getVar("OEPATH", d)
70                 if voepath:
71                         oepath += voepath.split(":")
72                 for p in oepath:
73                         p = data.expand(p, d)
74                         if os.access(os.path.join(p, fn), os.R_OK):
75                                 f = open(os.path.join(p, fn), 'r')
76                 if f is None:
77                         raise IOError("file not found")
78         else:
79                 f = open(fn,'r')
80
81         if ext != ".oeclass":
82                 data.setVar('FILE', fn, d)
83                 i = (data.getVar("INHERIT", d, 1) or "").split()
84                 if not "base" in i and __classname__ != "base":
85                         i[0:0] = ["base"]
86                 inherit(i, d)
87
88         lineno = 0
89         while 1:
90                 lineno = lineno + 1
91                 s = f.readline()
92                 if not s: break
93                 w = s.strip()
94                 if not w: continue              # skip empty lines
95                 s = s.rstrip()
96                 if s[0] == '#': continue        # skip comments
97                 while s[-1] == '\\':
98                         s2 = f.readline()[:-1].strip()
99                         s = s[:-1] + s2
100                 feeder(lineno, s, fn, d)
101         if ext == ".oeclass":
102                 classes.remove(__classname__)
103         else:
104                 if include == 0:
105                         data.expandKeys(d)
106                         data.update_data(d)
107                         set_additional_vars(fn, d, include)
108                         anonqueue = data.getVar("__anonqueue", d, 1) or []
109                         for anon in anonqueue:
110                                 data.setVar("__anonfunc", anon["content"], d)
111                                 data.setVarFlags("__anonfunc", anon["flags"], d)
112                                 from oe import build
113                                 try:
114                                         t = data.getVar('T', d)
115                                         data.setVar('T', '${TMPDIR}/', d)
116                                         build.exec_func("__anonfunc", d)
117                                         data.delVar('T', d)
118                                         if t:
119                                                 data.setVar('T', t, d)
120                                 except Exception, e:
121                                         oe.error("executing anonymous function: %s" % e)
122                                         pass
123                         data.delVar("__anonqueue", d)
124                         data.delVar("__anonfunc", d)
125
126                         for var in d.keys():
127                                 if data.getVarFlag(var, 'handler', d):
128                                         oe.event.register(data.getVar(var, d))
129                                         continue
130
131                                 if not data.getVarFlag(var, 'task', d):
132                                         continue
133
134                                 deps = data.getVarFlag(var, 'deps', d) or []
135                                 postdeps = data.getVarFlag(var, 'postdeps', d) or []
136                                 oe.build.add_task(var, deps, d)
137                                 for p in postdeps:
138                                         pdeps = data.getVarFlag(p, 'deps', d) or []
139                                         pdeps.append(var)
140                                         data.setVarFlag(p, 'deps', pdeps, d)
141                                         oe.build.add_task(p, pdeps, d)
142         if oldfile:
143                 data.setVar('FILE', oldfile, d)
144         return d
145
146 def feeder(lineno, s, fn, d):
147         global __func_start_regexp__, __inherit_regexp__, __export_func_regexp__, __addtask_regexp__, __addhandler_regexp__, __infunc__, __body__, __oepath_found__, classes, oe
148         if __infunc__:
149                 if s == '}':
150                         __body__.append('')
151                         data.setVar(__infunc__, '\n'.join(__body__), d)
152                         data.setVarFlag(__infunc__, "func", 1, d)
153                         data.setVar(__infunc__, string.join(__body__, '\n'), d)
154                         if __infunc__ == "__anonymous":
155                                 anonqueue = oe.data.getVar("__anonqueue", d) or []
156                                 anonitem = {}
157                                 anonitem["content"] = oe.data.getVar("__anonymous", d)
158                                 anonitem["flags"] = oe.data.getVarFlags("__anonymous", d)
159                                 anonqueue.append(anonitem)
160                                 oe.data.setVar("__anonqueue", anonqueue, d)
161                                 oe.data.delVarFlags("__anonymous", d)
162                                 oe.data.delVar("__anonymous", d)
163                         __infunc__ = ""
164                         __body__ = []
165                 else:
166                         __body__.append(s)
167                 return
168
169         m = __func_start_regexp__.match(s)
170         if m:
171                 __infunc__ = m.group("func") or "__anonymous"
172                 key = __infunc__
173                 if data.getVar(key, d):
174                         # clean up old version of this piece of metadata, as its
175                         # flags could cause problems
176                         data.setVarFlag(key, 'python', None, d)
177                         data.setVarFlag(key, 'fakeroot', None, d)
178                 if m.group("py") is not None:
179                         data.setVarFlag(key, "python", "1", d)
180                 else:
181                         data.delVarFlag(key, "python", d)
182                 if m.group("fr") is not None:
183                         data.setVarFlag(key, "fakeroot", "1", d)
184                 else:
185                         data.delVarFlag(key, "fakeroot", d)
186                 return
187
188         m = __export_func_regexp__.match(s)
189         if m:
190                 fns = m.group(1)
191                 n = __word__.findall(fns)
192                 for f in n:
193                         allvars = []
194                         allvars.append(f)
195                         allvars.append(classes[-1] + "_" + f)
196
197                         vars = [[ allvars[0], allvars[1] ]]
198                         if len(classes) > 1 and classes[-2] is not None:
199                                 allvars.append(classes[-2] + "_" + f)
200                                 vars = []
201                                 vars.append([allvars[2], allvars[1]])
202                                 vars.append([allvars[0], allvars[2]])
203
204                         for (var, calledvar) in vars:
205                                 if data.getVar(var, d) and not data.getVarFlag(var, 'export_func', d):
206                                         continue
207
208                                 if data.getVar(var, d):
209                                         data.setVarFlag(var, 'python', None, d)
210                                         data.setVarFlag(var, 'func', None, d)
211
212                                 for flag in [ "func", "python" ]:
213                                         if data.getVarFlag(calledvar, flag, d):
214                                                 data.setVarFlag(var, flag, data.getVarFlag(calledvar, flag, d), d)
215                                 for flag in [ "dirs" ]:
216                                         if data.getVarFlag(var, flag, d):
217                                                 data.setVarFlag(calledvar, flag, data.getVarFlag(var, flag, d), d)
218
219                                 if data.getVarFlag(calledvar, "python", d):
220                                         data.setVar(var, "\treturn oe.build.exec_func('" + calledvar + "', d)\n", d)
221                                 else:
222                                         data.setVar(var, "\t" + calledvar + "\n", d)
223                                 data.setVarFlag(var, 'export_func', '1', d)
224
225                 return
226
227         m = __addtask_regexp__.match(s)
228         if m:
229                 func = m.group("func")
230                 before = m.group("before")
231                 after = m.group("after")
232                 if func is None:
233                         return
234                 var = "do_" + func
235
236                 data.setVarFlag(var, "task", 1, d)
237
238                 if after is not None:
239                         # set up deps for function
240                         data.setVarFlag(var, "deps", after.split(), d)
241                 if before is not None:
242                         # set up things that depend on this func
243                         data.setVarFlag(var, "postdeps", before.split(), d)
244                 return
245
246         m = __addhandler_regexp__.match(s)
247         if m:
248                 fns = m.group(1)
249                 hs = __word__.findall(fns)
250                 for h in hs:
251                         data.setVarFlag(h, "handler", 1, d)
252                 return
253
254         m = __inherit_regexp__.match(s)
255         if m:
256
257                 files = m.group(1)
258                 n = __word__.findall(files)
259                 inherit(n, d)
260                 return
261
262         from oe.parse import ConfHandler
263         return ConfHandler.feeder(lineno, s, fn, d)
264
265 __pkgsplit_cache__={}
266 def vars_from_file(mypkg, d):
267         if not mypkg:
268                 return (None, None, None)
269         if mypkg in __pkgsplit_cache__:
270                 return __pkgsplit_cache__[mypkg]
271
272         myfile = os.path.splitext(os.path.basename(mypkg))
273         parts = myfile[0].split('_')
274         __pkgsplit_cache__[mypkg] = parts
275         exp = 3 - len(parts)
276         tmplist = []
277         while exp != 0:
278                 exp -= 1
279                 tmplist.append(None)
280         parts.extend(tmplist)
281         return parts
282
283 def set_additional_vars(file, d, include):
284         """Deduce rest of variables, e.g. ${A} out of ${SRC_URI}"""
285
286         debug(2,"OE %s: set_additional_vars" % file)
287
288         src_uri = data.getVar('SRC_URI', d)
289         if not src_uri:
290                 return
291         src_uri = data.expand(src_uri, d)
292
293         a = data.getVar('A', d)
294         if a:
295                 a = data.expand(a, d).split()
296         else:
297                 a = []
298
299         from oe import fetch
300         try:
301                 fetch.init(src_uri.split())
302         except fetch.NoMethodError:
303                 pass
304
305         a += fetch.localpaths(d)
306         del fetch
307         data.setVar('A', "".join(a), d)
308
309
310 # Add us to the handlers list
311 from oe.parse import handlers
312 handlers.append({'supports': supports, 'handle': handle, 'init': init})
313 del handlers