Blame SOURCES/0003-Post-transaction-summary-is-logged-for-API-users-RhBug-1855158.patch

862ba9
From 9ed390d08a9f2b66f4e352532fa526fc64e329d4 Mon Sep 17 00:00:00 2001
862ba9
From: Marek Blaha <mblaha@redhat.com>
862ba9
Date: Tue, 28 Jul 2020 09:50:10 +0200
862ba9
Subject: [PATCH 1/3] Remove unused loops attribute from
862ba9
 DepSolveProgressCallBack
862ba9
862ba9
---
862ba9
 dnf/cli/output.py | 5 -----
862ba9
 1 file changed, 5 deletions(-)
862ba9
862ba9
diff --git a/dnf/cli/output.py b/dnf/cli/output.py
862ba9
index de188ffbd1..44d5f8b89f 100644
862ba9
--- a/dnf/cli/output.py
862ba9
+++ b/dnf/cli/output.py
862ba9
@@ -1987,10 +1987,6 @@ def historyInfoCmdPkgsAltered(self, old, pats=[]):
862ba9
 class DepSolveProgressCallBack(dnf.callback.Depsolve):
862ba9
     """Provides text output callback functions for Dependency Solver callback."""
862ba9
 
862ba9
-    def __init__(self):
862ba9
-        """requires yum-cli log and errorlog functions as arguments"""
862ba9
-        self.loops = 0
862ba9
-
862ba9
     def pkg_added(self, pkg, mode):
862ba9
         """Print information about a package being added to the
862ba9
         transaction set.
862ba9
@@ -2037,7 +2033,6 @@ def start(self):
862ba9
         process.
862ba9
         """
862ba9
         logger.debug(_('--> Starting dependency resolution'))
862ba9
-        self.loops += 1
862ba9
 
862ba9
     def end(self):
862ba9
         """Output a message stating that dependency resolution has finished."""
862ba9
862ba9
From 0ee646f4965c597f2832ed3df9d9f0e6546dcc54 Mon Sep 17 00:00:00 2001
862ba9
From: Marek Blaha <mblaha@redhat.com>
862ba9
Date: Wed, 21 Oct 2020 11:47:43 +0200
862ba9
Subject: [PATCH 2/3] Remove unused parameter of _make_lists()
862ba9
862ba9
---
862ba9
 dnf/cli/output.py | 7 ++++---
862ba9
 1 file changed, 4 insertions(+), 3 deletions(-)
862ba9
862ba9
diff --git a/dnf/cli/output.py b/dnf/cli/output.py
862ba9
index 44d5f8b89f..af8a968770 100644
862ba9
--- a/dnf/cli/output.py
862ba9
+++ b/dnf/cli/output.py
862ba9
@@ -52,7 +52,8 @@
862ba9
 
862ba9
 logger = logging.getLogger('dnf')
862ba9
 
