#1097 Clean up KojiModuleBuilder.get_session
Merged 4 months ago by mprahl. Opened 4 months ago by mprahl.

@@ -48,9 +48,9 @@ 

  logging.basicConfig(level=logging.DEBUG)

  

  

- def get_session(config, owner, login=True):

+ def get_session(config, login=True):

      from module_build_service.builder.KojiModuleBuilder import KojiModuleBuilder

-     return KojiModuleBuilder.get_session(config, owner, login=login)

+     return KojiModuleBuilder.get_session(config, login=login)

  

  

  def strip_suffixes(s, suffixes):

@@ -228,7 +228,7 @@ 

      def _koji_rpms_in_tag(self, tag):

          """ Return the list of koji rpms in a tag. """

          log.debug("Listing rpms in koji tag %s", tag)

-         session = get_session(self.config, self.owner, login=False)

+         session = get_session(self.config, login=False)

  

          try:

              rpms, builds = session.listTaggedRPMS(tag, latest=True)

@@ -307,7 +307,7 @@ 

                  }

              }

          }

-         session = get_session(self.config, None, login=False)

+         session = get_session(self.config, login=False)

          # Only add the CG build owner if the user exists in Koji

          if session.getUser(self.owner):

              ret[u'owner'] = self.owner

@@ -794,7 +794,7 @@ 

          """

          Tags the Content Generator build to module.cg_build_koji_tag.

          """

-         session = get_session(self.config, self.owner)

+         session = get_session(self.config)

  

          tag_name = self.module.cg_build_koji_tag

          if not tag_name:

@@ -849,7 +849,7 @@ 

              filters is created and imported.

          """

          self.devel = devel

-         session = get_session(self.config, self.owner)

+         session = get_session(self.config)

          self._load_koji_tag(session)

  

          file_dir = self._prepare_file_directory()

@@ -172,7 +172,7 @@ 

          log.debug("Using koji profile %r" % config.koji_profile)

          log.debug("Using koji_config: %s" % config.koji_config)

  

-         self.koji_session = self.get_session(config, owner)

+         self.koji_session = self.get_session(config)

          self.arches = get_build_arches(self.mmd, self.config)

  

          if not self.arches:

@@ -237,7 +237,7 @@ 

              reusable_module = get_reusable_module(db_session, module_build)

              if not reusable_module:

                  return filtered_rpms

-             koji_session = KojiModuleBuilder.get_session(conf, None, login=False)

+             koji_session = KojiModuleBuilder.get_session(conf, login=False)

              # Get all the RPMs and builds of the reusable module in Koji

              rpms, builds = koji_session.listTaggedRPMS(reusable_module.koji_tag, latest=True)

              # Convert the list to a dict where each key is the build_id

@@ -445,7 +445,7 @@ 

  

      @staticmethod

      @module_build_service.utils.retry(wait_on=(xmlrpclib.ProtocolError, koji.GenericError))

-     def get_session(config, owner, login=True):

+     def get_session(config, login=True):

          koji_config = munch.Munch(koji.read_config(

              profile_name=config.koji_profile,

              user_config=config.koji_config,

@@ -457,31 +457,32 @@ 

          log.info("Connecting to koji %r.", address)

          koji_session = koji.ClientSession(address, opts=koji_config)

  

-         if login:

-             authtype = koji_config.authtype

-             log.info("Authenticate session with %r.", authtype)

-             if authtype == "kerberos":

-                 ccache = getattr(config, "krb_ccache", None)

-                 keytab = getattr(config, "krb_keytab", None)

-                 principal = getattr(config, "krb_principal", None)

-                 log.debug("  ccache: %r, keytab: %r, principal: %r" % (

-                     ccache, keytab, principal))

-                 if keytab and principal:

-                     koji_session.krb_login(

-                         principal=principal,

-                         keytab=keytab,

-                         ccache=ccache

-                     )

-                 else:

-                     koji_session.krb_login(ccache=ccache)

-             elif authtype == "ssl":

-                 koji_session.ssl_login(

-                     os.path.expanduser(koji_config.cert),

-                     None,

-                     os.path.expanduser(koji_config.serverca)

+         if not login:

+             return koji_session

+ 

+         authtype = koji_config.authtype

+         log.info("Authenticate session with %r.", authtype)

+         if authtype == "kerberos":

+             ccache = getattr(config, "krb_ccache", None)

+             keytab = getattr(config, "krb_keytab", None)

+             principal = getattr(config, "krb_principal", None)

+             log.debug("  ccache: %r, keytab: %r, principal: %r" % (ccache, keytab, principal))

+             if keytab and principal:

+                 koji_session.krb_login(

+                     principal=principal,

+                     keytab=keytab,

+                     ccache=ccache

                  )

              else:

-                 raise ValueError("Unrecognized koji authtype %r" % authtype)

+                 koji_session.krb_login(ccache=ccache)

+         elif authtype == "ssl":

+             koji_session.ssl_login(

+                 os.path.expanduser(koji_config.cert),

+                 None,

+                 os.path.expanduser(koji_config.serverca)

+             )

+         else:

+             raise ValueError("Unrecognized koji authtype %r" % authtype)

  

          return koji_session

  

@@ -1087,7 +1088,7 @@ 

          """

          # If the component has not been built before, then None is returned. Instead, let's

          # return 0.0 so the type is consistent

