用于检查rpm的Python是更高版本

时间:2017-08-19 12:51:34

标签: python python-2.7 rpm

任何人都可以帮我检查Linux以满足rpm版本

下面的命令,我曾经获得包版本。

  rpm -qi libstdc++6 | awk -F': ' '/Version/ {print $2}'

使用子流程,我得到了以下

 6.2.1+r239768

现在我要比较6.2.1+r239768大于5.1.1

以下是我预期的结果

output = 6.2.1+r239768
print "This rpm version is supported" 
output = 6.3+r23
print "This rpm version is supported"
output = 7.1.1+r57678
print "This rpm version is supported"
output = 5.1.1+r23677
print "This rpm version is not supported"
output = 4.1+r56888
print "This rpm version is not supported"

4 个答案:

答案 0 :(得分:3)

您可以使用pkg_resources模块,因为它是setuptools的一部分,您应该已经拥有该模块:

In []:
from pkg_resources import parse_version
parse_version('6.2.1+r239768') > parse_version('5.1.1')

Out[]:
True

所以要得到你的输出:

In []:
versions = ['6.2.1+r239768', '6.3+r23', '7.1.1+r57678', '5.1.1+r23677', '4.1+r56888']
base = parse_version('6.2.1+r239768')
for v in versions:
    print("Output = {}".format(v))
    print("This rpm version is {}supported".format('not ' if parse_version(v) < base else ''))

Out[]:
Output = 6.2.1+r239768
This rpm version is supported
Output = 6.3+r23
This rpm version is supported
Output = 7.1.1+r57678
This rpm version is supported
Output = 5.1.1+r23677
This rpm version is not supported
Output = 4.1+r56888
This rpm version is not supported

答案 1 :(得分:1)

我觉得你应该更好地使用python API for rpm

由于我现在手头没有基于RPM的系统,所以无法提供代码。

以下代码taken from Section 16.5可以相应调整

#!/usr/bin/python
# Reads in package header, compares to installed package.
# Usage:
# python vercompare.py libstdc++6.rpm
#
import os
import sys

import rpm
def readRpmHeader(ts, filename):
    """ Read an rpm header. """
    fd = os.open(filename, os.O_RDONLY)
    try:
        h = ts.hdrFromFdno(fd)
    finally:
        os.close(fd)
    return h

ts = rpm.TransactionSet()
h = readRpmHeader( ts, sys.argv[1] )
pkg_ds = h.dsOfHeader()
for inst_h in ts.dbMatch('name', h['name']):
    inst_ds = inst_h.dsOfHeader()
    if pkg_ds.EVR() >= inst_ds.EVR():
        print "Package file is same or newer, OK to upgrade."
    else:
        print "Package file is older than installed version."

答案 2 :(得分:1)

$ rpmdev-vercmp 6.2.1+r239768 6.3+r23 6.2.1+r239768 < 6.3+r23

您可以阅读退出代码: rpmdev-vercmp <EVR1> <EVR2>

如果EVR相等则退出状态为0,如果EVR1更新则退出11,如果EVR2则退出12 更新。其他退出状态表明存在问题。

答案 3 :(得分:0)

python rpm库为您提供了一种比较EVR(Epoch,Version,Release)的方法,但似乎没有为您提供将字符串拆分为这些部分的代码。尽管如此,这是微不足道的。这是一个小工作计划。

#! /usr/bin/env python

import re
import sys

import rpm

evr_re = re.compile(r'^(?:([^:]*):)?([^-]+)(?:-(.*))?$')


def str_to_evr(s):
    mo = evr_re.match(s)
    if not mo:
        raise ValueError("not a valid version string")
    return mo.groups()


def main():
    if len(sys.argv) != 3:
        raise SystemExit("usage: vercmp VER1 VER2")
    try:
        evr_a = str_to_evr(sys.argv[1].strip())
        evr_b = str_to_evr(sys.argv[2].strip())
        result = rpm.labelCompare(evr_a, evr_b)
    except Exception as exc:
        raise SystemExit("Problem converting or comparing versions: %s %s" % (
            type(exc).__name__, exc))
    print (result)


if __name__ == "__main__":
    main()