diff mbox series

[1/1] utils.py: add var_in_numeric_range()

Message ID 20240130215648.3115416-1-joe.slater@windriver.com
State New
Headers show
Series [1/1] utils.py: add var_in_numeric_range() | expand

Commit Message

Slater, Joseph Jan. 30, 2024, 9:56 p.m. UTC
From: Joe Slater <joe.slater@windriver.com>

Add a function returning a value based on whether a
bitbake variable is in a numeric range.

Issue: LINUXEXEC-29701

(LOCAL REV: NOT UPSTREAM) - sent to bitbake/master

Signed-off-by: Joe Slater <joe.slater@windriver.com>
---
 lib/bb/utils.py | 11 +++++++++++
 1 file changed, 11 insertions(+)
diff mbox series

Patch

diff --git a/lib/bb/utils.py b/lib/bb/utils.py
index 3f7f82d17..04b406ab1 100644
--- a/lib/bb/utils.py
+++ b/lib/bb/utils.py
@@ -1780,6 +1780,17 @@  def is_local_uid(uid=''):
                 return True
     return False
 
+def var_in_numeric_range(d, var, first=1, last=0, truevalue="", falsevalue=""):
+    """
+    Return a value depending on whether a bitbake variable is in a numeric
+    range.  Conventionally, 0 as the end of the range matches all values.
+    """
+    try:
+        v = int(d.getVar(var))
+    except:
+        return falsevalue
+    return truevalue if v >= first and (v <= last or last == 0) else falsevalue
+
 def mkstemp(suffix=None, prefix=None, dir=None, text=False):
     """
     Generates a unique filename, independent of time.