2 # ex:ts=4:sw=4:sts=4:et
3 # -*- tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*-
4 """class for handling configuration data files
6 Reads a .conf file and obtains its metadata
8 Copyright (C) 2003, 2004 Chris Larson
9 Copyright (C) 2003, 2004 Phil Blundell
11 This program is free software; you can redistribute it and/or modify it under
12 the terms of the GNU General Public License as published by the Free Software
13 Foundation; either version 2 of the License, or (at your option) any later
16 This program is distributed in the hope that it will be useful, but WITHOUT
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License along with
21 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
22 Place, Suite 330, Boston, MA 02111-1307 USA."""
24 import re, bb.data, os, sys
25 from bb import debug, fatal
26 from bb.parse import ParseError
28 #__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)$")
29 __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>\?=)|(?P<append>\+=)|(?P<prepend>=\+)|(?P<predot>=\.)|(?P<postdot>\.=)|=)\s*(?P<apo>['\"]?)(?P<value>.*)(?P=apo)$")
30 __include_regexp__ = re.compile( r"include\s+(.+)" )
31 __require_regexp__ = re.compile( r"require\s+(.+)" )
34 if not bb.data.getVar('TOPDIR', data):
35 bb.data.setVar('TOPDIR', os.getcwd(), data)
36 if not bb.data.getVar('BBPATH', data):
37 bb.data.setVar('BBPATH', os.path.join(sys.prefix, 'share', 'bitbake'), data)
40 return localpath(fn, d)[-5:] == ".conf"
43 if os.path.exists(fn):
48 localfn = bb.fetch.localpath(fn, d)
49 except bb.MalformedUrl:
56 def obtain(fn, data = bb.data.init()):
58 fn = bb.data.expand(fn, data)
59 localfn = bb.data.expand(localpath(fn, data), data)
62 dldir = bb.data.getVar('DL_DIR', data, 1)
64 debug(1, "obtain: DL_DIR not defined")
69 except bb.fetch.NoMethodError:
70 (type, value, traceback) = sys.exc_info()
71 debug(1, "obtain: no method: %s" % value)
76 except bb.fetch.MissingParameterError:
77 (type, value, traceback) = sys.exc_info()
78 debug(1, "obtain: missing parameters: %s" % value)
80 except bb.fetch.FetchError:
81 (type, value, traceback) = sys.exc_info()
82 debug(1, "obtain: failed: %s" % value)
87 def include(oldfn, fn, data = bb.data.init(), error_out = False):
90 error_out If True a ParseError will be reaised if the to be included
92 if oldfn == fn: # prevent infinate recursion
96 fn = bb.data.expand(fn, data)
97 oldfn = bb.data.expand(oldfn, data)
99 from bb.parse import handle
101 ret = handle(fn, data, True)
104 raise ParseError("Could not include required file %(fn)s" % vars() )
105 debug(2, "CONF file '%s' not found" % fn)
107 def handle(fn, data = bb.data.init(), include = 0):
109 inc_string = "including"
111 inc_string = "reading"
115 bb.data.inheritFromOS(data)
118 oldfile = bb.data.getVar('FILE', data)
120 fn = obtain(fn, data)
122 if not os.path.isabs(fn):
124 vbbpath = bb.data.getVar("BBPATH", data)
126 bbpath += vbbpath.split(":")
128 currname = os.path.join(bb.data.expand(p, data), fn)
129 if os.access(currname, os.R_OK):
130 f = open(currname, 'r')
132 debug(1, "CONF %s %s" % (inc_string, currname))
135 raise IOError("file '%s' not found" % fn)
138 debug(1, "CONF %s %s" % (inc_string,fn))
142 bb.parse.mark_dependency(data, abs_fn)
145 bb.data.setVar('FILE', fn, data)
151 if not w: continue # skip empty lines
153 if s[0] == '#': continue # skip comments
155 s2 = f.readline()[:-1].strip()
158 feeder(lineno, s, fn, data)
161 bb.data.setVar('FILE', oldfile, data)
164 def feeder(lineno, s, fn, data = bb.data.init()):
165 m = __config_regexp__.match(s)
167 groupd = m.groupdict()
169 if "exp" in groupd and groupd["exp"] != None:
170 bb.data.setVarFlag(key, "export", 1, data)
171 if "ques" in groupd and groupd["ques"] != None:
172 val = bb.data.getVar(key, data)
174 val = groupd["value"]
175 elif "colon" in groupd and groupd["colon"] != None:
176 val = bb.data.expand(groupd["value"], data)
177 elif "append" in groupd and groupd["append"] != None:
178 val = "%s %s" % ((bb.data.getVar(key, data) or ""), groupd["value"])
179 elif "prepend" in groupd and groupd["prepend"] != None:
180 val = "%s %s" % (groupd["value"], (bb.data.getVar(key, data) or ""))
181 elif "postdot" in groupd and groupd["postdot"] != None:
182 val = "%s%s" % ((bb.data.getVar(key, data) or ""), groupd["value"])
183 elif "predot" in groupd and groupd["predot"] != None:
184 val = "%s%s" % (groupd["value"], (bb.data.getVar(key, data) or ""))
186 val = groupd["value"]
187 if 'flag' in groupd and groupd['flag'] != None:
188 # bb.note("setVarFlag(%s, %s, %s, data)" % (key, groupd['flag'], val))
189 bb.data.setVarFlag(key, groupd['flag'], val, data)
191 bb.data.setVar(key, val, data)
194 m = __include_regexp__.match(s)
196 s = bb.data.expand(m.group(1), data)
197 # debug(2, "CONF %s:%d: including %s" % (fn, lineno, s))
201 m = __require_regexp__.match(s)
203 s = bb.data.expand(m.group(1), data)
204 include(fn, s, data, True)
207 raise ParseError("%s:%d: unparsed line: '%s'" % (fn, lineno, s));
209 # Add us to the handlers list
210 from bb.parse import handlers
211 handlers.append({'supports': supports, 'handle': handle, 'init': init})