862ba9
-def _make_lists(transaction, goal):
862ba9
+
862ba9
+def _make_lists(transaction):
862ba9
     b = dnf.util.Bunch({
862ba9
         'downgraded': [],
862ba9
         'erased': [],
862ba9
@@ -1101,7 +1102,7 @@ def list_transaction(self, transaction, total_width=None):
862ba9
             # in order to display module changes when RPM transaction is empty
862ba9
             transaction = []
862ba9
 
862ba9
-        list_bunch = _make_lists(transaction, self.base._goal)
862ba9
+        list_bunch = _make_lists(transaction)
862ba9
         pkglist_lines = []
862ba9
         data = {'n' : {}, 'v' : {}, 'r' : {}}
862ba9
         a_wid = 0 # Arch can't get "that big" ... so always use the max.
862ba9
@@ -1488,7 +1489,7 @@ def _tsi_or_pkg_nevra_cmp(item1, item2):
862ba9
             return (item1.arch > item2.arch) - (item1.arch < item2.arch)
862ba9
 
862ba9
         out = ''
862ba9
-        list_bunch = _make_lists(transaction, self.base._goal)
862ba9
+        list_bunch = _make_lists(transaction)
862ba9
 
862ba9
         skipped_conflicts, skipped_broken = self._skipped_packages(
862ba9
             report_problems=False, transaction=transaction)
862ba9
862ba9
From 865b7183453684de6a25e77fddf5a2d11fbffba8 Mon Sep 17 00:00:00 2001
862ba9
From: Marek Blaha <mblaha@redhat.com>
862ba9
Date: Wed, 21 Oct 2020 17:59:46 +0200
862ba9
Subject: [PATCH 3/3] Post transaction summary is logged for API users
862ba9
 (RhBug:1855158)
862ba9
862ba9
Post transaction summary is always logged into /var/log/dnf.log.
862ba9
When transaction is called from cli, the summary is also printed to
862ba9
stdout in columns (as previously).
862ba9
862ba9
= changelog =
862ba9
msg:           Packages installed/removed via DNF API are logged into dnf.log
862ba9
type:          enhancement
862ba9
resolves:      https://bugzilla.redhat.com/show_bug.cgi?id=1855158
862ba9
---
862ba9
 dnf/base.py       |  46 ++++++++++++-
862ba9
 dnf/cli/cli.py    |   8 ++-
862ba9
 dnf/cli/output.py | 167 ++++++++--------------------------------------
862ba9
 dnf/util.py       | 102 +++++++++++++++++++++++++++-
862ba9
 4 files changed, 177 insertions(+), 146 deletions(-)
862ba9
862ba9
diff --git a/dnf/base.py b/dnf/base.py
862ba9
index 075e74265a..c0d7712605 100644
862ba9
--- a/dnf/base.py
862ba9
+++ b/dnf/base.py
862ba9
@@ -28,12 +28,12 @@
862ba9
 import dnf
862ba9
 import libdnf.transaction
862ba9
 
862ba9
+from copy import deepcopy
862ba9
 from dnf.comps import CompsQuery
862ba9
 from dnf.i18n import _, P_, ucd
862ba9
 from dnf.util import _parse_specs
862ba9
 from dnf.db.history import SwdbInterface
862ba9
 from dnf.yum import misc
862ba9
-from functools import reduce
862ba9
 try:
862ba9
     from collections.abc import Sequence
862ba9
 except ImportError:
862ba9
@@ -549,7 +549,7 @@ def _ts(self):
862ba9
         if self.conf.ignorearch:
862ba9
             self._rpm_probfilter.add(rpm.RPMPROB_FILTER_IGNOREARCH)
862ba9
 
862ba9
-        probfilter = reduce(operator.or_, self._rpm_probfilter, 0)
862ba9
+        probfilter = functools.reduce(operator.or_, self._rpm_probfilter, 0)
862ba9
         self._priv_ts.setProbFilter(probfilter)
862ba9
         return self._priv_ts
862ba9
 
862ba9
@@ -890,6 +890,15 @@ def do_transaction(self, display=()):
862ba9
         self._plugins.unload_removed_plugins(self.transaction)
862ba9
         self._plugins.run_transaction()
862ba9
 
862ba9
+        # log post transaction summary
862ba9
+        def _pto_callback(action, tsis):
862ba9
+            msgs = []
862ba9
+            for tsi in tsis:
862ba9
+                msgs.append('{}: {}'.format(action, str(tsi)))
862ba9
+            return msgs
862ba9
+        for msg in dnf.util._post_transaction_output(self, self.transaction, _pto_callback):
862ba9
+            logger.debug(msg)
862ba9
+
862ba9
         return tid
862ba9
 
862ba9
     def _trans_error_summary(self, errstring):
862ba9
@@ -1311,7 +1320,7 @@ def _do_package_lists(self, pkgnarrow='all', patterns=None, showdups=None,
862ba9
         if patterns is None or len(patterns) == 0:
862ba9
             return list_fn(None)
862ba9
         yghs = map(list_fn, patterns)
862ba9
-        return reduce(lambda a, b: a.merge_lists(b), yghs)
862ba9
+        return functools.reduce(lambda a, b: a.merge_lists(b), yghs)
862ba9
 
862ba9
     def _list_pattern(self, pkgnarrow, pattern, showdups, ignore_case,
862ba9
                       reponame=None):
862ba9
@@ -2579,6 +2588,37 @@ def setup_loggers(self):
862ba9
         """
862ba9
         self._logging._setup_from_dnf_conf(self.conf, file_loggers_only=True)
862ba9
 
862ba9
+    def _skipped_packages(self, report_problems, transaction):
862ba9
+        """returns set of conflicting packages and set of packages with broken dependency that would
862ba9
+        be additionally installed when --best and --allowerasing"""
862ba9
+        if self._goal.actions & (hawkey.INSTALL | hawkey.UPGRADE | hawkey.UPGRADE_ALL):
862ba9
+            best = True
862ba9
+        else:
862ba9
+            best = False
862ba9
+        ng = deepcopy(self._goal)
862ba9
+        params = {"allow_uninstall": self._allow_erasing,
862ba9
+                  "force_best": best,
862ba9
+                  "ignore_weak": True}
862ba9
+        ret = ng.run(**params)
862ba9
+        if not ret and report_problems:
862ba9
+            msg = dnf.util._format_resolve_problems(ng.problem_rules())
862ba9
+            logger.warning(msg)
862ba9
+        problem_conflicts = set(ng.problem_conflicts(available=True))
862ba9
+        problem_dependency = set(ng.problem_broken_dependency(available=True)) - problem_conflicts
862ba9
+
862ba9
+        def _nevra(item):
862ba9
+            return hawkey.NEVRA(name=item.name, epoch=item.epoch, version=item.version,
862ba9
+                                release=item.release, arch=item.arch)
862ba9
+
862ba9
+        # Sometimes, pkg is not in transaction item, therefore, comparing by nevra
862ba9
+        transaction_nevras = [_nevra(tsi) for tsi in transaction]
862ba9
+        skipped_conflicts = set(
862ba9
+            [pkg for pkg in problem_conflicts if _nevra(pkg) not in transaction_nevras])
862ba9
+        skipped_dependency = set(
862ba9
+            [pkg for pkg in problem_dependency if _nevra(pkg) not in transaction_nevras])
862ba9
+
862ba9
+        return skipped_conflicts, skipped_dependency
862ba9
+
862ba9
 
862ba9
 def _msg_installed(pkg):
862ba9
     name = ucd(pkg)
862ba9
diff --git a/dnf/cli/cli.py b/dnf/cli/cli.py
862ba9
index 0bc2c119d0..334000362c 100644
862ba9
--- a/dnf/cli/cli.py
862ba9
+++ b/dnf/cli/cli.py
862ba9
@@ -252,8 +252,12 @@ def do_transaction(self, display=()):
862ba9
             trans = None
862ba9
 
862ba9
         if trans:
862ba9
-            msg = self.output.post_transaction_output(trans)
862ba9
-            logger.info(msg)
862ba9
+            # the post transaction summary is already written to log during
862ba9
+            # Base.do_transaction() so here only print the messages to the
862ba9
+            # user arranged in columns
862ba9
+            print()
862ba9
+            print('\n'.join(self.output.post_transaction_output(trans)))
862ba9
+            print()
862ba9
             for tsi in trans:
862ba9
                 if tsi.state == libdnf.transaction.TransactionItemState_ERROR:
862ba9
                     raise dnf.exceptions.Error(_('Transaction failed'))
862ba9
diff --git a/dnf/cli/output.py b/dnf/cli/output.py
862ba9
index af8a968770..6d729b63ba 100644
862ba9
--- a/dnf/cli/output.py
862ba9
+++ b/dnf/cli/output.py
862ba9
@@ -21,9 +21,7 @@
862ba9
 from __future__ import print_function
862ba9
 from __future__ import unicode_literals
862ba9
 
862ba9
-from copy import deepcopy
862ba9
 import fnmatch
862ba9
-import functools
862ba9
 import hawkey
862ba9
 import itertools
862ba9
 import libdnf.transaction
862ba9
@@ -53,51 +51,6 @@
862ba9
 logger = logging.getLogger('dnf')
862ba9
 
862ba9
 
862ba9
-def _make_lists(transaction):
862ba9
-    b = dnf.util.Bunch({
862ba9
-        'downgraded': [],
862ba9
-        'erased': [],
862ba9
-        'erased_clean': [],
862ba9
-        'erased_dep': [],
862ba9
-        'installed': [],
862ba9
-        'installed_group': [],
862ba9
-        'installed_dep': [],
862ba9
-        'installed_weak': [],
862ba9
-        'reinstalled': [],
862ba9
-        'upgraded': [],
862ba9
-        'failed': [],
862ba9
-    })
862ba9
-
862ba9
-    for tsi in transaction:
862ba9
-        if tsi.state == libdnf.transaction.TransactionItemState_ERROR:
862ba9
-            b.failed.append(tsi)
862ba9
-        elif tsi.action == libdnf.transaction.TransactionItemAction_DOWNGRADE:
862ba9
-            b.downgraded.append(tsi)
862ba9
-        elif tsi.action == libdnf.transaction.TransactionItemAction_INSTALL:
862ba9
-            if tsi.reason == libdnf.transaction.TransactionItemReason_GROUP:
862ba9
-                b.installed_group.append(tsi)
862ba9
-            elif tsi.reason == libdnf.transaction.TransactionItemReason_DEPENDENCY:
862ba9
-                b.installed_dep.append(tsi)
862ba9
-            elif tsi.reason == libdnf.transaction.TransactionItemReason_WEAK_DEPENDENCY:
862ba9
-                b.installed_weak.append(tsi)
862ba9
-            else:
862ba9
-                # TransactionItemReason_USER
862ba9
-                b.installed.append(tsi)
862ba9
-        elif tsi.action == libdnf.transaction.TransactionItemAction_REINSTALL:
862ba9
-            b.reinstalled.append(tsi)
862ba9
-        elif tsi.action == libdnf.transaction.TransactionItemAction_REMOVE:
862ba9
-            if tsi.reason == libdnf.transaction.TransactionItemReason_CLEAN:
862ba9
-                b.erased_clean.append(tsi)
862ba9
-            elif tsi.reason == libdnf.transaction.TransactionItemReason_DEPENDENCY:
862ba9
-                b.erased_dep.append(tsi)
862ba9
-            else:
862ba9
-                b.erased.append(tsi)
862ba9
-        elif tsi.action == libdnf.transaction.TransactionItemAction_UPGRADE:
862ba9
-            b.upgraded.append(tsi)
862ba9
-
862ba9
-    return b
862ba9
-
862ba9
-
862ba9
 def _spread_in_columns(cols_count, label, lst):
862ba9
     left = itertools.chain((label,), itertools.repeat(''))
862ba9
     lst_length = len(lst)
862ba9
@@ -1057,37 +1010,6 @@ def list_group_transaction(self, comps, history, diff):
862ba9
             out[0:0] = self._banner(col_data, (_('Group'), _('Packages'), '', ''))
862ba9
         return '\n'.join(out)
862ba9
 
862ba9
-    def _skipped_packages(self, report_problems, transaction):
862ba9
-        """returns set of conflicting packages and set of packages with broken dependency that would
862ba9
-        be additionally installed when --best and --allowerasing"""
862ba9
-        if self.base._goal.actions & (hawkey.INSTALL | hawkey.UPGRADE | hawkey.UPGRADE_ALL):
862ba9
-            best = True
862ba9
-        else:
862ba9
-            best = False
862ba9
-        ng = deepcopy(self.base._goal)
862ba9
-        params = {"allow_uninstall": self.base._allow_erasing,
862ba9
-                  "force_best": best,
862ba9
-                  "ignore_weak": True}
862ba9
-        ret = ng.run(**params)
862ba9
-        if not ret and report_problems:
862ba9
-            msg = dnf.util._format_resolve_problems(ng.problem_rules())
862ba9
-            logger.warning(msg)
862ba9
-        problem_conflicts = set(ng.problem_conflicts(available=True))
862ba9
-        problem_dependency = set(ng.problem_broken_dependency(available=True)) - problem_conflicts
862ba9
-
862ba9
-        def _nevra(item):
862ba9
-            return hawkey.NEVRA(name=item.name, epoch=item.epoch, version=item.version,
862ba9
-                                release=item.release, arch=item.arch)
862ba9
-
862ba9
-        # Sometimes, pkg is not in transaction item, therefore, comparing by nevra
862ba9
-        transaction_nevras = [_nevra(tsi) for tsi in transaction]
862ba9
-        skipped_conflicts = set(
862ba9
-            [pkg for pkg in problem_conflicts if _nevra(pkg) not in transaction_nevras])
862ba9
-        skipped_dependency = set(
862ba9
-            [pkg for pkg in problem_dependency if _nevra(pkg) not in transaction_nevras])
862ba9
-
862ba9
-        return skipped_conflicts, skipped_dependency
862ba9
-
862ba9
     def list_transaction(self, transaction, total_width=None):
862ba9
         """Return a string representation of the transaction in an
862ba9
         easy-to-read format.
862ba9
@@ -1102,7 +1024,7 @@ def list_transaction(self, transaction, total_width=None):
862ba9
             # in order to display module changes when RPM transaction is empty
862ba9
             transaction = []
862ba9
 
862ba9
-        list_bunch = _make_lists(transaction)
862ba9
+        list_bunch = dnf.util._make_lists(transaction)
862ba9
         pkglist_lines = []
862ba9
         data = {'n' : {}, 'v' : {}, 'r' : {}}
862ba9
         a_wid = 0 # Arch can't get "that big" ... so always use the max.
862ba9
@@ -1271,7 +1193,7 @@ def format_line(group):
862ba9
         # show skipped conflicting packages
862ba9
         if not self.conf.best and self.base._goal.actions & forward_actions:
862ba9
             lines = []
862ba9
-            skipped_conflicts, skipped_broken = self._skipped_packages(
862ba9
+            skipped_conflicts, skipped_broken = self.base._skipped_packages(
862ba9
                 report_problems=True, transaction=transaction)
862ba9
             skipped_broken = dict((str(pkg), pkg) for pkg in skipped_broken)
862ba9
             for pkg in sorted(skipped_conflicts):
862ba9
@@ -1436,13 +1358,8 @@ def format_line(group):
862ba9
                                   max_msg_count, count, msg_pkgs))
862ba9
         return ''.join(out)
862ba9
 
862ba9
-    def post_transaction_output(self, transaction):
862ba9
-        """Returns a human-readable summary of the results of the
862ba9
-        transaction.
862ba9
 
862ba9
-        :return: a string containing a human-readable summary of the
862ba9
-           results of the transaction
862ba9
-        """
862ba9
+    def _pto_callback(self, action, tsis):
862ba9
         #  Works a bit like calcColumns, but we never overflow a column we just
862ba9
         # have a dynamic number of columns.
862ba9
         def _fits_in_cols(msgs, num):
862ba9
@@ -1472,61 +1389,33 @@ def _fits_in_cols(msgs, num):
862ba9
                 col_lens[col] *= -1
862ba9
             return col_lens
862ba9
 
862ba9
-        def _tsi_or_pkg_nevra_cmp(item1, item2):
862ba9
-            """Compares two transaction items or packages by nevra.
862ba9
-               Used as a fallback when tsi does not contain package object.
862ba9
-            """
862ba9
-            ret = (item1.name > item2.name) - (item1.name < item2.name)
862ba9
-            if ret != 0:
862ba9
-                return ret
862ba9
-            nevra1 = hawkey.NEVRA(name=item1.name, epoch=item1.epoch, version=item1.version,
862ba9
-                                  release=item1.release, arch=item1.arch)
862ba9
-            nevra2 = hawkey.NEVRA(name=item2.name, epoch=item2.epoch, version=item2.version,
862ba9
-                                  release=item2.release, arch=item2.arch)
862ba9
-            ret = nevra1.evr_cmp(nevra2, self.sack)
862ba9
-            if ret != 0:
862ba9
-                return ret
862ba9
-            return (item1.arch > item2.arch) - (item1.arch < item2.arch)
862ba9
-
862ba9
-        out = ''
862ba9
-        list_bunch = _make_lists(transaction)
862ba9
-
862ba9
-        skipped_conflicts, skipped_broken = self._skipped_packages(
862ba9
-            report_problems=False, transaction=transaction)
862ba9
-        skipped = skipped_conflicts.union(skipped_broken)
862ba9
-
862ba9
-        for (action, tsis) in [(_('Upgraded'), list_bunch.upgraded),
862ba9
-                               (_('Downgraded'), list_bunch.downgraded),
862ba9
-                               (_('Installed'), list_bunch.installed +
862ba9
-                                list_bunch.installed_group +
862ba9
-                                list_bunch.installed_weak +
862ba9
-                                list_bunch.installed_dep),
862ba9
-                               (_('Reinstalled'), list_bunch.reinstalled),
862ba9
-                               (_('Skipped'), skipped),
862ba9
-                               (_('Removed'), list_bunch.erased +
862ba9
-                                   list_bunch.erased_dep +
862ba9
-                                   list_bunch.erased_clean),
862ba9
-                               (_('Failed'), list_bunch.failed)]:
862ba9
-            if not tsis:
862ba9
-                continue
862ba9
-            msgs = []
862ba9
-            out += '\n%s:\n' % action
862ba9
-            for tsi in sorted(tsis, key=functools.cmp_to_key(_tsi_or_pkg_nevra_cmp)):
862ba9
-                msgs.append(str(tsi))
862ba9
-            for num in (8, 7, 6, 5, 4, 3, 2):
862ba9
-                cols = _fits_in_cols(msgs, num)
862ba9
-                if cols:
862ba9
-                    break
862ba9
-            if not cols:
862ba9
-                cols = [-(self.term.columns - 2)]
862ba9
-            while msgs:
862ba9
-                current_msgs = msgs[:len(cols)]
862ba9
-                out += '  '
862ba9
-                out += self.fmtColumns(zip(current_msgs, cols), end=u'\n')
862ba9
-                msgs = msgs[len(cols):]
862ba9
-
862ba9
+        if not tsis:
862ba9
+            return ''
862ba9
+        out = []
862ba9
+        msgs = []
862ba9
+        out.append('{}:'.format(action))
862ba9
+        for tsi in tsis:
862ba9
+            msgs.append(str(tsi))
862ba9
+        for num in (8, 7, 6, 5, 4, 3, 2):
862ba9
+            cols = _fits_in_cols(msgs, num)
862ba9
+            if cols:
862ba9
+                break
862ba9
+        if not cols:
862ba9
+            cols = [-(self.term.columns - 2)]
862ba9
+        while msgs:
862ba9
+            current_msgs = msgs[:len(cols)]
862ba9
+            out.append('  {}'.format(self.fmtColumns(zip(current_msgs, cols))))
862ba9
+            msgs = msgs[len(cols):]
862ba9
         return out
862ba9
 
862ba9
+
862ba9
+    def post_transaction_output(self, transaction):
862ba9
+        """
862ba9
+        Return a human-readable summary of the transaction. Packages in sections
862ba9
+        are arranged to columns.
862ba9
+        """
862ba9
+        return dnf.util._post_transaction_output(self.base, transaction, self._pto_callback)
862ba9
+
862ba9
     def setup_progress_callbacks(self):
862ba9
         """Set up the progress callbacks and various
862ba9
            output bars based on debug level.
862ba9
diff --git a/dnf/util.py b/dnf/util.py
862ba9
index 8cf362706d..0beb04424d 100644
862ba9
--- a/dnf/util.py
862ba9
+++ b/dnf/util.py
862ba9
@@ -24,13 +24,14 @@
862ba9
 
862ba9
 from .pycomp import PY3, basestring
862ba9
 from dnf.i18n import _, ucd
862ba9
-from functools import reduce
862ba9
 import argparse
862ba9
 import dnf
862ba9
 import dnf.callback
862ba9
 import dnf.const
862ba9
 import dnf.pycomp
862ba9
 import errno
862ba9
+import functools
862ba9
+import hawkey
862ba9
 import itertools
862ba9
 import locale
862ba9
 import logging
862ba9
@@ -41,6 +42,7 @@
862ba9
 import tempfile
862ba9
 import time
862ba9
 import libdnf.repo
862ba9
+import libdnf.transaction
862ba9
 
862ba9
 logger = logging.getLogger('dnf')
862ba9
 
862ba9
@@ -195,7 +197,7 @@ def group_by_filter(fn, iterable):
862ba9
     def splitter(acc, item):
862ba9
         acc[not bool(fn(item))].append(item)
862ba9
         return acc
862ba9
-    return reduce(splitter, iterable, ([], []))
862ba9
+    return functools.reduce(splitter, iterable, ([], []))
862ba9
 
862ba9
 def insert_if(item, iterable, condition):
862ba9
     """Insert an item into an iterable by a condition."""
862ba9
@@ -504,3 +506,99 @@ def __setattr__(self, what, val):
862ba9
         def setter(item):
862ba9
             setattr(item, what, val)
862ba9
         return list(map(setter, self))
862ba9
+
862ba9
+
862ba9
+def _make_lists(transaction):
862ba9
+    b = Bunch({
862ba9
+        'downgraded': [],
862ba9
+        'erased': [],
862ba9
+        'erased_clean': [],
862ba9
+        'erased_dep': [],
862ba9
+        'installed': [],
862ba9
+        'installed_group': [],
862ba9
+        'installed_dep': [],
862ba9
+        'installed_weak': [],
862ba9
+        'reinstalled': [],
862ba9
+        'upgraded': [],
862ba9
+        'failed': [],
862ba9
+    })
862ba9
+
862ba9
+    for tsi in transaction:
862ba9
+        if tsi.state == libdnf.transaction.TransactionItemState_ERROR:
862ba9
+            b.failed.append(tsi)
862ba9
+        elif tsi.action == libdnf.transaction.TransactionItemAction_DOWNGRADE:
862ba9
+            b.downgraded.append(tsi)
862ba9
+        elif tsi.action == libdnf.transaction.TransactionItemAction_INSTALL:
862ba9
+            if tsi.reason == libdnf.transaction.TransactionItemReason_GROUP:
862ba9
+                b.installed_group.append(tsi)
862ba9
+            elif tsi.reason == libdnf.transaction.TransactionItemReason_DEPENDENCY:
862ba9
+                b.installed_dep.append(tsi)
862ba9
+            elif tsi.reason == libdnf.transaction.TransactionItemReason_WEAK_DEPENDENCY:
862ba9
+                b.installed_weak.append(tsi)
862ba9
+            else:
862ba9
+                # TransactionItemReason_USER
862ba9
+                b.installed.append(tsi)
862ba9
+        elif tsi.action == libdnf.transaction.TransactionItemAction_REINSTALL:
862ba9
+            b.reinstalled.append(tsi)
862ba9
+        elif tsi.action == libdnf.transaction.TransactionItemAction_REMOVE:
862ba9
+            if tsi.reason == libdnf.transaction.TransactionItemReason_CLEAN:
862ba9
+                b.erased_clean.append(tsi)
862ba9
+            elif tsi.reason == libdnf.transaction.TransactionItemReason_DEPENDENCY:
862ba9
+                b.erased_dep.append(tsi)
862ba9
+            else:
862ba9
+                b.erased.append(tsi)
862ba9
+        elif tsi.action == libdnf.transaction.TransactionItemAction_UPGRADE:
862ba9
+            b.upgraded.append(tsi)
862ba9
+
862ba9
+    return b
862ba9
+
862ba9
+
862ba9
+def _post_transaction_output(base, transaction, action_callback):
862ba9
+    """Returns a human-readable summary of the results of the
862ba9
+    transaction.
862ba9
+
862ba9
+    :param action_callback: function generating output for specific action. It
862ba9
+       takes two parameters - action as a string and list of affected packages for
862ba9
+       this action
862ba9
+    :return: a list of lines containing a human-readable summary of the
862ba9
+       results of the transaction
862ba9
+    """
862ba9
+    def _tsi_or_pkg_nevra_cmp(item1, item2):
862ba9
+        """Compares two transaction items or packages by nevra.
862ba9
+           Used as a fallback when tsi does not contain package object.
862ba9
+        """
862ba9
+        ret = (item1.name > item2.name) - (item1.name < item2.name)
862ba9
+        if ret != 0:
862ba9
+            return ret
862ba9
+        nevra1 = hawkey.NEVRA(name=item1.name, epoch=item1.epoch, version=item1.version,
862ba9
+                              release=item1.release, arch=item1.arch)
862ba9
+        nevra2 = hawkey.NEVRA(name=item2.name, epoch=item2.epoch, version=item2.version,
862ba9
+                              release=item2.release, arch=item2.arch)
862ba9
+        ret = nevra1.evr_cmp(nevra2, base.sack)
862ba9
+        if ret != 0:
862ba9
+            return ret
862ba9
+        return (item1.arch > item2.arch) - (item1.arch < item2.arch)
862ba9
+
862ba9
+    list_bunch = dnf.util._make_lists(transaction)
862ba9
+
862ba9
+    skipped_conflicts, skipped_broken = base._skipped_packages(
862ba9
+        report_problems=False, transaction=transaction)
862ba9
+    skipped = skipped_conflicts.union(skipped_broken)
862ba9
+
862ba9
+    out = []
862ba9
+    for (action, tsis) in [(_('Upgraded'), list_bunch.upgraded),
862ba9
+                           (_('Downgraded'), list_bunch.downgraded),
862ba9
+                           (_('Installed'), list_bunch.installed +
862ba9
+                            list_bunch.installed_group +
862ba9
+                            list_bunch.installed_weak +
862ba9
+                            list_bunch.installed_dep),
862ba9
+                           (_('Reinstalled'), list_bunch.reinstalled),
862ba9
+                           (_('Skipped'), skipped),
862ba9
+                           (_('Removed'), list_bunch.erased +
862ba9
+                               list_bunch.erased_dep +
862ba9
+                               list_bunch.erased_clean),
862ba9
+                           (_('Failed'), list_bunch.failed)]:
862ba9
+        out.extend(action_callback(
862ba9
+            action, sorted(tsis, key=functools.cmp_to_key(_tsi_or_pkg_nevra_cmp))))
862ba9
+
862ba9
+    return out