Sunday, March 13, 2022

[SOLVED] How to call function with arguments in rpm spec file?

Issue

I'm building an rpm to install a component on my machines, but I would like to check if certain python libraries are already installed so as not to reinstall them unnecessarily. So, in the %post section of my spec file, I'm doing this:

function check4pythonlib() {
    library=$1
    if [[ $(/usr/local/bin/python2.7 -c "import $library" 2> /dev/null ; echo $?) -eq 0 ]]; then
        echo "$library is installed"
    else
        echo "$library is not installed"
        echo "Installing $library..."
        cd /path/to/lib/$library
        /usr/local/bin/python2.7 setup.py build
        /usr/local/bin/python2.7 setup.py install
    fi
}

check4pythonlib pythonlib1
check4pythonlib pythonlib2

I'm writing all output to a log file, and I see this:

 is not installed
Installing ...
/usr/local/bin/python2.7: can't open file 'setup.py': [Errno 2] No such file or directory
/usr/local/bin/python2.7: can't open file 'setup.py': [Errno 2] No such file or directory
 is not installed
Installing ...
/usr/local/bin/python2.7: can't open file 'setup.py': [Errno 2] No such file or directory
/usr/local/bin/python2.7: can't open file 'setup.py': [Errno 2] No such file or directory

It seems the argument is not being passed to the function. I have also tried enclosing the arguments in double quotes, but it does not work either. What can I do to properly pass the arguments during rpm installation, so that this works?


Solution

So, I noticed that when running that section of the spec file with set -vx as Etan Reisner suggested, something weird would occur... the output looked like this:

function check4pythonlib() {
    library=pythonlib1
    if [[ $(/usr/local/bin/python2.7 -c "import " 2> /dev/null ; echo $?) ]]; then
        echo " is installed"
    else
        echo " is not installed"
        echo "Installing ..."
        cd /path/to/lib/
        /usr/local/bin/python2.7 setup.py build
        /usr/local/bin/python2.7 setup.py install
    fi
}

So clearly the argument pythonlib1 was making it inside the function, but for some reason I could not see the variable I had assigned it to (in this case $library).

So this is what I ended up doing:

function check4pythonlib() {
    if [[ $(/usr/local/bin/python2.7 -c "import $1" 2> /dev/null ; echo $?) ]]; then
        echo "$1 is installed"
    else
        echo "$1 is not installed"
        echo "Installing $1..."
        cd /path/to/lib/$1
        /usr/local/bin/python2.7 setup.py build
        /usr/local/bin/python2.7 setup.py install
    fi
}

I replaced the variable $library directly with the argument, in this case $1. Dirty, but now it works. Now I just need to figure out why variables seem to disappear when used inside spec files...



Answered By - Tino
Answer Checked By - Senaida (WPSolving Volunteer)