diff mbox series

[2/3] data_smart: Add setVarFilter function to implement variabl filtering

Message ID 20250810222355.2486772-2-richard.purdie@linuxfoundation.org
State Accepted, archived
Commit a9471c10d1de039474ddb4738abd286b928d82f4
Headers show
Series [1/3] lib/bb: Add filter support | expand

Commit Message

Richard Purdie Aug. 10, 2025, 10:23 p.m. UTC
Adds a new setVarFilter() API to the data store allowing filters to be
applied to variables.

Note that filters are applied to the non-override part of the variable name
so a filter set against RDEPENDS would apply against RDEPENDS:${PN} and
friends.

The filter function is applied before returning the final variable value.

Signed-off-by: Joshua Watt <JPEWhacker@gmail.com>
Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
---
 lib/bb/data_smart.py | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
diff mbox series

Patch

diff --git a/lib/bb/data_smart.py b/lib/bb/data_smart.py
index 8e7dd983841..2e0d3085882 100644
--- a/lib/bb/data_smart.py
+++ b/lib/bb/data_smart.py
@@ -24,6 +24,7 @@  from collections.abc import MutableMapping
 import logging
 import hashlib
 import bb, bb.codeparser
+import bb.filter
 from bb   import utils
 from bb.COW  import COWDictBase
 
@@ -427,6 +428,7 @@  class DataSmart(MutableMapping):
 
         self.inchistory = IncludeHistory()
         self.varhistory = VariableHistory(self)
+        self.filters = {}
         self._tracking = False
         self._var_renames = {}
         self._var_renames.update(bitbake_renamed_vars)
@@ -678,6 +680,7 @@  class DataSmart(MutableMapping):
 
         srcflags = self.getVarFlags(key, False, True) or {}
         for i in srcflags:
+
             if i not in (__setvar_keyword__):
                 continue
             src = srcflags[i]
@@ -895,6 +898,12 @@  class DataSmart(MutableMapping):
                 if expand:
                     value = parser.value
 
+        if value and expand and flag == "_content":
+            basevar = var.split(":")[0]
+            if basevar in self.filters:
+                value = bb.filter.apply_filters(value, [self.filters[basevar],])
+                parser.value = value
+
         if parser:
             self.expand_cache[cachename] = parser
 
@@ -1000,6 +1009,7 @@  class DataSmart(MutableMapping):
         data.varhistory = self.varhistory.copy()
         data.varhistory.dataroot = data
         data.inchistory = self.inchistory.copy()
+        data.filters = self.filters.copy()
 
         data._tracking = self._tracking
         data._var_renames = self._var_renames
@@ -1028,6 +1038,15 @@  class DataSmart(MutableMapping):
             if referrervalue and isinstance(referrervalue, str) and ref in referrervalue:
                 self.setVar(key, referrervalue.replace(ref, value))
 
+    def setVarFilter(self, var, filter):
+        if filter:
+            self.filters[var] = filter
+        else:
+            try:
+                del self.filters[var]
+            except KeyError:
+                pass
+
     def localkeys(self):
         for key in self.dict:
             if key not in ['_data']: