Blame SOURCES/0014-feat-repomanage-Add-new-option-oldonly.patch

6ca87e
From eb1d6edf55c167d575ce3d16bd6349e382d05600 Mon Sep 17 00:00:00 2001
6ca87e
From: Masahiro Matsuya <mmatsuya@redhat.com>
6ca87e
Date: Wed, 13 Apr 2022 18:42:03 +0900
6ca87e
Subject: [PATCH] feat(repomanage): Add new option --oldonly
6ca87e
6ca87e
= changelog =
6ca87e
msg:           repomanage: Add new option --oldonly
6ca87e
type:          enhancement
6ca87e
resolves:      https://bugzilla.redhat.com/show_bug.cgi?id=2034736
6ca87e
resolves:      https://bugzilla.redhat.com/show_bug.cgi?id=2058676
6ca87e
---
6ca87e
 doc/repomanage.rst    |  3 +++
6ca87e
 plugins/repomanage.py | 46 ++++++++++++++++++++++++++++++++++++++++---
6ca87e
 2 files changed, 46 insertions(+), 3 deletions(-)
6ca87e
6ca87e
diff --git a/doc/repomanage.rst b/doc/repomanage.rst
6ca87e
index e3171ef..3c01289 100644
6ca87e
--- a/doc/repomanage.rst
6ca87e
+++ b/doc/repomanage.rst
6ca87e
@@ -47,6 +47,9 @@ The following options set what packages are displayed. These options are mutuall
6ca87e
 ``--old``
6ca87e
     Show older packages (for a package or a stream show all versions except the newest one).
6ca87e
 
6ca87e
+``--oldonly``
6ca87e
+    Show older packages (same as --old, but exclude the newest packages even when it's included in the older stream versions).
6ca87e
+
6ca87e
 ``--new``
6ca87e
     Show newest packages.
6ca87e
 
6ca87e
diff --git a/plugins/repomanage.py b/plugins/repomanage.py
6ca87e
index 67a6fc7..d23497a 100644
6ca87e
--- a/plugins/repomanage.py
6ca87e
+++ b/plugins/repomanage.py
6ca87e
@@ -57,6 +57,12 @@ class RepoManageCommand(dnf.cli.Command):
6ca87e
     def run(self):
6ca87e
         if self.opts.new and self.opts.old:
6ca87e
             raise dnf.exceptions.Error(_("Pass either --old or --new, not both!"))
6ca87e
+        if self.opts.new and self.opts.oldonly:
6ca87e
+            raise dnf.exceptions.Error(_("Pass either --oldonly or --new, not both!"))
6ca87e
+        if self.opts.old and self.opts.oldonly:
6ca87e
+            raise dnf.exceptions.Error(_("Pass either --old or --oldonly, not both!"))
6ca87e
+        if not self.opts.old and not self.opts.oldonly:
6ca87e
+            self.opts.new = True
6ca87e
 
6ca87e
         verfile = {}
6ca87e
         pkgdict = {}
6ca87e
@@ -123,8 +129,7 @@ class RepoManageCommand(dnf.cli.Command):
6ca87e
         # modular packages
6ca87e
         keepnum_latest_stream_artifacts = set()
6ca87e
 
6ca87e
-        # if new
6ca87e
-        if not self.opts.old:
6ca87e
+        if self.opts.new:
6ca87e
             # regular packages
6ca87e
             for (n, a) in pkgdict.keys():
6ca87e
                 evrlist = pkgdict[(n, a)]
6ca87e
@@ -146,7 +151,6 @@ class RepoManageCommand(dnf.cli.Command):
6ca87e
                     for stream in streams_by_version[i]:
6ca87e
                         keepnum_latest_stream_artifacts.update(set(stream.getArtifacts()))
6ca87e
 
6ca87e
-
6ca87e
         if self.opts.old:
6ca87e
             # regular packages
6ca87e
             for (n, a) in pkgdict.keys():
6ca87e
@@ -169,6 +173,40 @@ class RepoManageCommand(dnf.cli.Command):
6ca87e
                     for stream in streams_by_version[i]:
6ca87e
                         keepnum_latest_stream_artifacts.update(set(stream.getArtifacts()))
6ca87e
 
6ca87e
+        if self.opts.oldonly:
6ca87e
+            # regular packages
6ca87e
+            for (n, a) in pkgdict.keys():
6ca87e
+                evrlist = pkgdict[(n, a)]
6ca87e
+
6ca87e
+                oldevrs = evrlist[:-keepnum]
6ca87e
+
6ca87e
+                for package in oldevrs:
6ca87e
+                    nevra = self._package_to_nevra(package)
6ca87e
+                    for fpkg in verfile[nevra]:
6ca87e
+                        outputpackages.append(fpkg)
6ca87e
+
6ca87e
+            # modular packages
6ca87e
+            keepnum_newer_stream_artifacts = set()
6ca87e
+
6ca87e
+            for streams_by_version in module_dict.values():
6ca87e
+                sorted_stream_versions = sorted(streams_by_version.keys())
6ca87e
+
6ca87e
+                new_sorted_stream_versions = sorted_stream_versions[-keepnum:]
6ca87e
+
6ca87e
+                for i in new_sorted_stream_versions:
6ca87e
+                    for stream in streams_by_version[i]:
6ca87e
+                        keepnum_newer_stream_artifacts.update(set(stream.getArtifacts()))
6ca87e
+
6ca87e
+            for streams_by_version in module_dict.values():
6ca87e
+                sorted_stream_versions = sorted(streams_by_version.keys())
6ca87e
+
6ca87e
+                old_sorted_stream_versions = sorted_stream_versions[:-keepnum]
6ca87e
+
6ca87e
+                for i in old_sorted_stream_versions:
6ca87e
+                    for stream in streams_by_version[i]:
6ca87e
+                        for artifact in stream.getArtifacts():
6ca87e
+                            if artifact not in keepnum_newer_stream_artifacts:
6ca87e
+                                keepnum_latest_stream_artifacts.add(artifact)
6ca87e
 
6ca87e
         modular_packages = [self._package_to_path(x) for x in query.filter(pkg__eq=query.filter(nevra_strict=keepnum_latest_stream_artifacts)).available()]
6ca87e
         outputpackages = outputpackages + modular_packages
6ca87e
@@ -183,6 +221,8 @@ class RepoManageCommand(dnf.cli.Command):
6ca87e
     def set_argparser(parser):
6ca87e
         parser.add_argument("-o", "--old", action="store_true",
6ca87e
                             help=_("Print the older packages"))
6ca87e
+        parser.add_argument("-O", "--oldonly", action="store_true",
6ca87e
+                            help=_("Print the older packages. Exclude the newest packages."))
6ca87e
         parser.add_argument("-n", "--new", action="store_true",
6ca87e
                             help=_("Print the newest packages"))
6ca87e
         parser.add_argument("-s", "--space", action="store_true",
6ca87e
-- 
6ca87e
2.36.1
6ca87e