Blame SOURCES/BZ-1455318-package-cleanup-dont-remove-required.patch

911d2c
commit f84a2dbcc28312105246fac51771481640759da5
911d2c
Author: Phil Dibowitz <phil@ipom.com>
911d2c
Date:   Thu Jul 2 11:40:55 2015 -0700
911d2c
911d2c
    Add option to remove newest dupes instead of oldest dupes
911d2c
    
911d2c
    Sometimes in a failed transaction you need to remove the newest of the dupes and
911d2c
    not the oldest. This provides that option.
911d2c
911d2c
diff --git a/package-cleanup.py b/package-cleanup.py
911d2c
index acad9f2..13cfb89 100755
911d2c
--- a/package-cleanup.py
911d2c
+++ b/package-cleanup.py
911d2c
@@ -79,6 +79,9 @@ class PackageCleanup(YumUtilBase):
911d2c
         dupegrp.add_option("--cleandupes", default=False, 
911d2c
                     dest="cleandupes", action="store_true",
911d2c
                     help='Scan for duplicates in your rpmdb and remove older ')
911d2c
+        dupegrp.add_option("--removenewestdupes", default=False, 
911d2c
+                    dest="removenewestdupes", action="store_true",
911d2c
+                    help='Remove the newest dupes instead of the oldest dupes.')
911d2c
         dupegrp.add_option("--noscripts", default=False,
911d2c
                     dest="noscripts", action="store_true",
911d2c
                     help="disable rpm scriptlets from running when cleaning duplicates")
911d2c
@@ -172,7 +175,7 @@ class PackageCleanup(YumUtilBase):
911d2c
             
911d2c
         return results
911d2c
 
911d2c
-    def _remove_old_dupes(self):
911d2c
+    def _remove_dupes(self, newest=False):
911d2c
         """add older duplicate pkgs to be removed in the transaction"""
911d2c
         dupedict = self._find_installed_duplicates()
911d2c
 
911d2c
@@ -180,7 +183,11 @@ class PackageCleanup(YumUtilBase):
911d2c
         for (name,dupelists) in dupedict.items():
911d2c
             for dupelist in dupelists:
911d2c
                 dupelist.sort()
911d2c
-                for lowpo in dupelist[0:-1]:
911d2c
+                if newest:
911d2c
+                    plist = dupelist[1:]
911d2c
+                else:
911d2c
+                    plist = dupelist[0:-1]
911d2c
+                for lowpo in plist:
911d2c
                     removedupes.append(lowpo)
911d2c
 
911d2c
         for po in removedupes:
911d2c
@@ -373,7 +380,7 @@ class PackageCleanup(YumUtilBase):
911d2c
                 sys.exit(1)
911d2c
             if opts.noscripts:
911d2c
                 self.conf.tsflags.append('noscripts')
911d2c
-            self._remove_old_dupes()
911d2c
+            self._remove_dupes(opts.removenewestdupes)
911d2c
             self.run_with_package_names.add('yum-utils')
911d2c
 
911d2c
             if hasattr(self, 'doUtilBuildTransaction'):
911d2c
commit 27784822a342debd3ecfa04686a35f2e7e576023
911d2c
Author: Michal Domonkos <mdomonko@redhat.com>
911d2c
Date:   Wed Oct 11 19:26:38 2017 +0200
911d2c
911d2c
    package-cleanup: don't remove required dupes. BZ 1455318
911d2c
    
911d2c
    Note: Turning removedupes into a set for O(1) complexity on
911d2c
    removedupes.remove().
911d2c
911d2c
diff --git a/package-cleanup.py b/package-cleanup.py
911d2c
index 13cfb89..9f74609 100755
911d2c
--- a/package-cleanup.py
911d2c
+++ b/package-cleanup.py
911d2c
@@ -176,10 +176,12 @@ class PackageCleanup(YumUtilBase):
911d2c
         return results
911d2c
 
911d2c
     def _remove_dupes(self, newest=False):
911d2c
-        """add older duplicate pkgs to be removed in the transaction"""
911d2c
-        dupedict = self._find_installed_duplicates()
911d2c
+        """add duplicate pkgs to be removed in the transaction,
911d2c
+           return a dict of excluded dupes and their requiring packages"""
911d2c
 
911d2c
-        removedupes = []
911d2c
+        # Find dupes
911d2c
+        dupedict = self._find_installed_duplicates()
911d2c
+        removedupes = set()
911d2c
         for (name,dupelists) in dupedict.items():
911d2c
             for dupelist in dupelists:
911d2c
                 dupelist.sort()
911d2c
@@ -188,11 +190,37 @@ class PackageCleanup(YumUtilBase):
911d2c
                 else:
911d2c
                     plist = dupelist[0:-1]
911d2c
                 for lowpo in plist:
