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\-_+.][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__includeall_regexp__ = re.compile( r"include_all\s+(.+)" ) 47__export_regexp__ = re.compile( r"export\s+([a-zA-Z0-9\-_+.${}/~]+)$" ) 48__unset_regexp__ = re.compile( r"unset\s+([a-zA-Z0-9\-_+.${}/~]+)$" ) 49__unset_flag_regexp__ = re.compile( r"unset\s+([a-zA-Z0-9\-_+.${}/~]+)\[([a-zA-Z0-9\-_+.][a-zA-Z0-9\-_+.@]+)\]$" ) 50__addpylib_regexp__ = re.compile(r"addpylib\s+(.+)\s+(.+)" ) 51__addfragments_regexp__ = re.compile(r"addfragments\s+(.+)\s+(.+)\s+(.+)" ) 52 53def init(data): 54 return 55 56def supports(fn, d): 57 return fn[-5:] == ".conf" 58 59def include(parentfn, fns, lineno, data, error_out): 60 """ 61 error_out: A string indicating the verb (e.g. "include", "inherit") to be 62 used in a ParseError that will be raised if the file to be included could 63 not be included. Specify False to avoid raising an error in this case. 64 """ 65 fns = data.expand(fns) 66 parentfn = data.expand(parentfn) 67 68 # "include" or "require" accept zero to n space-separated file names to include. 69 for fn in fns.split(): 70 include_single_file(parentfn, fn, lineno, data, error_out) 71 72def include_single_file(parentfn, fn, lineno, data, error_out): 73 """ 74 Helper function for include() which does not expand or split its parameters. 75 """ 76 if parentfn == fn: # prevent infinite recursion 77 return None 78 79 if not os.path.isabs(fn): 80 dname = os.path.dirname(parentfn) 81 bbpath = "%s:%s" % (dname, data.getVar("BBPATH")) 82 abs_fn, attempts = bb.utils.which(bbpath, fn, history=True) 83 if abs_fn and bb.parse.check_dependency(data, abs_fn): 84 logger.warning("Duplicate inclusion for %s in %s" % (abs_fn, data.getVar('FILE'))) 85 for af in attempts: 86 bb.parse.mark_dependency(data, af) 87 if abs_fn: 88 fn = abs_fn 89 elif bb.parse.check_dependency(data, fn): 90 logger.warning("Duplicate inclusion for %s in %s" % (fn, data.getVar('FILE'))) 91 92 try: 93 bb.parse.handle(fn, data, True) 94 except (IOError, OSError) as exc: 95 if exc.errno == errno.ENOENT: 96 if error_out: 97 raise ParseError("Could not %s file %s" % (error_out, fn), parentfn, lineno) 98 logger.debug2("CONF file '%s' not found", fn) 99 else: 100 if error_out: 101 raise ParseError("Could not %s file %s: %s" % (error_out, fn, exc.strerror), parentfn, lineno) 102 else: 103 raise ParseError("Error parsing %s: %s" % (fn, exc.strerror), parentfn, lineno) 104 105# We have an issue where a UI might want to enforce particular settings such as 106# an empty DISTRO variable. If configuration files do something like assigning 107# a weak default, it turns out to be very difficult to filter out these changes, 108# particularly when the weak default might appear half way though parsing a chain 109# of configuration files. We therefore let the UIs hook into configuration file 110# parsing. This turns out to be a hard problem to solve any other way. 111confFilters = [] 112 113def handle(fn, data, include, baseconfig=False): 114 init(data) 115 116 if include == 0: 117 oldfile = None 118 else: 119 oldfile = data.getVar('FILE', False) 120 121 abs_fn = resolve_file(fn, data) 122 with open(abs_fn, 'r') as f: 123 124 statements = ast.StatementGroup() 125 lineno = 0 126 while True: 127 lineno = lineno + 1 128 s = f.readline() 129 if not s: 130 break 131 origlineno = lineno 132 origline = s 133 w = s.strip() 134 # skip empty lines 135 if not w: 136 continue 137 s = s.rstrip() 138 while s[-1] == '\\': 139 line = f.readline() 140 origline += line 141 s2 = line.rstrip() 142 lineno = lineno + 1 143 if (not s2 or s2 and s2[0] != "#") and s[0] == "#" : 144 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)) 145 146 s = s[:-1] + s2 147 # skip comments 148 if s[0] == '#': 149 continue 150 feeder(lineno, s, abs_fn, statements, baseconfig=baseconfig) 151 152 # DONE WITH PARSING... time to evaluate 153 data.setVar('FILE', abs_fn) 154 statements.eval(data) 155 if oldfile: 156 data.setVar('FILE', oldfile) 157 158 for f in confFilters: 159 f(fn, data) 160 161 return data 162 163# baseconfig is set for the bblayers/layer.conf cookerdata config parsing 164# The function is also used by BBHandler, conffile would be False 165def feeder(lineno, s, fn, statements, baseconfig=False, conffile=True): 166 m = __config_regexp__.match(s) 167 if m: 168 groupd = m.groupdict() 169 ast.handleData(statements, fn, lineno, groupd) 170 return 171 172 m = __include_regexp__.match(s) 173 if m: 174 ast.handleInclude(statements, fn, lineno, m, False) 175 return 176 177 m = __require_regexp__.match(s) 178 if m: 179 ast.handleInclude(statements, fn, lineno, m, True) 180 return 181 182 m = __includeall_regexp__.match(s) 183 if m: 184 ast.handleIncludeAll(statements, fn, lineno, m) 185 return 186 187 m = __export_regexp__.match(s) 188 if m: 189 ast.handleExport(statements, fn, lineno, m) 190 return 191 192 m = __unset_regexp__.match(s) 193 if m: 194 ast.handleUnset(statements, fn, lineno, m) 195 return 196 197 m = __unset_flag_regexp__.match(s) 198 if m: 199 ast.handleUnsetFlag(statements, fn, lineno, m) 200 return 201 202 m = __addpylib_regexp__.match(s) 203 if baseconfig and conffile and m: 204 ast.handlePyLib(statements, fn, lineno, m) 205 return 206 207 m = __addfragments_regexp__.match(s) 208 if m: 209 ast.handleAddFragments(statements, fn, lineno, m) 210 return 211 212 raise ParseError("unparsed line: '%s'" % s, fn, lineno); 213 214# Add us to the handlers list 215from bb.parse import handlers 216handlers.append({'supports': supports, 'handle': handle, 'init': init}) 217del handlers 218