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 | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 24 | (\[(?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 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\-_+.${}/~]+)$" ) |
| 48 | __unset_flag_regexp__ = re.compile( r"unset\s+([a-zA-Z0-9\-_+.${}/~]+)\[([a-zA-Z0-9\-_+.]+)\]$" ) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 49 | |
| 50 | def init(data): |
Andrew Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame] | 51 | return |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 52 | |
| 53 | def supports(fn, d): |
| 54 | return fn[-5:] == ".conf" |
| 55 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 56 | def include(parentfn, fns, lineno, data, error_out): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 57 | """ |
| 58 | error_out: A string indicating the verb (e.g. "include", "inherit") to be |
| 59 | used in a ParseError that will be raised if the file to be included could |
| 60 | not be included. Specify False to avoid raising an error in this case. |
| 61 | """ |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 62 | fns = data.expand(fns) |
| 63 | parentfn = data.expand(parentfn) |
| 64 | |
| 65 | # "include" or "require" accept zero to n space-separated file names to include. |
| 66 | for fn in fns.split(): |
| 67 | include_single_file(parentfn, fn, lineno, data, error_out) |
| 68 | |
| 69 | def include_single_file(parentfn, fn, lineno, data, error_out): |
| 70 | """ |
| 71 | Helper function for include() which does not expand or split its parameters. |
| 72 | """ |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 73 | if parentfn == fn: # prevent infinite recursion |
| 74 | return None |
| 75 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 76 | if not os.path.isabs(fn): |
| 77 | dname = os.path.dirname(parentfn) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 78 | bbpath = "%s:%s" % (dname, data.getVar("BBPATH")) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 79 | abs_fn, attempts = bb.utils.which(bbpath, fn, history=True) |
| 80 | if abs_fn and bb.parse.check_dependency(data, abs_fn): |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 81 | 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] | 82 | for af in attempts: |
| 83 | bb.parse.mark_dependency(data, af) |
| 84 | if abs_fn: |
| 85 | fn = abs_fn |
| 86 | elif bb.parse.check_dependency(data, fn): |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 87 | logger.warning("Duplicate inclusion for %s in %s" % (fn, data.getVar('FILE'))) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 88 | |
| 89 | try: |
| 90 | bb.parse.handle(fn, data, True) |
| 91 | except (IOError, OSError) as exc: |
| 92 | if exc.errno == errno.ENOENT: |
| 93 | if error_out: |
| 94 | raise ParseError("Could not %s file %s" % (error_out, fn), parentfn, lineno) |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 95 | logger.debug2("CONF file '%s' not found", fn) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 96 | else: |
| 97 | if error_out: |
| 98 | raise ParseError("Could not %s file %s: %s" % (error_out, fn, exc.strerror), parentfn, lineno) |
| 99 | else: |
| 100 | raise ParseError("Error parsing %s: %s" % (fn, exc.strerror), parentfn, lineno) |
| 101 | |
| 102 | # We have an issue where a UI might want to enforce particular settings such as |
| 103 | # an empty DISTRO variable. If configuration files do something like assigning |
| 104 | # a weak default, it turns out to be very difficult to filter out these changes, |
| 105 | # particularly when the weak default might appear half way though parsing a chain |
| 106 | # of configuration files. We therefore let the UIs hook into configuration file |
| 107 | # parsing. This turns out to be a hard problem to solve any other way. |
| 108 | confFilters = [] |
| 109 | |
| 110 | def handle(fn, data, include): |
| 111 | init(data) |
| 112 | |
| 113 | if include == 0: |
| 114 | oldfile = None |
| 115 | else: |
| 116 | oldfile = data.getVar('FILE', False) |
| 117 | |
| 118 | abs_fn = resolve_file(fn, data) |
Brad Bishop | 64c979e | 2019-11-04 13:55:29 -0500 | [diff] [blame] | 119 | with open(abs_fn, 'r') as f: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 120 | |
Brad Bishop | 64c979e | 2019-11-04 13:55:29 -0500 | [diff] [blame] | 121 | statements = ast.StatementGroup() |
| 122 | lineno = 0 |
| 123 | while True: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 124 | lineno = lineno + 1 |
Brad Bishop | 64c979e | 2019-11-04 13:55:29 -0500 | [diff] [blame] | 125 | s = f.readline() |
| 126 | if not s: |
| 127 | break |
Andrew Geissler | 615f2f1 | 2022-07-15 14:00:58 -0500 | [diff] [blame^] | 128 | origlineno = lineno |
| 129 | origline = s |
Brad Bishop | 64c979e | 2019-11-04 13:55:29 -0500 | [diff] [blame] | 130 | w = s.strip() |
| 131 | # skip empty lines |
| 132 | if not w: |
| 133 | continue |
| 134 | s = s.rstrip() |
| 135 | while s[-1] == '\\': |
Andrew Geissler | 615f2f1 | 2022-07-15 14:00:58 -0500 | [diff] [blame^] | 136 | line = f.readline() |
| 137 | origline += line |
| 138 | s2 = line.rstrip() |
Brad Bishop | 64c979e | 2019-11-04 13:55:29 -0500 | [diff] [blame] | 139 | lineno = lineno + 1 |
| 140 | if (not s2 or s2 and s2[0] != "#") and s[0] == "#" : |
Andrew Geissler | 615f2f1 | 2022-07-15 14:00:58 -0500 | [diff] [blame^] | 141 | 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)) |
| 142 | |
Brad Bishop | 64c979e | 2019-11-04 13:55:29 -0500 | [diff] [blame] | 143 | s = s[:-1] + s2 |
| 144 | # skip comments |
| 145 | if s[0] == '#': |
| 146 | continue |
| 147 | feeder(lineno, s, abs_fn, statements) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 148 | |
| 149 | # DONE WITH PARSING... time to evaluate |
| 150 | data.setVar('FILE', abs_fn) |
| 151 | statements.eval(data) |
| 152 | if oldfile: |
| 153 | data.setVar('FILE', oldfile) |
| 154 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 155 | for f in confFilters: |
| 156 | f(fn, data) |
| 157 | |
| 158 | return data |
| 159 | |
| 160 | def 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 Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 182 | 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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 192 | raise ParseError("unparsed line: '%s'" % s, fn, lineno); |
| 193 | |
| 194 | # Add us to the handlers list |
| 195 | from bb.parse import handlers |
| 196 | handlers.append({'supports': supports, 'handle': handle, 'init': init}) |
| 197 | del handlers |