911d2c
-                    removedupes.append(lowpo)
911d2c
-
911d2c
+                    removedupes.add(lowpo)
911d2c
+
911d2c
+        # Exclude any such dupes that would pull other installed packages into
911d2c
+        # the removal transaction (to prevent us from accidentally removing a
911d2c
+        # huge part of a working system) by performing a dry transaction(s)
911d2c
+        # first.
911d2c
+        excluded = {}
911d2c
+        while True:
911d2c
+            for po in removedupes:
911d2c
+                self.remove(po)
911d2c
+            changed = False
911d2c
+            for txmbr in self.tsInfo.getMembers():
911d2c
+                requiredby = self._checkRemove(txmbr)
911d2c
+                if requiredby:
911d2c
+                    removedupes.remove(txmbr.po)
911d2c
+                    excluded[txmbr.po] = requiredby
911d2c
+                    # Do another round, to cover any transitive deps within
911d2c
+                    # removedupes, for example: if foo requires bar requires
911d2c
+                    # baz and removedupes contains bar and baz, then
911d2c
+                    # _checkRemove(baz) won't return bar.
911d2c
+                    changed = True
911d2c
+            del self.tsInfo
911d2c
+            if not changed:
911d2c
+                break
911d2c
+
911d2c
+        # Mark the dupes for removal
911d2c
         for po in removedupes:
911d2c
             self.remove(po)
911d2c
 
911d2c
+        return excluded
911d2c
+
911d2c
 
911d2c
     def _should_show_leaf(self, po, leaf_regex, exclude_devel, exclude_bin):
911d2c
         """
911d2c
@@ -380,7 +408,13 @@ class PackageCleanup(YumUtilBase):
911d2c
                 sys.exit(1)
911d2c
             if opts.noscripts:
911d2c
                 self.conf.tsflags.append('noscripts')
911d2c
-            self._remove_dupes(opts.removenewestdupes)
911d2c
+            excluded = self._remove_dupes(opts.removenewestdupes)
911d2c
+            for po, requiredby in excluded.iteritems():
911d2c
+                count = len(requiredby)
911d2c
+                print ('Not removing %s because it is required by %s '
911d2c
+                       'installed package%s' %
911d2c
+                       (po.hdr.sprintf(opts.qf), count,
911d2c
+                        's' if count > 1 else ''))
911d2c
             self.run_with_package_names.add('yum-utils')
911d2c
 
911d2c
             if hasattr(self, 'doUtilBuildTransaction'):
911d2c
@@ -397,9 +431,20 @@ class PackageCleanup(YumUtilBase):
911d2c
 
911d2c
             if len(self.tsInfo) < 1:
911d2c
                 print 'No duplicates to remove'
911d2c
-                sys.exit(0)
911d2c
-                
911d2c
-            sys.exit(self.doUtilTransaction())
911d2c
+                errc = 0
911d2c
+            else:
911d2c
+                errc = self.doUtilTransaction()
911d2c
+
911d2c
+            if excluded:
911d2c
+                self.logger.warn(
911d2c
+                    'Warning: Some duplicates were not removed because they '
911d2c
+                    'are required by installed packages.\n'
911d2c
+                    'You can try --cleandupes with%s --removenewestdupes, '
911d2c
+                    'or review them with --dupes and remove manually.' %
911d2c
+                    ('out' if opts.removenewestdupes else '')
911d2c
+                )
911d2c
+
911d2c
+            sys.exit(errc)
911d2c
 
911d2c
     
911d2c
 if __name__ == '__main__':
911d2c
commit 4594bbc623b68bea6522047fb6267069c8ad94c8
911d2c
Author: Michal Domonkos <mdomonko@redhat.com>
911d2c
Date:   Wed Oct 11 20:23:06 2017 +0200
911d2c
911d2c
    package-cleanup: update man page
911d2c
    
911d2c
    Add missing options from --help and also clear up the wording of
911d2c
    --removenewestdupes.
911d2c
911d2c
diff --git a/docs/package-cleanup.1 b/docs/package-cleanup.1
911d2c
index 61959ac..5b7e1c0 100644
911d2c
--- a/docs/package-cleanup.1
911d2c
+++ b/docs/package-cleanup.1
911d2c
@@ -36,6 +36,10 @@ Scan for duplicates in the local RPM database.
911d2c
 .IP "\fB\-\-cleandupes\fP"
911d2c
 Scan for duplicates in the local RPM database and clean out the
911d2c
 older versions.
911d2c
+.IP "\fB\-\-removenewestdupes\fP"
911d2c
+Remove the newest dupes instead of the oldest dupes when cleaning duplicates.
911d2c
+.IP "\fB\-\-noscripts\fP"
911d2c
+Disable rpm scriptlets from running when cleaning duplicates.
911d2c
 .IP "\fB\-\-count <COUNT>\fP"
911d2c
 Number of duplicate/kernel packages to keep on the system (default 2)
911d2c
 .PP 
911d2c
diff --git a/package-cleanup.py b/package-cleanup.py
911d2c
index 9f74609..4a44326 100755
911d2c
--- a/package-cleanup.py
911d2c
+++ b/package-cleanup.py
911d2c
@@ -81,7 +81,7 @@ class PackageCleanup(YumUtilBase):
911d2c
                     help='Scan for duplicates in your rpmdb and remove older ')
911d2c
         dupegrp.add_option("--removenewestdupes", default=False, 
911d2c
                     dest="removenewestdupes", action="store_true",
911d2c
-                    help='Remove the newest dupes instead of the oldest dupes.')
911d2c
+                    help='Remove the newest dupes instead of the oldest dupes when cleaning duplicates.')
911d2c
         dupegrp.add_option("--noscripts", default=False,
911d2c
                     dest="noscripts", action="store_true",
911d2c
                     help="disable rpm scriptlets from running when cleaning duplicates")