Skip to content

Commit 96804d0

Browse files
committed
backends libdnf5: restore the old get_releasever implementation
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
1 parent d4adc0d commit 96804d0

File tree

1 file changed

+62
-2
lines changed

1 file changed

+62
-2
lines changed

src/fedrq/backends/libdnf5/backend/__init__.py

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import libdnf5.common
4545
import libdnf5.conf
4646
import libdnf5.rpm
47+
import rpm
4748
except ImportError as exc:
4849
raise MissingBackendError(str(exc)) from None
4950

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

946947

948+
def _dnf_getreleasever() -> str: # pragma: no cover
949+
# This is taken from dnf and slightly modified to fit fedrq's code style standards.
950+
#
951+
# SPDX-License-Identifier: GPL-2.0-or-later
952+
# Copyright (C) 2012-2015 Red Hat, Inc.
953+
DISTROVERPKG = (
954+
"system-release(releasever)",
955+
"system-release",
956+
"distribution-release(releasever)",
957+
"distribution-release",
958+
"redhat-release",
959+
"suse-release",
960+
)
961+
ts = rpm.TransactionSet("/")
962+
ts.setVSFlags(~(rpm._RPMVSF_NOSIGNATURES | rpm._RPMVSF_NODIGESTS))
963+
for distroverpkg in map(lambda p: p.encode("utf-8"), DISTROVERPKG):
964+
idx = ts.dbMatch("provides", distroverpkg)
965+
if not len(idx):
966+
continue
967+
try:
968+
hdr = next(idx)
969+
except StopIteration:
970+
raise RuntimeError(
971+
"Error: rpmdb failed to list provides. Try: rpm --rebuilddb"
972+
) from None
973+
releasever = hdr["version"]
974+
try:
975+
try:
976+
# header returns bytes -> look for bytes
977+
# it may fail because rpm returns a decoded string since 10 Apr 2019
978+
off = hdr[rpm.RPMTAG_PROVIDENAME].index(distroverpkg)
979+
except ValueError:
980+
# header returns a string -> look for a string
981+
off = hdr[rpm.RPMTAG_PROVIDENAME].index(distroverpkg.decode("utf8"))
982+
flag = hdr[rpm.RPMTAG_PROVIDEFLAGS][off]
983+
ver = hdr[rpm.RPMTAG_PROVIDEVERSION][off]
984+
if (
985+
flag == rpm.RPMSENSE_EQUAL
986+
and ver
987+
and hdr["name"] not in (distroverpkg, distroverpkg.decode("utf8"))
988+
):
989+
# override the package version
990+
releasever = ver
991+
except (ValueError, KeyError, IndexError):
992+
pass
993+
if isinstance(releasever, bytes):
994+
releasever = releasever.decode("utf-8")
995+
return releasever
996+
return ""
997+
998+
947999
@functools.cache
9481000
def get_releasever() -> str:
9491001
"""
9501002
Return the system releasever
9511003
"""
1004+
# Use our copy of dnf4's code for now.
1005+
# For some reason, `detect_release` from libdnf5 is broken in some libdnf5
1006+
# versions and returns an empty string instead of the correct value.
1007+
# Plus, having to create a Base object just for this is expensive.
1008+
# See also the discussion in https://github.com/rpm-software-management/dnf5/pull/1804.
1009+
# libdnf5 is considering breaking this API.
1010+
return _dnf_getreleasever()
1011+
9521012
# libdnf5 >= 5.0.10
9531013
# https://github.com/rpm-software-management/dnf5/pull/448
954-
base = libdnf5.base.Base()
955-
return libdnf5.conf.Vars.detect_release(base.get_weak_ptr(), "/").get()
1014+
# base = libdnf5.base.Base()
1015+
# return libdnf5.conf.Vars.detect_release(base.get_weak_ptr(), "/").get()
9561016

9571017

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

0 commit comments

Comments
 (0)