blob: 2e84b913d80c4fee6cf132f92018f258c13b4f1a [file] [log] [blame]
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001"""
2 class for handling configuration data files
3
4 Reads a .conf file and obtains its metadata
5
6"""
7
8# Copyright (C) 2003, 2004 Chris Larson
9# Copyright (C) 2003, 2004 Phil Blundell
10#
Brad Bishopc342db32019-05-15 21:57:59 -040011# SPDX-License-Identifier: GPL-2.0-only
Patrick Williamsc124f4f2015-09-15 14:41:29 -050012#
Patrick Williamsc124f4f2015-09-15 14:41:29 -050013
14import errno
15import re
16import os
17import bb.utils
18from bb.parse import ParseError, resolve_file, ast, logger, handle
19
20__config_regexp__ = re.compile( r"""
21 ^
Brad Bishopd7bf8c12018-02-25 22:55:05 -050022 (?P<exp>export\s+)?
Brad Bishop6e60e8b2018-02-01 10:27:11 -050023 (?P<var>[a-zA-Z0-9\-_+.${}/~]+?)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050024 (\[(?P<flag>[a-zA-Z0-9\-_+.]+)\])?
25
26 \s* (
27 (?P<colon>:=) |
28 (?P<lazyques>\?\?=) |
29 (?P<ques>\?=) |
30 (?P<append>\+=) |
31 (?P<prepend>=\+) |
32 (?P<predot>=\.) |
33 (?P<postdot>\.=) |
34 =
35 ) \s*
36
37 (?!'[^']*'[^']*'$)
38 (?!\"[^\"]*\"[^\"]*\"$)
39 (?P<apo>['\"])
40 (?P<value>.*)
41 (?P=apo)
42 $
43 """, re.X)
44__include_regexp__ = re.compile( r"include\s+(.+)" )
45__require_regexp__ = re.compile( r"require\s+(.+)" )
Brad Bishop6e60e8b2018-02-01 10:27:11 -050046__export_regexp__ = re.compile( r"export\s+([a-zA-Z0-9\-_+.${}/~]+)$" )
47__unset_regexp__ = re.compile( r"unset\s+([a-zA-Z0-9\-_+.${}/~]+)$" )
48__unset_flag_regexp__ = re.compile( r"unset\s+([a-zA-Z0-9\-_+.${}/~]+)\[([a-zA-Z0-9\-_+.]+)\]$" )
Patrick Williamsc124f4f2015-09-15 14:41:29 -050049
50def init(data):
51 topdir = data.getVar('TOPDIR', False)
52 if not topdir:
53 data.setVar('TOPDIR', os.getcwd())
54
55
56def supports(fn, d):
57 return fn[-5:] == ".conf"
58
Brad Bishopd7bf8c12018-02-25 22:55:05 -050059def include(parentfn, fns, lineno, data, error_out):
Patrick Williamsc124f4f2015-09-15 14:41:29 -050060 """
61 error_out: A string indicating the verb (e.g. "include", "inherit") to be
62 used in a ParseError that will be raised if the file to be included could
63 not be included. Specify False to avoid raising an error in this case.
64 """
Brad Bishopd7bf8c12018-02-25 22:55:05 -050065 fns = data.expand(fns)
66 parentfn = data.expand(parentfn)
67
68 # "include" or "require" accept zero to n space-separated file names to include.
69 for fn in fns.split():
70 include_single_file(parentfn, fn, lineno, data, error_out)
71
72def include_single_file(parentfn, fn, lineno, data, error_out):
73 """
74 Helper function for include() which does not expand or split its parameters.
75 """
Patrick Williamsc124f4f2015-09-15 14:41:29 -050076 if parentfn == fn: # prevent infinite recursion
77 return None
78
Patrick Williamsc124f4f2015-09-15 14:41:29 -050079 if not os.path.isabs(fn):
80 dname = os.path.dirname(parentfn)
Brad Bishop6e60e8b2018-02-01 10:27:11 -050081 bbpath = "%s:%s" % (dname, data.getVar("BBPATH"))
Patrick Williamsc124f4f2015-09-15 14:41:29 -050082 abs_fn, attempts = bb.utils.which(bbpath, fn, history=True)
83 if abs_fn and bb.parse.check_dependency(data, abs_fn):
Brad Bishop6e60e8b2018-02-01 10:27:11 -050084 logger.warning("Duplicate inclusion for %s in %s" % (abs_fn, data.getVar('FILE')))
Patrick Williamsc124f4f2015-09-15 14:41:29 -050085 for af in attempts:
86 bb.parse.mark_dependency(data, af)
87 if abs_fn:
88 fn = abs_fn
89 elif bb.parse.check_dependency(data, fn):
Brad Bishop6e60e8b2018-02-01 10:27:11 -050090 logger.warning("Duplicate inclusion for %s in %s" % (fn, data.getVar('FILE')))
Patrick Williamsc124f4f2015-09-15 14:41:29 -050091
92 try:
93 bb.parse.handle(fn, data, True)
94 except (IOError, OSError) as exc:
95 if exc.errno == errno.ENOENT:
96 if error_out:
97 raise ParseError("Could not %s file %s" % (error_out, fn), parentfn, lineno)
98 logger.debug(2, "CONF file '%s' not found", fn)
99 else:
100 if error_out:
101 raise ParseError("Could not %s file %s: %s" % (error_out, fn, exc.strerror), parentfn, lineno)
102 else:
103 raise ParseError("Error parsing %s: %s" % (fn, exc.strerror), parentfn, lineno)
104
105# We have an issue where a UI might want to enforce particular settings such as
106# an empty DISTRO variable. If configuration files do something like assigning
107# a weak default, it turns out to be very difficult to filter out these changes,
108# particularly when the weak default might appear half way though parsing a chain
109# of configuration files. We therefore let the UIs hook into configuration file
110# parsing. This turns out to be a hard problem to solve any other way.
111confFilters = []
112
113def handle(fn, data, include):
114 init(data)
115
116 if include == 0:
117 oldfile = None
118 else:
119 oldfile = data.getVar('FILE', False)
120
121 abs_fn = resolve_file(fn, data)
122 f = open(abs_fn, 'r')
123
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500124 statements = ast.StatementGroup()
125 lineno = 0
126 while True:
127 lineno = lineno + 1
128 s = f.readline()
129 if not s:
130 break
131 w = s.strip()
132 # skip empty lines
133 if not w:
134 continue
135 s = s.rstrip()
136 while s[-1] == '\\':
Brad Bishop19323692019-04-05 15:28:33 -0400137 s2 = f.readline().rstrip()
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500138 lineno = lineno + 1
139 if (not s2 or s2 and s2[0] != "#") and s[0] == "#" :
140 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))
141 s = s[:-1] + s2
142 # skip comments
143 if s[0] == '#':
144 continue
145 feeder(lineno, s, abs_fn, statements)
146
147 # DONE WITH PARSING... time to evaluate
148 data.setVar('FILE', abs_fn)
149 statements.eval(data)
150 if oldfile:
151 data.setVar('FILE', oldfile)
152
153 f.close()
154
155 for f in confFilters:
156 f(fn, data)
157
158 return data
159
160def feeder(lineno, s, fn, statements):
161 m = __config_regexp__.match(s)
162 if m:
163 groupd = m.groupdict()
164 ast.handleData(statements, fn, lineno, groupd)
165 return
166
167 m = __include_regexp__.match(s)
168 if m:
169 ast.handleInclude(statements, fn, lineno, m, False)
170 return
171
172 m = __require_regexp__.match(s)
173 if m:
174 ast.handleInclude(statements, fn, lineno, m, True)
175 return
176
177 m = __export_regexp__.match(s)
178 if m:
179 ast.handleExport(statements, fn, lineno, m)
180 return
181
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600182 m = __unset_regexp__.match(s)
183 if m:
184 ast.handleUnset(statements, fn, lineno, m)
185 return
186
187 m = __unset_flag_regexp__.match(s)
188 if m:
189 ast.handleUnsetFlag(statements, fn, lineno, m)
190 return
191
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500192 raise ParseError("unparsed line: '%s'" % s, fn, lineno);
193
194# Add us to the handlers list
195from bb.parse import handlers
196handlers.append({'supports': supports, 'handle': handle, 'init': init})
197del handlers