Skip to contentSkip to conversation

lib/bb: Add filter support

PATCH New Richard Purdie 1 participant 3 messages Aug 10, 2025

lore
Share & tools
lore
#0 lore raw

PATCH 1/3 lib/bb: Add filter support

From: Joshua Watt <[email protected]>

Add the python API for applying filters to a string and being able to register functions as filters.

Filter functions are pure functions where an input is translated into an output and there are no external data accesses. This means translations can be cached as they won't change.

---
 bin/bitbake-selftest   |   1 +
 lib/bb/filter.py       | 142 +++++++++++++++++++++++++++++++++++++++++
 lib/bb/tests/filter.py |  88 +++++++++++++++++++++++++
 3 files changed, 231 insertions(+)
 create mode 100644 lib/bb/filter.py
 create mode 100644 lib/bb/tests/filter.py
bin/bitbake-selftest +1 -0
index 8c1e6d3e7e7..fb7c57dd837 100755
@@ -33,6 +33,7 @@ tests = ["bb.tests.codeparser",
          "bb.tests.siggen",
          "bb.tests.utils",
          "bb.tests.compression",
+         "bb.tests.filter",
          "hashserv.tests",
          "prserv.tests",
          "layerindexlib.tests.layerindexobj",
lib/bb/filter.py new file +142 -0

142 lines not on this page · raw message

lib/bb/tests/filter.py new file +88 -0

88 lines not on this page · raw message

#1 lore raw

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

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.

---
 lib/bb/data_smart.py | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
lib/bb/data_smart.py +19 -0
index 8e7dd983841..2e0d3085882 100644
@@ -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']:
#2 lore raw

PATCH 3/3 Add bb.utils.explode_deps as a filter function

In reply to Richard Purdie

From: Joshua Watt <[email protected]>

Marks bb.utils.explode_deps as callable from filter functions

---
 lib/bb/utils.py | 2 ++
 1 file changed, 2 insertions(+)
lib/bb/utils.py +2 -0
index 0cf3b1814c4..366836bfc9a 100644
@@ -34,6 +34,7 @@ from contextlib import contextmanager
 from ctypes import cdll
 import bb
 import bb.msg
+import bb.filter
 
 logger = logging.getLogger("BitBake.Util")
 python_extensions = importlib.machinery.all_suffixes()
@@ -184,6 +185,7 @@ def vercmp_string_op(a, b, op):
     else:
         raise VersionStringException('Unsupported comparison operator "%s"' % op)
 
[email protected]_proc(name="bb.utils.explode_deps")
 def explode_deps(s):
     """
     Takes an RDEPENDS style string of format::