Skip to content

Document base initialization for API users including reading config and variables #2219

@xylle

Description

@xylle

Hello,
I work with "Rocky Linux 9.0 (Blue Onyx)", python 3.9.10 and dnf 4.10.0
I make a plugin for dnf, and it's work an expected

import dnf
import json
from dnfpluginscore import _, logger

@dnf.plugin.register_command
class JsonUpdatesCommand(dnf.cli.Command):
    aliases = ('json-updates',)
    summary = _('Lister les mises à jour disponibles en JSON')

    def __init__(self, cli):
        super(JsonUpdatesCommand, self).__init__(cli)

    def configure(self):
        pass

    def run(self):
        self.base.fill_sack(load_system_repo=True, load_available_repos=True)
        query = self.base.sack.query().upgrades()

        updates = []
        for pkg in query:
            installed_query = self.base.sack.query().installed().filter(name=pkg.name)
            installed_pkg = next(iter(installed_query), None)
            if installed_pkg:
                installed_version = f"{installed_pkg.version}-{installed_pkg.release}"
            else:
                installed_version = "Non installé"
            updates.append({
                'paquet': pkg.name,
                'version_installee': installed_version,
                'version_disponible': f'{pkg.version}-{pkg.release}'
            })

        print(json.dumps(updates, indent=2))


def load_commands():
    return [JsonUpdatesCommand]

Partial output :

[
  {
    "paquet": "yum",
    "version_installee": "4.10.0-5.el9_0",
    "version_disponible": "4.14.0-17.el9"
  },
  {
    "paquet": "python3-dnf",
    "version_installee": "4.10.0-5.el9_0",
    "version_disponible": "4.14.0-17.el9"
  },
....

When I want to make a python script for the same thing, he doesn't work

#!/usr/bin/env python3

import dnf

base = dnf.Base()
base.fill_sack(load_system_repo=True, load_available_repos=True)
q = base.sack.query()
i = q.upgrades()

packages = list(i)
print("Installed package:")
for pkg in i:
    print(pkg, pkg.reponame)

The list of pkg update are empty on the same server
When I try i = q.installed() in place of i = q.upgrades(), I have the list of installed package.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions