From 3db5202aadcf29363ec1a47d6bc4faf4f642510a Mon Sep 17 00:00:00 2001
From: Chenxiong Qi <cqi@redhat.com>
Date: Jun 02 2020 06:10:11 +0000
Subject: Refactor images filter methods


This change refactors two filter methods
filter_out_images_with_higher_srpm_nvr and
filter_out_modularity_mismatch.

Signed-off-by: Chenxiong Qi <cqi@redhat.com>

---

diff --git a/freshmaker/lightblue.py b/freshmaker/lightblue.py
index 9e41c43..42c2f28 100644
--- a/freshmaker/lightblue.py
+++ b/freshmaker/lightblue.py
@@ -33,6 +33,7 @@ import kobo.rpmlib
 from concurrent.futures import ThreadPoolExecutor
 from http import HTTPStatus
 from itertools import groupby
+from kobo.rpmlib import compare_nvr, parse_nvr
 
 from freshmaker import log, conf
 from freshmaker.kojiservice import koji_service
@@ -40,6 +41,12 @@ from freshmaker.utils import sorted_by_nvr, is_pkg_modular
 import koji
 
 
+def is_older_nvr(left_nvr: str, right_nvr: str):
+    """Check if left NVR is older than right NVR"""
+    return -1 == compare_nvr(
+        parse_nvr(left_nvr), parse_nvr(right_nvr), ignore_epoch=True)
+
+
 class LightBlueError(Exception):
     """Base class representing errors from LightBlue server"""
 
@@ -722,32 +729,19 @@ class LightBlue(object):
             rpms = image.get_rpms()
             if rpms is None:
                 ret.append(image)
-            image_included = False
-            for rpm in rpms or []:
-                image_srpm_nvr = kobo.rpmlib.parse_nvr(rpm["srpm_nevra"])
-                for srpm_nvr in srpm_name_to_nvrs.get(rpm.get("srpm_name"), []):
-                    input_srpm_nvr = kobo.rpmlib.parse_nvr(srpm_nvr)
-                    # compare_nvr return values:
-                    #   - nvr1 newer than nvr2: 1
-                    #   - same nvrs: 0
-                    #   - nvr1 older: -1
-                    # We want to rebuild only images with SRPM NVR lower than
-                    # input SRPM NVR, therefore we check for -1.
-                    if kobo.rpmlib.compare_nvr(
-                            image_srpm_nvr, input_srpm_nvr, ignore_epoch=True) == -1:
-                        ret.append(image)
-                        image_included = True
-                        break
-                if image_included:
-                    break
+                continue
+            if any(is_older_nvr(rpm["srpm_nevra"], srpm_nvr)
+                   for rpm in rpms
+                   for srpm_nvr in srpm_name_to_nvrs.get(rpm.get("srpm_name"), [])):
+                ret.append(image)
             else:
                 # Oh-no, the mighty for/else block!
                 # The else clause executes after the loop completes normally.
                 # This means that the loop did not encounter a break statement.
                 # In our case, this means that we filtered out the image.
                 log.info("Will not rebuild %s, because it does not contain "
-                         "older version of any input package: %r" % (
-                             image.nvr, srpm_name_to_nvrs.values()))
+                         "older version of any input package: %r",
+                         image.nvr, srpm_name_to_nvrs.values())
         return ret
 
     def filter_out_modularity_mismatch(self, images, srpm_name_to_nvrs):
@@ -769,23 +763,19 @@ class LightBlue(object):
             rpms = image.get_rpms()
             if rpms is None:
                 ret.append(image)
-            image_included = False
+                continue
             # Include the image if the SRPM from the advisory is modular, and the SRPM of the same
             # name in the image is also modular. Also, include the image if the opposite is true.
-            for rpm in rpms or []:
-                for srpm_nvr in srpm_name_to_nvrs.get(rpm.get("srpm_name"), []):
-                    if ((is_pkg_modular(srpm_nvr) and is_pkg_modular(rpm["srpm_nevra"])) or
-                            (not is_pkg_modular(srpm_nvr) and not is_pkg_modular(rpm["srpm_nevra"]))):
-                        ret.append(image)
-                        image_included = True
-                        break
-                if image_included:
-                    break
+            if any((is_pkg_modular(srpm_nvr) and is_pkg_modular(rpm["srpm_nevra"])) or
+                   (not is_pkg_modular(srpm_nvr) and not is_pkg_modular(rpm["srpm_nevra"]))
+                   for rpm in rpms
+                   for srpm_nvr in srpm_name_to_nvrs.get(rpm.get("srpm_name"), [])):
+                ret.append(image)
             else:
                 log.info(
                     "Will not rebuild %s because there is a modularity mismatch between the RPMs "
-                    "from the image and the advisory: %r" % (
-                        image.nvr, srpm_name_to_nvrs.values()))
+                    "from the image and the advisory: %r",
+                    image.nvr, srpm_name_to_nvrs.values())
         return ret
 
     def filter_out_images_based_on_content_set(self, images, content_sets):

