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# 11# SPDX-License-Identifier: GPL-2.0-only 12# 13 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 ^ 22 (?P<exp>export\s+)? 23 (?P<var>[a-zA-Z0-9\-_+.${}/~:]+?) 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+(.+)" ) 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\-_+.]+)\]$" ) 49 50def init(data): 51 return 52 53def supports(fn, d): 54 return fn[-5:] == ".conf" 55 56def include(parentfn, fns, lineno, data, error_out): 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 """ 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 69def include_single_file(parentfn, fn, lineno, data, error_out): 70 """ 71 Helper function for include() which does not expand or split its parameters. 72 """ 73 if parentfn == fn: # prevent infinite recursion 74 return None 75 76 if not os.path.isabs(fn): 77 dname = os.path.dirname(parentfn) 78 bbpath = "%s:%s" % (dname, data.getVar("BBPATH")) 79 abs_fn, attempts = bb.utils.which(bbpath, fn, history=True) 80 if abs_fn and bb.parse.check_dependency(data, abs_fn): 81 logger.warning("Duplicate inclusion for %s in %s" % (abs_fn, data.getVar('FILE'))) 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): 87 logger.warning("Duplicate inclusion for %s in %s" % (fn, data.getVar('FILE'))) 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) 95 logger.debug2("CONF file '%s' not found", fn) 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. 108confFilters = [] 109 110def 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) 119 with open(abs_fn, 'r') as f: 120 121 statements = ast.StatementGroup() 122 lineno = 0 123 while True: 124 lineno = lineno + 1 125 s = f.readline() 126 if not s: 127 break 128 origlineno = lineno 129 origline = s 130 w = s.strip() 131 # skip empty lines 132 if not w: 133 continue 134 s = s.rstrip() 135 while s[-1] == '\\': 136 line = f.readline() 137 origline += line 138 s2 = line.rstrip() 139 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 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 143 s = s[:-1] + s2 144 # skip comments 145 if s[0] == '#': 146 continue 147 feeder(lineno, s, abs_fn, statements) 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 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 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 192 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 198