From 9127687fee924316039eb134dafcdcd5c0555d8e Mon Sep 17 00:00:00 2001 From: Stanislav Ochotnicky Date: Thu, 4 May 2017 10:31:26 +0200 Subject: [PATCH] Implement import content generator output into koji This commit prepares a directory with outputs that will be uploaded to koji (just the mmd file for now). It also adjusts koji_import to call CGImport with the metadata and the directory to import the results. Last step will be to call this method at the end of successful module build --- .../builder/KojiContentGenerator.py | 35 +++++++++++++++++-- tests/test_content_generator.py | 8 +++++ 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/module_build_service/builder/KojiContentGenerator.py b/module_build_service/builder/KojiContentGenerator.py index 0e8928c6..582f7f2c 100644 --- a/module_build_service/builder/KojiContentGenerator.py +++ b/module_build_service/builder/KojiContentGenerator.py @@ -23,9 +23,13 @@ import calendar -import logging -import platform import hashlib +import logging +import json +import os +import platform +import shutil +import tempfile import koji @@ -164,7 +168,34 @@ class KojiContentGenerator(object): return ret + def _prepare_file_directory(self): + """ Creates a temporary directory that will contain all the files + mentioned in the outputs section + + Returns path to the temporary directory + """ + prepdir = tempfile.mkdtemp(prefix="koji-cg-import") + mmd_path = os.path.join(prepdir, "modulemd.yaml") + with open(mmd_path, "w") as mmd_f: + mmd_f.write(self.mmd) + return prepdir + + def koji_import(self): + """This method imports given module into the configured koji instance as + a content generator based build + + Raises an exception when error is encountered during import""" session = KojiModuleBuilder.get_session(self.config, self.owner) metadata = self._get_content_generator_metadata() + file_dir = self._prepare_file_directory() + try: + build_info = session.CGImport(metadata, file_dir) + log.debug("Content generator import done: %s", + json.dumps(build_info, sort_keys=True, indent=4)) + except Exception, e: + log.error("Content generator import failed: %s", e) + raise e + finally: + shutil.rmtree(file_dir) diff --git a/tests/test_content_generator.py b/tests/test_content_generator.py index e2206240..23e372f9 100644 --- a/tests/test_content_generator.py +++ b/tests/test_content_generator.py @@ -74,6 +74,7 @@ class TestBuild(unittest.TestCase): @patch("platform.machine") @patch("module_build_service.builder.KojiContentGenerator.KojiContentGenerator._koji_rpms_in_tag") def test_get_generator_json(self, rpms_in_tag, machine, distro, pkg_res): + """ Test generation of content generator json """ self.maxDiff = None distro.return_value = ("Fedora", "25", "Twenty Five") machine.return_value = "i686" @@ -93,3 +94,10 @@ class TestBuild(unittest.TestCase): ret = self.cg._get_content_generator_metadata() rpms_in_tag.assert_called_once() self.assertEqual(expected_output, ret) + + + def test_prepare_file_directory(self): + """ Test preparation of directory with output files """ + dir_path = self.cg._prepare_file_directory() + with open(path.join(dir_path, "modulemd.yaml")) as mmd: + self.assertEqual(len(mmd.read()), 1134)