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

时间:2020-06-24 12:46:38

标签: bash rpm rpm-spec

我之前从未使用过rpm规范文件,因此答案很明显。我的自定义rpm有几个版本可以简化为1.0.0和2.0.0。例如,它可以与packageA一起使用,也可以不与packageA一起使用,但是如果为版本2.0.0安装了packageA,则它至少应为7.0.0。为了进行测试,我创建了hello-world.spec文件。

from sqlalchemy import Column, ForeignKey
from sqlalchemy.dialects.mysql import *
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship
import datetime

Base = declarative_base()

class IterHousingsTask(Base):
    __tablename__ = 'iter_housings_task'
    __table_args__ = {'mysql_charset': 'utf8mb4', 'mysql_collate': 'utf8mb4_bin'}

    id = Column(INTEGER, autoincrement=True, primary_key=True)
    domain = Column(MEDIUMTEXT)
    uuid = Column(VARCHAR(350), unique=True, nullable=False)

    creation_time = Column(DATETIME)
    end_time = Column(DATETIME)
    elapsed = Column(INTEGER)
    total_added_listings = Column(INTEGER, default=0)
    total_updated_listings = Column(INTEGER, default=0)
    total_pages = Column(INTEGER, default=0)
    last_page_scraped = Column(INTEGER, default=0)
    day = Column(DATE)
    is_done = Column(BOOLEAN)

    housings = relationship("Housing")

    def __init__(self, uuid=None, day=None, is_done=False, creation_time=None, domain=None):
        self.uuid = uuid
        self.day = day
        self.is_done = is_done
        self.creation_time = creation_time
        self.domain = domain


class Housing(Base):
    __tablename__ = 'housing'
    __table_args__ = {'mysql_charset':'utf8mb4', 'mysql_collate': 'utf8mb4_bin'}

    id = Column(INTEGER, autoincrement=True, primary_key=True)
    internal_id = Column(MEDIUMTEXT)
    first_scraping_time = Column(DATETIME)
    first_scraping_date = Column(DATE)
    is_active = Column(BOOLEAN)
    url = Column(VARCHAR(350), unique=True)
    flags = Column(MEDIUMTEXT)
    email = Column(MEDIUMTEXT)
    phone = Column(MEDIUMTEXT)
    url_1 = Column(VARCHAR(350))
    size = Column(FLOAT)
    available = Column(DATE)
    description = Column(LONGTEXT)
    task_id = Column(VARCHAR(350), ForeignKey(IterHousingsTask.uuid))


def create_all(engine):
    print("creating databases")
    Base.metadata.create_all(engine)

在安装packageA-client的系统上:

$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,并且预期会出现错误

然后我尝试在未安装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

我希望该错误将是[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不正确,因为未找到packageA-client。这里出了什么问题,什么是实现这种逻辑的正确方法。

2 个答案:

答案 0 :(得分:2)

您可能正在寻找Conflicts标签:

Conflicts: packageA < 7.0.0

仅当安装了packageA并且 早于7.0.0时,此命令才会阻止安装。此外,如果packageA尚未安装,则不会导致安装。

我相信这也会阻止在安装{em> packageA 之后安装hello-world的旧版本。

答案 1 :(得分:-1)

查看是否有一个包含整体功能的虚拟“提供”。如果有,请要求。

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

通过声明性地定义需求和冲突,您的逻辑将由解析器(求解器引擎的一部分)处理,这意味着您的程序包不必在安装过程中就可以运行其逻辑,并且逻辑将在更大范围的场景中正常工作(并且在“此程序包中的内容”之外失败)