Replies: 7 comments 2 replies
-
seems a shame to have to use a SHELL expression for this, but .. how does one do that in pure RPM macro language ? |
Beta Was this translation helpful? Give feedback.
-
Good question 👍 A couple of fundamental problems there:
For example:
(Whether you use --eval or --define is a matter of style, but it's better to split stuff over multiple --define/--eval uses (they're simply processed in order), makes it easier to handle for us humans.) Turning that into a parametric macro gets you something like this:
Note quoting around the %1 argument test: macros always expand to string values, so testing for integer value would only give you errors. The rest kinda depends what exactly you want to do with it. Hope that helps. |
Beta Was this translation helpful? Give feedback.
-
Many thanks @pmatilai ! That helped alot! |
Beta Was this translation helpful? Give feedback.
-
Best solution found :
$ rpm --eval '%define is_enabled() %["%{expr
%{%1}}"=="1"?"Enabled":"Disabled"]
%global a_var 1
%{warn:%{is_enabled:a_var}}'
warning: Enabled
$
Thanks Panu !
But I'm still not quite getting why this breaks it:
rpm --eval '%define is_enabled()
%["%{expr:%{%1}}"=="1"?"Enabled":"Disabled"]
ie. just adding the colon after expr ^- here
makes rpm emit : "error: parse error in expression: %{a_var}".
Why ? I am calling all other macros with ':' (colons) separating macro name
from parameters -
why can't I do that with expr ?
But anyway, problem solved ! Many thanks.
…On Tue, 14 Jun 2022 at 08:48, Panu Matilainen ***@***.***> wrote:
Good question 👍
A couple of fundamental problems there:
- function-like (aka parametric) macros, such as is_enabled() should
not be %global because with %global, the body is expanded at definition
time and any tests around eg %1 just melt away useless. Use %define
instead, and lot of weirdness goes away. Alternatively you can escape the
%1, but as a general guideline anything but simple constants, %define is
what you usually want. The "always use global" guidelines in various places
are misguided, really.
- the %{?%1} in is_enabled() tests whether that macro got an argument
or not, which is not what you want. Test with an expression instead.
For example:
[pmatilai🎩︎localhost ~]$ rpm --eval '%["%{?a_var}"=="1"?"yes":"no"]'
no
[pmatilai🎩︎localhost ~]$ rpm --define "a_var 0" --eval '%["%{?a_var}"=="1"?"yes":"no"]'
no
[pmatilai🎩︎localhost ~]$ rpm --define "a_var 1" --eval '%["%{?a_var}"=="1"?"yes":"no"]'
yes
(Whether you use --eval or --define is a matter of style, but it's better
to split stuff over multiple --define/--eval uses (they're simply processed
in order), makes it easier to handle for us humans.)
Turning that into a parametric macro gets you something like this:
[pmatilai🎩︎localhost rpm]$ rpm --define '%is_enabled() %["%{?1}"=="1"?"yes":"no"]' --eval '%{is_enabled %a_var}'
no
[pmatilai🎩︎localhost rpm]$ rpm --define 'a_var 0' --define '%is_enabled() %["%{?1}"=="1"?"yes":"no"]' --eval '%{is_enabled %a_var}'
no
[pmatilai🎩︎localhost rpm]$ rpm --define 'a_var 1' --define '%is_enabled() %["%{?1}"=="1"?"yes":"no"]' --eval '%{is_enabled %a_var}'
yes
Note quoting around the %1 argument test: macros always expand to string
values, so testing for integer value would only give you errors. The rest
kinda depends what exactly you want to do with it.
Hope that helps.
—
Reply to this email directly, view it on GitHub
<#2094 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AZTWV4GZQJKVMWNKOXW6RS3VPA2NRANCNFSM5YVWQDZQ>
.
You are receiving this because you authored the thread.Message ID:
***@***.***
.com>
|
Beta Was this translation helpful? Give feedback.
-
Aargh !
Why does this work with only RPM v4.17 (Fedora 36), not with RPM v4.14.3
(Rocky EL8) :
with RPM v4.17:
$ rpm --eval '%define is_enabled() %["%{expr %{%1}}"=="1"?"%2":"%3"]
%global a_var 1
%{warn:%{is_enabled a_var OK NOT_OK}}'
warning: OK
$
with RPM v4.14.3:
$ rpm --eval '%define is_enabled() %["%{expr %{%1}}"=="1"?"%2":"%3"]
%global a_var 1
%{warn %{is_enabled a_var OK NOT_OK}}'
warning:
$
…On Wed, 15 Jun 2022 at 18:44, Jason Vas Dias ***@***.***> wrote:
Best solution found :
$ rpm --eval '%define is_enabled() %["%{expr
%{%1}}"=="1"?"Enabled":"Disabled"]
%global a_var 1
%{warn:%{is_enabled:a_var}}'
warning: Enabled
$
Thanks Panu !
But I'm still not quite getting why this breaks it:
rpm --eval '%define is_enabled()
%["%{expr:%{%1}}"=="1"?"Enabled":"Disabled"]
ie. just adding the colon after expr ^- here
makes rpm emit : "error: parse error in expression: %{a_var}".
Why ? I am calling all other macros with ':' (colons) separating macro
name from parameters -
why can't I do that with expr ?
But anyway, problem solved ! Many thanks.
On Tue, 14 Jun 2022 at 08:48, Panu Matilainen ***@***.***>
wrote:
> Good question 👍
>
> A couple of fundamental problems there:
>
> - function-like (aka parametric) macros, such as is_enabled() should
> not be %global because with %global, the body is expanded at definition
> time and any tests around eg %1 just melt away useless. Use %define
> instead, and lot of weirdness goes away. Alternatively you can escape the
> %1, but as a general guideline anything but simple constants, %define is
> what you usually want. The "always use global" guidelines in various places
> are misguided, really.
> - the %{?%1} in is_enabled() tests whether that macro got an argument
> or not, which is not what you want. Test with an expression instead.
>
> For example:
>
> [pmatilai🎩︎localhost ~]$ rpm --eval '%["%{?a_var}"=="1"?"yes":"no"]'
>
> no
>
> [pmatilai🎩︎localhost ~]$ rpm --define "a_var 0" --eval '%["%{?a_var}"=="1"?"yes":"no"]'
>
> no
>
> [pmatilai🎩︎localhost ~]$ rpm --define "a_var 1" --eval '%["%{?a_var}"=="1"?"yes":"no"]'
>
> yes
>
>
> (Whether you use --eval or --define is a matter of style, but it's better
> to split stuff over multiple --define/--eval uses (they're simply processed
> in order), makes it easier to handle for us humans.)
>
> Turning that into a parametric macro gets you something like this:
>
> [pmatilai🎩︎localhost rpm]$ rpm --define '%is_enabled() %["%{?1}"=="1"?"yes":"no"]' --eval '%{is_enabled %a_var}'
>
> no
>
> [pmatilai🎩︎localhost rpm]$ rpm --define 'a_var 0' --define '%is_enabled() %["%{?1}"=="1"?"yes":"no"]' --eval '%{is_enabled %a_var}'
>
> no
>
> [pmatilai🎩︎localhost rpm]$ rpm --define 'a_var 1' --define '%is_enabled() %["%{?1}"=="1"?"yes":"no"]' --eval '%{is_enabled %a_var}'
>
> yes
>
>
> Note quoting around the %1 argument test: macros always expand to string
> values, so testing for integer value would only give you errors. The rest
> kinda depends what exactly you want to do with it.
>
> Hope that helps.
>
> —
> Reply to this email directly, view it on GitHub
> <#2094 (comment)>,
> or unsubscribe
> <https://github.com/notifications/unsubscribe-auth/AZTWV4GZQJKVMWNKOXW6RS3VPA2NRANCNFSM5YVWQDZQ>
> .
> You are receiving this because you authored the thread.Message ID:
> <rpm-software-management/rpm/repo-discussions/2094/comments/2944689@
> github.com>
>
|
Beta Was this translation helpful? Give feedback.
-
Or, more simply:
v4.17.0:
$ rpm --eval '%define is_enabled() %{expr "%{%1}"=="1"?"%2":"%3"}
%global a_var 1
%{warn %{is_enabled a_var OK NOT_OK}}'
warning: OK
v4.14.3:
$ rpm --eval '%define is_enabled() %{expr "%{%1}"=="1"?"%2":"%3"}
%global a_var 1
%{warn %{is_enabled a_var OK NOT_OK}}'
warning:
$
…On Fri, 17 Jun 2022 at 18:27, Jason Vas Dias ***@***.***> wrote:
Aargh !
Why does this work with only RPM v4.17 (Fedora 36), not with RPM v4.14.3
(Rocky EL8) :
with RPM v4.17:
$ rpm --eval '%define is_enabled() %["%{expr %{%1}}"=="1"?"%2":"%3"]
%global a_var 1
%{warn:%{is_enabled a_var OK NOT_OK}}'
warning: OK
$
with RPM v4.14.3:
$ rpm --eval '%define is_enabled() %["%{expr %{%1}}"=="1"?"%2":"%3"]
%global a_var 1
%{warn %{is_enabled a_var OK NOT_OK}}'
warning:
$
On Wed, 15 Jun 2022 at 18:44, Jason Vas Dias ***@***.***>
wrote:
> Best solution found :
>
> $ rpm --eval '%define is_enabled() %["%{expr
> %{%1}}"=="1"?"Enabled":"Disabled"]
> %global a_var 1
> %{warn:%{is_enabled:a_var}}'
> warning: Enabled
> $
>
> Thanks Panu !
>
> But I'm still not quite getting why this breaks it:
> rpm --eval '%define is_enabled()
> %["%{expr:%{%1}}"=="1"?"Enabled":"Disabled"]
> ie. just adding the colon after expr ^- here
> makes rpm emit : "error: parse error in expression: %{a_var}".
> Why ? I am calling all other macros with ':' (colons) separating macro
> name from parameters -
> why can't I do that with expr ?
>
> But anyway, problem solved ! Many thanks.
>
> On Tue, 14 Jun 2022 at 08:48, Panu Matilainen ***@***.***>
> wrote:
>
>> Good question 👍
>>
>> A couple of fundamental problems there:
>>
>> - function-like (aka parametric) macros, such as is_enabled() should
>> not be %global because with %global, the body is expanded at definition
>> time and any tests around eg %1 just melt away useless. Use %define
>> instead, and lot of weirdness goes away. Alternatively you can escape the
>> %1, but as a general guideline anything but simple constants, %define is
>> what you usually want. The "always use global" guidelines in various places
>> are misguided, really.
>> - the %{?%1} in is_enabled() tests whether that macro got an
>> argument or not, which is not what you want. Test with an expression
>> instead.
>>
>> For example:
>>
>> [pmatilai🎩︎localhost ~]$ rpm --eval '%["%{?a_var}"=="1"?"yes":"no"]'
>>
>> no
>>
>> [pmatilai🎩︎localhost ~]$ rpm --define "a_var 0" --eval '%["%{?a_var}"=="1"?"yes":"no"]'
>>
>> no
>>
>> [pmatilai🎩︎localhost ~]$ rpm --define "a_var 1" --eval '%["%{?a_var}"=="1"?"yes":"no"]'
>>
>> yes
>>
>>
>> (Whether you use --eval or --define is a matter of style, but it's
>> better to split stuff over multiple --define/--eval uses (they're simply
>> processed in order), makes it easier to handle for us humans.)
>>
>> Turning that into a parametric macro gets you something like this:
>>
>> [pmatilai🎩︎localhost rpm]$ rpm --define '%is_enabled() %["%{?1}"=="1"?"yes":"no"]' --eval '%{is_enabled %a_var}'
>>
>> no
>>
>> [pmatilai🎩︎localhost rpm]$ rpm --define 'a_var 0' --define '%is_enabled() %["%{?1}"=="1"?"yes":"no"]' --eval '%{is_enabled %a_var}'
>>
>> no
>>
>> [pmatilai🎩︎localhost rpm]$ rpm --define 'a_var 1' --define '%is_enabled() %["%{?1}"=="1"?"yes":"no"]' --eval '%{is_enabled %a_var}'
>>
>> yes
>>
>>
>> Note quoting around the %1 argument test: macros always expand to string
>> values, so testing for integer value would only give you errors. The rest
>> kinda depends what exactly you want to do with it.
>>
>> Hope that helps.
>>
>> —
>> Reply to this email directly, view it on GitHub
>> <#2094 (comment)>,
>> or unsubscribe
>> <https://github.com/notifications/unsubscribe-auth/AZTWV4GZQJKVMWNKOXW6RS3VPA2NRANCNFSM5YVWQDZQ>
>> .
>> You are receiving this because you authored the thread.Message ID:
>> <rpm-software-management/rpm/repo-discussions/2094/comments/2944689@
>> github.com>
>>
>
|
Beta Was this translation helpful? Give feedback.
-
OK, I found in the macro reference page :
"Note that in rpm >= 4.17, conditionals on built-in macros simply test for
existence of that built-in, just like with any other macros. In older
versions, the behavior of conditionals on built-ins is undefined."
but I am not testing a built-in here ?
When I change 'is_enabled' to just be '"%{%1}"==1?"%2":"%3",
I do get :
warning: 1==1?OK:NOT_OK
so, it looks like 'expr' in v4.14.3 does not honor the ternary conditional
operator 'A?B:C' ?
Is it meant to ? Where is the list of expressions that expr supports
listed for v4.14.3 ?
…On Fri, 17 Jun 2022 at 18:33, Jason Vas Dias ***@***.***> wrote:
Or, more simply:
v4.17.0:
$ rpm --eval '%define is_enabled() %{expr "%{%1}"=="1"?"%2":"%3"}
%global a_var 1
%{warn %{is_enabled a_var OK NOT_OK}}'
warning: OK
v4.14.3:
$ rpm --eval '%define is_enabled() %{expr "%{%1}"=="1"?"%2":"%3"}
> %global a_var 1
> %{warn %{is_enabled a_var OK NOT_OK}}'
warning:
$
On Fri, 17 Jun 2022 at 18:27, Jason Vas Dias ***@***.***>
wrote:
> Aargh !
>
> Why does this work with only RPM v4.17 (Fedora 36), not with RPM v4.14.3
> (Rocky EL8) :
>
> with RPM v4.17:
> $ rpm --eval '%define is_enabled() %["%{expr %{%1}}"=="1"?"%2":"%3"]
> %global a_var 1
> %{warn:%{is_enabled a_var OK NOT_OK}}'
> warning: OK
> $
>
> with RPM v4.14.3:
> $ rpm --eval '%define is_enabled() %["%{expr %{%1}}"=="1"?"%2":"%3"]
> %global a_var 1
> %{warn %{is_enabled a_var OK NOT_OK}}'
> warning:
> $
>
>
> On Wed, 15 Jun 2022 at 18:44, Jason Vas Dias ***@***.***>
> wrote:
>
>> Best solution found :
>>
>> $ rpm --eval '%define is_enabled() %["%{expr
>> %{%1}}"=="1"?"Enabled":"Disabled"]
>> %global a_var 1
>> %{warn:%{is_enabled:a_var}}'
>> warning: Enabled
>> $
>>
>> Thanks Panu !
>>
>> But I'm still not quite getting why this breaks it:
>> rpm --eval '%define is_enabled()
>> %["%{expr:%{%1}}"=="1"?"Enabled":"Disabled"]
>> ie. just adding the colon after expr ^- here
>> makes rpm emit : "error: parse error in expression: %{a_var}".
>> Why ? I am calling all other macros with ':' (colons) separating macro
>> name from parameters -
>> why can't I do that with expr ?
>>
>> But anyway, problem solved ! Many thanks.
>>
>> On Tue, 14 Jun 2022 at 08:48, Panu Matilainen ***@***.***>
>> wrote:
>>
>>> Good question 👍
>>>
>>> A couple of fundamental problems there:
>>>
>>> - function-like (aka parametric) macros, such as is_enabled()
>>> should not be %global because with %global, the body is expanded at
>>> definition time and any tests around eg %1 just melt away useless. Use
>>> %define instead, and lot of weirdness goes away. Alternatively you can
>>> escape the %1, but as a general guideline anything but simple constants,
>>> %define is what you usually want. The "always use global" guidelines in
>>> various places are misguided, really.
>>> - the %{?%1} in is_enabled() tests whether that macro got an
>>> argument or not, which is not what you want. Test with an expression
>>> instead.
>>>
>>> For example:
>>>
>>> [pmatilai🎩︎localhost ~]$ rpm --eval '%["%{?a_var}"=="1"?"yes":"no"]'
>>>
>>> no
>>>
>>> [pmatilai🎩︎localhost ~]$ rpm --define "a_var 0" --eval '%["%{?a_var}"=="1"?"yes":"no"]'
>>>
>>> no
>>>
>>> [pmatilai🎩︎localhost ~]$ rpm --define "a_var 1" --eval '%["%{?a_var}"=="1"?"yes":"no"]'
>>>
>>> yes
>>>
>>>
>>> (Whether you use --eval or --define is a matter of style, but it's
>>> better to split stuff over multiple --define/--eval uses (they're simply
>>> processed in order), makes it easier to handle for us humans.)
>>>
>>> Turning that into a parametric macro gets you something like this:
>>>
>>> [pmatilai🎩︎localhost rpm]$ rpm --define '%is_enabled() %["%{?1}"=="1"?"yes":"no"]' --eval '%{is_enabled %a_var}'
>>>
>>> no
>>>
>>> [pmatilai🎩︎localhost rpm]$ rpm --define 'a_var 0' --define '%is_enabled() %["%{?1}"=="1"?"yes":"no"]' --eval '%{is_enabled %a_var}'
>>>
>>> no
>>>
>>> [pmatilai🎩︎localhost rpm]$ rpm --define 'a_var 1' --define '%is_enabled() %["%{?1}"=="1"?"yes":"no"]' --eval '%{is_enabled %a_var}'
>>>
>>> yes
>>>
>>>
>>> Note quoting around the %1 argument test: macros always expand to
>>> string values, so testing for integer value would only give you errors. The
>>> rest kinda depends what exactly you want to do with it.
>>>
>>> Hope that helps.
>>>
>>> —
>>> Reply to this email directly, view it on GitHub
>>> <#2094 (comment)>,
>>> or unsubscribe
>>> <https://github.com/notifications/unsubscribe-auth/AZTWV4GZQJKVMWNKOXW6RS3VPA2NRANCNFSM5YVWQDZQ>
>>> .
>>> You are receiving this because you authored the thread.Message ID:
>>> <rpm-software-management/rpm/repo-discussions/2094/comments/2944689@
>>> github.com>
>>>
>>
|
Beta Was this translation helpful? Give feedback.
-
Good day -
Sorry my RPM skills are a bit rusty - could anyone please point me in the right direction to do:
I know I can print different strings for instance a %{warn...} message (or shell command for that matter) by doing something
like:
$ rpm --eval '
%global is_enabled() %{?%1:"Enabled"}%{!?%1:"Disabled"}
%global a_var %{nil}
%{warn %{is_enabled:%{a_var}}}
'
warning: "Disabled"
$
But what if I want to emit some string if and only if 'a_var' is 0, 1, 2, 3 - ie some value? This seems to be eluding me ...
$ rpm --eval '
%global is_enabled() %{?%1:"Enabled"}%{!?%1:"Disabled"}
%global a_var 1
%{warn %{is_enabled:%[%{a_var}==1]}}
'
warning: "Disabled"
$ rpm --eval '
%global is_enabled() %{?%1:"Enabled"}%{!?%1:"Disabled"}
%global a_var 1
%{warn:%{is_enabled:%{expr:"%{a_var}==1"}}}
'
warning: "Disabled"
Please, how can I get a macro to return a conditional value like that without having to use %if .. %endif ?
ie. ONLY if a_var is 1 (not %{nil} or 0 or any other value) do I want a macro to return a certain value.
What is the best way of doing this?
Is there a more complete / comprehensive / expansive reference to the RPM macro language other than :
https://rpm-software-management.github.io/rpm/manual/macros.html or
https://docs.fedoraproject.org/en-US/packaging-guidelines/RPMMacros/
these days ?
Thanks in advance for any replies !
Best Regards,
Jason
Beta Was this translation helpful? Give feedback.
All reactions