DNF python API - how to run repo-queries?

I’m struggling with converting the following DNF command line call:

dnf -q --repo=rawhide-source repoquery --whatdepends "mariadb-devel" --alldeps --qf "%{name}-%{evr}.%{arch}"

to the DNF python API.

It seems to me like only some queries are available through the API, but not repo-queries.
https://dnf.readthedocs.io/en/latest/api_queries.html

I have something like this, thanks to ChatGPT, but it’s probably broken on multiple places.
I’m stopping by here as I wasn’t able to find in the documentation, that my goal is even possible.

import dnf

# Create a DNF Base object
base = dnf.Base()

# Set repository IDs
repo_ids = ['rawhide-source', 'rawhide']

# Load the repository data
base.read_all_repos()

# Enable the specified repositories
for repo_id in repo_ids:
    repo = base.repos.get(repo_id)
    if repo:
        repo.enable()

# Initialize the sack
base.fill_sack(load_system_repo=False)

# Query packages that require "mariadb-devel" and resolve dependencies
query = base.sack.query().whatrequires('mariadb-devel').alldeps()
results = base.sack.query().filter(pkgs=query)

# Print package names that require "mariadb-devel"
for pkg in results:
    print(pkg.name)

# Close the DNF Base object
base.close()

The actual querying is done via hawkey. This might help: python-hawkey Tutorial — Hawkey 0.8.1-1 documentation. For your case, does this do more or less what you want?

import dnf

# Create a DNF Base object
base = dnf.Base()

# Set repository IDs
repo_ids = ['rawhide-source', 'rawhide']

# Load the repository data
base.read_all_repos()

# Enable the specified repositories
for repo_id in repo_ids:
    repo = base.repos.get(repo_id)
    if repo:
        repo.enable()

# Initialize the sack
base.fill_sack(load_system_repo=False)

# Query packages that require "mariadb-devel"
query = base.sack.query().filterm(requires=['mariadb-devel']).latest(1)
for pkg in query.run():
    print(str(pkg))

@gotmax23 wrote a really nifty wrapper around repoquery that should be useful here - Overview - rpms/fedrq - src.fedoraproject.org

Yes, I did :smile:. It provides the same API (based on the original hawkey version) for both dnf and libdnf5. Something like this should work for your usecase:

import fedrq.config

# Pass backend="libdnf5" if you're feeling adventurous :)
config = fedrq.config.get_config()
#
rq = config.get_rq("rawhide")
query = rq.query(requires=rq.query(name="mariadb-devel"), arch="src", latest=1)
for package in query:
    print(package)

Or with the equivalent CLI command:

fedrq wr mariadb-devel -s
1 Like