Search notes:

Python library: apt_pkg

Print the information about a given package

import os
import apt_pkg

apt_pkg.init()

pkg_dir  = '/var/lib/apt/lists/'
pkg_name = 'xxgdb'

for filename in os.listdir(pkg_dir):
    filepath = os.path.join(pkg_dir, filename)

    if not filename.endswith('Packages'):
        continue

    pkg_tag_file = apt_pkg.TagFile(open(filepath, 'r'))

    for pkg in pkg_tag_file:
        if pkg.get('Package') == pkg_name:
            print(f'Package:        {pkg.get("Package"       )}')
            print(f'Version:        {pkg.get("Version"       )}')
            print(f'Architecture:   {pkg.get("Architecture"  )}')
            print(f'Maintainer:     {pkg.get("Maintainer"    )}')
            print(f'Installed-Size: {pkg.get("Installed-Size")}')
            print(f'Depends:        {pkg.get("Depends"       )}')
            print(f'Recommends:     {pkg.get("Recommends"    )}')
            print(f'Suggests:       {pkg.get("Suggests"      )}')
            print(f'Section:        {pkg.get("Section"       )}')
            print(f'Priority:       {pkg.get("Priority"      )}')
            print(f'Homepage:       {pkg.get("Homepage"      )}')
            print(f'Description:    {pkg.get("Description"   )}')
            print(f'Source:         {pkg.get("Source"        )}')
            quit()

print(f'Package {pkg} not found in the package index files.')

Print list of all sections

import apt_pkg
import os

apt_pkg.init()

pkg_dir = '/var/lib/apt/lists/'
sections = set()

for filename in os.listdir(pkg_dir):
    filepath = os.path.join(pkg_dir, filename)

    if not filename.endswith('Packages'):
     # Only package index files are interesting
       continue

    pkg_tag_file = apt_pkg.TagFile(open(filepath, 'r'))

    for pkg in pkg_tag_file:
        section = pkg.get('Section')
        if section is not None:
            sections.add(section)

for section in sorted(sections):
    print(section)

All packages in a given section

import os
import apt_pkg

apt_pkg.init()

pkg_dir = '/var/lib/apt/lists/'

for filename in os.listdir(pkg_dir):
    filepath = os.path.join(pkg_dir, filename)

    if not filename.endswith('Packages'):
     # Only package index files are interesting
       continue

  # Initialize new package tag file object
    pkg_tag_file = apt_pkg.TagFile(open(filepath, 'r'))

    for pkg in pkg_tag_file:
        if pkg.get('Section') == 'fonts':
            print(pkg.get('Package'))

Installation

On Debian:
sudo apt install -y python3-apt --fix-missing

Index

Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 8 attempt to write a readonly database in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php:78 Stack trace: #0 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(78): PDOStatement->execute(Array) #1 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(30): insert_webrequest_('/notes/developm...', 1759399949, '216.73.216.42', 'Mozilla/5.0 App...', NULL) #2 /home/httpd/vhosts/renenyffenegger.ch/httpsdocs/notes/development/languages/Python/libraries/apt_pkg/index(122): insert_webrequest() #3 {main} thrown in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php on line 78