1#!/usr/bin/env python 2""" 3 class for handling configuration data files 4 5 Reads a .conf file and obtains its metadata 6 7""" 8 9# Copyright (C) 2003, 2004 Chris Larson 10# Copyright (C) 2003, 2004 Phil Blundell 11# 12# SPDX-License-Identifier: GPL-2.0-only 13# 14 15import errno 16import re 17import os 18import bb.utils 19from bb.parse import ParseError, resolve_file, ast, logger, handle 20 21__config_regexp__ = re.compile( r""" 22 ^ 23 (?P<exp>export\s+)? 24 (?P<var>[a-zA-Z0-9\-_+.${}/~]+?) 25 (\[(?P<flag>[a-zA-Z0-9\-_+.]+)\])? 26 27 \s* ( 28 (?P<colon>:=) | 29 (?P<lazyques>\?\?=) | 30 (?P<ques>\?=) | 31 (?P<append>\+=) | 32 (?P<prepend>=\+) | 33 (?P<predot>=\.) | 34 (?P<postdot>\.=) | 35 = 36 ) \s* 37 38 (?!'[^']*'[^']*'$) 39 (?!\"[^\"]*\"[^\"]*\"$) 40 (?P<apo>['\"]) 41 (?P<value>.*) 42 (?P=apo) 43 $ 44 """, re.X) 45__include_regexp__ = re.compile( r"include\s+(.+)" ) 46__require_regexp__ = re.compile( r"require\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\-_+.]+)\]$" ) 50 51def init(data): 52 topdir = data.getVar('TOPDIR', False) 53 if not topdir: 54 data.setVar('TOPDIR', os.getcwd()) 55 56 57def supports(fn, d): 58 return fn[-5:] == ".conf" 59 60def include(parentfn, fns, lineno, data, error_out): 61 """ 62 error_out: A string indicating the verb (e.g. "include", "inherit") to be 63 used in a ParseError that will be raised if the file to be included could 64 not be included. Specify False to avoid raising an error in this case. 65 """ 66 fns = data.expand(fns) 67 parentfn = data.expand(parentfn) 68 69 # "include" or "require" accept zero to n space-separated file names to include. 70 for fn in fns.split(): 71 include_single_file(parentfn, fn, lineno, data, error_out) 72 73def include_single_file(parentfn, fn, lineno, data, error_out): 74 """ 75 Helper function for include() which does not expand or split its parameters. 76 """ 77 if parentfn == fn: # prevent infinite recursion 78 return None 79 80 if not os.path.isabs(fn): 81 dname = os.path.dirname(parentfn) 82 bbpath = "%s:%s" % (dname, data.getVar("BBPATH")) 83 abs_fn, attempts = bb.utils.which(bbpath, fn, history=True) 84 if abs_fn and bb.parse.check_dependency(data, abs_fn): 85 logger.warning("Duplicate inclusion for %s in %s" % (abs_fn, data.getVar('FILE'))) 86 for af in attempts: 87 bb.parse.mark_dependency(data, af) 88 if abs_fn: 89 fn = abs_fn 90 elif bb.parse.check_dependency(data, fn): 91 logger.warning("Duplicate inclusion for %s in %s" % (fn, data.getVar('FILE'))) 92 93 try: 94 bb.parse.handle(fn, data, True) 95 except (IOError, OSError) as exc: 96 if exc.errno == errno.ENOENT: 97 if error_out: 98 raise ParseError("Could not %s file %s" % (error_out, fn), parentfn, lineno) 99 logger.debug(2, "CONF file '%s' not found", fn) 100 else: 101 if error_out: 102 raise ParseError("Could not %s file %s: %s" % (error_out, fn, exc.strerror), parentfn, lineno) 103 else: 104 raise ParseError("Error parsing %s: %s" % (fn, exc.strerror), parentfn, lineno) 105 106# We have an issue where a UI might want to enforce particular settings such as 107# an empty DISTRO variable. If configuration files do something like assigning 108# a weak default, it turns out to be very difficult to filter out these changes, 109# particularly when the weak default might appear half way though parsing a chain 110# of configuration files. We therefore let the UIs hook into configuration file 111# parsing. This turns out to be a hard problem to solve any other way. 112confFilters = [] 113 114def handle(fn, data, include): 115 init(data) 116 117 if include == 0: 118 oldfile = None 119 else: 120 oldfile = data.getVar('FILE', False) 121 122 abs_fn = resolve_file(fn, data) 123 f = open(abs_fn, 'r') 124 125 statements = ast.StatementGroup() 126 lineno = 0 127 while True: 128 lineno = lineno + 1 129 s = f.readline() 130 if not s: 131 break 132 w = s.strip() 133 # skip empty lines 134 if not w: 135 continue 136 s = s.rstrip() 137 while s[-1] == '\\': 138 s2 = f.readline().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 on line %s of file %s (%s).\nPlease clarify whether this is all a comment or should be parsed." % (lineno, fn, s)) 142 s = s[:-1] + s2 143 # skip comments 144 if s[0] == '#': 145 continue 146 feeder(lineno, s, abs_fn, statements) 147 148 # DONE WITH PARSING... time to evaluate 149 data.setVar('FILE', abs_fn) 150 statements.eval(data) 151 if oldfile: 152 data.setVar('FILE', oldfile) 153 154 f.close() 155 156 for f in confFilters: 157 f(fn, data) 158 159 return data 160 161def feeder(lineno, s, fn, statements): 162 m = __config_regexp__.match(s) 163 if m: 164 groupd = m.groupdict() 165 ast.handleData(statements, fn, lineno, groupd) 166 return 167 168 m = __include_regexp__.match(s) 169 if m: 170 ast.handleInclude(statements, fn, lineno, m, False) 171 return 172 173 m = __require_regexp__.match(s) 174 if m: 175 ast.handleInclude(statements, fn, lineno, m, True) 176 return 177 178 m = __export_regexp__.match(s) 179 if m: 180 ast.handleExport(statements, fn, lineno, m) 181 return 182 183 m = __unset_regexp__.match(s) 184 if m: 185 ast.handleUnset(statements, fn, lineno, m) 186 return 187 188 m = __unset_flag_regexp__.match(s) 189 if m: 190 ast.handleUnsetFlag(statements, fn, lineno, m) 191 return 192 193 raise ParseError("unparsed line: '%s'" % s, fn, lineno); 194 195# Add us to the handlers list 196from bb.parse import handlers 197handlers.append({'supports': supports, 'handle': handle, 'init': init}) 198del handlers 199