#10 Unitfy API of get_modules
Merged 5 years ago by cqi. Opened 5 years ago by cqi.
cqi/ursa-major unitfy-API-get_modules  into  master

file modified
+34 -14
@@ -106,24 +106,44 @@ 

              return json.load(f)

  

  

- def make_mbs_response(module_builds):

+ def make_mbs_response(module_builds, per_page=None):

      """Helper function to make fake MBS response JSON

  

      When calling this method, developer only needs to pass a list of module

      builds. Finally, a full response JSON data with only one page data set is

      returned.

      """

-     return {

-         'items': module_builds,

-         'meta': {

-             'prev': None,

-             'last': None,

-             'next': None,

-             'total': 1,

-             'per_page': 10,

-             'first': 'https://mbs.example.com/module-build-service/1/module-builds/'

-                      '?per_page=10&page=1',

-             'pages': 1,

-             'page': 1

+     total = len(module_builds)

+     per_page = per_page or total

+     pages = int(total / per_page)

+     if total % per_page > 0:

+         pages += 1

+ 

+     def page_url(page_number):

+         return (

+             'https://mbs.example.com/module-build-service/1/module-builds/'

+             '?per_page={}&page={}'.format(per_page, page_number)

+         )

+ 

+     result = []

+     offset = 0

+ 

+     for page in range(1, pages + 1):

+         page_result = {

+             'items': module_builds[offset:offset + per_page],

+             'meta': {

+                 'first': page_url(1), 'prev': None, 'next': None, 'last': page_url(pages),

+                 'total': total, 'per_page': per_page, 'pages': pages, 'page': page

+             }

          }

-     }

+ 

+         meta = page_result['meta']

+         if page - 1 > 0:

+             meta['prev'] = page_url(page - 1)

+         if page + 1 <= pages:

+             meta['next'] = page_url(page + 1)

+ 

+         offset += per_page

+         result.append(page_result)

+ 

+     return result

file modified
+136 -2
@@ -24,10 +24,13 @@ 

  import json

  import os

  import tempfile

+ import pytest

  

  from mock import patch, Mock

- from tests import TEST_DATA_DIR, make_mbs_response, make_mmd

+ from tests import TEST_DATA_DIR, make_mbs_response, make_mmd, URSA_MAJOR_TEST_CONFIG

+ from ursa_major import ModuleConfig

  from ursa_major.cli import main

+ from ursa_major.handlers.add_module import AddModuleHandler

  

  

  def get_tag_side_effect(info):
@@ -105,7 +108,7 @@ 

                                       requires={'platform': 'f30'},

                                       buildrequires={'platform': 'f30'}).dumps(),

              },

-         ])

+         ])[0]

  

          # For this test, we write an empty tag config file.

          self.write_tag_config_file(tag_configs_content or {})
@@ -352,3 +355,134 @@ 

              }

          }

          assert expected == self.read_tag_config_file()

+ 

+ 

+ class TestAddModuleFunctions:

+ 

+     # NOTE: once MBS supports filtering modules builds by requires or

+     # buildrequires, the test data should be updated because it is unnecessary

+     # to filter module builds locally by Ursa-Major itself.

+ 

