filbranden / rpms / dnf

Forked from rpms/dnf 4 years ago
Clone
Blob Blame History Raw
From 9c157b51af2eb2df8ee3d92e20847d4f191bca95 Mon Sep 17 00:00:00 2001
From: Filipe Brandenburger <filbranden@fb.com>
Date: Thu, 2 Apr 2020 11:10:13 -0700
Subject: [PATCH] Fix behavior of `install-n` command and friends

Commit de9643afbdf5bb (first shipped in dnf 4.2.8) changed opts.command
from a list to a simple string, but code in InstallCommand and other
similar command classes still handled it as if it was a list.

This broke silently because Python will iterate over a string character
by character, so it never produced a syntax error or an exception that
would be noticed.

But it broke behavior of the `install-n` form (and related forms on
other commands, many of the `-n` extensions and some other command
aliases or specializations), since they would no longer detect that
command to restrict search of packages.

Before this commit, this command would (incorrectly) succeed:
  $ dnf install-n joe-4.6

After this commit:
  $ dnf install-n joe-4.6
  No match for argument: joe-4.6
    * Maybe you meant: joe
  Error: Unable to find a match: joe-4.6

Similarly tested for other commands.

Pushed upstream as:
https://github.com/rpm-software-management/dnf/pull/1611

This should first make it into dnf version 4.2.22

Closes: #1611
Approved by: m-blaha
---
 dnf/cli/commands/autoremove.py |  5 +++--
 dnf/cli/commands/install.py    | 16 ++++++++--------
 dnf/cli/commands/remove.py     |  5 +++--
 dnf/cli/commands/repoquery.py  |  6 ++----
 dnf/cli/commands/updateinfo.py |  4 ++--
 5 files changed, 18 insertions(+), 18 deletions(-)

diff --git a/dnf/cli/commands/autoremove.py b/dnf/cli/commands/autoremove.py
index 23603f52..5bd59f20 100644
--- a/dnf/cli/commands/autoremove.py
+++ b/dnf/cli/commands/autoremove.py
@@ -65,8 +65,9 @@ class AutoremoveCommand(commands.Command):
 
     def run(self):
         if any([self.opts.grp_specs, self.opts.pkg_specs, self.opts.filenames]):
-            forms = [self.nevra_forms[command] for command in self.opts.command
-                     if command in list(self.nevra_forms.keys())]
+            forms = []
+            if self.opts.command in self.nevra_forms:
+                forms = [self.nevra_forms[self.opts.command]]
 
             self.base.autoremove(forms,
                                  self.opts.pkg_specs,
diff --git a/dnf/cli/commands/install.py b/dnf/cli/commands/install.py
index 56efef21..38a90b61 100644
--- a/dnf/cli/commands/install.py
+++ b/dnf/cli/commands/install.py
@@ -74,12 +74,12 @@ class InstallCommand(commands.Command):
         nevra_forms = self._get_nevra_forms_from_command()
 
         self.cli._populate_update_security_filter(self.opts, self.base.sack.query())
-        if self.opts.command == ['localinstall'] and (self.opts.grp_specs or self.opts.pkg_specs):
+        if self.opts.command == 'localinstall' and (self.opts.grp_specs or self.opts.pkg_specs):
             self._log_not_valid_rpm_file_paths(self.opts.grp_specs)
             if self.base.conf.strict:
                 raise dnf.exceptions.Error(_('Nothing to do.'))
         skipped_grp_specs = []
-        if self.opts.grp_specs and self.opts.command != ['localinstall']:
+        if self.opts.grp_specs and self.opts.command != 'localinstall':
             if dnf.base.WITH_MODULES:
                 try:
                     module_base = dnf.module.module_base.ModuleBase(self.base)
@@ -108,10 +108,10 @@ class InstallCommand(commands.Command):
             self._inform_not_a_valid_combination(skipped_grp_specs)
             if self.base.conf.strict:
                 raise dnf.exceptions.Error(_('Nothing to do.'))
-        elif skipped_grp_specs and self.opts.command != ['localinstall']:
+        elif skipped_grp_specs and self.opts.command != 'localinstall':
             self._install_groups(skipped_grp_specs)
 
-        if self.opts.command != ['localinstall']:
+        if self.opts.command != 'localinstall':
             errs = self._install_packages(nevra_forms)
 
         if (len(errs) != 0 or len(err_pkgs) != 0 or error_module_specs) and self.base.conf.strict:
@@ -120,10 +120,10 @@ class InstallCommand(commands.Command):
                                                            packages=err_pkgs)
 
     def _get_nevra_forms_from_command(self):
-        return [self.nevra_forms[command]
-                for command in self.opts.command
-                if command in list(self.nevra_forms.keys())
-                ]
+        if self.opts.command in self.nevra_forms:
+            return [self.nevra_forms[self.opts.command]]
+        else:
+            return []
 
     def _log_not_valid_rpm_file_paths(self, grp_specs):
         group_names = map(lambda g: '@' + g, grp_specs)
diff --git a/dnf/cli/commands/remove.py b/dnf/cli/commands/remove.py
index f8059e46..5760d758 100644
--- a/dnf/cli/commands/remove.py
+++ b/dnf/cli/commands/remove.py
@@ -79,8 +79,9 @@ class RemoveCommand(commands.Command):
 
     def run(self):
 
-        forms = [self.nevra_forms[command] for command in self.opts.command
-                 if command in list(self.nevra_forms.keys())]
+        forms = []
+        if self.opts.command in self.nevra_forms:
+            forms = [self.nevra_forms[self.opts.command]]
 
         # local pkgs not supported in erase command
         self.opts.pkg_specs += self.opts.filenames
diff --git a/dnf/cli/commands/repoquery.py b/dnf/cli/commands/repoquery.py
index f5cb36fe..214abbb2 100644
--- a/dnf/cli/commands/repoquery.py
+++ b/dnf/cli/commands/repoquery.py
@@ -417,10 +417,8 @@ class RepoQueryCommand(commands.Command):
         )
         if self.opts.key:
             kwark = {}
-            forms = [self.nevra_forms[command] for command in self.opts.command
-                     if command in list(self.nevra_forms.keys())]
-            if forms:
-                kwark["forms"] = forms
+            if self.opts.command in self.nevra_forms:
+                kwark["forms"] = [self.nevra_forms[self.opts.command]]
             pkgs = []
             query_results = q.filter(empty=True)
             for key in self.opts.key:
diff --git a/dnf/cli/commands/updateinfo.py b/dnf/cli/commands/updateinfo.py
index 77923bd8..946398d5 100644
--- a/dnf/cli/commands/updateinfo.py
+++ b/dnf/cli/commands/updateinfo.py
@@ -112,9 +112,9 @@ class UpdateInfoCommand(commands.Command):
         self.cli.demands.available_repos = True
         self.cli.demands.sack_activation = True
 
-        if self.opts.command[0] in self.direct_commands:
+        if self.opts.command in self.direct_commands:
             # we were called with direct command
-            self.opts.spec_action = self.direct_commands[self.opts.command[0]]
+            self.opts.spec_action = self.direct_commands[self.opts.command]
         else:
             if self.opts._spec_action:
                 self.opts.spec_action = self.opts._spec_action
-- 
2.25.1