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.parse import ParseError
27 #__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)$")
28 __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)$")
29 __include_regexp__ = re.compile( r"include\s+(.+)" )
30 __require_regexp__ = re.compile( r"require\s+(.+)" )
33 if not bb.data.getVar('TOPDIR', data):
34 bb.data.setVar('TOPDIR', os.getcwd(), data)
35 if not bb.data.getVar('BBPATH', data):
36 bb.data.setVar('BBPATH', os.path.join(sys.prefix, 'share', 'bitbake'), data)
39 return localpath(fn, d)[-5:] == ".conf"
42 if os.path.exists(fn):
47 localfn = bb.fetch.localpath(fn, d)
48 except bb.MalformedUrl:
57 fn = bb.data.expand(fn, data)
58 localfn = bb.data.expand(localpath(fn, data), data)
61 dldir = bb.data.getVar('DL_DIR', data, 1)
63 bb.msg.debug(1, bb.msg.domain.Parsing, "obtain: DL_DIR not defined")
68 except bb.fetch.NoMethodError:
69 (type, value, traceback) = sys.exc_info()
70 bb.msg.debug(1, bb.msg.domain.Parsing, "obtain: no method: %s" % value)
75 except bb.fetch.MissingParameterError:
76 (type, value, traceback) = sys.exc_info()
77 bb.msg.debug(1, bb.msg.domain.Parsing, "obtain: missing parameters: %s" % value)
79 except bb.fetch.FetchError:
80 (type, value, traceback) = sys.exc_info()
81 bb.msg.debug(1, bb.msg.domain.Parsing, "obtain: failed: %s" % value)
86 def include(oldfn, fn, data, error_out = False):
89 error_out If True a ParseError will be reaised if the to be included
91 if oldfn == fn: # prevent infinate recursion
95 fn = bb.data.expand(fn, data)
96 oldfn = bb.data.expand(oldfn, data)
98 from bb.parse import handle
100 ret = handle(fn, data, True)
103 raise ParseError("Could not include required file %(fn)s" % vars() )
104 bb.msg.debug(2, bb.msg.domain.Parsing, "CONF file '%s' not found" % fn)
106 def handle(fn, data, include = 0):
108 inc_string = "including"
110 inc_string = "reading"
114 bb.data.inheritFromOS(data)
117 oldfile = bb.data.getVar('FILE', data)
119 fn = obtain(fn, data)
121 if not os.path.isabs(fn):
123 vbbpath = bb.data.getVar("BBPATH", data)
125 bbpath += vbbpath.split(":")
127 currname = os.path.join(bb.data.expand(p, data), fn)
128 if os.access(currname, os.R_OK):
129 f = open(currname, 'r')
131 bb.msg.debug(2, bb.msg.domain.Parsing, "CONF %s %s" % (inc_string, currname))
134 raise IOError("file '%s' not found" % fn)
137 bb.msg.debug(1, bb.msg.domain.Parsing, "CONF %s %s" % (inc_string,fn))
141 bb.parse.mark_dependency(data, abs_fn)
144 bb.data.setVar('FILE', fn, data)
150 if not w: continue # skip empty lines
152 if s[0] == '#': continue # skip comments
154 s2 = f.readline()[:-1].strip()
157 feeder(lineno, s, fn, data)
160 bb.data.setVar('FILE', oldfile, data)
163 def feeder(lineno, s, fn, data):
164 m = __config_regexp__.match(s)
166 groupd = m.groupdict()
168 if "exp" in groupd and groupd["exp"] != None:
169 bb.data.setVarFlag(key, "export", 1, data)
170 if "ques" in groupd and groupd["ques"] != None:
171 val = bb.data.getVar(key, data)
173 val = groupd["value"]
174 elif "colon" in groupd and groupd["colon"] != None:
175 val = bb.data.expand(groupd["value"], data)
176 elif "append" in groupd and groupd["append"] != None:
177 val = "%s %s" % ((bb.data.getVar(key, data) or ""), groupd["value"])
178 elif "prepend" in groupd and groupd["prepend"] != None:
179 val = "%s %s" % (groupd["value"], (bb.data.getVar(key, data) or ""))
180 elif "postdot" in groupd and groupd["postdot"] != None:
181 val = "%s%s" % ((bb.data.getVar(key, data) or ""), groupd["value"])
182 elif "predot" in groupd and groupd["predot"] != None:
183 val = "%s%s" % (groupd["value"], (bb.data.getVar(key, data) or ""))
185 val = groupd["value"]
186 if 'flag' in groupd and groupd['flag'] != None:
187 bb.msg.debug(3, bb.msg.domain.Parsing, "setVarFlag(%s, %s, %s, data)" % (key, groupd['flag'], val))
188 bb.data.setVarFlag(key, groupd['flag'], val, data)
190 bb.data.setVar(key, val, data)
193 m = __include_regexp__.match(s)
195 s = bb.data.expand(m.group(1), data)
196 bb.msg.debug(3, bb.msg.domain.Parsing, "CONF %s:%d: including %s" % (fn, lineno, s))
200 m = __require_regexp__.match(s)
202 s = bb.data.expand(m.group(1), data)
203 include(fn, s, data, True)
206 raise ParseError("%s:%d: unparsed line: '%s'" % (fn, lineno, s));
208 # Add us to the handlers list
209 from bb.parse import handlers
210 handlers.append({'supports': supports, 'handle': handle, 'init': init})