Blob Blame History Raw
From eb1d6edf55c167d575ce3d16bd6349e382d05600 Mon Sep 17 00:00:00 2001
From: Masahiro Matsuya <mmatsuya@redhat.com>
Date: Wed, 13 Apr 2022 18:42:03 +0900
Subject: [PATCH] feat(repomanage): Add new option --oldonly

= changelog =
msg:           repomanage: Add new option --oldonly
type:          enhancement
resolves:      https://bugzilla.redhat.com/show_bug.cgi?id=2034736
resolves:      https://bugzilla.redhat.com/show_bug.cgi?id=2058676
---
 doc/repomanage.rst    |  3 +++
 plugins/repomanage.py | 46 ++++++++++++++++++++++++++++++++++++++++---
 2 files changed, 46 insertions(+), 3 deletions(-)

diff --git a/doc/repomanage.rst b/doc/repomanage.rst
index e3171ef..3c01289 100644
--- a/doc/repomanage.rst
+++ b/doc/repomanage.rst
@@ -47,6 +47,9 @@ The following options set what packages are displayed. These options are mutuall
 ``--old``
     Show older packages (for a package or a stream show all versions except the newest one).
 
+``--oldonly``
+    Show older packages (same as --old, but exclude the newest packages even when it's included in the older stream versions).
+
 ``--new``
     Show newest packages.
 
diff --git a/plugins/repomanage.py b/plugins/repomanage.py
index 67a6fc7..d23497a 100644
--- a/plugins/repomanage.py
+++ b/plugins/repomanage.py
@@ -57,6 +57,12 @@ class RepoManageCommand(dnf.cli.Command):
     def run(self):
         if self.opts.new and self.opts.old:
             raise dnf.exceptions.Error(_("Pass either --old or --new, not both!"))
+        if self.opts.new and self.opts.oldonly:
+            raise dnf.exceptions.Error(_("Pass either --oldonly or --new, not both!"))
+        if self.opts.old and self.opts.oldonly:
+            raise dnf.exceptions.Error(_("Pass either --old or --oldonly, not both!"))
+        if not self.opts.old and not self.opts.oldonly:
+            self.opts.new = True
 
         verfile = {}
         pkgdict = {}
@@ -123,8 +129,7 @@ class RepoManageCommand(dnf.cli.Command):
         # modular packages
         keepnum_latest_stream_artifacts = set()
 
-        # if new
-        if not self.opts.old:
+        if self.opts.new:
             # regular packages
             for (n, a) in pkgdict.keys():
                 evrlist = pkgdict[(n, a)]
@@ -146,7 +151,6 @@ class RepoManageCommand(dnf.cli.Command):
                     for stream in streams_by_version[i]:
                         keepnum_latest_stream_artifacts.update(set(stream.getArtifacts()))
 
-
         if self.opts.old:
             # regular packages
             for (n, a) in pkgdict.keys():
@@ -169,6 +173,40 @@ class RepoManageCommand(dnf.cli.Command):
                     for stream in streams_by_version[i]:
                         keepnum_latest_stream_artifacts.update(set(stream.getArtifacts()))
 
+        if self.opts.oldonly:
+            # regular packages
+            for (n, a) in pkgdict.keys():
+                evrlist = pkgdict[(n, a)]
+
+                oldevrs = evrlist[:-keepnum]
+
+                for package in oldevrs:
+                    nevra = self._package_to_nevra(package)
+                    for fpkg in verfile[nevra]:
+                        outputpackages.append(fpkg)
+
+            # modular packages
+            keepnum_newer_stream_artifacts = set()
+
+            for streams_by_version in module_dict.values():
+                sorted_stream_versions = sorted(streams_by_version.keys())
+
+                new_sorted_stream_versions = sorted_stream_versions[-keepnum:]
+
+                for i in new_sorted_stream_versions:
+                    for stream in streams_by_version[i]:
+                        keepnum_newer_stream_artifacts.update(set(stream.getArtifacts()))
+
+            for streams_by_version in module_dict.values():
+                sorted_stream_versions = sorted(streams_by_version.keys())
+
+                old_sorted_stream_versions = sorted_stream_versions[:-keepnum]
+
+                for i in old_sorted_stream_versions:
+                    for stream in streams_by_version[i]:
+                        for artifact in stream.getArtifacts():
+                            if artifact not in keepnum_newer_stream_artifacts:
+                                keepnum_latest_stream_artifacts.add(artifact)
 
         modular_packages = [self._package_to_path(x) for x in query.filter(pkg__eq=query.filter(nevra_strict=keepnum_latest_stream_artifacts)).available()]
         outputpackages = outputpackages + modular_packages
@@ -183,6 +221,8 @@ class RepoManageCommand(dnf.cli.Command):
     def set_argparser(parser):
         parser.add_argument("-o", "--old", action="store_true",
                             help=_("Print the older packages"))
+        parser.add_argument("-O", "--oldonly", action="store_true",
+                            help=_("Print the older packages. Exclude the newest packages."))
         parser.add_argument("-n", "--new", action="store_true",
                             help=_("Print the newest packages"))
         parser.add_argument("-s", "--space", action="store_true",
-- 
2.36.1