Skip to contentSkip to conversation

utils.py: add ifVarInRange()

PATCH In review [email protected] 2 participants 3 messages Jan 18, 2024 – Jan 30, 2024

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 utils.py: add ifVarInRange()

From: Joe Slater <[email protected]>

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

---
 lib/bb/utils.py | 11 +++++++++++
 1 file changed, 11 insertions(+)
lib/bb/utils.py +11 -0
index 068b631c..76f08623 100644
@@ -1810,6 +1810,17 @@ def is_local_uid(uid=''):
                 return True
     return False
 
+def ifVarInRange(d, var, first=1, last=0, tval="", fval=""):
+    """
+    Return a value depending on whether a bitbake variable is in a numeric
+    range.  Conventionally, 0 as the emd pf the range matches all values.
+    """
+    try:
+        v = int(d.getVar(var))
+    except:
+        return fval
+    return tval if v >= first and (v <= last or last == 0) else fval
+
 def mkstemp(suffix=None, prefix=None, dir=None, text=False):
     """
     Generates a unique filename, independent of time.
Signature · 1 line
2.25.1
#1 lore raw

Hi Joe,

Quoted 15 lines · Joe Slater via lists.openembedded.org

From: Joe Slater <[email protected]>

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

---
 lib/bb/utils.py | 11 +++++++++++
 1 file changed, 11 insertions(+)
lib/bb/utils.py +0 -0
index 068b631c..76f08623 100644

lib/bb/utils.py def is_local_uid(uid=''): · line 1816

Quoted 7 lines
lib/bb/utils.py +4 -0
@@ -1810,6 +1810,17 @@ def is_local_uid(uid=''):
                 return True
     return False
 
+def ifVarInRange(d, var, first=1, last=0, tval="", fval=""):
+    """
+    Return a value depending on whether a bitbake variable is in a numeric
+    range.  Conventionally, 0 as the emd pf the range matches all values.

"the emd pf"?

the end of?

lib/bb/utils.py def is_local_uid(uid=''): · line 1826

Quoted 10 lines
lib/bb/utils.py +7 -0
+    """
+    try:
+        v = int(d.getVar(var))
+    except:
+        return fval
+    return tval if v >= first and (v <= last or last == 0) else fval
+
 def mkstemp(suffix=None, prefix=None, dir=None, text=False):
     """
     Generates a unique filename, independent of time.

If we're going to add something like this, I think the api needs a bit of work. I'd probably expect an ifXXX function to return a True/False. I'd guess that is what tval and fval represent but it took me a minute to realise that. Wouldn't they actually default to True and False?

There is no mention of Int or numeric in the function name and python could easily use floats in situations like this.

So just to be clear I'm not really sold on the function name, the default values or the names of the parameters. The typos also need fixing. It all feels a bit rushed.

Cheers,

Richard

#2 lore raw

Hi Richard,

I'm sending a patch that changes the function below from ifVarInRange() to var_in_numeric_range(). The arguments are similar to the contains_any() function.

Since the function is not likely to be used that often, I was wondering if it would be better placed someplace that wasn't always parsed, like a bbclass. In case you're wondering, the intended use is for something like

SRC_URI = "${SOURCEFORGE_MIRROR}/libpng/${BPN}/${PV}/${BPN}-${PV}.tar.xz \
           file://ldflags-tests.patch \
           file://0001-configure-Pass-LDFLAGS-to-link-tests.patch \
           file://CVE-2018-25032.patch \
           file://run-ptest \
           ${@bb.utils.var_in_numeric_range(d,'OUR_MINOR_VERSION',truevalue='file://CVE-2022-37434.patch',last=14)} \
           ${@bb.utils.var_in_numeric_range(d,'OUR_MINOR_VERSION',10,0,'file://CVE-2023-45853.patch')} \
           "

Which is lifted from the zlib recipe for testing.

Joe

Quoted 68 lines

-----Original Message-----
From: Richard Purdie <[email protected]>
Sent: Friday, January 19, 2024 1:38 AM
To: Slater, Joseph <[email protected]>; bitbake- [email protected]
Cc: MacLeod, Randy <[email protected]>
Subject: Re: [bitbake-devel] [bitbake][PATCH 1/1] utils.py: add ifVarInRange()

Hi Joe,

Quoted 15 lines · Joe Slater via lists.openembedded.org

From: Joe Slater <[email protected]>

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

---
 lib/bb/utils.py | 11 +++++++++++
 1 file changed, 11 insertions(+)
lib/bb/utils.py index renamed from lib/bb/utils.py +0 -0

068b631c..76f08623 100644

lib/bb/utils.py def is_local_uid(uid=''): · line 1816

Quoted 7 lines
lib/bb/utils.py +4 -0
@@ -1810,6 +1810,17 @@ def is_local_uid(uid=''):
                 return True
     return False
 
+def ifVarInRange(d, var, first=1, last=0, tval="", fval=""):
+    """
+    Return a value depending on whether a bitbake variable is in a numeric
+    range.  Conventionally, 0 as the emd pf the range matches all values.

"the emd pf"?

the end of?

lib/bb/utils.py def is_local_uid(uid=''): · line 1826

Quoted 10 lines
lib/bb/utils.py +7 -0
+    """
+    try:
+        v = int(d.getVar(var))
+    except:
+        return fval
+    return tval if v >= first and (v <= last or last == 0) else fval
+
 def mkstemp(suffix=None, prefix=None, dir=None, text=False):
     """
     Generates a unique filename, independent of time.

If we're going to add something like this, I think the api needs a bit of work. I'd probably expect an ifXXX function to return a True/False.
I'd guess that is what tval and fval represent but it took me a minute to realise that. Wouldn't they actually default to True and False?

There is no mention of Int or numeric in the function name and python could easily use floats in situations like this.

So just to be clear I'm not really sold on the function name, the default values or the names of the parameters. The typos also need fixing. It all feels a bit rushed.

Cheers,

Richard