Search code examples
bashrpmrpm-spec

The right way to check if package is installed in rpm spec file


I never worked with rpm spec files before so the answer can be quite obvious. My custom rpm has several versions to simplify let it be 1.0.0 and 2.0.0. For example, it can work with or without packageA but if packageA is installed for version 2.0.0 it should be at least 7.0.0. For testing purposes I create hello-world.spec file.

$cat hello.spec
[mylaptop]# cat ~/hello.spec 
Name:       hello-world
Version:    1
Release:    1
Summary:    Most simple RPM package
License:    FIXME
%define packageA_installed %(rpm -qa packageA-client)

 
%define version 2.2.0


%if "%packageA_installed"
%global with_packageA 1
# just for test purpose it will be deleted after testing and I will only set with_packageA
Requires: packageA-client == 1
%else
# just for test purpose it will be deleted after testing and I will only set with_packageA 
Requires: packageA-client == 0
%global with_packageA 0
%endif

# I need check if packageA is installed and current rpm version 2.2.0
%if "%with_packageA" == "1" && "%{version}" == "2.2.0"
#if true - for 2.2.0 can work only with 7.0.0 and higher packageA
Requires: packageA-client >= 7.0.0
%endif
.......

On the system where packageA-client is installed:

[mylaptop(with packageA-client)]# rpm -qa packageA-client 
packageA-client-7.0.0-93073D.RedHat7.x86_64
[mylaptop(with packageA-client)]# rpm -i hello-world-1-1.x86_64.rpm 
error: Failed dependencies:
      packageA-client = 1 is needed by hello-world-1-1.x86_64

It means that packageA-client was found and error is expected

Then I try to run the same on the system where is packageA-client is not installed:

[mylaptop(without packageA-client)]# rpm -qa packageA-client
[mylaptop(without packageA-client)]# rpm -i ~/hello-world-1-1.x86_64.rpm 
error: Failed dependencies:
    packageA-client = 1 is needed by hello-world-1-1.x86_64
    packageA-cllent >= 7.0.0 is needed by hello-world-1-1.x86_64
[mylaptop(without packageA-client)]#

I expect that error will be packageA-client = 0 is needed by hello-world-1-1.x86_64 since it should go to the else condition because if not true since packageA-client was not found. What is wrong here and what is the right way to implement logic like this.


Solution

  • The Conflicts tag may be what you are looking for:

    Conflicts: packageA < 7.0.0
    

    This will only block installation if packageA is installed and is older than 7.0.0. Further, it will not cause packageA to be installed if it isn't already.

    I believe it will also prevent an older version of packageA from being installed after hello-world is installed.