+     @pytest.mark.parametrize(

+         'module_config,module_builds,expected_koji_tags', [

+             # None of these koji_tags exist in tag inheritance.

+             (ModuleConfig({'name': 'mariadb', 'stream': '10.3',

+                            'version': '3120190304180825', 'context': 'f636be4b'}),

+              [{'id': 1,

+                'name': 'mariadb',

+                'koji_tag': 'module-mariadb-10.3-3020190301191548-a5b0195c'},

+               {'id': 2,

+                'name': 'mariadb',

+                'koji_tag': 'module-mariadb-10.3-3120190304180825-f636be4b'}],

+              []),

+ 

+             # koji_tag exists in tag inheritance

+             (ModuleConfig({'name': 'mariadb', 'stream': '10.4',

+                            'version': '3120190304180825', 'context': 'f636be4b'}),

+              [{'id': 1,

+                'name': 'mariadb',

+                'koji_tag': 'module-mariadb-10.4-3020190313091759-a5b0195c'},

+               {'id': 2,

+                'name': 'mariadb',

+                # This older koji_tag was added previously

+                'koji_tag': 'module-mariadb-10.4-3020190304180835-a5b0195c'}],

+              ['module-mariadb-10.4-3020190304180835-a5b0195c']),

+ 

+             # Modules are filtered by requires.

+             # None of these koji_tags exist in tag inheritance.

+             (ModuleConfig({'name': 'mariadb', 'stream': '10.3',

+                            'version': '3120190304180825', 'context': 'f636be4b',

+                            'requires': {'platform': 'f30'}}),

+              [{'id': 1,

+                'name': 'mariadb',

+                'koji_tag': 'module-mariadb-10.3-3020190301191548-a5b0195c',

+                'modulemd': make_mmd(name='mariadb', stream='10.3',

+                                     version='3020190301191548', context='a5b0195c',

+                                     requires={'platform': 'f30'},

+                                     buildrequires={'platform': 'f30'}).dumps(),

+                },

+               {'id': 2,

+                'name': 'mariadb',

+                'koji_tag': 'module-mariadb-10.3-3120190304180825-f636be4b',

+                'modulemd': make_mmd(name='mariadb', stream='10.3',

+                                     version='3120190304180825', context='f636be4b',

+                                     requires={'platform': 'f29'},

+                                     buildrequires={'platform': 'f29'}).dumps(),

+                }],

+              []),

+ 

+             # koji_tag exists in tag inheritance and module builds are

+             # filtered by both requires and buildrequires.

+             (ModuleConfig({'name': 'mariadb', 'stream': '10.4',

+                            'version': '3120190304180825', 'context': 'f636be4b',

+                            'requires': {'platform': 'f30'},

+                            'buildrequires': {'platform': 'f30'}}),

+              [{'id': 1,

+                'name': 'mariadb',

+                'koji_tag': 'module-mariadb-10.4-3020190313091759-a5b0195c',

+                'modulemd': make_mmd(name='mariadb', stream='10.4',

+                                     version='3020190313091759', context='a5b0195c',

+                                     requires={'platform': 'f29'},

+                                     buildrequires={'platform': 'f29'}).dumps(),

+                },

+               {'id': 2,

+                'name': 'mariadb',

+                # This is the koji_tag for platform:f30

+                'koji_tag': 'module-mariadb-10.4-3020190304180835-a5b0195c',

+                'modulemd': make_mmd(name='mariadb', stream='10.4',

+                                     version='3020190304180835', context='a5b0195c',

+                                     requires={'platform': 'f30'},

+                                     buildrequires={'platform': 'f30'}).dumps(),

+                }],

+              ['module-mariadb-10.4-3020190304180835-a5b0195c']),

+ 

+             # No module builds match the platform:f30

+             (ModuleConfig({'name': 'mariadb', 'stream': '10.4',

+                            'version': '3120190304180825', 'context': 'f636be4b',

+                            'requires': {'platform': 'f30'},

+                            'buildrequires': {'platform': 'f30'}}),

+              [{'id': 1,

+                'name': 'mariadb',

+                'koji_tag': 'module-mariadb-10.4-3020190313091759-a5b0195c',

+                'modulemd': make_mmd(name='mariadb', stream='10.4',

+                                     version='3020190313091759', context='a5b0195c',

+                                     requires={'platform': 'f29'},

+                                     buildrequires={'platform': 'f29'}).dumps(),

+                },

+               # Although this koji_tag was added to tag inheritance, but no

+               # koji_tag is found that matches platform:f30.

+               {'id': 2,

+                'name': 'mariadb',

+                'koji_tag': 'module-mariadb-10.4-3020190304180835-a5b0195c',

+                'modulemd': make_mmd(name='mariadb', stream='10.4',

+                                     version='3020190304180835', context='a5b0195c',

+                                     requires={'platform': 'f28'},

+                                     buildrequires={'platform': 'f28'}).dumps(),

+                }],

+              []),

+         ])

