英文:
Why do the two Python APIs for accessing the APT cache return different sets of packages?
问题
The python-apt
package 提供了两种访问 APT 缓存的 API:
-
> 一个
Cache
对象表示了 APT 使用的缓存,其中包含有关软件包的信息。该对象本身不提供修改缓存或已安装软件包的功能,有关此功能,请参见DepCache
和PackageManager
类。 -
> 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))
# 在我的系统上,这个输出是: 92488 64447
尽管似乎后者是前者的子集:
print(aptcache_pkgs - cache_pkgs)
# 在我的系统上,这个输出是: set()
一些脚本,比如Ubuntu 中的这个脚本,会同时使用这两种方法,如下所示:
# 我们需要另一个具有更多软件包详细信息的缓存
with apt.Cache() as aptcache:
for pkg in cache.packages:
aptcache[pkg.get_fullname()]
这两种访问 APT 缓存的方法之间有什么区别,为什么它们返回不同的软件包集合?
英文:
The python-apt
package provides two APIs for accessing the APT cache:
-
> A
Cache
object represents the cache used by APT which contains information about packages. The object itself provides no means to modify the cache or the installed packages, see the classesDepCache
andPackageManager
for such functionality. -
> The APT cache file contains a hash table mapping names of binary packages to their metadata. A
Cache
object is the in-core representation of the same. It provides access to APTs idea of the list of available packages.
It is unclear to me why they would contain different sets of packages, but indeed they do:
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
Though it appears that the latter is a subset of the former:
print(aptcache_pkgs - cache_pkgs)
# on my system, this outputs: set()
Some scripts like this one from Ubuntu will use both, like this:
# we need another cache that has more pkg details
with apt.Cache() as aptcache:
for pkg in cache.packages:
aptcache[pkg.get_fullname()]
What is the distinction between these two methods of accessing the APT cache and why do they return different sets of packages?
答案1
得分: 0
来自该项目的维护者Julian Andres Klode的回答:
apt.Cache
仅包括真实的软件包,而apt_pkg
中的那个也包括虚拟软件包。您可以在apt/cache.py
中看到它是如何过滤apt_pkg.Cache
的。
英文:
Answer from Julian Andres Klode, a maintainer of the project:
> apt.Cache
only includes real packages, the one in apt_pkg
also has virtual packages. You can see that in apt/cache.py
how it filters the apt_pkg.Cache
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论