Skip to contentSkip to conversation

data_smart: Preserve history when renaming variables

PATCH New Richard Purdie 1 participant 3 messages Oct 16, 2018

lore
Share & tools
lore
#0 lore raw

PATCH 1/3 data_smart: Preserve history when renaming variables

Currently when a variable is renamed, all the variable history is lost. This makes it hard for users to understand/debug where values came from.

Improve the code so that history is preserved across renamed variables.

(Expanded variable key names are a special case of variable renaming)

---
 lib/bb/data_smart.py | 11 +++++++++++
 1 file changed, 11 insertions(+)
lib/bb/data_smart.py +11 -0
index 7b09af5cf1..4434142a02 100644
@@ -267,6 +267,16 @@ class VariableHistory(object):
             return
         self.variables[var].append(loginfo.copy())
 
+    def rename_variable_hist(self, oldvar, newvar):
+        if not self.dataroot._tracking:
+            return
+        if oldvar not in self.variables:
+            return
+        if newvar not in self.variables:
+            self.variables[newvar] = []
+        for i in self.variables[oldvar]:
+            self.variables[newvar].append(i.copy())
+
     def variable(self, var):
         remote_connector = self.dataroot.getVar('_remote_data', False)
         if remote_connector:
@@ -619,6 +629,7 @@ class DataSmart(MutableMapping):
 
         val = self.getVar(key, 0, parsing=True)
         if val is not None:
+            self.varhistory.rename_variable_hist(key, newkey)
             loginfo['variable'] = newkey
             loginfo['op'] = 'rename from %s' % key
             loginfo['detail'] = val
Signature · 1 line
2.17.1
#1 lore raw

PATCH 2/3 data: Fix whitespace on _remove operations

We have some slightly odd behaviours with the current implementation of _remove operations. For example:

TEST = " A B"
TEST_remove = "C"

would trigger TEST to become "A B" even thought it doesn't contain "C".

In particular, this means that an inactive remove operator added in a bbappend could change the task checksum which is not desireable.

Fix the operation to preserve whitespace, adding new tests to make this explict and test further corner cases. Also update the manual to match.

---
 .../bitbake-user-manual-metadata.xml          |  6 +++---
 lib/bb/data_smart.py                          |  5 +++--
 lib/bb/tests/data.py                          | 20 +++++++++++++++----
 3 files changed, 22 insertions(+), 9 deletions(-)
doc/bitbake-user-manual/bitbake-user-manual-metadata.xml +3 -3
index bc08c814d1..fc55ef6483 100644
@@ -342,7 +342,7 @@
 
             <para>
                 When you use this syntax, BitBake expects one or more strings.
-                Surrounding spaces are removed as well.
+                Surrounding spaces and spacing are preserved.
                 Here is an example:
                 <literallayout class='monospaced'>
      FOO = "123 456 789 123456 123 456 123 456"
@@ -352,8 +352,8 @@
      FOO2_remove = "abc def"
                 </literallayout>
                 The variable <filename>FOO</filename> becomes
-                "789 123456" and <filename>FOO2</filename> becomes
-                "ghi abcdef".
+                "  789 123456    " and <filename>FOO2</filename> becomes
+                "  ghi abcdef    ".
             </para>
 
             <para>
lib/bb/data_smart.py +3 -2
index 4434142a02..0a8488ca1b 100644
@@ -42,6 +42,7 @@ __setvar_keyword__ = ["_append", "_prepend", "_remove"]
 __setvar_regexp__ = re.compile('(?P<base>.*?)(?P<keyword>_append|_prepend|_remove)(_(?P<add>[^A-Z]*))?$')
 __expand_var_regexp__ = re.compile(r"\${[^{}@\n\t :]+}")
 __expand_python_regexp__ = re.compile(r"\${@.+?}")
+__whitespace_split__ = re.compile('(\s)')
 
 def infer_caller_details(loginfo, parent = False, varval = True):
     """Save the caller the trouble of specifying everything."""
@@ -818,8 +819,8 @@ class DataSmart(MutableMapping):
 
             if removes:
                 filtered = filter(lambda v: v not in removes,
-                                  value.split())
-                value = " ".join(filtered)
+                                  __whitespace_split__.split(value))
+                value = "".join(filtered)
                 if expand and var in self.expand_cache:
                     # We need to ensure the expand cache has the correct value
                     # flag == "_content" here
lib/bb/tests/data.py +16 -4
index a4a9dd30fb..8279115e03 100644
@@ -281,7 +281,7 @@ class TestConcatOverride(unittest.TestCase):
     def test_remove(self):
         self.d.setVar("TEST", "${VAL} ${BAR}")
         self.d.setVar("TEST_remove", "val")
-        self.assertEqual(self.d.getVar("TEST"), "bar")
+        self.assertEqual(self.d.getVar("TEST"), " bar")
 
     def test_remove_cleared(self):
         self.d.setVar("TEST", "${VAL} ${BAR}")
@@ -300,7 +300,7 @@ class TestConcatOverride(unittest.TestCase):
         self.d.setVar("TEST", "${VAL} ${BAR}")
         self.d.setVar("TEST_remove", "val")
         self.d.setVar("TEST_TEST", "${TEST} ${TEST}")
-        self.assertEqual(self.d.getVar("TEST_TEST"), "bar bar")
+        self.assertEqual(self.d.getVar("TEST_TEST"), " bar  bar")
 
     def test_empty_remove(self):
         self.d.setVar("TEST", "")