+     @patch('ursa_major.koji_service.koji')

+     @patch('requests.get')

+     def test_find_module_tags_from_koji_inheritance(

+             self, get, koji, module_config, module_builds, expected_koji_tags):

+         koji_session = koji.ClientSession.return_value

+         koji_session.getInheritanceData.return_value = [

+             {'parent_id': 100,

+              'name': 'module-ant-1.10-20181122140939-819b5873'},

+             {'parent_id': 101,

+              'name': '"module-hyperfine-latest-3120190318171218-f636be4b'},

+             {'parent_id': 102,

+              'name': 'module-exa-latest-2820190316115637-8de94f32'},

+             {'parent_id': 103,

+              'name': 'module-mariadb-10.4-3020190304180835-a5b0195c'},

+         ]

+ 

+         get.return_value = Mock(status_code=200)

+         get.return_value.json.side_effect = make_mbs_response(module_builds)

+ 

+         handler = AddModuleHandler(config=URSA_MAJOR_TEST_CONFIG)

+         handler.connect_koji()

+         handler.connect_mbs()

+         found_koji_tags = handler.find_module_tags_from_koji_inheritance(

+             'f30-build', module_config)

+         assert expected_koji_tags == found_koji_tags

@@ -128,7 +128,7 @@ 

      def test_update_module_koji_tag(self, update_tag_inheritance, koji, mail_api):

          self.handler.connect_mbs = mock.MagicMock()

          self.handler._mbs = mock.MagicMock()

