diff mbox series

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

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

Commit Message

Slater, Joseph Jan. 18, 2024, 5:38 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.

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

Comments

Richard Purdie Jan. 19, 2024, 9:38 a.m. UTC | #1
Hi Joe,

On Thu, 2024-01-18 at 09:38 -0800, Joe Slater via
lists.openembedded.org wrote:
> From: Joe Slater <joe.slater@windriver.com>
> 
> Add a function returning a value based on whether a
> bitbake variable is in a numeric range.
> 
> Signed-off-by: Joe Slater <joe.slater@windriver.com>
> ---
>  lib/bb/utils.py | 11 +++++++++++
>  1 file changed, 11 insertions(+)
> 
> diff --git a/lib/bb/utils.py b/lib/bb/utils.py
> index 068b631c..76f08623 100644
> --- a/lib/bb/utils.py
> +++ b/lib/bb/utils.py
> @@ -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?


> +    """
> +    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
Slater, Joseph Jan. 30, 2024, 9:54 p.m. UTC | #2
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

> -----Original Message-----
> From: Richard Purdie <richard.purdie@linuxfoundation.org>
> Sent: Friday, January 19, 2024 1:38 AM
> To: Slater, Joseph <joe.slater@windriver.com>; bitbake-
> devel@lists.openembedded.org
> Cc: MacLeod, Randy <Randy.MacLeod@windriver.com>
> Subject: Re: [bitbake-devel] [bitbake][PATCH 1/1] utils.py: add ifVarInRange()
> 
> Hi Joe,
> 
> On Thu, 2024-01-18 at 09:38 -0800, Joe Slater via lists.openembedded.org wrote:
> > From: Joe Slater <joe.slater@windriver.com>
> >
> > Add a function returning a value based on whether a bitbake variable
> > is in a numeric range.
> >
> > Signed-off-by: Joe Slater <joe.slater@windriver.com>
> > ---
> >  lib/bb/utils.py | 11 +++++++++++
> >  1 file changed, 11 insertions(+)
> >
> > diff --git a/lib/bb/utils.py b/lib/bb/utils.py index
> > 068b631c..76f08623 100644
> > --- a/lib/bb/utils.py
> > +++ b/lib/bb/utils.py
> > @@ -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?
> 
> 
> > +    """
> > +    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
> 
> 
>
diff mbox series

Patch

diff --git a/lib/bb/utils.py b/lib/bb/utils.py
index 068b631c..76f08623 100644
--- a/lib/bb/utils.py
+++ b/lib/bb/utils.py
@@ -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.