-         koji_session = KojiModuleBuilder.get_session(conf, None, login=False)

+         koji_session = KojiModuleBuilder.get_session(conf, login=False)

          return koji_session.getAverageBuildDuration(component) or 0.0

  

      @classmethod

@@ -1103,7 +1104,7 @@ 

          :return: {component_name: weight_as_float, ...}

          """

  

-         koji_session = KojiModuleBuilder.get_session(conf, None)

+         koji_session = KojiModuleBuilder.get_session(conf)

  

          # Get our own userID, so we can limit the builds to only modular builds

          user_info = koji_session.getLoggedInUser()

@@ -1194,7 +1195,7 @@ 

              build = models.ModuleBuild.get_build_from_nsvc(

                  db_session, mmd.get_name(), mmd.get_stream(), mmd.get_version(),

                  mmd.get_context())

-             koji_session = KojiModuleBuilder.get_session(conf, None, login=False)

+             koji_session = KojiModuleBuilder.get_session(conf, login=False)

              rpms = koji_session.listTaggedRPMS(build.koji_tag, latest=True)[0]

              nvrs = set(kobo.rpmlib.make_nvr(rpm, force_epoch=True) for rpm in rpms)

              return list(nvrs)

@@ -1217,7 +1218,7 @@ 

          :return: koji tag

          """

  

-         session = KojiModuleBuilder.get_session(conf, None, login=False)

+         session = KojiModuleBuilder.get_session(conf, login=False)

          rpm_md = session.getRPM(rpm)

          if not rpm_md:

              return None

@@ -588,8 +588,7 @@ 

          if not self.koji_session:

              # If Koji is not configured on the system, then just return 0.0 for components

              try:

-                 self.koji_session = KojiModuleBuilder.get_session(

-                     self.config, self.owner, login=False)

+                 self.koji_session = KojiModuleBuilder.get_session(self.config, login=False)

                  # If the component has not been built before, then None is returned. Instead,

                  # let's return 0.0 so the type is consistent

                  return self.koji_session.getAverageBuildDuration(component.package) or 0.0

@@ -67,7 +67,7 @@ 

  

          if conf.system == 'koji':

              # We don't do this on behalf of users

-             koji_session = KojiModuleBuilder.get_session(conf, None, login=False)

+             koji_session = KojiModuleBuilder.get_session(conf, login=False)

              log.info('Querying tasks for statuses:')

              res = models.ComponentBuild.query.filter_by(

                  state=koji.BUILD_STATES['BUILDING']).options(

@@ -271,7 +271,7 @@ 

              return

  

          koji_session = module_build_service.builder.KojiModuleBuilder.KojiModuleBuilder\

-             .get_session(config, None)

+             .get_session(config)

  

          for module_build in session.query(models.ModuleBuild) \

                  .filter_by(state=models.BUILD_STATES['build']).all():

@@ -302,7 +302,7 @@ 

  

          now = datetime.utcnow()

  

-         koji_session = KojiModuleBuilder.get_session(config, None)

+         koji_session = KojiModuleBuilder.get_session(config)

          for target in koji_session.getBuildTargets():

              koji_tag = target["dest_tag_name"]

              module = session.query(models.ModuleBuild).filter_by(

@@ -126,7 +126,7 @@ 

      :rtype: list[Modulemd.Module]

      """

      from module_build_service.builder.KojiModuleBuilder import KojiModuleBuilder

-     koji_session = KojiModuleBuilder.get_session(conf, None, login=False)

+     koji_session = KojiModuleBuilder.get_session(conf, login=False)

      repos = koji_session.getExternalRepoList(tag)

      build_tags = find_build_tags_from_external_repos(koji_session, repos)

      if not build_tags:

@@ -265,7 +265,7 @@ 

      resolver = GenericResolver.create(conf)

  

      built_rpms = []

-     koji_session = KojiModuleBuilder.get_session(conf, None, login=False)

+     koji_session = KojiModuleBuilder.get_session(conf, login=False)

  

      for nsvc in modules_nsvc:

          name, stream, version, context = nsvc.split(':')

@@ -49,7 +49,7 @@ 

  class FakeKojiModuleBuilder(KojiModuleBuilder):

  

      @module_build_service.utils.retry(wait_on=(xmlrpclib.ProtocolError, koji.GenericError))

-     def get_session(self, config, owner):

+     def get_session(self, config, login=True):

          koji_session = MagicMock()

          koji_session.getRepo.return_value = {'create_event': 'fake event'}

  

@@ -682,7 +682,7 @@ 

      @patch('koji.ClientSession')

      def test_get_anonymous_session(self, ClientSession):

          mbs_config = mock.Mock(koji_profile='koji', koji_config='conf/koji.conf')

-         session = KojiModuleBuilder.get_session(mbs_config, 'someone', login=False)

+         session = KojiModuleBuilder.get_session(mbs_config, login=False)

          assert ClientSession.return_value == session

          assert ClientSession.return_value.krb_login.assert_not_called