-         self.handler.mbs.get_modules_with_requires.return_value = [

+         self.handler.mbs.get_modules.return_value = [

              {'koji_tag': 'module-123456'},

              {'koji_tag': 'module-908765'},

          ]

@@ -1,160 +0,0 @@ 

- # -*- coding: utf-8 -*-

- # Copyright (c) 2019  Red Hat, Inc.

- #

- # Permission is hereby granted, free of charge, to any person obtaining a copy

- # of this software and associated documentation files (the "Software"), to deal

- # in the Software without restriction, including without limitation the rights

- # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell

- # copies of the Software, and to permit persons to whom the Software is

- # furnished to do so, subject to the following conditions:

- #

- # The above copyright notice and this permission notice shall be included in all

- # copies or substantial portions of the Software.

- #

- # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR

- # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,

- # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE

- # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER

- # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,

- # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE

- # SOFTWARE.

- #

- # Written by Chenxiong Qi <cqi@redhat.com>

- 

- import pytest

- 

- from mock import Mock, patch

- from tests import make_mmd, make_mbs_response, URSA_MAJOR_TEST_CONFIG

- from ursa_major.handlers.base import BaseHandler

- from ursa_major import ModuleConfig

- 

- 

- class TestBaseHanlder:

- 

-     # NOTE: once MBS supports filtering modules builds by requires or

-     # buildrequires, the test data should be updated because it is unnecessary

-     # to filter module builds locally by Ursa-Major itself.

- 

-     @pytest.mark.parametrize(

-         'module_config,module_builds,expected_koji_tags', [

-             # None of these koji_tags exist in tag inheritance.

-             (ModuleConfig({'name': 'mariadb', 'stream': '10.3',

-                            'version': '3120190304180825', 'context': 'f636be4b'}),

-              [{'id': 1,

-                'name': 'mariadb',

-                'koji_tag': 'module-mariadb-10.3-3020190301191548-a5b0195c'},

-               {'id': 2,

-                'name': 'mariadb',

-                'koji_tag': 'module-mariadb-10.3-3120190304180825-f636be4b'}],

-              []),

- 

-             # koji_tag exists in tag inheritance

-             (ModuleConfig({'name': 'mariadb', 'stream': '10.4',

-                            'version': '3120190304180825', 'context': 'f636be4b'}),

-              [{'id': 1,

-                'name': 'mariadb',

-                'koji_tag': 'module-mariadb-10.4-3020190313091759-a5b0195c'},

-               {'id': 2,

-                'name': 'mariadb',

-                # This older koji_tag was added previously

-                'koji_tag': 'module-mariadb-10.4-3020190304180835-a5b0195c'}],

-              ['module-mariadb-10.4-3020190304180835-a5b0195c']),

- 

-             # Modules are filtered by requires.

-             # None of these koji_tags exist in tag inheritance.

-             (ModuleConfig({'name': 'mariadb', 'stream': '10.3',

-                            'version': '3120190304180825', 'context': 'f636be4b',

-                            'requires': {'platform': 'f30'}}),

-              [{'id': 1,

-                'name': 'mariadb',

-                'koji_tag': 'module-mariadb-10.3-3020190301191548-a5b0195c',

-                'modulemd': make_mmd(name='mariadb', stream='10.3',

-                                     version='3020190301191548', context='a5b0195c',

-                                     requires={'platform': 'f30'},

-                                     buildrequires={'platform': 'f30'}).dumps(),

-                },

-               {'id': 2,

-                'name': 'mariadb',

-                'koji_tag': 'module-mariadb-10.3-3120190304180825-f636be4b',

-                'modulemd': make_mmd(name='mariadb', stream='10.3',

-                                     version='3120190304180825', context='f636be4b',

-                                     requires={'platform': 'f29'},

-                                     buildrequires={'platform': 'f29'}).dumps(),

-                }],

-              []),

- 

-             # koji_tag exists in tag inheritance and module builds are

-             # filtered by both requires and buildrequires.

-             (ModuleConfig({'name': 'mariadb', 'stream': '10.4',

-                            'version': '3120190304180825', 'context': 'f636be4b',

-                            'requires': {'platform': 'f30'},

-                            'buildrequires': {'platform': 'f30'}}),

-              [{'id': 1,

-                'name': 'mariadb',

-                'koji_tag': 'module-mariadb-10.4-3020190313091759-a5b0195c',

-                'modulemd': make_mmd(name='mariadb', stream='10.4',

-                                     version='3020190313091759', context='a5b0195c',

-                                     requires={'platform': 'f29'},

-                                     buildrequires={'platform': 'f29'}).dumps(),

-                },

-               {'id': 2,

-                'name': 'mariadb',

-                # This is the koji_tag for platform:f30

-                'koji_tag': 'module-mariadb-10.4-3020190304180835-a5b0195c',

-                'modulemd': make_mmd(name='mariadb', stream='10.4',

-                                     version='3020190304180835', context='a5b0195c',

-                                     requires={'platform': 'f30'},

-                                     buildrequires={'platform': 'f30'}).dumps(),

-                }],

-              ['module-mariadb-10.4-3020190304180835-a5b0195c']),

- 

-             # No module builds match the platform:f30

-             (ModuleConfig({'name': 'mariadb', 'stream': '10.4',

-                            'version': '3120190304180825', 'context': 'f636be4b',

-                            'requires': {'platform': 'f30'},

-                            'buildrequires': {'platform': 'f30'}}),

-              [{'id': 1,

-                'name': 'mariadb',

-                'koji_tag': 'module-mariadb-10.4-3020190313091759-a5b0195c',

-                'modulemd': make_mmd(name='mariadb', stream='10.4',

-                                     version='3020190313091759', context='a5b0195c',

-                                     requires={'platform': 'f29'},

-                                     buildrequires={'platform': 'f29'}).dumps(),

-                },

-               # Although this koji_tag was added to tag inheritance, but no

-               # koji_tag is found that matches platform:f30.

-               {'id': 2,

-                'name': 'mariadb',

-                'koji_tag': 'module-mariadb-10.4-3020190304180835-a5b0195c',

-                'modulemd': make_mmd(name='mariadb', stream='10.4',

-                                     version='3020190304180835', context='a5b0195c',

-                                     requires={'platform': 'f28'},

-                                     buildrequires={'platform': 'f28'}).dumps(),

-                }],

-              []),

-         ])

-     @patch('ursa_major.koji_service.koji')

-     @patch('requests.get')

-     def test_find_module_tags_from_koji_inheritance(

-             self, get, koji, module_config, module_builds, expected_koji_tags):

-         koji_session = koji.ClientSession.return_value

-         koji_session.getInheritanceData.return_value = [

-             {'parent_id': 100,

-              'name': 'module-ant-1.10-20181122140939-819b5873'},

-             {'parent_id': 101,

-              'name': '"module-hyperfine-latest-3120190318171218-f636be4b'},

-             {'parent_id': 102,

-              'name': 'module-exa-latest-2820190316115637-8de94f32'},

-             {'parent_id': 103,

-              'name': 'module-mariadb-10.4-3020190304180835-a5b0195c'},

-         ]

- 

-         get.return_value = Mock(status_code=200)

-         get.return_value.json.return_value = make_mbs_response(module_builds)

- 

-         handler = BaseHandler(config=URSA_MAJOR_TEST_CONFIG)

-         handler.connect_koji()

-         handler.connect_mbs()

-         found_koji_tags = handler.find_module_tags_from_koji_inheritance(

-             'f30-build', module_config)

-         assert expected_koji_tags == found_koji_tags

file modified
+1 -1
@@ -162,7 +162,7 @@ 

                                       requires={'platform': 'f30'},

                                       buildrequires={'platform': 'f30'}).dumps(),

              }

