检查软件包是否安装在 rpm spec 文件中的正确方法

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

我以前从未使用过 rpm spec 文件,所以答案很明显。我的自定义 rpm 有几个版本来简化,让它成为 1.0.0 和 2.0.0。例如,它可以使用或不使用 packageA,但如果为版本 2.0.0 安装了 packageA,则它至少应为 7.0.0。出于测试目的,我创建了 hello-world.spec 文件。

$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
.......

在安装 packageA-client 的系统上:

[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

这意味着找到 packageA-client 并且预计会出现错误

然后我尝试 运行 在未安装 packageA-client 的系统上执行相同的操作:

[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)]#

我预计错误会是 packageA-client = 0 is needed by hello-world-1-1.x86_64 因为它应该进入 else 条件,因为 if 不正确,因为找不到 packageA-client。这里有什么问题以及实现这样的逻辑的正确方法是什么。

看看有没有包含全部功能的虚拟“提供”。有的话求一下。

然后使用版本范围将冲突指令放入包中,以避免提供错误版本号的包。

通过声明式定义需求和冲突,您的逻辑将由解析器(解析器引擎的一部分)处理,这意味着您的包不必在安装到 运行 这是逻辑,逻辑将在更广泛的场景中正常工作(并且在“这个包中放置的任何内容”之外失败)

Conflicts 标签可能是您要查找的内容:

Conflicts: packageA < 7.0.0

如果安装了 packageA 早于 7.0.0,这只会阻止安装。此外,如果 packageA 尚未安装,则不会导致安装。

我相信它还会在安装 hello-world 之后阻止安装 packageA 的旧版本。