【问题标题】:python apt_pkg to obtain individual pkg details?python apt_pkg 获取个人 pkg 详细信息?
【发布时间】:2014-10-15 19:23:32
【问题描述】:

我一直在使用 apt_pkg 和 apt 库的组合来从每个包中获取以下详细信息:

package.name
package.installedVersion
package.description
package.homepage
package.priority

我能够通过以下方式获得我需要的东西,我不完全确定这是获得结果的最佳方法:

import apt_pkg, apt

apt_pkg.InitConfig()
apt_pkg.InitSystem()

aptpkg_cache = apt_pkg.GetCache() #Low level 
apt_cache = apt.Cache() #High level

apt_cache.update()
apt_cache.open()

pkgs = {}
list_pkgs = []

for package in aptpkg_cache.Packages:
       try:
          #I use this to pass in the pkg name from the apt_pkg.packages
          #to the high level apt_cache which allows me to obtain the
          #details I need. Is it better to just stick to one library here?
          #In other words, can I obtain this with just apt_pkg instead of using apt?

          selected_package = apt_cache[package.name]

          #Verify that the package can be upgraded
          if check_pkg_status(package) == "upgradable":
           pkgs["name"] = selected_package.name
               pkgs["version"] = selected_package.installedVersion
               pkgs["desc"] = selected_package.description
               pkgs["homepage"] = selected_package.homepage
               pkgs["severity"] = selected_package.prority

               list_pkgs.append(pkgs)
          else:
               print "Package: " + package.name + " does not exist"
               pass #Not upgradable?

        except:
          pass #This is one of the main reasons why I want to try a different method.
              #I'm using this Try/Catch because there are a lot of times that when
              #I pass in package.name to apt_cache[], I get error that package does not
              #exists... 


def check_pkg_status(package):
        versions = package.VersionList
        version = versions[0]
        for other_version in versions:
            if apt_pkg.VersionCompare(version.VerStr, other_version.VerStr)<0:
                version = other_version

        if package.CurrentVer:
            current = package.CurrentVer
            if apt_pkg.VersionCompare(current.VerStr, version.VerStr)<0:
                return "upgradable"
            else:
                return "current"
        else:
            return "uninstalled"

我想找到一种使用 apt_pkg/apt 获取每个可能升级/更新候选包的详细信息的好方法?

我目前这样做的方式是,我只获取系统中已有软件包的更新/升级,尽管 我注意到 Debian 的更新管理器向我显示了我系统中没有的软件包。

【问题讨论】:

标签: python debian apt


【解决方案1】:

以下脚本基于您的 python 代码,适用于我的 Ubuntu 12.04,也适用于任何具有 python-apt 0.8+ 的系统

import apt

apt_cache = apt.Cache() #High level

apt_cache.update()
apt_cache.open()

list_pkgs = []

for package_name in apt_cache.keys():
    selected_package = apt_cache[package_name]

    #Verify that the package can be upgraded
    if selected_package.isUpgradable:
        pkg = dict(
            name=selected_package.name,
            version= selected_package.installedVersion,
            desc= selected_package.description,
            homepage= selected_package.homepage,
            severity= selected_package.priority)
        list_pkgs.append(pkg)

print list_pkgs

【讨论】:

  • 这让我大吃一惊,我得到了 195 个可用更新的列表,但是当我运行 updater 时,它显示了 201 个??
  • @Dayan 可能是某些升级版本的 pkgs 有新的依赖项,因此需要安装更多 pkgs。尝试比较这两个列表,看看缺少的六个列表中有什么
猜你喜欢
  • 2013-07-02
  • 1970-01-01
  • 1970-01-01
  • 2023-03-04
  • 2011-01-13
  • 2011-09-01
  • 2013-03-31
  • 1970-01-01
  • 2013-09-06
相关资源
最近更新 更多