-         ])

+         ])[0]

  

          self.koji_session.getInheritanceData.return_value = [

              {

file modified
+23 -10
@@ -24,7 +24,7 @@ 

  

  import mock

  

- from tests import UrsaMajorTestCase, MockResponse

+ from tests import UrsaMajorTestCase, MockResponse, make_mbs_response

  from ursa_major.mbs import MBS

  

  
@@ -48,13 +48,25 @@ 

  

      @mock.patch('ursa_major.mbs.requests.get')

      def test_get_modules_without_auto_next_page(self, get):

-         get.side_effect = self._mock_requests_get_for_modules_list

+         get.return_value = mock.Mock(status_code=200)

+         get.return_value.json.side_effect = make_mbs_response([

+             # First page

+             {'id': 1, 'name': 'mariadb', 'stream': '10.4'},

+             {'id': 2, 'name': 'mariadb', 'stream': '10.3'},

+             # Second page

+             {'id': 3, 'name': 'ant', 'stream': '1.10'},

+             {'id': 4, 'name': 'fish', 'stream': '3'},

+             # Third page

+             {'id': 5, 'name': 'ruby', 'stream': 'master'},

+         ], per_page=2)

  

          mbs = MBS('http://mbs.example.com')

-         modules = mbs.get_modules(auto_next_page=False, name='testmodule')

+         modules = mbs.get_modules(auto_next_page=False, name='mariadb')

  

-         data = self.load_json_from_file('mbs_modules_testmodule_page1.json')

-         expected = data['items']

+         expected = [

+             {'id': 1, 'name': 'mariadb', 'stream': '10.4'},

+             {'id': 2, 'name': 'mariadb', 'stream': '10.3'},

+         ]

  

          self.assertEqual(modules, expected)

  
@@ -109,7 +121,7 @@ 

          mbs = MBS('http://mbs.example.com')

          self.assertFalse(mbs.module_has_requires(604, {'platform': 'f28'}))

  

-     def test_get_modules_with_requires(self):

+     def test_get_modules(self):

          mbs_resp_data = self.load_json_from_file('test_get_modules_with_requires.json')

          mbs = MBS('http://mbs.example.com')

  
@@ -128,15 +140,16 @@ 

              with mock.patch('ursa_major.mbs.requests.get') as get:

                  get.return_value = MockResponse(mbs_resp_data, 200)

  

-                 module_builds = mbs.get_modules_with_requires(

+                 module_builds = mbs.get_modules(

                      name=name,

                      stream=stream,

                      requires=requires,

                      buildrequires=buildrequires)

  

-                 get.assert_called_once_with(

-                     mbs.module_builds_api_url,

-                     params={'name': name, 'stream': stream, 'verbose': 'true'})

+                 params = {'name': name, 'stream': stream}

+                 if requires or buildrequires:

+                     params['verbose'] = 'true'

+                 get.assert_called_once_with(mbs.module_builds_api_url, params=params)

  

                  build_ids = sorted(build_info['id'] for build_info in module_builds)

                  assert expected_build_ids == build_ids

file modified
+1 -1
@@ -129,7 +129,7 @@ 

                                       requires={'platform': 'f30'},

                                       buildrequires={'platform': 'f30'}).dumps(),

              }

-         ])

