Skip to content

Commit

Permalink
backends libdnf5: restore the old get_releasever implementation
Browse files Browse the repository at this point in the history
This partially reverts commit 467bdfe.
See explanation in the code comments for why this was reverted.

Ref: rpm-software-management/dnf5#281
Ref: rpm-software-management/dnf5#1804
  • Loading branch information
gotmax23 committed Nov 7, 2024
1 parent d4adc0d commit 96804d0
Showing 1 changed file with 62 additions and 2 deletions.
64 changes: 62 additions & 2 deletions src/fedrq/backends/libdnf5/backend/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import libdnf5.common
import libdnf5.conf
import libdnf5.rpm
import rpm
except ImportError as exc:
raise MissingBackendError(str(exc)) from None

Expand Down Expand Up @@ -944,15 +945,74 @@ def backend(self) -> BackendMod:
return t.cast(BackendMod, sys.modules[__name__])


def _dnf_getreleasever() -> str: # pragma: no cover
# This is taken from dnf and slightly modified to fit fedrq's code style standards.
#
# SPDX-License-Identifier: GPL-2.0-or-later
# Copyright (C) 2012-2015 Red Hat, Inc.
DISTROVERPKG = (
"system-release(releasever)",
"system-release",
"distribution-release(releasever)",
"distribution-release",
"redhat-release",
"suse-release",
)
ts = rpm.TransactionSet("/")
ts.setVSFlags(~(rpm._RPMVSF_NOSIGNATURES | rpm._RPMVSF_NODIGESTS))
for distroverpkg in map(lambda p: p.encode("utf-8"), DISTROVERPKG):
idx = ts.dbMatch("provides", distroverpkg)
if not len(idx):
continue
try:
hdr = next(idx)
except StopIteration:
raise RuntimeError(
"Error: rpmdb failed to list provides. Try: rpm --rebuilddb"
) from None
releasever = hdr["version"]
try:
try:
# header returns bytes -> look for bytes
# it may fail because rpm returns a decoded string since 10 Apr 2019
off = hdr[rpm.RPMTAG_PROVIDENAME].index(distroverpkg)
except ValueError:
# header returns a string -> look for a string
off = hdr[rpm.RPMTAG_PROVIDENAME].index(distroverpkg.decode("utf8"))
flag = hdr[rpm.RPMTAG_PROVIDEFLAGS][off]
ver = hdr[rpm.RPMTAG_PROVIDEVERSION][off]
if (
flag == rpm.RPMSENSE_EQUAL
and ver
and hdr["name"] not in (distroverpkg, distroverpkg.decode("utf8"))
):
# override the package version
releasever = ver
except (ValueError, KeyError, IndexError):
pass
if isinstance(releasever, bytes):
releasever = releasever.decode("utf-8")
return releasever
return ""


@functools.cache
def get_releasever() -> str:
"""
Return the system releasever
"""
# Use our copy of dnf4's code for now.
# For some reason, `detect_release` from libdnf5 is broken in some libdnf5
# versions and returns an empty string instead of the correct value.
# Plus, having to create a Base object just for this is expensive.
# See also the discussion in https://github.com/rpm-software-management/dnf5/pull/1804.
# libdnf5 is considering breaking this API.
return _dnf_getreleasever()

# libdnf5 >= 5.0.10
# https://github.com/rpm-software-management/dnf5/pull/448
base = libdnf5.base.Base()
return libdnf5.conf.Vars.detect_release(base.get_weak_ptr(), "/").get()
# base = libdnf5.base.Base()
# return libdnf5.conf.Vars.detect_release(base.get_weak_ptr(), "/").get()


def get_changelogs(package: Package) -> Iterator[ChangelogEntry]:
Expand Down

0 comments on commit 96804d0

Please sign in to comment.