python-apt
包提供了两个访问APT缓存的API:
apt_pkg.Cache
A
对象表示 APT 使用的缓存,其中包含有关包的信息。该对象本身不提供修改缓存或已安装包的方法,请参阅类Cache
和DepCache
以了解此类功能。PackageManager
apt.Cache
APT 缓存文件包含一个哈希表,将二进制包的名称映射到它们的元数据。
对象是它的核心表示。它提供了对可用包列表的 APT 概念的访问。Cache
我不清楚为什么它们会包含不同的包集,但确实如此:
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缓存的方法有什么区别,为什么会返回不同的包集?
项目维护者 Julian Andres Klode 的回答:
只包含真实包,apt.Cache
中的也有虚拟包。您可以在apt_pkg
中看到它如何过滤apt/cache.py
.apt_pkg.Cache