+         ])[0]

  

          cmd = [

              '--name', 'mariadb', '--stream', '10.4',

@@ -284,18 +284,12 @@ 

          :param module_config: an object of `ModuleConfig`

          :return: tag name or None

          """

-         if module_config.requires or module_config.buildrequires:

-             modules = self.mbs.get_modules_with_requires(

-                 buildrequires=module_config.buildrequires,

-                 requires=module_config.requires,

-                 name=module_config.name,

-                 stream=module_config.stream,

-                 state=5)  # ready modules

-         else:

-             modules = self.mbs.get_modules(

-                 name=module_config.name,

-                 stream=module_config.stream,

-                 state=5)  # ready modules

+         modules = self.mbs.get_modules(

+             buildrequires=module_config.buildrequires,

+             requires=module_config.requires,

+             name=module_config.name,

+             stream=module_config.stream,

+             state=5)  # ready modules

          if modules:

              return modules[0]['koji_tag']

          else:

file modified
+14 -16
@@ -104,24 +104,22 @@ 

          Find koji tags in specified tag's inheritance data that belongs to

          the module of module_config.

  

-         :param str tag: tag name, from this tag to find out module koji_tag added before.

-         :param module_config: instance of ModuleConfig

+         :param str tag: tag name. This tag's parent tags will be retrieved from

+             Koji and from which to find out module(s)' tag(s).

+         :param module_config: instance of ModuleConfig, whose module build(s)

+             will be queried from MBS and find out which module(s)' koji_tag are

+             the parent tag of ``tag``.

          :type module_config: :class:`ModuleConfig`

-         :return: a list of module koji_tag inheriting from ``tag`` that was added before.

+         :return: list of tag name(s) which are parent tag of ``tag``.

          :rtype: list[str]

          """

-         if module_config.requires or module_config.buildrequires:

-             modules = self.mbs.get_modules_with_requires(

-                 requires=module_config.requires,

-                 buildrequires=module_config.buildrequires,

-                 name=module_config.name,

-                 stream=module_config.stream,

-                 state=5)  # only ready modules

-         else:

-             modules = self.mbs.get_modules(

-                 name=module_config.name,

-                 stream=module_config.stream,

-                 state=5)  # only ready modules

-         tags_from_mbs = set(module['koji_tag'] for module in modules)

          tags_from_koji = set(t['name'] for t in self.koji.get_inheritance_data(tag))

+         modules = self.mbs.get_modules(

+             requires=module_config.requires,

+             buildrequires=module_config.buildrequires,

+             name=module_config.name,

+             stream=module_config.stream,

+             state=5,

+             auto_next_page=True)  # only ready modules

+         tags_from_mbs = set(module['koji_tag'] for module in modules)

          return list(set(tags_from_koji) & set(tags_from_mbs))

@@ -110,7 +110,7 @@ 

                      log.error("More than one module found with same priority '%s' in "

                                "config:\n%s", module_priority, modules_with_cur_priority)

  

-                 modules_of_config = self.mbs.get_modules_with_requires(

+                 modules_of_config = self.mbs.get_modules(

                      requires=module_config.get('requires', {}),

                      buildrequires=module_config.get('buildrequires'),

                      name=module_config['name'],

@@ -64,7 +64,7 @@ 

                   self.module_name, self.module_stream, self.module_requires,

                   self.module_buildrequires)

  

-         modules = self.mbs.get_modules_with_requires(

+         modules = self.mbs.get_modules(

              name=self.module_name,

              stream=self.module_stream,

              requires=self.module_requires,

file modified
+30 -18
@@ -39,7 +39,7 @@ 

              self.server_url.rstrip('/') +

              '/module-build-service/{!s}/module-builds/'.format(api_version))

  

-     def get_modules(self, auto_next_page=True, **params):

+     def _get_modules(self, auto_next_page=True, **params):

          """

          Query MBS modules with specified query parameters.

  
@@ -51,11 +51,13 @@ 

          if resp.status_code != 200:

              resp.raise_for_status()

  

-         modules = resp.json()['items']

+         resp_data = resp.json()

+ 

+         modules = resp_data['items']

          if not auto_next_page:

              return modules

  

-         next_page = resp.json()['meta'].get('next', None)

+         next_page = resp_data['meta'].get('next', None)

          if next_page is not None:

              parsed_url = urlparse.urlparse(next_page)

              params = dict(urlparse.parse_qsl(parsed_url.query))
@@ -93,7 +95,7 @@ 

          mmd = self.get_module_mmd(build_id)

          return mmd_has_requires(mmd, requires)

  

-     def get_modules_with_requires(self, **kwargs):

+     def get_modules(self, **kwargs):

          """

          Query MBS modules with specified query parameters.

  
@@ -111,19 +113,29 @@ 

          # ourselves.

          requires = kwargs.pop('requires', None)

          buildrequires = kwargs.pop('buildrequires', None)

+         auto_next_page = kwargs.pop('auto_next_page', False)

+ 

+         inspect_modulemd = False

+         if requires or buildrequires:

+             inspect_modulemd = True

  

          # we need modulemd in result, so set verbose to true

-         kwargs.update({'verbose': 'true'})

-         modules = self.get_modules(auto_next_page=True, **kwargs)

- 

-         matched = []

-         for module in modules:

-             mmd = load_mmd(module['modulemd'])

-             b = True

-             if requires:

-                 b = b and mmd_has_requires(mmd, requires)

-             if buildrequires:

-                 b = b and mmd_has_buildrequires(mmd, buildrequires)

-             if b:

-                 matched.append(module)

-         return matched

+         if inspect_modulemd:

+             kwargs.update({'verbose': 'true'})

+ 

+         modules = self._get_modules(auto_next_page=auto_next_page, **kwargs)

+ 

+         if inspect_modulemd:

+             matched = []

+             for module in modules:

+                 mmd = load_mmd(module['modulemd'])

+                 b = True

+                 if requires:

+                     b = b and mmd_has_requires(mmd, requires)

+                 if buildrequires:

+                     b = b and mmd_has_buildrequires(mmd, buildrequires)

+                 if b:

+                     matched.append(module)

+             return matched

+ 

+         return modules

Originally, there are two API to query module builds from MBS, get_modules and
get_modules_with_requires. This patch refactors and unify the API to a single
method get_modules with two addiitonal optional arguments requires and
buildrequires. As a result, to get module builds with requires or buildrequires
or both, no need to write if-else in the old way.

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

Pull-Request has been merged by cqi

5 years ago