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