blob: 5415f2fccf149aae17342fc38a3a7fc3019cd44c [file] [log] [blame]
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001"""
2BitBake Smart Dictionary Implementation
3
4Functions for interacting with the data structure used by the
5BitBake build tools.
6
7"""
8
9# Copyright (C) 2003, 2004 Chris Larson
10# Copyright (C) 2004, 2005 Seb Frankengul
11# Copyright (C) 2005, 2006 Holger Hans Peter Freyther
12# Copyright (C) 2005 Uli Luckas
13# Copyright (C) 2005 ROAD GmbH
14#
Brad Bishopc342db32019-05-15 21:57:59 -040015# SPDX-License-Identifier: GPL-2.0-only
Patrick Williamsc124f4f2015-09-15 14:41:29 -050016#
Patrick Williamsc124f4f2015-09-15 14:41:29 -050017# Based on functions from the base bb module, Copyright 2003 Holger Schurig
18
19import copy, re, sys, traceback
Andrew Geissler5199d832021-09-24 16:47:35 -050020from collections.abc import MutableMapping
Patrick Williamsc124f4f2015-09-15 14:41:29 -050021import logging
22import hashlib
23import bb, bb.codeparser
24from bb import utils
25from bb.COW import COWDictBase
26
27logger = logging.getLogger("BitBake.Data")
28
Patrick Williams213cb262021-08-07 19:21:33 -050029__setvar_keyword__ = [":append", ":prepend", ":remove"]
30__setvar_regexp__ = re.compile(r'(?P<base>.*?)(?P<keyword>:append|:prepend|:remove)(:(?P<add>[^A-Z]*))?$')
31__expand_var_regexp__ = re.compile(r"\${[a-zA-Z0-9\-_+./~:]+?}")
Patrick Williams7784c422022-11-17 07:29:11 -060032__expand_python_regexp__ = re.compile(r"\${@(?:{.*?}|.)+?}")
Brad Bishop19323692019-04-05 15:28:33 -040033__whitespace_split__ = re.compile(r'(\s)')
34__override_regexp__ = re.compile(r'[a-z0-9]+')
Patrick Williamsc124f4f2015-09-15 14:41:29 -050035
Andrew Geissler7e0e3c02022-02-25 20:34:39 +000036bitbake_renamed_vars = {
37 "BB_ENV_WHITELIST": "BB_ENV_PASSTHROUGH",
38 "BB_ENV_EXTRAWHITE": "BB_ENV_PASSTHROUGH_ADDITIONS",
39 "BB_HASHBASE_WHITELIST": "BB_BASEHASH_IGNORE_VARS",
40 "BB_HASHCONFIG_WHITELIST": "BB_HASHCONFIG_IGNORE_VARS",
41 "BB_HASHTASK_WHITELIST": "BB_TASKHASH_IGNORE_TASKS",
42 "BB_SETSCENE_ENFORCE_WHITELIST": "BB_SETSCENE_ENFORCE_IGNORE_TASKS",
43 "MULTI_PROVIDER_WHITELIST": "BB_MULTI_PROVIDER_ALLOWED",
44 "BB_STAMP_WHITELIST": "is a deprecated variable and support has been removed",
45 "BB_STAMP_POLICY": "is a deprecated variable and support has been removed",
46}
47
Patrick Williamsc124f4f2015-09-15 14:41:29 -050048def infer_caller_details(loginfo, parent = False, varval = True):
49 """Save the caller the trouble of specifying everything."""
50 # Save effort.
51 if 'ignore' in loginfo and loginfo['ignore']:
52 return
53 # If nothing was provided, mark this as possibly unneeded.
54 if not loginfo:
55 loginfo['ignore'] = True
56 return
57 # Infer caller's likely values for variable (var) and value (value),
58 # to reduce clutter in the rest of the code.
59 above = None
60 def set_above():
61 try:
62 raise Exception
63 except Exception:
64 tb = sys.exc_info()[2]
65 if parent:
66 return tb.tb_frame.f_back.f_back.f_back
67 else:
68 return tb.tb_frame.f_back.f_back
69
70 if varval and ('variable' not in loginfo or 'detail' not in loginfo):
71 if not above:
72 above = set_above()
73 lcls = above.f_locals.items()
74 for k, v in lcls:
75 if k == 'value' and 'detail' not in loginfo:
76 loginfo['detail'] = v
77 if k == 'var' and 'variable' not in loginfo:
78 loginfo['variable'] = v
79 # Infer file/line/function from traceback
80 # Don't use traceback.extract_stack() since it fills the line contents which
81 # we don't need and that hits stat syscalls
82 if 'file' not in loginfo:
83 if not above:
84 above = set_above()
85 f = above.f_back
86 line = f.f_lineno
87 file = f.f_code.co_filename
88 func = f.f_code.co_name
89 loginfo['file'] = file
90 loginfo['line'] = line
91 if func not in loginfo:
92 loginfo['func'] = func
93
94class VariableParse:
95 def __init__(self, varname, d, val = None):
96 self.varname = varname
97 self.d = d
98 self.value = val
99
100 self.references = set()
101 self.execs = set()
102 self.contains = {}
103
104 def var_sub(self, match):
105 key = match.group()[2:-1]
106 if self.varname and key:
107 if self.varname == key:
108 raise Exception("variable %s references itself!" % self.varname)
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800109 var = self.d.getVarFlag(key, "_content")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500110 self.references.add(key)
111 if var is not None:
112 return var
113 else:
114 return match.group()
115
116 def python_sub(self, match):
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500117 if isinstance(match, str):
118 code = match
119 else:
120 code = match.group()[3:-1]
121
Patrick Williams7784c422022-11-17 07:29:11 -0600122 # Do not run code that contains one or more unexpanded variables
123 # instead return the code with the characters we removed put back
124 if __expand_var_regexp__.findall(code):
125 return "${@" + code + "}"
126
Brad Bishop19323692019-04-05 15:28:33 -0400127 if self.varname:
128 varname = 'Var <%s>' % self.varname
129 else:
130 varname = '<expansion>'
131 codeobj = compile(code.strip(), varname, "eval")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500132
133 parser = bb.codeparser.PythonParser(self.varname, logger)
134 parser.parse_python(code)
135 if self.varname:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500136 vardeps = self.d.getVarFlag(self.varname, "vardeps")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500137 if vardeps is None:
138 parser.log.flush()
139 else:
140 parser.log.flush()
141 self.references |= parser.references
142 self.execs |= parser.execs
143
144 for k in parser.contains:
145 if k not in self.contains:
146 self.contains[k] = parser.contains[k].copy()
147 else:
148 self.contains[k].update(parser.contains[k])
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600149 value = utils.better_eval(codeobj, DataContext(self.d), {'d' : self.d})
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500150 return str(value)
151
152
153class DataContext(dict):
154 def __init__(self, metadata, **kwargs):
155 self.metadata = metadata
156 dict.__init__(self, **kwargs)
157 self['d'] = metadata
158
159 def __missing__(self, key):
Andrew Geissler9aee5002022-03-30 16:27:02 +0000160 # Skip commonly accessed invalid variables
161 if key in ['bb', 'oe', 'int', 'bool', 'time', 'str', 'os']:
162 raise KeyError(key)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500163 value = self.metadata.getVar(key)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500164 if value is None or self.metadata.getVarFlag(key, 'func', False):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500165 raise KeyError(key)
166 else:
167 return value
168
169class ExpansionError(Exception):
170 def __init__(self, varname, expression, exception):
171 self.expression = expression
172 self.variablename = varname
173 self.exception = exception
Andrew Geissler5199d832021-09-24 16:47:35 -0500174 self.varlist = [varname or expression or ""]
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500175 if varname:
176 if expression:
177 self.msg = "Failure expanding variable %s, expression was %s which triggered exception %s: %s" % (varname, expression, type(exception).__name__, exception)
178 else:
179 self.msg = "Failure expanding variable %s: %s: %s" % (varname, type(exception).__name__, exception)
180 else:
181 self.msg = "Failure expanding expression %s which triggered exception %s: %s" % (expression, type(exception).__name__, exception)
182 Exception.__init__(self, self.msg)
183 self.args = (varname, expression, exception)
Andrew Geissler5199d832021-09-24 16:47:35 -0500184
185 def addVar(self, varname):
186 if varname:
187 self.varlist.append(varname)
188
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500189 def __str__(self):
Andrew Geissler5199d832021-09-24 16:47:35 -0500190 chain = "\nThe variable dependency chain for the failure is: " + " -> ".join(self.varlist)
191 return self.msg + chain
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500192
193class IncludeHistory(object):
194 def __init__(self, parent = None, filename = '[TOP LEVEL]'):
195 self.parent = parent
196 self.filename = filename
197 self.children = []
198 self.current = self
199
200 def copy(self):
201 new = IncludeHistory(self.parent, self.filename)
202 for c in self.children:
203 new.children.append(c)
204 return new
205
206 def include(self, filename):
207 newfile = IncludeHistory(self.current, filename)
208 self.current.children.append(newfile)
209 self.current = newfile
210 return self
211
212 def __enter__(self):
213 pass
214
215 def __exit__(self, a, b, c):
216 if self.current.parent:
217 self.current = self.current.parent
218 else:
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500219 bb.warn("Include log: Tried to finish '%s' at top level." % self.filename)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500220 return False
221
222 def emit(self, o, level = 0):
223 """Emit an include history file, and its children."""
224 if level:
225 spaces = " " * (level - 1)
226 o.write("# %s%s" % (spaces, self.filename))
227 if len(self.children) > 0:
228 o.write(" includes:")
229 else:
230 o.write("#\n# INCLUDE HISTORY:\n#")
231 level = level + 1
232 for child in self.children:
233 o.write("\n")
234 child.emit(o, level)
235
236class VariableHistory(object):
237 def __init__(self, dataroot):
238 self.dataroot = dataroot
239 self.variables = COWDictBase.copy()
240
241 def copy(self):
242 new = VariableHistory(self.dataroot)
243 new.variables = self.variables.copy()
244 return new
245
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500246 def __getstate__(self):
247 vardict = {}
248 for k, v in self.variables.iteritems():
249 vardict[k] = v
250 return {'dataroot': self.dataroot,
251 'variables': vardict}
252
253 def __setstate__(self, state):
254 self.dataroot = state['dataroot']
255 self.variables = COWDictBase.copy()
256 for k, v in state['variables'].items():
257 self.variables[k] = v
258
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500259 def record(self, *kwonly, **loginfo):
260 if not self.dataroot._tracking:
261 return
262 if len(kwonly) > 0:
263 raise TypeError
264 infer_caller_details(loginfo, parent = True)
265 if 'ignore' in loginfo and loginfo['ignore']:
266 return
267 if 'op' not in loginfo or not loginfo['op']:
268 loginfo['op'] = 'set'
269 if 'detail' in loginfo:
270 loginfo['detail'] = str(loginfo['detail'])
271 if 'variable' not in loginfo or 'file' not in loginfo:
272 raise ValueError("record() missing variable or file.")
273 var = loginfo['variable']
274
275 if var not in self.variables:
276 self.variables[var] = []
277 if not isinstance(self.variables[var], list):
278 return
279 if 'nodups' in loginfo and loginfo in self.variables[var]:
280 return
281 self.variables[var].append(loginfo.copy())
282
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800283 def rename_variable_hist(self, oldvar, newvar):
284 if not self.dataroot._tracking:
285 return
286 if oldvar not in self.variables:
287 return
288 if newvar not in self.variables:
289 self.variables[newvar] = []
290 for i in self.variables[oldvar]:
291 self.variables[newvar].append(i.copy())
292
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500293 def variable(self, var):
Andrew Geissler82c905d2020-04-13 13:39:40 -0500294 varhistory = []
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500295 if var in self.variables:
296 varhistory.extend(self.variables[var])
297 return varhistory
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500298
299 def emit(self, var, oval, val, o, d):
300 history = self.variable(var)
301
302 # Append override history
303 if var in d.overridedata:
304 for (r, override) in d.overridedata[var]:
305 for event in self.variable(r):
306 loginfo = event.copy()
Patrick Williams213cb262021-08-07 19:21:33 -0500307 if 'flag' in loginfo and not loginfo['flag'].startswith(("_", ":")):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500308 continue
309 loginfo['variable'] = var
310 loginfo['op'] = 'override[%s]:%s' % (override, loginfo['op'])
311 history.append(loginfo)
312
313 commentVal = re.sub('\n', '\n#', str(oval))
314 if history:
315 if len(history) == 1:
316 o.write("#\n# $%s\n" % var)
317 else:
318 o.write("#\n# $%s [%d operations]\n" % (var, len(history)))
319 for event in history:
320 # o.write("# %s\n" % str(event))
321 if 'func' in event:
322 # If we have a function listed, this is internal
323 # code, not an operation in a config file, and the
324 # full path is distracting.
325 event['file'] = re.sub('.*/', '', event['file'])
326 display_func = ' [%s]' % event['func']
327 else:
328 display_func = ''
329 if 'flag' in event:
330 flag = '[%s] ' % (event['flag'])
331 else:
332 flag = ''
333 o.write("# %s %s:%s%s\n# %s\"%s\"\n" % (event['op'], event['file'], event['line'], display_func, flag, re.sub('\n', '\n# ', event['detail'])))
334 if len(history) > 1:
335 o.write("# pre-expansion value:\n")
336 o.write('# "%s"\n' % (commentVal))
337 else:
338 o.write("#\n# $%s\n# [no history recorded]\n#\n" % var)
339 o.write('# "%s"\n' % (commentVal))
340
341 def get_variable_files(self, var):
342 """Get the files where operations are made on a variable"""
343 var_history = self.variable(var)
344 files = []
345 for event in var_history:
346 files.append(event['file'])
347 return files
348
349 def get_variable_lines(self, var, f):
350 """Get the line where a operation is made on a variable in file f"""
351 var_history = self.variable(var)
352 lines = []
353 for event in var_history:
354 if f== event['file']:
355 line = event['line']
356 lines.append(line)
357 return lines
358
Andrew Geissler7e0e3c02022-02-25 20:34:39 +0000359 def get_variable_refs(self, var):
360 """Return a dict of file/line references"""
361 var_history = self.variable(var)
362 refs = {}
363 for event in var_history:
364 if event['file'] not in refs:
365 refs[event['file']] = []
366 refs[event['file']].append(event['line'])
367 return refs
368
Andrew Geissler82c905d2020-04-13 13:39:40 -0500369 def get_variable_items_files(self, var):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500370 """
371 Use variable history to map items added to a list variable and
372 the files in which they were added.
373 """
Andrew Geissler82c905d2020-04-13 13:39:40 -0500374 d = self.dataroot
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500375 history = self.variable(var)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500376 finalitems = (d.getVar(var) or '').split()
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500377 filemap = {}
378 isset = False
379 for event in history:
380 if 'flag' in event:
381 continue
Patrick Williams213cb262021-08-07 19:21:33 -0500382 if event['op'] == ':remove':
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500383 continue
384 if isset and event['op'] == 'set?':
385 continue
386 isset = True
387 items = d.expand(event['detail']).split()
388 for item in items:
389 # This is a little crude but is belt-and-braces to avoid us
390 # having to handle every possible operation type specifically
391 if item in finalitems and not item in filemap:
392 filemap[item] = event['file']
393 return filemap
394
395 def del_var_history(self, var, f=None, line=None):
396 """If file f and line are not given, the entire history of var is deleted"""
397 if var in self.variables:
398 if f and line:
399 self.variables[var] = [ x for x in self.variables[var] if x['file']!=f and x['line']!=line]
400 else:
401 self.variables[var] = []
402
Andrew Geissler7e0e3c02022-02-25 20:34:39 +0000403def _print_rename_error(var, loginfo, renamedvars, fullvar=None):
404 info = ""
405 if "file" in loginfo:
406 info = " file: %s" % loginfo["file"]
407 if "line" in loginfo:
408 info += " line: %s" % loginfo["line"]
409 if fullvar and fullvar != var:
410 info += " referenced as: %s" % fullvar
411 if info:
412 info = " (%s)" % info.strip()
413 renameinfo = renamedvars[var]
414 if " " in renameinfo:
415 # A space signals a string to display instead of a rename
416 bb.erroronce('Variable %s %s%s' % (var, renameinfo, info))
417 else:
418 bb.erroronce('Variable %s has been renamed to %s%s' % (var, renameinfo, info))
419
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500420class DataSmart(MutableMapping):
421 def __init__(self):
422 self.dict = {}
423
424 self.inchistory = IncludeHistory()
425 self.varhistory = VariableHistory(self)
426 self._tracking = False
Andrew Geissler7e0e3c02022-02-25 20:34:39 +0000427 self._var_renames = {}
428 self._var_renames.update(bitbake_renamed_vars)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500429
430 self.expand_cache = {}
431
432 # cookie monster tribute
433 # Need to be careful about writes to overridedata as
434 # its only a shallow copy, could influence other data store
435 # copies!
436 self.overridedata = {}
437 self.overrides = None
438 self.overridevars = set(["OVERRIDES", "FILE"])
439 self.inoverride = False
440
441 def enableTracking(self):
442 self._tracking = True
443
444 def disableTracking(self):
445 self._tracking = False
446
447 def expandWithRefs(self, s, varname):
448
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600449 if not isinstance(s, str): # sanity check
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500450 return VariableParse(varname, self, s)
451
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500452 varparse = VariableParse(varname, self)
453
454 while s.find('${') != -1:
455 olds = s
456 try:
457 s = __expand_var_regexp__.sub(varparse.var_sub, s)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500458 try:
459 s = __expand_python_regexp__.sub(varparse.python_sub, s)
460 except SyntaxError as e:
461 # Likely unmatched brackets, just don't expand the expression
Andrew Geissler5199d832021-09-24 16:47:35 -0500462 if e.msg != "EOL while scanning string literal" and not e.msg.startswith("unterminated string literal"):
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500463 raise
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500464 if s == olds:
465 break
Andrew Geissler5199d832021-09-24 16:47:35 -0500466 except ExpansionError as e:
467 e.addVar(varname)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500468 raise
469 except bb.parse.SkipRecipe:
470 raise
Andrew Geissler5199d832021-09-24 16:47:35 -0500471 except bb.BBHandledException:
472 raise
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500473 except Exception as exc:
Brad Bishop19323692019-04-05 15:28:33 -0400474 tb = sys.exc_info()[2]
475 raise ExpansionError(varname, s, exc).with_traceback(tb) from exc
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500476
477 varparse.value = s
478
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500479 return varparse
480
481 def expand(self, s, varname = None):
482 return self.expandWithRefs(s, varname).value
483
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500484 def need_overrides(self):
Patrick Williamsd7e96312015-09-22 08:09:05 -0500485 if self.overrides is not None:
486 return
487 if self.inoverride:
488 return
489 for count in range(5):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500490 self.inoverride = True
491 # Can end up here recursively so setup dummy values
492 self.overrides = []
493 self.overridesset = set()
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500494 self.overrides = (self.getVar("OVERRIDES") or "").split(":") or []
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500495 self.overridesset = set(self.overrides)
496 self.inoverride = False
497 self.expand_cache = {}
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500498 newoverrides = (self.getVar("OVERRIDES") or "").split(":") or []
Patrick Williamsd7e96312015-09-22 08:09:05 -0500499 if newoverrides == self.overrides:
500 break
501 self.overrides = newoverrides
502 self.overridesset = set(self.overrides)
503 else:
504 bb.fatal("Overrides could not be expanded into a stable state after 5 iterations, overrides must be being referenced by other overridden variables in some recursive fashion. Please provide your configuration to bitbake-devel so we can laugh, er, I mean try and understand how to make it work.")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500505
506 def initVar(self, var):
507 self.expand_cache = {}
508 if not var in self.dict:
509 self.dict[var] = {}
510
511 def _findVar(self, var):
512 dest = self.dict
513 while dest:
514 if var in dest:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500515 return dest[var], self.overridedata.get(var, None)
516
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500517 if "_data" not in dest:
518 break
519 dest = dest["_data"]
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500520 return None, self.overridedata.get(var, None)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500521
522 def _makeShadowCopy(self, var):
523 if var in self.dict:
524 return
525
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500526 local_var, _ = self._findVar(var)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500527
528 if local_var:
529 self.dict[var] = copy.copy(local_var)
530 else:
531 self.initVar(var)
532
Andrew Geissler7e0e3c02022-02-25 20:34:39 +0000533 def hasOverrides(self, var):
534 return var in self.overridedata
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500535
536 def setVar(self, var, value, **loginfo):
537 #print("var=" + str(var) + " val=" + str(value))
Patrick Williams213cb262021-08-07 19:21:33 -0500538
Andrew Geissler595f6302022-01-24 19:11:47 +0000539 if not var.startswith("__anon_") and ("_append" in var or "_prepend" in var or "_remove" in var):
Patrick Williams213cb262021-08-07 19:21:33 -0500540 info = "%s" % var
Andrew Geissler7e0e3c02022-02-25 20:34:39 +0000541 if "file" in loginfo:
542 info += " file: %s" % loginfo["file"]
543 if "line" in loginfo:
544 info += " line: %s" % loginfo["line"]
Patrick Williams213cb262021-08-07 19:21:33 -0500545 bb.fatal("Variable %s contains an operation using the old override syntax. Please convert this layer/metadata before attempting to use with a newer bitbake." % info)
546
Andrew Geissler7e0e3c02022-02-25 20:34:39 +0000547 shortvar = var.split(":", 1)[0]
548 if shortvar in self._var_renames:
549 _print_rename_error(shortvar, loginfo, self._var_renames, fullvar=var)
550 # Mark that we have seen a renamed variable
551 self.setVar("_FAILPARSINGERRORHANDLED", True)
552
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800553 self.expand_cache = {}
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500554 parsing=False
555 if 'parsing' in loginfo:
556 parsing=True
557
558 if 'op' not in loginfo:
559 loginfo['op'] = "set"
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800560
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500561 match = __setvar_regexp__.match(var)
562 if match and match.group("keyword") in __setvar_keyword__:
563 base = match.group('base')
564 keyword = match.group("keyword")
565 override = match.group('add')
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500566 l = self.getVarFlag(base, keyword, False) or []
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500567 l.append([value, override])
568 self.setVarFlag(base, keyword, l, ignore=True)
569 # And cause that to be recorded:
570 loginfo['detail'] = value
571 loginfo['variable'] = base
572 if override:
573 loginfo['op'] = '%s[%s]' % (keyword, override)
574 else:
575 loginfo['op'] = keyword
576 self.varhistory.record(**loginfo)
577 # todo make sure keyword is not __doc__ or __module__
578 # pay the cookie monster
579
580 # more cookies for the cookie monster
Patrick Williams213cb262021-08-07 19:21:33 -0500581 if ':' in var:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500582 self._setvar_update_overrides(base, **loginfo)
583
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500584 if base in self.overridevars:
Patrick Williamsd7e96312015-09-22 08:09:05 -0500585 self._setvar_update_overridevars(var, value)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500586 return
587
588 if not var in self.dict:
589 self._makeShadowCopy(var)
590
591 if not parsing:
Patrick Williams213cb262021-08-07 19:21:33 -0500592 if ":append" in self.dict[var]:
593 del self.dict[var][":append"]
594 if ":prepend" in self.dict[var]:
595 del self.dict[var][":prepend"]
596 if ":remove" in self.dict[var]:
597 del self.dict[var][":remove"]
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500598 if var in self.overridedata:
599 active = []
600 self.need_overrides()
601 for (r, o) in self.overridedata[var]:
602 if o in self.overridesset:
603 active.append(r)
Patrick Williams213cb262021-08-07 19:21:33 -0500604 elif ":" in o:
605 if set(o.split(":")).issubset(self.overridesset):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500606 active.append(r)
607 for a in active:
608 self.delVar(a)
609 del self.overridedata[var]
610
611 # more cookies for the cookie monster
Patrick Williams213cb262021-08-07 19:21:33 -0500612 if ':' in var:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500613 self._setvar_update_overrides(var, **loginfo)
614
615 # setting var
616 self.dict[var]["_content"] = value
617 self.varhistory.record(**loginfo)
618
619 if var in self.overridevars:
Patrick Williamsd7e96312015-09-22 08:09:05 -0500620 self._setvar_update_overridevars(var, value)
621
622 def _setvar_update_overridevars(self, var, value):
623 vardata = self.expandWithRefs(value, var)
624 new = vardata.references
625 new.update(vardata.contains.keys())
626 while not new.issubset(self.overridevars):
627 nextnew = set()
628 self.overridevars.update(new)
629 for i in new:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500630 vardata = self.expandWithRefs(self.getVar(i), i)
Patrick Williamsd7e96312015-09-22 08:09:05 -0500631 nextnew.update(vardata.references)
632 nextnew.update(vardata.contains.keys())
633 new = nextnew
Patrick Williams7784c422022-11-17 07:29:11 -0600634 self.overrides = None
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500635
636 def _setvar_update_overrides(self, var, **loginfo):
637 # aka pay the cookie monster
Patrick Williams213cb262021-08-07 19:21:33 -0500638 override = var[var.rfind(':')+1:]
639 shortvar = var[:var.rfind(':')]
Brad Bishop19323692019-04-05 15:28:33 -0400640 while override and __override_regexp__.match(override):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500641 if shortvar not in self.overridedata:
642 self.overridedata[shortvar] = []
643 if [var, override] not in self.overridedata[shortvar]:
644 # Force CoW by recreating the list first
645 self.overridedata[shortvar] = list(self.overridedata[shortvar])
646 self.overridedata[shortvar].append([var, override])
647 override = None
Patrick Williams213cb262021-08-07 19:21:33 -0500648 if ":" in shortvar:
649 override = var[shortvar.rfind(':')+1:]
650 shortvar = var[:shortvar.rfind(':')]
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500651 if len(shortvar) == 0:
652 override = None
653
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500654 def getVar(self, var, expand=True, noweakdefault=False, parsing=False):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500655 return self.getVarFlag(var, "_content", expand, noweakdefault, parsing)
656
657 def renameVar(self, key, newkey, **loginfo):
658 """
659 Rename the variable key to newkey
660 """
Andrew Geissler82c905d2020-04-13 13:39:40 -0500661 if key == newkey:
662 bb.warn("Calling renameVar with equivalent keys (%s) is invalid" % key)
663 return
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500664
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500665 val = self.getVar(key, 0, parsing=True)
666 if val is not None:
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800667 self.varhistory.rename_variable_hist(key, newkey)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500668 loginfo['variable'] = newkey
669 loginfo['op'] = 'rename from %s' % key
670 loginfo['detail'] = val
671 self.varhistory.record(**loginfo)
672 self.setVar(newkey, val, ignore=True, parsing=True)
673
Andrew Geissler9aee5002022-03-30 16:27:02 +0000674 srcflags = self.getVarFlags(key, False, True) or {}
675 for i in srcflags:
676 if i not in (__setvar_keyword__):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500677 continue
Andrew Geissler9aee5002022-03-30 16:27:02 +0000678 src = srcflags[i]
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500679
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500680 dest = self.getVarFlag(newkey, i, False) or []
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500681 dest.extend(src)
682 self.setVarFlag(newkey, i, dest, ignore=True)
683
684 if key in self.overridedata:
685 self.overridedata[newkey] = []
686 for (v, o) in self.overridedata[key]:
687 self.overridedata[newkey].append([v.replace(key, newkey), o])
688 self.renameVar(v, v.replace(key, newkey))
689
Patrick Williams213cb262021-08-07 19:21:33 -0500690 if ':' in newkey and val is None:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500691 self._setvar_update_overrides(newkey, **loginfo)
692
693 loginfo['variable'] = key
694 loginfo['op'] = 'rename (to)'
695 loginfo['detail'] = newkey
696 self.varhistory.record(**loginfo)
697 self.delVar(key, ignore=True)
698
699 def appendVar(self, var, value, **loginfo):
700 loginfo['op'] = 'append'
701 self.varhistory.record(**loginfo)
Patrick Williams213cb262021-08-07 19:21:33 -0500702 self.setVar(var + ":append", value, ignore=True, parsing=True)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500703
704 def prependVar(self, var, value, **loginfo):
705 loginfo['op'] = 'prepend'
706 self.varhistory.record(**loginfo)
Patrick Williams213cb262021-08-07 19:21:33 -0500707 self.setVar(var + ":prepend", value, ignore=True, parsing=True)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500708
709 def delVar(self, var, **loginfo):
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800710 self.expand_cache = {}
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500711
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500712 loginfo['detail'] = ""
713 loginfo['op'] = 'del'
714 self.varhistory.record(**loginfo)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500715 self.dict[var] = {}
716 if var in self.overridedata:
717 del self.overridedata[var]
Patrick Williams213cb262021-08-07 19:21:33 -0500718 if ':' in var:
719 override = var[var.rfind(':')+1:]
720 shortvar = var[:var.rfind(':')]
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500721 while override and override.islower():
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500722 try:
723 if shortvar in self.overridedata:
724 # Force CoW by recreating the list first
725 self.overridedata[shortvar] = list(self.overridedata[shortvar])
726 self.overridedata[shortvar].remove([var, override])
727 except ValueError as e:
728 pass
729 override = None
Patrick Williams213cb262021-08-07 19:21:33 -0500730 if ":" in shortvar:
731 override = var[shortvar.rfind(':')+1:]
732 shortvar = var[:shortvar.rfind(':')]
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500733 if len(shortvar) == 0:
734 override = None
735
736 def setVarFlag(self, var, flag, value, **loginfo):
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800737 self.expand_cache = {}
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500738
Andrew Geissler7e0e3c02022-02-25 20:34:39 +0000739 if var == "BB_RENAMED_VARIABLES":
740 self._var_renames[flag] = value
741
742 if var in self._var_renames:
743 _print_rename_error(var, loginfo, self._var_renames)
744 # Mark that we have seen a renamed variable
745 self.setVar("_FAILPARSINGERRORHANDLED", True)
746
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500747 if 'op' not in loginfo:
748 loginfo['op'] = "set"
749 loginfo['flag'] = flag
750 self.varhistory.record(**loginfo)
751 if not var in self.dict:
752 self._makeShadowCopy(var)
753 self.dict[var][flag] = value
754
Patrick Williams213cb262021-08-07 19:21:33 -0500755 if flag == "_defaultval" and ':' in var:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500756 self._setvar_update_overrides(var, **loginfo)
Patrick Williamsd7e96312015-09-22 08:09:05 -0500757 if flag == "_defaultval" and var in self.overridevars:
758 self._setvar_update_overridevars(var, value)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500759
760 if flag == "unexport" or flag == "export":
761 if not "__exportlist" in self.dict:
762 self._makeShadowCopy("__exportlist")
763 if not "_content" in self.dict["__exportlist"]:
764 self.dict["__exportlist"]["_content"] = set()
765 self.dict["__exportlist"]["_content"].add(var)
766
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800767 def getVarFlag(self, var, flag, expand=True, noweakdefault=False, parsing=False, retparser=False):
768 if flag == "_content":
769 cachename = var
770 else:
771 if not flag:
772 bb.warn("Calling getVarFlag with flag unset is invalid")
773 return None
774 cachename = var + "[" + flag + "]"
775
776 if expand and cachename in self.expand_cache:
777 return self.expand_cache[cachename].value
778
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500779 local_var, overridedata = self._findVar(var)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500780 value = None
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800781 removes = set()
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500782 if flag == "_content" and overridedata is not None and not parsing:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500783 match = False
784 active = {}
785 self.need_overrides()
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500786 for (r, o) in overridedata:
Patrick Williams213cb262021-08-07 19:21:33 -0500787 # FIXME What about double overrides both with "_" in the name?
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500788 if o in self.overridesset:
789 active[o] = r
Patrick Williams213cb262021-08-07 19:21:33 -0500790 elif ":" in o:
791 if set(o.split(":")).issubset(self.overridesset):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500792 active[o] = r
793
794 mod = True
795 while mod:
796 mod = False
797 for o in self.overrides:
798 for a in active.copy():
Patrick Williams213cb262021-08-07 19:21:33 -0500799 if a.endswith(":" + o):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500800 t = active[a]
801 del active[a]
Patrick Williams213cb262021-08-07 19:21:33 -0500802 active[a.replace(":" + o, "")] = t
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500803 mod = True
804 elif a == o:
805 match = active[a]
806 del active[a]
807 if match:
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800808 value, subparser = self.getVarFlag(match, "_content", False, retparser=True)
809 if hasattr(subparser, "removes"):
810 # We have to carry the removes from the overridden variable to apply at the
811 # end of processing
812 removes = subparser.removes
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500813
814 if local_var is not None and value is None:
815 if flag in local_var:
816 value = copy.copy(local_var[flag])
817 elif flag == "_content" and "_defaultval" in local_var and not noweakdefault:
818 value = copy.copy(local_var["_defaultval"])
819
820
Patrick Williams213cb262021-08-07 19:21:33 -0500821 if flag == "_content" and local_var is not None and ":append" in local_var and not parsing:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500822 self.need_overrides()
Patrick Williams213cb262021-08-07 19:21:33 -0500823 for (r, o) in local_var[":append"]:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500824 match = True
825 if o:
Patrick Williams213cb262021-08-07 19:21:33 -0500826 for o2 in o.split(":"):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500827 if not o2 in self.overrides:
828 match = False
829 if match:
Patrick Williams213cb262021-08-07 19:21:33 -0500830 if value is None:
831 value = ""
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500832 value = value + r
833
Patrick Williams213cb262021-08-07 19:21:33 -0500834 if flag == "_content" and local_var is not None and ":prepend" in local_var and not parsing:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500835 self.need_overrides()
Patrick Williams213cb262021-08-07 19:21:33 -0500836 for (r, o) in local_var[":prepend"]:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500837
838 match = True
839 if o:
Patrick Williams213cb262021-08-07 19:21:33 -0500840 for o2 in o.split(":"):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500841 if not o2 in self.overrides:
842 match = False
843 if match:
Patrick Williams213cb262021-08-07 19:21:33 -0500844 if value is None:
845 value = ""
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500846 value = r + value
847
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800848 parser = None
849 if expand or retparser:
850 parser = self.expandWithRefs(value, cachename)
851 if expand:
852 value = parser.value
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500853
Patrick Williams213cb262021-08-07 19:21:33 -0500854 if value and flag == "_content" and local_var is not None and ":remove" in local_var and not parsing:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500855 self.need_overrides()
Patrick Williams213cb262021-08-07 19:21:33 -0500856 for (r, o) in local_var[":remove"]:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500857 match = True
858 if o:
Patrick Williams213cb262021-08-07 19:21:33 -0500859 for o2 in o.split(":"):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500860 if not o2 in self.overrides:
861 match = False
862 if match:
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800863 removes.add(r)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500864
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800865 if value and flag == "_content" and not parsing:
866 if removes and parser:
867 expanded_removes = {}
868 for r in removes:
869 expanded_removes[r] = self.expand(r).split()
870
871 parser.removes = set()
Andrew Geissler595f6302022-01-24 19:11:47 +0000872 val = []
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800873 for v in __whitespace_split__.split(parser.value):
874 skip = False
875 for r in removes:
876 if v in expanded_removes[r]:
877 parser.removes.add(r)
878 skip = True
879 if skip:
880 continue
Andrew Geissler595f6302022-01-24 19:11:47 +0000881 val.append(v)
882 parser.value = "".join(val)
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800883 if expand:
884 value = parser.value
885
886 if parser:
887 self.expand_cache[cachename] = parser
888
889 if retparser:
890 return value, parser
891
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500892 return value
893
894 def delVarFlag(self, var, flag, **loginfo):
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800895 self.expand_cache = {}
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500896
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500897 local_var, _ = self._findVar(var)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500898 if not local_var:
899 return
900 if not var in self.dict:
901 self._makeShadowCopy(var)
902
903 if var in self.dict and flag in self.dict[var]:
904 loginfo['detail'] = ""
905 loginfo['op'] = 'delFlag'
906 loginfo['flag'] = flag
907 self.varhistory.record(**loginfo)
908
909 del self.dict[var][flag]
910
911 def appendVarFlag(self, var, flag, value, **loginfo):
912 loginfo['op'] = 'append'
913 loginfo['flag'] = flag
914 self.varhistory.record(**loginfo)
915 newvalue = (self.getVarFlag(var, flag, False) or "") + value
916 self.setVarFlag(var, flag, newvalue, ignore=True)
917
918 def prependVarFlag(self, var, flag, value, **loginfo):
919 loginfo['op'] = 'prepend'
920 loginfo['flag'] = flag
921 self.varhistory.record(**loginfo)
922 newvalue = value + (self.getVarFlag(var, flag, False) or "")
923 self.setVarFlag(var, flag, newvalue, ignore=True)
924
925 def setVarFlags(self, var, flags, **loginfo):
926 self.expand_cache = {}
927 infer_caller_details(loginfo)
928 if not var in self.dict:
929 self._makeShadowCopy(var)
930
931 for i in flags:
932 if i == "_content":
933 continue
934 loginfo['flag'] = i
935 loginfo['detail'] = flags[i]
936 self.varhistory.record(**loginfo)
937 self.dict[var][i] = flags[i]
938
939 def getVarFlags(self, var, expand = False, internalflags=False):
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500940 local_var, _ = self._findVar(var)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500941 flags = {}
942
943 if local_var:
944 for i in local_var:
Patrick Williams213cb262021-08-07 19:21:33 -0500945 if i.startswith(("_", ":")) and not internalflags:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500946 continue
947 flags[i] = local_var[i]
948 if expand and i in expand:
949 flags[i] = self.expand(flags[i], var + "[" + i + "]")
950 if len(flags) == 0:
951 return None
952 return flags
953
954
955 def delVarFlags(self, var, **loginfo):
956 self.expand_cache = {}
957 if not var in self.dict:
958 self._makeShadowCopy(var)
959
960 if var in self.dict:
961 content = None
962
963 loginfo['op'] = 'delete flags'
964 self.varhistory.record(**loginfo)
965
966 # try to save the content
967 if "_content" in self.dict[var]:
968 content = self.dict[var]["_content"]
969 self.dict[var] = {}
970 self.dict[var]["_content"] = content
971 else:
972 del self.dict[var]
973
974 def createCopy(self):
975 """
976 Create a copy of self by setting _data to self
977 """
978 # we really want this to be a DataSmart...
979 data = DataSmart()
980 data.dict["_data"] = self.dict
981 data.varhistory = self.varhistory.copy()
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500982 data.varhistory.dataroot = data
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500983 data.inchistory = self.inchistory.copy()
984
985 data._tracking = self._tracking
Andrew Geissler7e0e3c02022-02-25 20:34:39 +0000986 data._var_renames = self._var_renames
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500987
988 data.overrides = None
989 data.overridevars = copy.copy(self.overridevars)
990 # Should really be a deepcopy but has heavy overhead.
991 # Instead, we're careful with writes.
992 data.overridedata = copy.copy(self.overridedata)
993
994 return data
995
996 def expandVarref(self, variable, parents=False):
997 """Find all references to variable in the data and expand it
998 in place, optionally descending to parent datastores."""
999
1000 if parents:
1001 keys = iter(self)
1002 else:
1003 keys = self.localkeys()
1004
1005 ref = '${%s}' % variable
1006 value = self.getVar(variable, False)
1007 for key in keys:
1008 referrervalue = self.getVar(key, False)
Andrew Geissler7e0e3c02022-02-25 20:34:39 +00001009 if referrervalue and isinstance(referrervalue, str) and ref in referrervalue:
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001010 self.setVar(key, referrervalue.replace(ref, value))
1011
1012 def localkeys(self):
1013 for key in self.dict:
Andrew Geissler82c905d2020-04-13 13:39:40 -05001014 if key not in ['_data']:
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001015 yield key
1016
1017 def __iter__(self):
1018 deleted = set()
1019 overrides = set()
1020 def keylist(d):
1021 klist = set()
1022 for key in d:
Andrew Geissler82c905d2020-04-13 13:39:40 -05001023 if key in ["_data"]:
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001024 continue
1025 if key in deleted:
1026 continue
1027 if key in overrides:
1028 continue
1029 if not d[key]:
1030 deleted.add(key)
1031 continue
1032 klist.add(key)
1033
1034 if "_data" in d:
1035 klist |= keylist(d["_data"])
1036
1037 return klist
1038
1039 self.need_overrides()
1040 for var in self.overridedata:
1041 for (r, o) in self.overridedata[var]:
1042 if o in self.overridesset:
1043 overrides.add(var)
Patrick Williams213cb262021-08-07 19:21:33 -05001044 elif ":" in o:
1045 if set(o.split(":")).issubset(self.overridesset):
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001046 overrides.add(var)
1047
1048 for k in keylist(self.dict):
1049 yield k
1050
1051 for k in overrides:
1052 yield k
1053
1054 def __len__(self):
Patrick Williamsc0f7c042017-02-23 20:41:17 -06001055 return len(frozenset(iter(self)))
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001056
1057 def __getitem__(self, item):
1058 value = self.getVar(item, False)
1059 if value is None:
1060 raise KeyError(item)
1061 else:
1062 return value
1063
1064 def __setitem__(self, var, value):
1065 self.setVar(var, value)
1066
1067 def __delitem__(self, var):
1068 self.delVar(var)
1069
1070 def get_hash(self):
1071 data = {}
1072 d = self.createCopy()
1073 bb.data.expandKeys(d)
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001074
Andrew Geissler7e0e3c02022-02-25 20:34:39 +00001075 config_ignore_vars = set((d.getVar("BB_HASHCONFIG_IGNORE_VARS") or "").split())
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001076 keys = set(key for key in iter(d) if not key.startswith("__"))
1077 for key in keys:
Andrew Geissler7e0e3c02022-02-25 20:34:39 +00001078 if key in config_ignore_vars:
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001079 continue
1080
1081 value = d.getVar(key, False) or ""
Andrew Geissler82c905d2020-04-13 13:39:40 -05001082 if type(value) is type(self):
1083 data.update({key:value.get_hash()})
1084 else:
1085 data.update({key:value})
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001086
Andrew Geisslerd1e89492021-02-12 15:35:20 -06001087 varflags = d.getVarFlags(key, internalflags = True, expand=["vardepvalue"])
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001088 if not varflags:
1089 continue
1090 for f in varflags:
1091 if f == "_content":
1092 continue
1093 data.update({'%s[%s]' % (key, f):varflags[f]})
1094
1095 for key in ["__BBTASKS", "__BBANONFUNCS", "__BBHANDLERS"]:
1096 bb_list = d.getVar(key, False) or []
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001097 data.update({key:str(bb_list)})
1098
1099 if key == "__BBANONFUNCS":
1100 for i in bb_list:
Patrick Williamsd8c66bc2016-06-20 12:57:21 -05001101 value = d.getVar(i, False) or ""
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001102 data.update({i:value})
1103
1104 data_str = str([(k, data[k]) for k in sorted(data.keys())])
Brad Bishop19323692019-04-05 15:28:33 -04001105 return hashlib.sha256(data_str.encode("utf-8")).hexdigest()