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