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