@@ -311,13 +311,25 @@ class TestConcatOverride(unittest.TestCase):
         self.d.setVar("BAR", "Z")
         self.d.setVar("TEST", "${BAR}/X Y")
         self.d.setVar("TEST_remove", "${BAR}/X")
-        self.assertEqual(self.d.getVar("TEST"), "Y")
+        self.assertEqual(self.d.getVar("TEST"), " Y")
 
     def test_remove_expansion_items(self):
         self.d.setVar("TEST", "A B C D")
         self.d.setVar("BAR", "B D")
         self.d.setVar("TEST_remove", "${BAR}")
-        self.assertEqual(self.d.getVar("TEST"), "A C")
+        self.assertEqual(self.d.getVar("TEST"), "A  C ")
+
+    def test_remove_preserve_whitespace(self):
+        # When the removal isn't active, the original value should be preserved
+        self.d.setVar("TEST", " A B")
+        self.d.setVar("TEST_remove", "C")
+        self.assertEqual(self.d.getVar("TEST"), " A B")
+
+    def test_remove_preserve_whitespace2(self):
+        # When the removal is active preserve the whitespace
+        self.d.setVar("TEST", " A B")
+        self.d.setVar("TEST_remove", "B")
+        self.assertEqual(self.d.getVar("TEST"), " A ")
 
 class TestOverrides(unittest.TestCase):
     def setUp(self):
Signature · 1 line
2.17.1
#2 lore raw

PATCH 3/3 data: Ensure task checksums account for remove data

In reply to Richard Purdie

Currently remove operations are not being accounted for in the task checksums. This is a fairly serious oversight and needs to be fixed.

To do so, we need internal data from getVarFlag combined with the expanded variable data so that only "active" remove operators are accounted for in the task checksum.

This code adds in an internal hook to get the data and accounts for the operator in the task checksum when the removal is active.

[YOCTO #12913]

---
 lib/bb/data.py       | 16 +++++++++++++++-
 lib/bb/data_smart.py |  7 +++++--
 2 files changed, 20 insertions(+), 3 deletions(-)
lib/bb/data.py +15 -1
index 80a7879cb6..55ba7b1329 100644
@@ -290,7 +290,7 @@ def build_dependencies(key, keys, shelldeps, varflagsexcl, d):
             return deps, value
         varflags = d.getVarFlags(key, ["vardeps", "vardepvalue", "vardepsexclude", "exports", "postfuncs", "prefuncs", "lineno", "filename"]) or {}
         vardeps = varflags.get("vardeps")
-        value = d.getVarFlag(key, "_content", False)
+        value, removes = d.getVarFlag(key, "_content", False, retremoves=True)
 
         def handle_contains(value, contains, d):
             newvalue = ""
@@ -309,6 +309,16 @@ def build_dependencies(key, keys, shelldeps, varflagsexcl, d):
                 return newvalue
             return value + newvalue
 
+        def handle_remove(value, deps, expandedval, removes, d):
+            activeremoves = []
+            for r in removes:
+                r2 = d.expandWithRefs(r, None)
+                if r2.value in expandedval.split():
+                    value += "\n_remove of %s(%s)" % (r, r2.value)
+                    deps |= r2.references
+                    deps = deps | (keys & r2.execs)
+            return value
+
         if "vardepvalue" in varflags:
            value = varflags.get("vardepvalue")
         elif varflags.get("func"):
@@ -328,6 +338,8 @@ def build_dependencies(key, keys, shelldeps, varflagsexcl, d):
                 deps = deps | parsedvar.references
                 deps = deps | (keys & parser.execs) | (keys & parsedvar.execs)
                 value = handle_contains(value, parsedvar.contains, d)
+                if removes:
+                    value = handle_remove(value, deps, parser.value, removes, d)
             if vardeps is None:
                 parser.log.flush()
             if "prefuncs" in varflags:
@@ -341,6 +353,8 @@ def build_dependencies(key, keys, shelldeps, varflagsexcl, d):
             deps |= parser.references
             deps = deps | (keys & parser.execs)
             value = handle_contains(value, parser.contains, d)
+            if removes:
+                value = handle_remove(value, deps, parser.value, removes, d)
 
         if "vardepvalueexclude" in varflags:
             exclude = varflags.get("vardepvalueexclude")
lib/bb/data_smart.py +5 -2
index 0a8488ca1b..60ea6755ee 100644
@@ -731,7 +731,7 @@ class DataSmart(MutableMapping):
                 self.dict["__exportlist"]["_content"] = set()
             self.dict["__exportlist"]["_content"].add(var)
 
-    def getVarFlag(self, var, flag, expand=True, noweakdefault=False, parsing=False):
+    def getVarFlag(self, var, flag, expand=True, noweakdefault=False, parsing=False, retremoves=False):
         local_var, overridedata = self._findVar(var)
         value = None
         if flag == "_content" and overridedata is not None and not parsing:
@@ -805,8 +805,8 @@ class DataSmart(MutableMapping):
                 cachename = var + "[" + flag + "]"
             value = self.expand(value, cachename)
 
+        removes = []
         if value and flag == "_content" and local_var is not None and "_remove" in local_var:
-            removes = []
             self.need_overrides()
             for (r, o) in local_var["_remove"]:
                 match = True
@@ -825,6 +825,9 @@ class DataSmart(MutableMapping):
                     # We need to ensure the expand cache has the correct value
                     # flag == "_content" here
                     self.expand_cache[var].value = value
+
+        if retremoves:
+            return value, removes
         return value
 
     def delVarFlag(self, var, flag, **loginfo):
Signature · 1 line
2.17.1