blob: 97aa13043145917a901efbfc3196002d889da258 [file] [log] [blame]
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001#!/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"""
5 class for handling configuration data files
6
7 Reads a .conf file and obtains its metadata
8
9"""
10
11# Copyright (C) 2003, 2004 Chris Larson
12# Copyright (C) 2003, 2004 Phil Blundell
13#
14# This program is free software; you can redistribute it and/or modify
15# it under the terms of the GNU General Public License version 2 as
16# published by the Free Software Foundation.
17#
18# This program is distributed in the hope that it will be useful,
19# but WITHOUT ANY WARRANTY; without even the implied warranty of
20# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21# GNU General Public License for more details.
22#
23# You should have received a copy of the GNU General Public License along
24# with this program; if not, write to the Free Software Foundation, Inc.,
25# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26
27import errno
28import re
29import os
30import bb.utils
31from bb.parse import ParseError, resolve_file, ast, logger, handle
32
33__config_regexp__ = re.compile( r"""
34 ^
Brad Bishopd7bf8c12018-02-25 22:55:05 -050035 (?P<exp>export\s+)?
Brad Bishop6e60e8b2018-02-01 10:27:11 -050036 (?P<var>[a-zA-Z0-9\-_+.${}/~]+?)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050037 (\[(?P<flag>[a-zA-Z0-9\-_+.]+)\])?
38
39 \s* (
40 (?P<colon>:=) |
41 (?P<lazyques>\?\?=) |
42 (?P<ques>\?=) |
43 (?P<append>\+=) |
44 (?P<prepend>=\+) |
45 (?P<predot>=\.) |
46 (?P<postdot>\.=) |
47 =
48 ) \s*
49
50 (?!'[^']*'[^']*'$)
51 (?!\"[^\"]*\"[^\"]*\"$)
52 (?P<apo>['\"])
53 (?P<value>.*)
54 (?P=apo)
55 $
56 """, re.X)
57__include_regexp__ = re.compile( r"include\s+(.+)" )
58__require_regexp__ = re.compile( r"require\s+(.+)" )
Brad Bishop6e60e8b2018-02-01 10:27:11 -050059__export_regexp__ = re.compile( r"export\s+([a-zA-Z0-9\-_+.${}/~]+)$" )
60__unset_regexp__ = re.compile( r"unset\s+([a-zA-Z0-9\-_+.${}/~]+)$" )
61__unset_flag_regexp__ = re.compile( r"unset\s+([a-zA-Z0-9\-_+.${}/~]+)\[([a-zA-Z0-9\-_+.]+)\]$" )
Patrick Williamsc124f4f2015-09-15 14:41:29 -050062
63def init(data):
64 topdir = data.getVar('TOPDIR', False)
65 if not topdir:
66 data.setVar('TOPDIR', os.getcwd())
67
68
69def supports(fn, d):
70 return fn[-5:] == ".conf"
71
Brad Bishopd7bf8c12018-02-25 22:55:05 -050072def include(parentfn, fns, lineno, data, error_out):
Patrick Williamsc124f4f2015-09-15 14:41:29 -050073 """
74 error_out: A string indicating the verb (e.g. "include", "inherit") to be
75 used in a ParseError that will be raised if the file to be included could
76 not be included. Specify False to avoid raising an error in this case.
77 """
Brad Bishopd7bf8c12018-02-25 22:55:05 -050078 fns = data.expand(fns)
79 parentfn = data.expand(parentfn)
80
81 # "include" or "require" accept zero to n space-separated file names to include.
82 for fn in fns.split():
83 include_single_file(parentfn, fn, lineno, data, error_out)
84
85def include_single_file(parentfn, fn, lineno, data, error_out):
86 """
87 Helper function for include() which does not expand or split its parameters.
88 """
Patrick Williamsc124f4f2015-09-15 14:41:29 -050089 if parentfn == fn: # prevent infinite recursion
90 return None
91
Patrick Williamsc124f4f2015-09-15 14:41:29 -050092 if not os.path.isabs(fn):
93 dname = os.path.dirname(parentfn)
Brad Bishop6e60e8b2018-02-01 10:27:11 -050094 bbpath = "%s:%s" % (dname, data.getVar("BBPATH"))
Patrick Williamsc124f4f2015-09-15 14:41:29 -050095 abs_fn, attempts = bb.utils.which(bbpath, fn, history=True)
96 if abs_fn and bb.parse.check_dependency(data, abs_fn):
Brad Bishop6e60e8b2018-02-01 10:27:11 -050097 logger.warning("Duplicate inclusion for %s in %s" % (abs_fn, data.getVar('FILE')))
Patrick Williamsc124f4f2015-09-15 14:41:29 -050098 for af in attempts:
99 bb.parse.mark_dependency(data, af)
100 if abs_fn:
101 fn = abs_fn
102 elif bb.parse.check_dependency(data, fn):
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500103 logger.warning("Duplicate inclusion for %s in %s" % (fn, data.getVar('FILE')))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500104
105 try:
106 bb.parse.handle(fn, data, True)
107 except (IOError, OSError) as exc:
108 if exc.errno == errno.ENOENT:
109 if error_out:
110 raise ParseError("Could not %s file %s" % (error_out, fn), parentfn, lineno)
111 logger.debug(2, "CONF file '%s' not found", fn)
112 else:
113 if error_out:
114 raise ParseError("Could not %s file %s: %s" % (error_out, fn, exc.strerror), parentfn, lineno)
115 else:
116 raise ParseError("Error parsing %s: %s" % (fn, exc.strerror), parentfn, lineno)
117
118# We have an issue where a UI might want to enforce particular settings such as
119# an empty DISTRO variable. If configuration files do something like assigning
120# a weak default, it turns out to be very difficult to filter out these changes,
121# particularly when the weak default might appear half way though parsing a chain
122# of configuration files. We therefore let the UIs hook into configuration file
123# parsing. This turns out to be a hard problem to solve any other way.
124confFilters = []
125
126def handle(fn, data, include):
127 init(data)
128
129 if include == 0:
130 oldfile = None
131 else:
132 oldfile = data.getVar('FILE', False)
133
134 abs_fn = resolve_file(fn, data)
135 f = open(abs_fn, 'r')
136
137 if include:
138 bb.parse.mark_dependency(data, abs_fn)
139
140 statements = ast.StatementGroup()
141 lineno = 0
142 while True:
143 lineno = lineno + 1
144 s = f.readline()
145 if not s:
146 break
147 w = s.strip()
148 # skip empty lines
149 if not w:
150 continue
151 s = s.rstrip()
152 while s[-1] == '\\':
153 s2 = f.readline().strip()
154 lineno = lineno + 1
155 if (not s2 or s2 and s2[0] != "#") and s[0] == "#" :
156 bb.fatal("There is a confusing multiline, partially commented expression on line %s of file %s (%s).\nPlease clarify whether this is all a comment or should be parsed." % (lineno, fn, s))
157 s = s[:-1] + s2
158 # skip comments
159 if s[0] == '#':
160 continue
161 feeder(lineno, s, abs_fn, statements)
162
163 # DONE WITH PARSING... time to evaluate
164 data.setVar('FILE', abs_fn)
165 statements.eval(data)
166 if oldfile:
167 data.setVar('FILE', oldfile)
168
169 f.close()
170
171 for f in confFilters:
172 f(fn, data)
173
174 return data
175
176def feeder(lineno, s, fn, statements):
177 m = __config_regexp__.match(s)
178 if m:
179 groupd = m.groupdict()
180 ast.handleData(statements, fn, lineno, groupd)
181 return
182
183 m = __include_regexp__.match(s)
184 if m:
185 ast.handleInclude(statements, fn, lineno, m, False)
186 return
187
188 m = __require_regexp__.match(s)
189 if m:
190 ast.handleInclude(statements, fn, lineno, m, True)
191 return
192
193 m = __export_regexp__.match(s)
194 if m:
195 ast.handleExport(statements, fn, lineno, m)
196 return
197
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600198 m = __unset_regexp__.match(s)
199 if m:
200 ast.handleUnset(statements, fn, lineno, m)
201 return
202
203 m = __unset_flag_regexp__.match(s)
204 if m:
205 ast.handleUnsetFlag(statements, fn, lineno, m)
206 return
207
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500208 raise ParseError("unparsed line: '%s'" % s, fn, lineno);
209
210# Add us to the handlers list
211from bb.parse import handlers
212handlers.append({'supports': supports, 'handle': handle, 'init': init})
213del handlers