mirror of
https://git.yoctoproject.org/poky
synced 2026-05-30 00:20:08 +00:00
bitbake/data_smart.py: Allow the data expand function to keep track of references (including those from python code)
Signed-off-by: Richard Purdie <rpurdie@linux.intel.com>
This commit is contained in:
@@ -39,6 +39,39 @@ __setvar_regexp__ = re.compile('(?P<base>.*?)(?P<keyword>_append|_prepend)(_(?P<
|
|||||||
__expand_var_regexp__ = re.compile(r"\${[^{}]+}")
|
__expand_var_regexp__ = re.compile(r"\${[^{}]+}")
|
||||||
__expand_python_regexp__ = re.compile(r"\${@.+?}")
|
__expand_python_regexp__ = re.compile(r"\${@.+?}")
|
||||||
|
|
||||||
|
class VariableParse:
|
||||||
|
def __init__(self, varname, d, val = None):
|
||||||
|
self.varname = varname
|
||||||
|
self.d = d
|
||||||
|
self.value = val
|
||||||
|
|
||||||
|
self.references = set()
|
||||||
|
self.funcrefs = set()
|
||||||
|
|
||||||
|
def var_sub(self, match):
|
||||||
|
key = match.group()[2:-1]
|
||||||
|
if self.varname and key:
|
||||||
|
if self.varname == key:
|
||||||
|
raise Exception("variable %s references itself!" % self.varname)
|
||||||
|
var = self.d.getVar(key, 1)
|
||||||
|
if var is not None:
|
||||||
|
self.references.add(key)
|
||||||
|
return var
|
||||||
|
else:
|
||||||
|
return match.group()
|
||||||
|
|
||||||
|
def python_sub(self, match):
|
||||||
|
code = match.group()[3:-1]
|
||||||
|
codeobj = compile(code.strip(), self.varname or "<expansion>", "eval")
|
||||||
|
|
||||||
|
parser = bb.rptest.PythonParser()
|
||||||
|
parser.parse_python(code)
|
||||||
|
self.references |= parser.references
|
||||||
|
self.funcrefs |= parser.execs
|
||||||
|
|
||||||
|
value = utils.better_eval(codeobj, {"d": self.d})
|
||||||
|
return str(value)
|
||||||
|
|
||||||
|
|
||||||
class DataSmart:
|
class DataSmart:
|
||||||
def __init__(self, special = COWDictBase.copy(), seen = COWDictBase.copy() ):
|
def __init__(self, special = COWDictBase.copy(), seen = COWDictBase.copy() ):
|
||||||
@@ -50,35 +83,21 @@ class DataSmart:
|
|||||||
|
|
||||||
self.expand_cache = {}
|
self.expand_cache = {}
|
||||||
|
|
||||||
def expand(self, s, varname):
|
def expandWithRefs(self, s, varname):
|
||||||
def var_sub(match):
|
|
||||||
key = match.group()[2:-1]
|
|
||||||
if varname and key:
|
|
||||||
if varname == key:
|
|
||||||
raise Exception("variable %s references itself!" % varname)
|
|
||||||
var = self.getVar(key, 1)
|
|
||||||
if var is not None:
|
|
||||||
return var
|
|
||||||
else:
|
|
||||||
return match.group()
|
|
||||||
|
|
||||||
def python_sub(match):
|
|
||||||
code = match.group()[3:-1]
|
|
||||||
codeobj = compile(code.strip(), varname or "<expansion>", "eval")
|
|
||||||
value = utils.better_eval(codeobj, {"d": self})
|
|
||||||
return str(value)
|
|
||||||
|
|
||||||
if not isinstance(s, basestring): # sanity check
|
if not isinstance(s, basestring): # sanity check
|
||||||
return s
|
return VariableParse(varname, self, s)
|
||||||
|
|
||||||
if varname and varname in self.expand_cache:
|
if varname and varname in self.expand_cache:
|
||||||
return self.expand_cache[varname]
|
return self.expand_cache[varname]
|
||||||
|
|
||||||
|
varparse = VariableParse(varname, self)
|
||||||
|
|
||||||
while s.find('${') != -1:
|
while s.find('${') != -1:
|
||||||
olds = s
|
olds = s
|
||||||
try:
|
try:
|
||||||
s = __expand_var_regexp__.sub(var_sub, s)
|
s = __expand_var_regexp__.sub(varparse.var_sub, s)
|
||||||
s = __expand_python_regexp__.sub(python_sub, s)
|
s = __expand_python_regexp__.sub(varparse.python_sub, s)
|
||||||
if s == olds:
|
if s == olds:
|
||||||
break
|
break
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
@@ -87,10 +106,16 @@ class DataSmart:
|
|||||||
bb.msg.note(1, bb.msg.domain.Data, "%s:%s while evaluating:\n%s" % (sys.exc_info()[0], sys.exc_info()[1], s))
|
bb.msg.note(1, bb.msg.domain.Data, "%s:%s while evaluating:\n%s" % (sys.exc_info()[0], sys.exc_info()[1], s))
|
||||||
raise
|
raise
|
||||||
|
|
||||||
if varname:
|
varparse.value = s
|
||||||
self.expand_cache[varname] = s
|
|
||||||
|
|
||||||
return s
|
if varname:
|
||||||
|
self.expand_cache[varname] = varparse
|
||||||
|
|
||||||
|
return varparse
|
||||||
|
|
||||||
|
def expand(self, s, varname):
|
||||||
|
return self.expandWithRefs(s, varname).value
|
||||||
|
|
||||||
|
|
||||||
def finalize(self):
|
def finalize(self):
|
||||||
"""Performs final steps upon the datastore, including application of overrides"""
|
"""Performs final steps upon the datastore, including application of overrides"""
|
||||||
|
|||||||
Reference in New Issue
Block a user