如何使用python程序获取包的Yum依赖项列表。这里的包本身是一个在CentOS 5.5上可用的包更新

2024-06-26 01:37:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试编写一个python程序,以获取使用pythonyumapi的包可用的依赖项列表。在

下面是我获取依赖项列表的代码,类似于“yum deplist chkconfig-1.3.49.3-2.el6”。这将生成所有所需软件包的列表,而不管系统上是否有已安装的列表。在

但我正在尝试编写一个包装器,它相当于命令“yum update chkconfig-1.3.49.3-2.el6”。此命令将生成系统上未安装且必需的依赖项。在

以下是我迄今为止尝试过的代码。还有没有其他方法可以访问pythonyumapi来满足我们的需求呢。这是实际的函数“customMethod”。其他的“compare”和“listcomare”用于比较列表中的RPM,并获取其中最新的RPM。在

    import sys, re
    import yum, rpm
    from yum import _
    sys.path.insert(0, '/usr/share/yum-cli')
    import output

    class YumFrame(yum.YumBase, output.YumOutput):
        def __init__(self):
            try:
                yum.YumBase.__init__(self)
                output.YumOutput.__init__(self)
            except Exception, e:
                raise e
            self.pattern1 = re.compile(r'^([a-zA-Z0-9_\-\+]*)-([a-zA-Z0-9_\.]*)-([a-zA-Z0-9_\.]*)')

        def compare(self, pkg1, pkg2):
            Info1 = self.pattern1.search(pkg1).groups()
            Info2 = self.pattern1.search(pkg2).groups()
            n1, v1, r1 = Info1
            n2, v2, r2 = Info2
            if n1 == n2:
                return rpm.labelCompare(('1', v1, r1), ('1', v2, r2))
            else:
                return 2

        def listCompare(self, input):
            latest = input[0]
            refinedList = []
            for index, item in enumerate(input):
                result = self.compare(item, latest)
                if result == 1:
                    latest = item
                elif result == 2:
                    refinedList.append(item)

            refinedList.append(latest)
            return refinedList

        def customMethod(self, package):
            pkgs = []
            completeList = []
            ematch, match, unmatch = self.pkgSack.matchPackageNames([package])
            for po in ematch + match:
                pkgs.append(po)
            print "Matched Object: " + str(pkgs)
            results = self.findDeps(pkgs)
            for value in results.itervalues():
                for packageObject in value.itervalues():
                    actualList = []
                    for item in packageObject:
                        completeList.append(item.name + "-" + item.ver + "-" + item.rel)
            completeList = self.listCompare(completeList)
            completeList = list(set(completeList))
            return completeList

    if __name__ == "__main__":
        yumObj = YumFrame()
        print yumObj.customMethod("chkconfig-1.3.49.3-2.el6")

提前谢谢你

M内存


Tags: inimportself列表forreturndefitem
1条回答
网友
1楼 · 发布于 2024-06-26 01:37:27

我不是python方面的专家,但在Yum CLI界面中有一个简单的方法:

yum erase [package name]

它将在实际删除之前向您显示一个依赖项列表。在

希望有帮助

相关问题 更多 >