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

huangapple go评论51阅读模式
英文:

Why do the two Python APIs for accessing the APT cache return different sets of packages?

问题

The python-apt package 提供了两种访问 APT 缓存的 API:

  • apt_pkg.Cache

    > 一个 Cache 对象表示了 APT 使用的缓存,其中包含有关软件包的信息。该对象本身不提供修改缓存或已安装软件包的功能,有关此功能,请参见DepCachePackageManager类。

  • 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))
# 在我的系统上,这个输出是: 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:

  • apt_pkg.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 classes DepCache and PackageManager for such functionality.

  • apt.Cache

    > 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.

huangapple
  • 本文由 发表于 2023年3月7日 02:04:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/75654312.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定