#509 Refactor images filter methods
Closed 4 years ago by mprahl. Opened 4 years ago by cqi.

file modified
+22 -32
@@ -33,6 +33,7 @@ 

  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 @@ 

  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 @@ 

              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!
qwan commented 4 years ago

Could you also update these comments?

                  # 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 @@ 

              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):

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

looks good. @cqi feel free to merge this PR

@cqi can you please resolve the conflicts so we can merge this?

rebased onto 3db5202

4 years ago

Could you also update these comments?

Besides qwan's comment, it looks good.

Freshmaker has been migrated to GitHub. Please reopen your PR on GitHub.

Pull-Request has been closed by mprahl

4 years ago