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__addpylib_regexp__ = re.compile(r"addpylib\s+(.+)\s+(.+)" ) 50 51def init(data): 52 return 53 54def supports(fn, d): 55 return fn[-5:] == ".conf" 56 57def include(parentfn, fns, lineno, data, error_out): 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 """ 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 70def include_single_file(parentfn, fn, lineno, data, error_out): 71 """ 72 Helper function for include() which does not expand or split its parameters. 73 """ 74 if parentfn == fn: # prevent infinite recursion 75 return None 76 77 if not os.path.isabs(fn): 78 dname = os.path.dirname(parentfn) 79 bbpath = "%s:%s" % (dname, data.getVar("BBPATH")) 80 abs_fn, attempts = bb.utils.which(bbpath, fn, history=True) 81 if abs_fn and bb.parse.check_dependency(data, abs_fn): 82 logger.warning("Duplicate inclusion for %s in %s" % (abs_fn, data.getVar('FILE'))) 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): 88 logger.warning("Duplicate inclusion for %s in %s" % (fn, data.getVar('FILE'))) 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) 96 logger.debug2("CONF file '%s' not found", fn) 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, 106# particularly when the weak default might appear half way though parsing a chain 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. 109confFilters = [] 110 111def handle(fn, data, include, baseconfig=False): 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) 120 with open(abs_fn, 'r') as f: 121 122 statements = ast.StatementGroup() 123 lineno = 0 124 while True: 125 lineno = lineno + 1 126 s = f.readline() 127 if not s: 128 break 129 origlineno = lineno 130 origline = s 131 w = s.strip() 132 # skip empty lines 133 if not w: 134 continue 135 s = s.rstrip() 136 while s[-1] == '\\': 137 line = f.readline() 138 origline += line 139 s2 = line.rstrip() 140 lineno = lineno + 1 141 if (not s2 or s2 and s2[0] != "#") and s[0] == "#" : 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 144 s = s[:-1] + s2 145 # skip comments 146 if s[0] == '#': 147 continue 148 feeder(lineno, s, abs_fn, statements, baseconfig=baseconfig) 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 156 for f in confFilters: 157 f(fn, data) 158 159 return data 160 161# baseconfig is set for the bblayers/layer.conf cookerdata config parsing 162# The function is also used by BBHandler, conffile would be False 163def feeder(lineno, s, fn, statements, baseconfig=False, conffile=True): 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 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 195 m = __addpylib_regexp__.match(s) 196 if baseconfig and conffile and m: 197 ast.handlePyLib(statements, fn, lineno, m) 198 return 199 200 raise ParseError("unparsed line: '%s'" % s, fn, lineno); 201 202# Add us to the handlers list 203from bb.parse import handlers 204handlers.append({'supports': supports, 'handle': handle, 'init': init}) 205del handlers 206