diff --git a/pungi/__init__.py b/pungi/__init__.py index 24b84d6d..9f20a3fd 100644 --- a/pungi/__init__.py +++ b/pungi/__init__.py @@ -4,6 +4,15 @@ import os import re +try: + from pdc_client import PDCClient + import gi + gi.require_version('Modulemd', '1.0') # noqa + from gi.repository import Modulemd +except: + Modulemd = None + + def get_full_version(): """ Find full version of Pungi: if running from git, this will return cleaned diff --git a/pungi/phases/createrepo.py b/pungi/phases/createrepo.py index 22ee0df2..662fab58 100644 --- a/pungi/phases/createrepo.py +++ b/pungi/phases/createrepo.py @@ -33,20 +33,11 @@ from ..wrappers.scm import get_dir_from_scm from ..wrappers.createrepo import CreaterepoWrapper from .base import PhaseBase from ..util import find_old_compose, temp_dir, get_arch_variant_data +from pungi import Modulemd import productmd.rpms -try: - from pdc_client import PDCClient - import gi - gi.require_version('Modulemd', '1.0') # noqa - from gi.repository import Modulemd - WITH_MODULES = True -except: - WITH_MODULES = False - - createrepo_lock = threading.Lock() createrepo_dirs = set() @@ -193,7 +184,7 @@ def create_variant_repo(compose, arch, variant, pkg_type): shutil.copy2(product_id_path, os.path.join(repo_dir, "repodata", "productid")) # call modifyrepo to inject modulemd if needed - if arch in variant.arch_mmds and WITH_MODULES: + if arch in variant.arch_mmds and Modulemd is not None: modules = [] for mmd in variant.arch_mmds[arch].values(): # Create copy of architecture specific mmd to filter out packages diff --git a/pungi/phases/gather/__init__.py b/pungi/phases/gather/__init__.py index d7082b97..58e69c2b 100644 --- a/pungi/phases/gather/__init__.py +++ b/pungi/phases/gather/__init__.py @@ -27,6 +27,7 @@ from .link import link_files from pungi.util import get_arch_variant_data, get_arch_data, get_variant_data from pungi.phases.base import PhaseBase from pungi.arch import split_name_arch, get_compatible_arches +from pungi import Modulemd def get_gather_source(name): @@ -68,9 +69,7 @@ class GatherPhase(PhaseBase): except ValueError as exc: errors = exc.message.split('\n') - # This must be imported here to avoid circular deps problems. - from pungi.phases.pkgset.sources import source_koji - if not source_koji.WITH_MODULES: + if not Modulemd: # Modules are not supported, check if we need them for variant in self.compose.variants.values(): if variant.modules: diff --git a/pungi/phases/gather/sources/source_module.py b/pungi/phases/gather/sources/source_module.py index 4f663088..969a1fb3 100644 --- a/pungi/phases/gather/sources/source_module.py +++ b/pungi/phases/gather/sources/source_module.py @@ -22,16 +22,7 @@ Get a package list based on modulemd metadata loaded in pkgset phase. import pungi.arch import pungi.phases.gather.source import kobo.rpmlib - - -try: - from pdc_client import PDCClient - import gi - gi.require_version('Modulemd', '1.0') # noqa - from gi.repository import Modulemd - WITH_MODULES = True -except: - WITH_MODULES = False +from pungi import Modulemd class GatherSourceModule(pungi.phases.gather.source.GatherSourceBase): @@ -52,11 +43,10 @@ class GatherSourceModule(pungi.phases.gather.source.GatherSourceBase): return packages, groups # Check if we even support modules in Pungi. - if not WITH_MODULES: + if not Modulemd: log.write( - "pdc_client module, pygobject module or libmodulemd " - "library is not installed, support for modules is " - "disabled\n") + "pygobject module or libmodulemd library is not installed, " + "support for modules is disabled\n") return packages, groups # TODO: Enable multilib here and handle "multilib" field in the diff --git a/pungi/phases/pkgset/sources/source_koji.py b/pungi/phases/pkgset/sources/source_koji.py index 27fa893c..be912b77 100644 --- a/pungi/phases/pkgset/sources/source_koji.py +++ b/pungi/phases/pkgset/sources/source_koji.py @@ -25,27 +25,23 @@ from pungi.wrappers.comps import CompsWrapper import pungi.phases.pkgset.pkgsets from pungi.arch import get_valid_arches from pungi.util import is_arch_multilib, retry +from pungi import Modulemd from pungi.phases.pkgset.common import create_arch_repos, create_global_repo, populate_arch_pkgsets from pungi.phases.gather import get_packages_to_gather - import pungi.phases.pkgset.source try: from pdc_client import PDCClient - import gi - gi.require_version('Modulemd', '1.0') # noqa - from gi.repository import Modulemd - WITH_MODULES = True + WITH_PDC = True except: - WITH_MODULES = False + WITH_PDC = False def get_pdc_client_session(compose): - if not WITH_MODULES: - compose.log_warning("pdc_client module, pygobject module or " - "libmodulemd library is not installed, " + if not WITH_PDC: + compose.log_warning("pdc_client module is not installed, " "support for modules is disabled") return None try: @@ -241,6 +237,12 @@ def populate_global_pkgset(compose, koji_wrapper, path_prefix, event_id): # to compose_tags list. if session: for module in variant.get_modules(): + if not Modulemd: + raise ValueError( + "pygobject module or libmodulemd library is not installed, " + "support for modules is disabled, but compose contains " + "modules.") + pdc_module = get_module(compose, session, module["name"]) pdc_modules.append(pdc_module) mmd = Modulemd.Module.new_from_string(pdc_module["modulemd"]) diff --git a/tests/helpers.py b/tests/helpers.py index c5bd9f2b..4eeabdd2 100644 --- a/tests/helpers.py +++ b/tests/helpers.py @@ -14,16 +14,8 @@ import six from kobo.rpmlib import parse_nvr from pungi.util import get_arch_variant_data -from pungi import paths, checks +from pungi import paths, checks, Modulemd -try: - import gi # noqa - gi.require_version('Modulemd', '1.0') # noqa - from gi.repository import Modulemd # noqa - import pdc_client # noqa - HAS_MODULE_SUPPORT = True -except ImportError: - HAS_MODULE_SUPPORT = False class PungiTestCase(unittest.TestCase): def setUp(self): @@ -61,7 +53,8 @@ class MockVariant(mock.Mock): return [] def add_fake_module(self, nsvc, rpm_nvrs=None): - if not HAS_MODULE_SUPPORT: + if not Modulemd: + # No support for modules return name, stream, version, context = nsvc.split(":") mmd = Modulemd.Module() diff --git a/tests/test_createrepophase.py b/tests/test_createrepophase.py index 1bce0aa0..b9a96c31 100644 --- a/tests/test_createrepophase.py +++ b/tests/test_createrepophase.py @@ -18,15 +18,7 @@ from pungi.phases.createrepo import (CreaterepoPhase, create_variant_repo, get_productids_from_scm) from tests.helpers import DummyCompose, PungiTestCase, copy_fixture, touch - -try: - import gi # noqa - gi.require_version('Modulemd', '1.0') # noqa - from gi.repository import Modulemd # noqa - import pdc_client # noqa - HAS_MODULE_SUPPORT = True -except ImportError: - HAS_MODULE_SUPPORT = False +from pungi import Modulemd class TestCreaterepoPhase(PungiTestCase): @@ -716,13 +708,11 @@ class TestCreateVariantRepo(PungiTestCase): with open(list_file) as f: self.assertEqual(f.read(), 'Packages/b/bash-4.3.30-2.fc21.src.rpm\n') + @unittest.skipUnless(Modulemd is not None, 'Skipped test, no module support.') @mock.patch('pungi.phases.createrepo.run') @mock.patch('pungi.phases.createrepo.CreaterepoWrapper') def test_variant_repo_modules_artifacts_not_in_compose( self, CreaterepoWrapperCls, run): - if not HAS_MODULE_SUPPORT: - self.skipTest("Skipped test, no module support.") - compose = DummyCompose(self.topdir, { 'createrepo_checksum': 'sha256', }) @@ -759,13 +749,11 @@ class TestCreateVariantRepo(PungiTestCase): repo.get_modifyrepo_cmd.mock_calls, [mock.call(repodata_dir, ANY, compress_type='gz', mdtype='modules')]) + @unittest.skipUnless(Modulemd is not None, 'Skipped test, no module support.') @mock.patch('pungi.phases.createrepo.run') @mock.patch('pungi.phases.createrepo.CreaterepoWrapper') def test_variant_repo_modules_artifacts( self, CreaterepoWrapperCls, run): - if not HAS_MODULE_SUPPORT: - self.skipTest("Skipped test, no module support.") - compose = DummyCompose(self.topdir, { 'createrepo_checksum': 'sha256', }) diff --git a/tests/test_gather_source_module.py b/tests/test_gather_source_module.py index b1cb746f..5e7fa672 100644 --- a/tests/test_gather_source_module.py +++ b/tests/test_gather_source_module.py @@ -5,22 +5,14 @@ import os import sys import unittest -try: - import gi # noqa - gi.require_version('Modulemd', '1.0') # noqa - from gi.repository import Modulemd # noqa - import pdc_client # noqa - HAS_MODULE_SUPPORT = True -except ImportError: - HAS_MODULE_SUPPORT = False - sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..")) from pungi.phases.gather.sources.source_module import GatherSourceModule from tests import helpers +from pungi import Modulemd -@unittest.skipUnless(HAS_MODULE_SUPPORT, 'Skipped test, no module support.') +@unittest.skipUnless(Modulemd is not None, 'Skipped test, no module support.') class TestGatherSourceModule(helpers.PungiTestCase): def setUp(self): super(TestGatherSourceModule, self).setUp() diff --git a/tests/test_pkgset_source_koji.py b/tests/test_pkgset_source_koji.py index 7a9f4460..bb475a3b 100644 --- a/tests/test_pkgset_source_koji.py +++ b/tests/test_pkgset_source_koji.py @@ -10,17 +10,9 @@ import re sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..")) -try: - import gi # noqa - gi.require_version('Modulemd', '1.0') # noqa - from gi.repository import Modulemd # noqa - import pdc_client # noqa - HAS_MODULE_SUPPORT = True -except ImportError: - HAS_MODULE_SUPPORT = False - from pungi.phases.pkgset.sources import source_koji from tests import helpers +from pungi import Modulemd EVENT_INFO = {'id': 15681980, 'ts': 1460956382.81936} TAG_INFO = { @@ -127,7 +119,7 @@ class TestPopulateGlobalPkgset(helpers.PungiTestCase): with open(self.pkgset_path) as f: self.assertEqual(f.read(), 'DATA') - @unittest.skipUnless(HAS_MODULE_SUPPORT, 'Modulemd/pdc_client are not available') # noqa + @unittest.skipUnless(Modulemd is not None, 'Modulemd not available') # noqa @mock.patch('six.moves.cPickle.dumps') @mock.patch('pungi.phases.pkgset.pkgsets.KojiPackageSet') @mock.patch('pungi.phases.pkgset.sources.source_koji.get_module')