From 16896194a81ca091eecb48aecc3bffc9c11b9889 Mon Sep 17 00:00:00 2001
From: Igor Gnatenko <ignatenko@redhat.com>
Date: Mon, 31 Oct 2016 17:24:26 +0100
Subject: [PATCH] dnf: fix compatibility with DNF 2.0 (#3325)

* dnf: fix compatibility with DNF 2.0

* Reimplement (copy) old dnf.cli.commands.parse_spec_group_file(),
  upstream uses argparse since 2.0.
* add_remote_rpm() has been changed to the add_remote_rpms()

Closes: https://github.com/ansible/ansible-modules-extras/issues/3310
Signed-off-by: Igor Gnatenko <ignatenko@redhat.com>

* fixup! dnf: fix compatibility with DNF 2.0
---
 .../modules/extras/packaging/os/dnf.py        | 32 +++++++++++++++----
 1 file changed, 26 insertions(+), 6 deletions(-)

diff --git a/lib/ansible/modules/extras/packaging/os/dnf.py b/lib/ansible/modules/extras/packaging/os/dnf.py
index 8631260ee43..09c7d921e29 100644
--- a/lib/ansible/modules/extras/packaging/os/dnf.py
+++ b/lib/ansible/modules/extras/packaging/os/dnf.py
@@ -265,13 +265,35 @@ def _mark_package_install(module, base, pkg_spec):
         module.fail_json(msg="No package {} available.".format(pkg_spec))
 
 
+def _parse_spec_group_file(names):
+    pkg_specs, grp_specs, filenames = [], [], []
+    for name in names:
+        if name.endswith(".rpm"):
+            filenames.append(name)
+        elif name.startswith("@"):
+            grp_specs.append(name[1:])
+        else:
+            pkg_specs.append(name)
+    return pkg_specs, grp_specs, filenames
+
+
+def _install_remote_rpms(base, filenames):
+    if int(dnf.__version__.split(".")[0]) >= 2:
+        pkgs = list(sorted(base.add_remote_rpms(list(filenames)), reverse=True))
+    else:
+        pkgs = []
+        for filename in filenames:
+            pkgs.append(base.add_remote_rpm(filename))
+    for pkg in pkgs:
+        base.package_install(pkg)
+
+
 def ensure(module, base, state, names):
     allow_erasing = False
     if names == ['*'] and state == 'latest':
         base.upgrade_all()
     else:
-        pkg_specs, group_specs, filenames = dnf.cli.commands.parse_spec_group_file(
-            names)
+        pkg_specs, group_specs, filenames = _parse_spec_group_file(names)
         if group_specs:
             base.read_comps()
 
@@ -286,8 +308,7 @@ def ensure(module, base, state, names):
 
         if state in ['installed', 'present']:
             # Install files.
-            for filename in (f.strip() for f in filenames):
-                base.package_install(base.add_remote_rpm(filename))
+            _install_remote_rpms(base, (f.strip() for f in filenames))
             # Install groups.
             for group in (g.strip() for g in groups):
                 base.group_install(group, dnf.const.GROUP_PACKAGE_TYPES)
@@ -297,8 +318,7 @@ def ensure(module, base, state, names):
 
         elif state == 'latest':
             # "latest" is same as "installed" for filenames.
-            for filename in filenames:
-                base.package_install(base.add_remote_rpm(filename))
+            _install_remote_rpms(base, filenames)
             for group in groups:
                 try:
                     base.group_upgrade(group)