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 w = s.strip() 129 # skip empty lines 130 if not w: 131 continue 132 s = s.rstrip() 133 while s[-1] == '\\': 134 s2 = f.readline().rstrip() 135 lineno = lineno + 1 136 if (not s2 or s2 and s2[0] != "#") and s[0] == "#" : 137 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)) 138 s = s[:-1] + s2 139 # skip comments 140 if s[0] == '#': 141 continue 142 feeder(lineno, s, abs_fn, statements) 143 144 # DONE WITH PARSING... time to evaluate 145 data.setVar('FILE', abs_fn) 146 statements.eval(data) 147 if oldfile: 148 data.setVar('FILE', oldfile) 149 150 f.close() 151 152 for f in confFilters: 153 f(fn, data) 154 155 return data 156 157def feeder(lineno, s, fn, statements): 158 m = __config_regexp__.match(s) 159 if m: 160 groupd = m.groupdict() 161 ast.handleData(statements, fn, lineno, groupd) 162 return 163 164 m = __include_regexp__.match(s) 165 if m: 166 ast.handleInclude(statements, fn, lineno, m, False) 167 return 168 169 m = __require_regexp__.match(s) 170 if m: 171 ast.handleInclude(statements, fn, lineno, m, True) 172 return 173 174 m = __export_regexp__.match(s) 175 if m: 176 ast.handleExport(statements, fn, lineno, m) 177 return 178 179 m = __unset_regexp__.match(s) 180 if m: 181 ast.handleUnset(statements, fn, lineno, m) 182 return 183 184 m = __unset_flag_regexp__.match(s) 185 if m: 186 ast.handleUnsetFlag(statements, fn, lineno, m) 187 return 188 189 raise ParseError("unparsed line: '%s'" % s, fn, lineno); 190 191# Add us to the handlers list 192from bb.parse import handlers 193handlers.append({'supports': supports, 'handle': handle, 'init': init}) 194del handlers 195