使用Python比较RPM包

时间:2012-02-21 15:30:42

标签: python csv rpm

我正在尝试将包含所需Linux软件包的csv文件与当前安装的软件包进行比较。比较应输出未安装或比当前安装的软件包更新的任何软件包。

问题是我无法遍历已安装软件包的列表并显示所有匹配,例如具有相同名称和版本的软件包,但不同的体系结构应显示两次(例如compat-libstdc ++ - 33) ,但我只是用下面的脚本获得了第一次点击。

#!/usr/bin/python

import rpm
import csv
import sys
import os

'''
Script to check installed rpms against a csv file containing the package name and version similar to the list below:
atk,1.12.2
libart_lgpl,2.3
info,4.9
libsepol,1.15.2
libusb,0.1.12
libfontenc,1.4.2

'''

if len(sys.argv) !=2:
        print ''
        print 'Usage: ', sys.argv[0], '/path/to/csv_input_file'
        print ''
        sys.exit(1)

if not os.path.isfile(sys.argv[1]):
        print ''
        print sys.argv[1], 'not found!'
        print ''
        sys.exit(1)

else:
        input_csv = sys.argv[1]

pkgRequired = csv.reader(open(input_csv),delimiter=',')

pkgInstalledName = []
pkgInstalledVersion = []
pkgInstalledArch = []

ts = rpm.TransactionSet()
mi = ts.dbMatch()

for h in mi:
        pkgInstalledName.append((h['name']))
        pkgInstalledVersion.append((h['version']))
        pkgInstalledArch.append((h['arch']))

for row in pkgRequired:
        pkgRequiredName = row[0]
        pkgRequiredVersion = row[1]
        #pkgRequiredArch = row[2]

        if pkgRequiredName in pkgInstalledName:

                if pkgInstalledVersion[pkgInstalledName.index(pkgRequiredName)] >= pkgRequiredVersion:
                        pass
                else:
                        print '\nInstalled:  ',pkgInstalledName[pkgInstalledName.index(pkgRequiredName)], pkgInstalledVersion[pkgInstalledName.index(pkgRequiredName)], pkgInstalledArch[pkgInstalledName.index(pkgRequiredName)], ' \nRequired: ', ' ', pkgRequiredName,pkgRequiredVersion

2 个答案:

答案 0 :(得分:0)

假设您正在读取已安装软件包列表的方式没有问题(我不熟悉rpm模块),那么您唯一的问题是使用index()函数。此函数返回具有指定值的项目的第一次出现 - 并且它不是您想要的。

正确的实施(效率也更高)将是:

installedPackages = {} #create a hash table, mapping package names to LISTS of installed package versions and architectures
for h in mi: 
  l = installedPackages.get(h['name'], list()) #return either the existing list, or a new one if this is the first time that the name appears.
  l.append( (h['version'], h['arch']) )

...
if requiredPackageName in installedPackages:
  for ver, arch in installedPackages[requiredPackageName]: print ...

答案 1 :(得分:0)

这就是我最终要做的工作。该脚本目前没有检查所需包的体系结构,但至少它显示了已安装的arch。该脚本可以工作(据我所知),但可以作为我的第一个在python上改进:)

    #!/usr/bin/python

import rpm
import csv
import sys
import os

'''
Script to check installed rpms against a csv file containing the package name and version similar to the list below:
atk,1.12.2
libart_lgpl,2.3
info,4.9
libsepol,1.15.2
libusb,0.1.12
libfontenc,1.4.2

'''

#silverbullet - 20120301

if len(sys.argv) !=2:
        print ''
        print 'Usage: ', sys.argv[0], '/path/to/csv_input_file'
        print ''
        sys.exit(1)

if not os.path.isfile(sys.argv[1]):
        print ''
        print sys.argv[1], 'not found!'
        print ''
        sys.exit(1)

else:
        input_csv = sys.argv[1]

pkgRequired = csv.reader(open(input_csv),delimiter=',')

pkgInstalledName = []
pkgInstalledVersion = []
pkgInstalledArch = []

ts = rpm.TransactionSet()
mi = ts.dbMatch()

for h in mi:
        pkgInstalledName.append((h['name']))
        pkgInstalledVersion.append((h['version']))
        pkgInstalledArch.append((h['arch']))

for row in pkgRequired:
        try:
                pkgRequiredName = row[0]
                pkgRequiredVersion = row[1]
                #pkgRequiredArch = row[2] - This is not implemented yet, ie, script will ignore architecture in csv input file
        except:
                print "Unexpected Error. Check if input is csv format with no blank lines. "#, sys.exc_info()[1]
                break
        else:
                for pos, pkg in enumerate(pkgInstalledName):
                        if pkg == pkgRequiredName:
                                if pkgInstalledVersion[pos] >= pkgRequiredVersion:
                                        pass
                                else:
                                        print '\nInstalled:', pkgInstalledName[pos], pkgInstalledVersion[pos], pkgInstalledArch[pos], '\nRequired: ', pkg, pkgRequiredVersion