Bug 1673075

Summary: The base.sack.query().available() example does not seem to work out of box
Product: [Fedora] Fedora Reporter: Jan Pazdziora (Red Hat) <jpazdziora>
Component: dnfAssignee: Marek Blaha <mblaha>
Status: CLOSED ERRATA QA Contact: Fedora Extras Quality Assurance <extras-qa>
Severity: unspecified Docs Contact:
Priority: medium    
Version: 29CC: dmach, jmracek, jpazdziora, jrohel, mblaha, mhatina, packaging-team-maint, pkratoch, rpm-software-management, vmukhame
Target Milestone: ---Keywords: Triaged
Target Release: ---   
Hardware: Unspecified   
OS: Unspecified   
Whiteboard:
Fixed In Version: dnf-4.2.5-4.fc29 Doc Type: If docs needed, set a value
Doc Text:
Story Points: ---
Clone Of: Environment:
Last Closed: 2019-08-31 01:38:50 UTC Type: Bug
Regression: --- Mount Type: ---
Documentation: --- CRM:
Verified Versions: Category: ---
oVirt Team: --- RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: --- Target Upstream Version:
Embargoed:

Description Jan Pazdziora (Red Hat) 2019-02-06 15:42:41 UTC
Description of problem:

The page https://dnf.readthedocs.io/en/latest/api_queries.html describes now query() might be used. The first example shows:

q = base.sack.query()
i = q.installed()
i = i.filter(name='pepper')
packages = list(i) # i only gets evaluated here

a = q.available()
a = a.filter(name='pepper')
for pkg in a: # a only gets evaluated here
    print(pkg.name)

and the first part can be made working in standalone script as

#!/usr/bin/python3
import dnf
base = dnf.Base()
base.fill_sack()
q = base.sack.query()
i = q.installed()
for p in i:
        print(p, p.reponame)

I myself would have preferred to have such a standalone example shown but the q.installed() part works well.

However, the q.available() part does not work out of box -- running

#!/usr/bin/python3
import dnf
base = dnf.Base()
base.fill_sack()
q = base.sack.query()
a = q.available()
a = a.filter(name = "zsh")
for p in a:
        print(p, p.reponame)

does not print anything even if running

dnf list zsh

works fine.

It'd be nice to have complete examples, not just snippets without context and without obvious way of making them working.

Version-Release number of selected component (if applicable):

dnf-4.0.9-2.fc29.noarch

How reproducible:

Deterministic.

Steps to Reproduce:
1. Read https://dnf.readthedocs.io/en/latest/api_queries.html.
2. Be happy that there is available() which is documented to
    Return a new query limiting the original query to the not-installed packages, that is packages available from the repositories.
3. Based on the example snippet on that page, create and run script

#!/usr/bin/python3
import dnf
base = dnf.Base()
base.fill_sack()
q = base.sack.query()
a = q.available()
a = a.filter(name = "zsh")
for p in a:
        print(p, p.reponame)


Actual results:

Nothing printed.

Expected results:

The zsh package information printed.

Additional info:

Of course, it's well possible that the example and my approach is correct and the bug is in the dnf code -- in that case the code should be fixed. ;-)

Comment 1 Marek Blaha 2019-02-07 06:09:03 UTC
True, there is `base.read_all_repos()` line missing in the example just before base.fill_sack() line. Without this, dnf is working only with @System repo, so there are no "available" package.
We will update the documentation.

Comment 2 Jan Pazdziora (Red Hat) 2019-02-07 08:10:06 UTC
Thanks. Confirming that adding base.read_all_repos() makes the code work:

zsh-5.6.2-3.fc29.x86_64 updates
zsh-5.6.2-1.fc29.x86_64 fedora

Comment 4 Fedora Update System 2019-08-14 07:23:59 UTC
FEDORA-2019-d4b6ede072 has been submitted as an update to Fedora 29. https://bodhi.fedoraproject.org/updates/FEDORA-2019-d4b6ede072

Comment 5 Fedora Update System 2019-08-16 20:12:21 UTC
dnf-4.2.5-4.fc29, dnf-plugins-extras-4.0.4-2.fc29, libdnf-0.31.0-6.fc29 has been pushed to the Fedora 29 testing repository. If problems still persist, please make note of it in this bug report.
See https://fedoraproject.org/wiki/QA:Updates_Testing for
instructions on how to install test updates.
You can provide feedback for this update here: https://bodhi.fedoraproject.org/updates/FEDORA-2019-d4b6ede072

Comment 6 Fedora Update System 2019-08-31 01:38:50 UTC
dnf-4.2.5-4.fc29, dnf-plugins-extras-4.0.4-2.fc29, libdnf-0.31.0-6.fc29 has been pushed to the Fedora 29 stable repository. If problems still persist, please make note of it in this bug report.