Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1 | """ |
| 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 Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 11 | # SPDX-License-Identifier: GPL-2.0-only |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 12 | # |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 13 | |
| 14 | import errno |
| 15 | import re |
| 16 | import os |
| 17 | import bb.utils |
| 18 | from bb.parse import ParseError, resolve_file, ast, logger, handle |
| 19 | |
| 20 | __config_regexp__ = re.compile( r""" |
| 21 | ^ |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 22 | (?P<exp>export\s+)? |
Andrew Geissler | 5f35090 | 2021-07-23 13:09:54 -0400 | [diff] [blame] | 23 | (?P<var>[a-zA-Z0-9\-_+.${}/~:]+?) |
Patrick Williams | 8e7b46e | 2023-05-01 14:19:06 -0500 | [diff] [blame^] | 24 | (\[(?P<flag>[a-zA-Z0-9\-_+.][a-zA-Z0-9\-_+.@]*)\])? |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 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 Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 46 | __export_regexp__ = re.compile( r"export\s+([a-zA-Z0-9\-_+.${}/~]+)$" ) |
| 47 | __unset_regexp__ = re.compile( r"unset\s+([a-zA-Z0-9\-_+.${}/~]+)$" ) |
Andrew Geissler | fc113ea | 2023-03-31 09:59:46 -0500 | [diff] [blame] | 48 | __unset_flag_regexp__ = re.compile( r"unset\s+([a-zA-Z0-9\-_+.${}/~]+)\[([a-zA-Z0-9\-_+.][a-zA-Z0-9\-_+.@]+)\]$" ) |
Andrew Geissler | 517393d | 2023-01-13 08:55:19 -0600 | [diff] [blame] | 49 | __addpylib_regexp__ = re.compile(r"addpylib\s+(.+)\s+(.+)" ) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 50 | |
| 51 | def init(data): |
Andrew Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame] | 52 | return |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 53 | |
| 54 | def supports(fn, d): |
| 55 | return fn[-5:] == ".conf" |
| 56 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 57 | def include(parentfn, fns, lineno, data, error_out): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 58 | """ |
| 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 Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 63 | 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 | |
| 70 | def 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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 74 | if parentfn == fn: # prevent infinite recursion |
| 75 | return None |
| 76 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 77 | if not os.path.isabs(fn): |
| 78 | dname = os.path.dirname(parentfn) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 79 | bbpath = "%s:%s" % (dname, data.getVar("BBPATH")) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 80 | abs_fn, attempts = bb.utils.which(bbpath, fn, history=True) |
| 81 | if abs_fn and bb.parse.check_dependency(data, abs_fn): |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 82 | logger.warning("Duplicate inclusion for %s in %s" % (abs_fn, data.getVar('FILE'))) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 83 | 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 Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 88 | logger.warning("Duplicate inclusion for %s in %s" % (fn, data.getVar('FILE'))) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 89 | |
| 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 Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 96 | logger.debug2("CONF file '%s' not found", fn) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 97 | 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 Williams | 8e7b46e | 2023-05-01 14:19:06 -0500 | [diff] [blame^] | 106 | # particularly when the weak default might appear half way though parsing a chain |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 107 | # 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. |
| 109 | confFilters = [] |
| 110 | |
Andrew Geissler | 517393d | 2023-01-13 08:55:19 -0600 | [diff] [blame] | 111 | def handle(fn, data, include, baseconfig=False): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 112 | 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 Bishop | 64c979e | 2019-11-04 13:55:29 -0500 | [diff] [blame] | 120 | with open(abs_fn, 'r') as f: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 121 | |
Brad Bishop | 64c979e | 2019-11-04 13:55:29 -0500 | [diff] [blame] | 122 | statements = ast.StatementGroup() |
| 123 | lineno = 0 |
| 124 | while True: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 125 | lineno = lineno + 1 |
Brad Bishop | 64c979e | 2019-11-04 13:55:29 -0500 | [diff] [blame] | 126 | s = f.readline() |
| 127 | if not s: |
| 128 | break |
Andrew Geissler | 615f2f1 | 2022-07-15 14:00:58 -0500 | [diff] [blame] | 129 | origlineno = lineno |
| 130 | origline = s |
Brad Bishop | 64c979e | 2019-11-04 13:55:29 -0500 | [diff] [blame] | 131 | w = s.strip() |
| 132 | # skip empty lines |
| 133 | if not w: |
| 134 | continue |
| 135 | s = s.rstrip() |
| 136 | while s[-1] == '\\': |
Andrew Geissler | 615f2f1 | 2022-07-15 14:00:58 -0500 | [diff] [blame] | 137 | line = f.readline() |
| 138 | origline += line |
| 139 | s2 = line.rstrip() |
Brad Bishop | 64c979e | 2019-11-04 13:55:29 -0500 | [diff] [blame] | 140 | lineno = lineno + 1 |
| 141 | if (not s2 or s2 and s2[0] != "#") and s[0] == "#" : |
Andrew Geissler | 615f2f1 | 2022-07-15 14:00:58 -0500 | [diff] [blame] | 142 | 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 Bishop | 64c979e | 2019-11-04 13:55:29 -0500 | [diff] [blame] | 144 | s = s[:-1] + s2 |
| 145 | # skip comments |
| 146 | if s[0] == '#': |
| 147 | continue |
Andrew Geissler | 517393d | 2023-01-13 08:55:19 -0600 | [diff] [blame] | 148 | feeder(lineno, s, abs_fn, statements, baseconfig=baseconfig) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 149 | |
| 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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 156 | for f in confFilters: |
| 157 | f(fn, data) |
| 158 | |
| 159 | return data |
| 160 | |
Andrew Geissler | 517393d | 2023-01-13 08:55:19 -0600 | [diff] [blame] | 161 | # baseconfig is set for the bblayers/layer.conf cookerdata config parsing |
| 162 | # The function is also used by BBHandler, conffile would be False |
| 163 | def feeder(lineno, s, fn, statements, baseconfig=False, conffile=True): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 164 | 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 Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 185 | 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 Geissler | 517393d | 2023-01-13 08:55:19 -0600 | [diff] [blame] | 195 | m = __addpylib_regexp__.match(s) |
| 196 | if baseconfig and conffile and m: |
| 197 | ast.handlePyLib(statements, fn, lineno, m) |
| 198 | return |
| 199 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 200 | raise ParseError("unparsed line: '%s'" % s, fn, lineno); |
| 201 | |
| 202 | # Add us to the handlers list |
| 203 | from bb.parse import handlers |
| 204 | handlers.append({'supports': supports, 'handle': handle, 'init': init}) |
| 205 | del handlers |