为什么访问APT缓存的两个Python API返回不同的包集?

问题描述 投票:0回答:1

python-apt
包提供了两个访问APT缓存的API:

  • apt_pkg.Cache

    A

    Cache
    对象表示 APT 使用的缓存,其中包含有关包的信息。该对象本身不提供修改缓存或已安装包的方法,请参阅类
    DepCache
    PackageManager
    以了解此类功能。

  • apt.Cache

    APT 缓存文件包含一个哈希表,将二进制包的名称映射到它们的元数据。

    Cache
    对象是它的核心表示。它提供了对可用包列表的 APT 概念的访问。

我不清楚为什么它们会包含不同的包集,但确实如此:

import apt, apt_pkg

cache = apt_pkg.Cache(apt.progress.base.OpProgress())
cache_pkgs = set(pkg.get_fullname() for pkg in cache.packages)

aptcache = apt.Cache(apt.progress.base.OpProgress())
aptcache_pkgs = set(pkg.fullname for pkg in aptcache)

print(len(cache_pkgs), len(aptcache_pkgs))
# on my system, this outputs: 92488 64447

虽然看起来后者是前者的子集:

print(aptcache_pkgs - cache_pkgs)
# on my system, this outputs: set()

一些脚本像这个来自 Ubuntu 的脚本会同时使用这两者,像这样:

# we need another cache that has more pkg details
with apt.Cache() as aptcache:
    for pkg in cache.packages:
        aptcache[pkg.get_fullname()]

这两种访问APT缓存的方法有什么区别,为什么会返回不同的包集?

apt python-apt
1个回答
0
投票

项目维护者 Julian Andres Klode 的回答

apt.Cache
只包含真实包,
apt_pkg
中的也有虚拟包。您可以在
apt/cache.py
中看到它如何过滤
apt_pkg.Cache
.

© www.soinside.com 2019 - 2024. All rights reserved.