Move some variable sets out of the parser, add support for anonymous functions (execu...
[vuplus_bitbake] / bin / oe / parse / ConfHandler.py
1 """class for handling configuration data files
2
3    Reads the file and obtains its metadata"""
4
5 import re, oe.data, os, sys
6 from oe import debug, fatal
7
8 #__config_regexp__  = re.compile( r"(?P<exp>export\s*)?(?P<var>[a-zA-Z0-9\-_+.${}]+)\s*(?P<colon>:)?(?P<ques>\?)?=\s*(?P<apo>['\"]?)(?P<value>.*)(?P=apo)$")
9 __config_regexp__  = re.compile( r"(?P<exp>export\s*)?(?P<var>[a-zA-Z0-9\-_+.${}]+)(\[(?P<flag>[a-zA-Z0-9\-_+.]+)\])?\s*(?P<colon>:)?(?P<ques>\?)?=\s*(?P<apo>['\"]?)(?P<value>.*)(?P=apo)$")
10 __include_regexp__ = re.compile( r"include\s+(.+)" )
11
12 def init(data):
13         if not oe.data.getVar('TOPDIR', data):
14                 oe.data.setVar('TOPDIR', os.getcwd(), data)
15         if not oe.data.getVar('OEPATH', data):
16                 oebuild = os.path.abspath(sys.argv[0])
17                 oebin = os.path.dirname(oebuild)
18                 oedir = os.path.dirname(oebin)
19                 oe.data.setVar('OEPATH', "${TOPDIR}:%s:%s:${HOME}/.oe:${OEDIR}/bin:${OEDIR}:%s/share/oe" % (oebin, oedir, sys.prefix), data)
20
21 def supports(fn, d):
22         return localpath(fn, d)[-5:] == ".conf"
23
24 def localpath(fn, d):
25         if os.path.exists(fn):
26                 return fn
27
28         localfn = None
29         try:
30                 localfn = oe.fetch.localpath(fn, d)
31         except oe.MalformedUrl:
32                 pass
33
34         if not localfn:
35                 localfn = fn
36         return localfn
37
38 def obtain(fn, data = {}):
39         import sys, oe
40         fn = oe.data.expand(fn, data)
41         localfn = oe.data.expand(localpath(fn, data), data)
42
43         if localfn != fn:
44                 dldir = oe.data.getVar('DL_DIR', data, 1)
45                 if not dldir:
46                         debug(1, "obtain: DL_DIR not defined")
47                         return localfn
48                 oe.mkdirhier(dldir)
49                 try:
50                         oe.fetch.init([fn])
51                 except oe.fetch.NoMethodError:
52                         (type, value, traceback) = sys.exc_info()
53                         debug(1, "obtain: no method: %s" % value)
54                         return localfn
55         
56                 try:
57                         oe.fetch.go(data)
58                 except oe.fetch.MissingParameterError:
59                         (type, value, traceback) = sys.exc_info()
60                         debug(1, "obtain: missing parameters: %s" % value)
61                         return localfn
62                 except oe.fetch.FetchError:
63                         (type, value, traceback) = sys.exc_info()
64                         debug(1, "obtain: failed: %s" % value)
65                         return localfn
66         return localfn
67
68
69 def include(oldfn, fn, data = {}):
70         if oldfn == fn: # prevent infinate recursion
71                 return None
72
73         import oe
74         fn = oe.data.expand(fn, data)
75         oldfn = oe.data.expand(oldfn, data)
76
77         from oe.parse import handle
78         try:
79                 ret = handle(fn, data, 1)
80         except IOError:
81                 debug(2, "CONF file '%s' not found" % fn)
82
83 def handle(fn, data = {}, include = 0):
84         if include:
85                 inc_string = "including"
86         else:
87                 inc_string = "reading"
88         init(data)
89         if include == 0:
90                 oe.data.inheritFromOS(data)
91         fn = obtain(fn, data)
92         oepath = ['.']
93         if not os.path.isabs(fn):
94                 f = None
95                 voepath = oe.data.getVar("OEPATH", data)
96                 if voepath:
97                         oepath += voepath.split(":")
98                 for p in oepath:
99                         currname = os.path.join(oe.data.expand(p, data), fn)
100                         if os.access(currname, os.R_OK):
101                                 f = open(currname, 'r')
102                                 debug(1, "CONF %s %s" % (inc_string, currname))
103                                 break
104                 if f is None:
105                         raise IOError("file not found")
106         else:
107                 f = open(fn,'r')
108                 debug(1, "CONF %s %s" % (inc_string,fn))
109         lineno = 0
110         while 1:
111                 lineno = lineno + 1
112                 s = f.readline()
113                 if not s: break
114                 w = s.strip()
115                 if not w: continue              # skip empty lines
116                 s = s.rstrip()
117                 if s[0] == '#': continue        # skip comments
118                 while s[-1] == '\\':
119                         s2 = f.readline()[:-1].strip()
120                         lineno = lineno + 1
121                         s = s[:-1] + s2
122                 feeder(lineno, s, fn, data)
123         return data
124
125 def feeder(lineno, s, fn, data = {}):
126         m = __config_regexp__.match(s)
127         if m:
128                 groupd = m.groupdict()
129                 key = groupd["var"]
130                 if "exp" in groupd and groupd["exp"] != None:
131                         oe.data.setVarFlag(key, "export", 1, data)
132                 if "ques" in groupd and groupd["ques"] != None:
133                         val = oe.data.getVar(key, data)
134                         if not val:
135                                 val = groupd["value"]
136                 elif "colon" in groupd and groupd["colon"] != None:
137                         val = oe.data.expand(groupd["value"], data)
138                 else:
139                         val = groupd["value"]
140                 if 'flag' in groupd and groupd['flag'] != None:
141                         #oe.note("setVarFlag(%s, %s, %s, data)" % (key, groupd['flag'], val))
142                         oe.data.setVarFlag(key, groupd['flag'], val, data)
143                 else:
144                         oe.data.setVar(key, val, data)
145                 return
146
147         m = __include_regexp__.match(s)
148         if m:
149                 s = oe.data.expand(m.group(1), data)
150                 #debug(2, "CONF %s:%d: including %s" % (fn, lineno, s))
151                 include(fn, s, data)
152                 return
153
154         fatal("PARSER: %s:%d: unparsed line" % (fn, lineno));
155
156 # Add us to the handlers list
157 from oe.parse import handlers
158 handlers.append({'supports': supports, 'handle': handle, 'init': init})
159 del handlers