1 """class for handling configuration data files
3 Reads the file and obtains its metadata"""
5 import re, oe.data, os, sys
6 from oe import debug, fatal
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+(.+)" )
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)
22 return localpath(fn, d)[-5:] == ".conf"
25 if os.path.exists(fn):
30 localfn = oe.fetch.localpath(fn, d)
31 except oe.MalformedUrl:
38 def obtain(fn, data = {}):
40 fn = oe.data.expand(fn, data)
41 localfn = oe.data.expand(localpath(fn, data), data)
44 dldir = oe.data.getVar('DL_DIR', data, 1)
46 debug(1, "obtain: DL_DIR not defined")
51 except oe.fetch.NoMethodError:
52 (type, value, traceback) = sys.exc_info()
53 debug(1, "obtain: no method: %s" % value)
58 except oe.fetch.MissingParameterError:
59 (type, value, traceback) = sys.exc_info()
60 debug(1, "obtain: missing parameters: %s" % value)
62 except oe.fetch.FetchError:
63 (type, value, traceback) = sys.exc_info()
64 debug(1, "obtain: failed: %s" % value)
69 def include(oldfn, fn, data = {}):
70 if oldfn == fn: # prevent infinate recursion
74 fn = oe.data.expand(fn, data)
75 oldfn = oe.data.expand(oldfn, data)
77 from oe.parse import handle
79 ret = handle(fn, data, 1)
81 debug(2, "CONF file '%s' not found" % fn)
83 def handle(fn, data = {}, include = 0):
85 inc_string = "including"
87 inc_string = "reading"
90 oe.data.inheritFromOS(data)
93 if not os.path.isabs(fn):
95 voepath = oe.data.getVar("OEPATH", data)
97 oepath += voepath.split(":")
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))
105 raise IOError("file not found")
108 debug(1, "CONF %s %s" % (inc_string,fn))
115 if not w: continue # skip empty lines
117 if s[0] == '#': continue # skip comments
119 s2 = f.readline()[:-1].strip()
122 feeder(lineno, s, fn, data)
125 def feeder(lineno, s, fn, data = {}):
126 m = __config_regexp__.match(s)
128 groupd = m.groupdict()
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)
135 val = groupd["value"]
136 elif "colon" in groupd and groupd["colon"] != None:
137 val = oe.data.expand(groupd["value"], data)
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)
144 oe.data.setVar(key, val, data)
147 m = __include_regexp__.match(s)
149 s = oe.data.expand(m.group(1), data)
150 #debug(2, "CONF %s:%d: including %s" % (fn, lineno, s))
154 fatal("PARSER: %s:%d: unparsed line" % (fn, lineno));
156 # Add us to the handlers list
157 from oe.parse import handlers
158 handlers.append({'supports': supports, 'handle': handle, 'init': init})