Package: pydebcontents
pydebcontents: Searching Debian Contents filesPackage repositories published by Debian (and its derivatives) have lots of different index files describing the Releases, Packages, Sources, and the file Contents of the packages.
The Debian wiki has afull description of the repository format.Access to the data within the Release, Packages, and Sources files is provided by thepython-debianmodule, available within the Debian archive and from PyPI.This module provides access to the Contents files.RequirementsThis module requires no Python modules outside of stdlib.Searching the Contents files is, however, dependent on the externalzgrepprogram being on your PATH;zgrepis used to transparently search the gzip-compressedContents.gzfiles.The Contents files need to be arranged as they would be found on a Debian mirror:dists/{release}/{component}/Contents-{arch}.gz.Users of theapt-cacher-ngpackage might like to use its local file cache for access to the Contents files in the expected format.InstallationFrom PyPI:pip install pydebcontentsFrom git:git clone https://salsa.debian.org/debian-irc-team/pydebcontents
cd pydebcontents
pip install .UsageThe module comes with a simple command-line interface that feels a bit like the standardapt-fileprogram.For example, to find all the README files shipped in packages:py-apt-file --base /var/cache/apt-cacher-ng/debrep/ search --mode glob  usr/share/doc/*/READMEThe only verb thatpy-apt-fileknows at present issearch.$ py-apt-file search --help
usage: py-apt-file search [-h] [--release RELEASE] [--arch ARCH] [--component COMP] [--mode {glob,regex,fixed}]
[--max MAX]
PATTERN

positional arguments:
PATTERN               glob, regular expression or fixed string

options:
-h, --help            show this help message and exit
--release RELEASE     release to search (default: sid)
--arch ARCH, --architecture ARCH
architecture to search (default: amd64)
--component COMP      archive components to search (default: all of them)
--mode {glob,regex,fixed}
match mode for pattern
--max MAX             maximum number of packages to returnFrom Python, the module can be used as:importpydebcontentscontents=pydebcontents.ContentsFile("/var/cache/apt-cacher-ng/debrep/","sid","amd64",["contrib"])contents.search("usr/share/doc/.*/README")AContentsDictstructure is returned, which is adictwhere the
keys are package entries (in the{section}/{package}format used in the Contents files), and the values are lists of matching filenames.The search term thatContentsFile.searchuses is astrrepresentation of a regular expression.
There are convenience functions inpydebcontentsfor handling search patterns, including navigating some of the foibles ofzgrepand the Contents file format:glob2reconverts glob syntax to regular expressionfixed2reconverts a fixed string into a regular expressionre2recleans up an existing regular expressionpattern2reis for programmatic use in selecting one of the above three functions.To-do list / limitationsA previous attempt at a Python-only implementation was too slow to be usable for searching the Contents files; this could be revisited.The mirrors are now carrying other compression formats such asxzthat will not be found or used at present.There is no utility provided to obtain the Contents files and arrange them on disk in a suitable tree.There is no ability to simply point at a Contents file on-disk that is not in the desired tree format.