(no commit message)
[bitbake.git] / bin / oe / parse / OEHandler.py
1 """class for handling .oe files
2
3    Reads the file and obtains its metadata"""
4
5 import re, oedata, string, os, sys
6 from oe import debug
7
8 from oeparse.ConfHandler import include
9
10 __func_start_regexp__ = re.compile( r"(\w+)\s*\(\s*\)\s*{$" )
11 __inherit_regexp__ = re.compile( r"inherit\s+(.+)" )
12 __export_func_regexp__ = re.compile( r"EXPORT_FUNCTIONS\s+(.+)" )
13 __addtask_regexp__ = re.compile( r"addtask\s+(.+)" )
14
15 __infunc__ = ""
16 __body__   = []
17 __oepath_found__ = 0
18
19 def supports(fn):
20         return fn[-3:] == ".oe"
21
22 def handle(fn, data = {}):
23         global __func_start_regexp__, __inherit_regexp__, __export_func_regexp__, __addtask_regexp__, __infunc__, __body__, __oepath_found__
24         oedata.setVar('TOPDIR', os.getcwd(), data)
25         oedata.setVar('OEDIR', os.path.join(sys.prefix, "share/oe"), data)
26         oedata.setVar('OEPATH', "${OEDIR}/bin:${OEDIR}:${TOPDIR}/bin:${TOPDIR}", data)
27         __body__ = []
28         __oepath_found__ = 0
29         __infunc__ = ""
30
31         fn = os.path.abspath(fn)
32         f = open(fn,'r')
33         lineno = 0
34         while 1:
35                 lineno = lineno + 1
36                 s = f.readline()
37                 if not s: break
38                 s = s.strip()
39                 if not s: continue              # skip empty lines
40                 if s[0] == '#': continue        # skip comments
41                 while s[-1] == '\\':
42                         s2 = f.readline()[:-1].strip()
43                         s = s[:-1] + s2
44                 feeder(lineno, s, fn, data)
45         return data
46
47 def feeder(lineno, s, fn, data = {}):
48         global __func_start_regexp__, __inherit_regexp__, __export_func_regexp__, __addtask_regexp__, __infunc__, __body__, __oepath_found__
49         if __infunc__:
50                 if s == '}':
51                         __body__.append('')
52                         oedata.setVar(__infunc__, string.join(__body__, '\n'), data)
53                         oedata.setVarFlag(__infunc__, "func", 1, data)
54                         __infunc__ = ""
55                         __body__ = []
56                 else:
57                         __body__.append(s)
58                 return
59
60         m = __func_start_regexp__.match(s)
61         if m:
62                 __infunc__ = m.group(1)
63                 return
64
65         __word__ = re.compile(r"\S+")
66
67         m = __export_func_regexp__.match(s)
68         if m:
69                 fns = m.group(1)
70                 n = __word__.findall(fns)
71                 for f in n:
72                         oedata.setVar(f, "\t%s_%s\n" % (fn, f), data)
73                 return
74
75         m = __addtask_regexp__.match(s)
76         if m:
77                 fns = m.group(1)
78                 n = __word__.findall(fns)
79                 if not envflags.has_key(n[0]):
80                         envflags[n[0]] = {}
81                 oedata.setVarFlag(n[0], "task", 1, data)
82                 return
83
84         m = __inherit_regexp__.match(s)
85         if m:
86                 files = m.group(1)
87                 n = __word__.findall(files)
88                 for f in n:
89                         file = oedata.expand(f, data)
90                         if file[0] != "/":
91                                 if data.has_key('OEPATH'):
92                                         __oepath_found__ = 0
93                                         for dir in oedata.expand(cfgenv['OEPATH'], data).split(":"):
94                                                 if os.access(os.path.join(dir, "classes", file + ".oeclass"), os.R_OK):
95                                                         file = os.path.join(dir, "classes",file + ".oeclass")
96                                                         __oepath_found__ = 1
97                                 if __oepath_found__ == 0:
98                                         debug(1, "unable to locate %s in OEPATH"  % file)
99
100                         if os.access(os.path.abspath(file), os.R_OK):
101                                 debug(2, "%s:%d: inheriting %s" % (fn, lineno, file))
102 #                               inherit_os_env(2, self.env)
103                                 include(fn, s, data)
104                         else:
105                                 debug(1, "%s:%d: could not import %s" % (fn, lineno, file))
106                 return
107
108         import oeparse.ConfHandler
109         return oeparse.ConfHandler.feeder(lineno, s, fn, data)
110
111 # Add us to the handlers list
112 from oeparse import handlers
113 handlers.append({'supports': supports, 'handle': handle})
114 del handlers