1

I have the following problem: I wrote a script, that extracts information about the installed packages of my system (Ubuntu 16.04 LTS). I am particularly interested in the source of the package. This means, that the data of APT-Sources from apt show <packagename> is crucial for me.

As of now, my script has to call apt show for every single installed package which creates an almost unacceptable workload in comparison to how small of a task this should be [the CPU load reaches almost 100%].

I was hoping, that there was some file on the system, that has all the information stored, that is output by apt show. Reading and parsing that file should be faster than calling apt show thousands of times. Is there such a file?


Please note, that I already tried to use dpkgand apt-cache, but both do not provide the APT-Sources information.


edit: Maybe some elaboration might be useful. My Python script calls apt list --installed to get the list of installed packages and parses this output into a list, containing only the package names as strings.

Then it calls apt show for every element in this list.

I would have liked, to only have a single file, read once, that contains information about the installed packages. I then would have my script parse this file, add the information to the list element and be done in one iteration. My hope was, that reading a large file once and parsing it, is faster than calling a CLI command many hundreds of times.

As such, I assume, that greping over multiple files multiple times would not really decrease the workload.

muru
  • 207,228
KyuMirthu
  • 113

2 Answers2

1

Here:

ls /var/lib/apt/lists

depend on the repository and section, for example to gather data about wget in main section for amd64 architecture you can use:

grep -A20 "Package: wget" /var/lib/apt/lists/*_ubuntu_dists_xenial-updates_main_binary-amd64_Packages

or as Muru suggested use awk for more flexible result:

awk -v RS='\n\n' -v pkg=wget '$2 == pkg' /var/lib/apt/lists/*_ubuntu_dists_xenial_main_binary-amd64*
  • Using RS (Record separator) we can easily get all data related to our package.

Note that apt also uses some binary caches made of above files to increase the speed of its queries, these caches are located here:

ls /var/cache/apt/
Ravexina
  • 57,256
0

I basically implemented a system to watch over the installed packages in our landscape, collecting information about them and checking, if some of those are installed with a wrong version. Since some of these packages come from our own intern repositories, it is necessary to know, where the package came from.

Using the Python APT API:

#! /usr/bin/python3
import apt
cache = apt.cache.Cache()
for pkg in cache:
    if pkg.is_installed:
         name = pkg.name
         version = pkg.installed.version
         origins = [o.site for o in pkg.installed.origins if o.site]
         print(name, version, origins)

Example output:

$ ./foo.py| head
a11y-profile-manager-indicator 0.1.10-0ubuntu3 ['jp.archive.ubuntu.com']
account-plugin-facebook 0.12+16.04.20160126-0ubuntu1 ['jp.archive.ubuntu.com', 'jp.archive.ubuntu.com']
account-plugin-flickr 0.12+16.04.20160126-0ubuntu1 ['jp.archive.ubuntu.com', 'jp.archive.ubuntu.com']
account-plugin-google 0.12+16.04.20160126-0ubuntu1 ['jp.archive.ubuntu.com', 'jp.archive.ubuntu.com']
accountsservice 0.6.40-2ubuntu11.3 ['jp.archive.ubuntu.com']
acl 2.2.52-3 ['jp.archive.ubuntu.com']
acpi-support 0.142 ['jp.archive.ubuntu.com']
acpid 1:2.0.26-1ubuntu2 ['jp.archive.ubuntu.com']
activity-log-manager 0.9.7-0ubuntu23.16.04.1 ['jp.archive.ubuntu.com']
adduser 3.113+nmu3ubuntu4 ['jp.archive.ubuntu.com', 'jp.archive.ubuntu.com']
muru
  • 207,228