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