blob: 7826dee7d3d459ba5e25d6b1211a121316237fe2 [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+)?
Andrew Geissler5f350902021-07-23 13:09:54 -040023 (?P<var>[a-zA-Z0-9\-_+.${}/~:]+?)
Patrick Williams8e7b46e2023-05-01 14:19:06 -050024 (\[(?P<flag>[a-zA-Z0-9\-_+.][a-zA-Z0-9\-_+.@]*)\])?
Patrick Williamsc124f4f2015-09-15 14:41:29 -050025
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\-_+.${}/~]+)$" )
Andrew Geisslerfc113ea2023-03-31 09:59:46 -050048__unset_flag_regexp__ = re.compile( r"unset\s+([a-zA-Z0-9\-_+.${}/~]+)\[([a-zA-Z0-9\-_+.][a-zA-Z0-9\-_+.@]+)\]$" )
Andrew Geissler517393d2023-01-13 08:55:19 -060049__addpylib_regexp__ = re.compile(r"addpylib\s+(.+)\s+(.+)" )
Patrick Williamsc124f4f2015-09-15 14:41:29 -050050
51def init(data):
Andrew Geissler595f6302022-01-24 19:11:47 +000052 return
Patrick Williamsc124f4f2015-09-15 14:41:29 -050053
54def supports(fn, d):
55 return fn[-5:] == ".conf"
56
Brad Bishopd7bf8c12018-02-25 22:55:05 -050057def include(parentfn, fns, lineno, data, error_out):
Patrick Williamsc124f4f2015-09-15 14:41:29 -050058 """
59 error_out: A string indicating the verb (e.g. "include", "inherit") to be
60 used in a ParseError that will be raised if the file to be included could
61 not be included. Specify False to avoid raising an error in this case.
62 """
Brad Bishopd7bf8c12018-02-25 22:55:05 -050063 fns = data.expand(fns)
64 parentfn = data.expand(parentfn)
65
66 # "include" or "require" accept zero to n space-separated file names to include.
67 for fn in fns.split():
68 include_single_file(parentfn, fn, lineno, data, error_out)
69
70def include_single_file(parentfn, fn, lineno, data, error_out):
71 """
72 Helper function for include() which does not expand or split its parameters.
73 """
Patrick Williamsc124f4f2015-09-15 14:41:29 -050074 if parentfn == fn: # prevent infinite recursion
75 return None
76
Patrick Williamsc124f4f2015-09-15 14:41:29 -050077 if not os.path.isabs(fn):
78 dname = os.path.dirname(parentfn)
Brad Bishop6e60e8b2018-02-01 10:27:11 -050079 bbpath = "%s:%s" % (dname, data.getVar("BBPATH"))
Patrick Williamsc124f4f2015-09-15 14:41:29 -050080 abs_fn, attempts = bb.utils.which(bbpath, fn, history=True)
81 if abs_fn and bb.parse.check_dependency(data, abs_fn):
Brad Bishop6e60e8b2018-02-01 10:27:11 -050082 logger.warning("Duplicate inclusion for %s in %s" % (abs_fn, data.getVar('FILE')))
Patrick Williamsc124f4f2015-09-15 14:41:29 -050083 for af in attempts:
84 bb.parse.mark_dependency(data, af)
85 if abs_fn:
86 fn = abs_fn
87 elif bb.parse.check_dependency(data, fn):
Brad Bishop6e60e8b2018-02-01 10:27:11 -050088 logger.warning("Duplicate inclusion for %s in %s" % (fn, data.getVar('FILE')))
Patrick Williamsc124f4f2015-09-15 14:41:29 -050089
90 try:
91 bb.parse.handle(fn, data, True)
92 except (IOError, OSError) as exc:
93 if exc.errno == errno.ENOENT:
94 if error_out:
95 raise ParseError("Could not %s file %s" % (error_out, fn), parentfn, lineno)
Andrew Geisslerd1e89492021-02-12 15:35:20 -060096 logger.debug2("CONF file '%s' not found", fn)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050097 else:
98 if error_out:
99 raise ParseError("Could not %s file %s: %s" % (error_out, fn, exc.strerror), parentfn, lineno)
100 else:
101 raise ParseError("Error parsing %s: %s" % (fn, exc.strerror), parentfn, lineno)
102
103# We have an issue where a UI might want to enforce particular settings such as
104# an empty DISTRO variable. If configuration files do something like assigning
105# a weak default, it turns out to be very difficult to filter out these changes,
Patrick Williams8e7b46e2023-05-01 14:19:06 -0500106# particularly when the weak default might appear half way though parsing a chain
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500107# of configuration files. We therefore let the UIs hook into configuration file
108# parsing. This turns out to be a hard problem to solve any other way.
109confFilters = []
110
Andrew Geissler517393d2023-01-13 08:55:19 -0600111def handle(fn, data, include, baseconfig=False):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500112 init(data)
113
114 if include == 0:
115 oldfile = None
116 else:
117 oldfile = data.getVar('FILE', False)
118
119 abs_fn = resolve_file(fn, data)
Brad Bishop64c979e2019-11-04 13:55:29 -0500120 with open(abs_fn, 'r') as f:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500121
Brad Bishop64c979e2019-11-04 13:55:29 -0500122 statements = ast.StatementGroup()
123 lineno = 0
124 while True:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500125 lineno = lineno + 1
Brad Bishop64c979e2019-11-04 13:55:29 -0500126 s = f.readline()
127 if not s:
128 break
Andrew Geissler615f2f12022-07-15 14:00:58 -0500129 origlineno = lineno
130 origline = s
Brad Bishop64c979e2019-11-04 13:55:29 -0500131 w = s.strip()
132 # skip empty lines
133 if not w:
134 continue
135 s = s.rstrip()
136 while s[-1] == '\\':
Andrew Geissler615f2f12022-07-15 14:00:58 -0500137 line = f.readline()
138 origline += line
139 s2 = line.rstrip()
Brad Bishop64c979e2019-11-04 13:55:29 -0500140 lineno = lineno + 1
141 if (not s2 or s2 and s2[0] != "#") and s[0] == "#" :
Andrew Geissler615f2f12022-07-15 14:00:58 -0500142 bb.fatal("There is a confusing multiline, partially commented expression starting on line %s of file %s:\n%s\nPlease clarify whether this is all a comment or should be parsed." % (origlineno, fn, origline))
143
Brad Bishop64c979e2019-11-04 13:55:29 -0500144 s = s[:-1] + s2
145 # skip comments
146 if s[0] == '#':
147 continue
Andrew Geissler517393d2023-01-13 08:55:19 -0600148 feeder(lineno, s, abs_fn, statements, baseconfig=baseconfig)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500149
150 # DONE WITH PARSING... time to evaluate
151 data.setVar('FILE', abs_fn)
152 statements.eval(data)
153 if oldfile:
154 data.setVar('FILE', oldfile)
155
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500156 for f in confFilters:
157 f(fn, data)
158
159 return data
160
Andrew Geissler517393d2023-01-13 08:55:19 -0600161# baseconfig is set for the bblayers/layer.conf cookerdata config parsing
162# The function is also used by BBHandler, conffile would be False
163def feeder(lineno, s, fn, statements, baseconfig=False, conffile=True):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500164 m = __config_regexp__.match(s)
165 if m:
166 groupd = m.groupdict()
167 ast.handleData(statements, fn, lineno, groupd)
168 return
169
170 m = __include_regexp__.match(s)
171 if m:
172 ast.handleInclude(statements, fn, lineno, m, False)
173 return
174
175 m = __require_regexp__.match(s)
176 if m:
177 ast.handleInclude(statements, fn, lineno, m, True)
178 return
179
180 m = __export_regexp__.match(s)
181 if m:
182 ast.handleExport(statements, fn, lineno, m)
183 return
184
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600185 m = __unset_regexp__.match(s)
186 if m:
187 ast.handleUnset(statements, fn, lineno, m)
188 return
189
190 m = __unset_flag_regexp__.match(s)
191 if m:
192 ast.handleUnsetFlag(statements, fn, lineno, m)
193 return
194
Andrew Geissler517393d2023-01-13 08:55:19 -0600195 m = __addpylib_regexp__.match(s)
196 if baseconfig and conffile and m:
197 ast.handlePyLib(statements, fn, lineno, m)
198 return
199
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500200 raise ParseError("unparsed line: '%s'" % s, fn, lineno);
201
202# Add us to the handlers list
203from bb.parse import handlers
204handlers.append({'supports': supports, 'handle': handle, 'init': init})
205del handlers