parse_py/ConfHandler.py: Remove broken default options (as identified by mithro)
[bitbake.git] / lib / bb / parse / parse_py / ConfHandler.py
1 #!/usr/bin/env python
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
5
6    Reads a .conf file and obtains its metadata
7
8    Copyright (C) 2003, 2004  Chris Larson
9    Copyright (C) 2003, 2004  Phil Blundell
10    
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
14    version.
15    
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.
19    
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.""" 
23
24 import re, bb.data, os, sys
25 from bb.parse import ParseError
26
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+(.+)" )
31
32 def init(data):
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)
37
38 def supports(fn, d):
39     return localpath(fn, d)[-5:] == ".conf"
40
41 def localpath(fn, d):
42     if os.path.exists(fn):
43         return fn
44
45     localfn = None
46     try:
47         localfn = bb.fetch.localpath(fn, d)
48     except bb.MalformedUrl:
49         pass
50
51     if not localfn:
52         localfn = fn
53     return localfn
54
55 def obtain(fn, data):
56     import sys, bb
57     fn = bb.data.expand(fn, data)
58     localfn = bb.data.expand(localpath(fn, data), data)
59
60     if localfn != fn:
61         dldir = bb.data.getVar('DL_DIR', data, 1)
62         if not dldir:
63             bb.msg.debug(1, bb.msg.domain.Parsing, "obtain: DL_DIR not defined")
64             return localfn
65         bb.mkdirhier(dldir)
66         try:
67             bb.fetch.init([fn])
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)
71             return localfn
72
73         try:
74             bb.fetch.go(data)
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)
78             return localfn
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)
82             return localfn
83     return localfn
84
85
86 def include(oldfn, fn, data, error_out = False):
87     """
88
89     error_out If True a ParseError will be reaised if the to be included
90     """
91     if oldfn == fn: # prevent infinate recursion
92         return None
93
94     import bb
95     fn = bb.data.expand(fn, data)
96     oldfn = bb.data.expand(oldfn, data)
97
98     from bb.parse import handle
99     try:
100         ret = handle(fn, data, True)
101     except IOError:
102         if error_out:
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)
105
106 def handle(fn, data, include = 0):
107     if include:
108         inc_string = "including"
109     else:
110         inc_string = "reading"
111     init(data)
112
113     if include == 0:
114         bb.data.inheritFromOS(data)
115         oldfile = None
116     else:
117         oldfile = bb.data.getVar('FILE', data)
118
119     fn = obtain(fn, data)
120     bbpath = []
121     if not os.path.isabs(fn):
122         f = None
123         vbbpath = bb.data.getVar("BBPATH", data)
124         if vbbpath:
125             bbpath += vbbpath.split(":")
126         for p in bbpath:
127             currname = os.path.join(bb.data.expand(p, data), fn)
128             if os.access(currname, os.R_OK):
129                 f = open(currname, 'r')
130                 abs_fn = currname
131                 bb.msg.debug(2, bb.msg.domain.Parsing, "CONF %s %s" % (inc_string, currname))
132                 break
133         if f is None:
134             raise IOError("file '%s' not found" % fn)
135     else:
136         f = open(fn,'r')
137         bb.msg.debug(1, bb.msg.domain.Parsing, "CONF %s %s" % (inc_string,fn))
138         abs_fn = fn
139
140     if include:
141         bb.parse.mark_dependency(data, abs_fn)
142
143     lineno = 0
144     bb.data.setVar('FILE', fn, data)
145     while 1:
146         lineno = lineno + 1
147         s = f.readline()
148         if not s: break
149         w = s.strip()
150         if not w: continue          # skip empty lines
151         s = s.rstrip()
152         if s[0] == '#': continue    # skip comments
153         while s[-1] == '\\':
154             s2 = f.readline()[:-1].strip()
155             lineno = lineno + 1
156             s = s[:-1] + s2
157         feeder(lineno, s, fn, data)
158
159     if oldfile:
160         bb.data.setVar('FILE', oldfile, data)
161     return data
162
163 def feeder(lineno, s, fn, data):
164     m = __config_regexp__.match(s)
165     if m:
166         groupd = m.groupdict()
167         key = groupd["var"]
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)
172             if val == None:
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 ""))
184         else:
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)
189         else:
190             bb.data.setVar(key, val, data)
191         return
192
193     m = __include_regexp__.match(s)
194     if m:
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))
197         include(fn, s, data)
198         return
199
200     m = __require_regexp__.match(s)
201     if m:
202         s = bb.data.expand(m.group(1), data)
203         include(fn, s, data, True)
204         return
205
206     raise ParseError("%s:%d: unparsed line: '%s'" % (fn, lineno, s));
207
208 # Add us to the handlers list
209 from bb.parse import handlers
210 handlers.append({'supports': supports, 'handle': handle, 'init': init})
211 del handlers