Backport of dnf5 support from 3007 to 3006

This commit is contained in:
David Murphy 2025-02-26 15:25:57 -07:00 committed by Daniel Wozniak
parent bf00196748
commit 59ddf3cf88
3 changed files with 31 additions and 15 deletions

1
changelog/67057.fixed.md Normal file
View file

@ -0,0 +1 @@
Added support for dnf5 (backport from 3007) and update to its new command syntax changes since 2023

View file

@ -14,7 +14,8 @@ Support for YUM/DNF
.. versionadded:: 3003
Support for ``tdnf`` on Photon OS.
.. versionadded:: 3006.10
Support for ``dnf5``` on Fedora 41
"""
import configparser
@ -114,12 +115,12 @@ def _get_hold(line, pattern=__HOLD_PATTERN, full=True):
dnf ==> vim-enhanced-2:7.4.827-1.fc22.*
"""
if full:
if _yum() == "dnf":
if _yum() in ("dnf", "dnf5"):
lock_re = rf"({pattern}-\S+)"
else:
lock_re = rf"(\d+:{pattern}-\S+)"
else:
if _yum() == "dnf":
if _yum() in ("dnf", "dnf5"):
lock_re = rf"({pattern}-\S+)"
else:
lock_re = rf"\d+:({pattern}-\S+)"
@ -137,7 +138,7 @@ def _get_hold(line, pattern=__HOLD_PATTERN, full=True):
def _yum():
"""
Determine package manager name (yum or dnf),
Determine package manager name (yum or dnf[5]),
depending on the executable existence in $PATH.
"""
@ -160,7 +161,10 @@ def _yum():
contextkey = "yum_bin"
if contextkey not in context:
for dir in os.environ.get("PATH", os.defpath).split(os.pathsep):
if _check(os.path.join(dir, "dnf")):
if _check(os.path.join(dir, "dnf5")):
context[contextkey] = "dnf5"
break
elif _check(os.path.join(dir, "dnf")):
context[contextkey] = "dnf"
break
elif _check(os.path.join(dir, "tdnf")):
@ -268,10 +272,11 @@ def _check_versionlock():
def _get_options(**kwargs):
"""
Returns a list of options to be used in the yum/dnf command, based on the
Returns a list of options to be used in the yum/dnf[5] command, based on the
kwargs passed.
"""
# Get repo options from the kwargs
# dnf5 aliases dnf options, so no need to change
fromrepo = kwargs.pop("fromrepo", "")
repo = kwargs.pop("repo", "")
disablerepo = kwargs.pop("disablerepo", "")
@ -1031,7 +1036,7 @@ def list_upgrades(refresh=True, **kwargs):
cmd = ["--quiet"]
cmd.extend(options)
cmd.extend(["list", "upgrades" if _yum() == "dnf" else "updates"])
cmd.extend(["list", "upgrades" if _yum() in ("dnf", "dnf5") else "updates"])
out = _call_yum(cmd, ignore_retcode=True)
if out["retcode"] != 0 and "Error:" in out:
return {}
@ -1747,6 +1752,8 @@ def install(
cmd.extend(["--best", "--allowerasing"])
_add_common_args(cmd)
cmd.append("install" if pkg_type != "advisory" else "update")
if _yum() == "dnf5":
cmd.extend(["--best", "--allowerasing"])
cmd.extend(targets)
out = _call_yum(cmd, ignore_retcode=False, redirect_stderr=True)
if out["retcode"] != 0:
@ -1980,7 +1987,7 @@ def upgrade(
salt '*' pkg.upgrade security=True exclude='kernel*'
"""
if _yum() == "dnf" and not obsoletes:
if _yum() in ("dnf", "dnf5") and not obsoletes:
# for dnf we can just disable obsoletes
_setopt = [
opt
@ -2018,7 +2025,7 @@ def upgrade(
cmd.append("upgrade" if not minimal else "upgrade-minimal")
else:
# do not force the removal of obsolete packages
if _yum() == "dnf":
if _yum() in ("dnf", "dnf5"):
cmd.append("upgrade" if not minimal else "upgrade-minimal")
else:
# for yum we have to use update instead of upgrade
@ -2073,7 +2080,7 @@ def remove(name=None, pkgs=None, **kwargs): # pylint: disable=W0613
On minions running systemd>=205, `systemd-run(1)`_ is now used to
isolate commands which modify installed packages from the
``salt-minion`` daemon's control group. This is done to keep systemd
from killing any yum/dnf commands spawned by Salt when the
from killing any yum/dnf[5] commands spawned by Salt when the
``salt-minion`` service is restarted. (see ``KillMode`` in the
`systemd.kill(5)`_ manpage for more information). If desired, usage of
`systemd-run(1)`_ can be suppressed by setting a :mod:`config option
@ -2192,7 +2199,7 @@ def purge(name=None, pkgs=None, **kwargs): # pylint: disable=W0613
On minions running systemd>=205, `systemd-run(1)`_ is now used to
isolate commands which modify installed packages from the
``salt-minion`` daemon's control group. This is done to keep systemd
from killing any yum/dnf commands spawned by Salt when the
from killing any yum/dnf[5] commands spawned by Salt when the
``salt-minion`` service is restarted. (see ``KillMode`` in the
`systemd.kill(5)`_ manpage for more information). If desired, usage of
`systemd-run(1)`_ can be suppressed by setting a :mod:`config option
@ -2374,7 +2381,7 @@ def unhold(name=None, pkgs=None, sources=None, **kwargs): # pylint: disable=W06
ret[target] = {"name": target, "changes": {}, "result": False, "comment": ""}
if _yum() == "dnf":
if _yum() in ("dnf", "dnf5"):
search_locks = [x for x in current_locks if x == target]
else:
# To accommodate yum versionlock's lack of support for removing
@ -3069,7 +3076,7 @@ def mod_repo(repo, basedir=None, **kwargs):
if use_copr:
# Is copr plugin installed?
copr_plugin_name = ""
if _yum() == "dnf":
if _yum() in ("dnf", "dnf5"):
copr_plugin_name = "dnf-plugins-core"
else:
copr_plugin_name = "yum-plugin-copr"

View file

@ -78,7 +78,7 @@ def list_repos_var():
@pytest.fixture(
ids=["yum", "dnf"],
ids=["yum", "dnf", "dnf5"],
params=[
{
"context": {"yum_bin": "yum"},
@ -90,6 +90,11 @@ def list_repos_var():
"grains": {"os": "Fedora", "osrelease": 27},
"cmd": ["dnf", "-y", "--best", "--allowerasing"],
},
{
"context": {"yum_bin": "dnf5"},
"grains": {"os": "Fedora", "osrelease": 39},
"cmd": ["dnf5", "-y"],
},
],
)
def yum_and_dnf(request):
@ -3193,7 +3198,10 @@ def test_59705_version_as_accidental_float_should_become_text(
new, full_pkg_string, yum_and_dnf
):
name = "fnord"
expected_cmd = yum_and_dnf + ["install", full_pkg_string]
expected_cmd = yum_and_dnf + ["install"]
if expected_cmd[0] == "dnf5":
expected_cmd += ["--best", "--allowerasing"]
expected_cmd += [full_pkg_string]
cmd_mock = MagicMock(
return_value={"pid": 12345, "retcode": 0, "stdout": "", "stderr": ""}
)