From 145c3adbefb54206193ffaa766a9b09ab3eb84d6 Mon Sep 17 00:00:00 2001 From: Jan Kaluza Date: Tue, 3 Mar 2020 08:07:42 +0100 Subject: [PATCH] Move buildinstall results to final directories if using pungi-buildinstall Koji plugin. If Koji pungi-buildinstall is used, then the buildinstall results are stored in the `output_dir` dir, but in "results" and "logs" subdirectories. We need to move them to final_output_dir. Signed-off-by: Jan Kaluza --- pungi/phases/buildinstall.py | 16 +++++++++++++++- pungi/util.py | 28 ++++++++++++++++++++++++++++ tests/test_buildinstall.py | 14 +++++++++++++- tests/test_util.py | 24 ++++++++++++++++++++++++ 4 files changed, 80 insertions(+), 2 deletions(-) diff --git a/pungi/phases/buildinstall.py b/pungi/phases/buildinstall.py index 603f0588..308f998c 100644 --- a/pungi/phases/buildinstall.py +++ b/pungi/phases/buildinstall.py @@ -28,7 +28,7 @@ from six.moves import shlex_quote from pungi.arch import get_valid_arches from pungi.util import get_volid, get_arch_variant_data from pungi.util import get_file_size, get_mtime, failable, makedirs -from pungi.util import copy_all, translate_path +from pungi.util import copy_all, translate_path, move_all from pungi.wrappers.lorax import LoraxWrapper from pungi.wrappers import iso from pungi.wrappers.scm import get_file_from_scm @@ -577,6 +577,20 @@ class BuildinstallThread(WorkerThread): makedirs(final_log_dir) log_dir = os.path.join(output_dir, "logs") copy_all(log_dir, final_log_dir) + elif lorax_use_koji_plugin: + # If Koji pungi-buildinstall is used, then the buildinstall results are + # not stored directly in `output_dir` dir, but in "results" and "logs" + # subdirectories. We need to move them to final_output_dir. + results_dir = os.path.join(output_dir, "results") + move_all(results_dir, final_output_dir, rm_src_dir=True) + + # Get the log_dir into which we should copy the resulting log files. + log_fname = "buildinstall-%s-logs/dummy" % variant.uid + final_log_dir = os.path.dirname(compose.paths.log.log_file(arch, log_fname)) + if not os.path.exists(final_log_dir): + makedirs(final_log_dir) + log_dir = os.path.join(output_dir, "logs") + move_all(log_dir, final_log_dir, rm_src_dir=True) log_file = compose.paths.log.log_file(arch, log_filename + "-RPMs") rpms = runroot.get_buildroot_rpms() diff --git a/pungi/util.py b/pungi/util.py index 76073c3e..9f080c37 100644 --- a/pungi/util.py +++ b/pungi/util.py @@ -662,6 +662,34 @@ def copy_all(src, dest): return recursive_file_list(src) +def move_all(src, dest, rm_src_dir=False): + """ + Copy all files and directories within ``src`` to the ``dest`` directory. + + This is equivalent to running ``mv src/* dest``. + + :param src: + Source directory to move from. + + :param dest: + Destination directory to move to. + + :param rm_src_dir: + If True, the `src` directory is removed once its content is moved. + """ + contents = os.listdir(src) + if not contents: + raise RuntimeError("Source directory %s is empty." % src) + makedirs(dest) + for item in contents: + source = os.path.join(src, item) + destination = os.path.join(dest, item) + shutil.move(source, destination) + + if rm_src_dir: + os.rmdir(src) + + def recursive_file_list(directory): """Return a list of files contained in ``directory``. diff --git a/tests/test_buildinstall.py b/tests/test_buildinstall.py index f351fad8..29b4664a 100644 --- a/tests/test_buildinstall.py +++ b/tests/test_buildinstall.py @@ -1200,8 +1200,9 @@ class BuildinstallThreadTestCase(PungiTestCase): @mock.patch("pungi.wrappers.kojiwrapper.KojiWrapper") @mock.patch("pungi.wrappers.kojiwrapper.get_buildroot_rpms") @mock.patch("pungi.phases.buildinstall.run") + @mock.patch("pungi.phases.buildinstall.move_all") def test_buildinstall_thread_with_lorax_using_koji_plugin( - self, run, get_buildroot_rpms, KojiWrapperMock, mock_tweak, mock_link + self, move_all, run, get_buildroot_rpms, KojiWrapperMock, mock_tweak, mock_link ): compose = BuildInstallCompose( self.topdir, @@ -1284,6 +1285,17 @@ class BuildinstallThreadTestCase(PungiTestCase): mock_link.call_args_list, [mock.call(compose, "x86_64", compose.variants["Server"], False)], ) + self.assertEqual( + move_all.call_args_list, + [ + mock.call(os.path.join(destdir, "results"), destdir, rm_src_dir=True), + mock.call( + os.path.join(destdir, "logs"), + os.path.join(self.topdir, "logs/x86_64/buildinstall-Server-logs"), + rm_src_dir=True, + ), + ], + ) @mock.patch("pungi.phases.buildinstall.link_boot_iso") @mock.patch("pungi.phases.buildinstall.tweak_buildinstall") diff --git a/tests/test_util.py b/tests/test_util.py index c7399a05..055a36a1 100644 --- a/tests/test_util.py +++ b/tests/test_util.py @@ -1008,6 +1008,30 @@ class TestCopyAll(PungiTestCase): self.assertEqual(os.readlink(os.path.join(self.dst, "symlink")), "broken") +class TestMoveAll(PungiTestCase): + def setUp(self): + super(TestMoveAll, self).setUp() + self.src = os.path.join(self.topdir, "src") + self.dst = os.path.join(self.topdir, "dst") + util.makedirs(self.src) + + def test_move_all(self): + touch(os.path.join(self.src, "target")) + util.move_all(self.src, self.dst) + + self.assertTrue(os.path.isfile(os.path.join(self.dst, "target"))) + self.assertTrue(os.path.exists(os.path.join(self.src))) + self.assertFalse(os.path.isfile(os.path.join(self.src, "target"))) + + def test_move_all_rm_src_dir(self): + touch(os.path.join(self.src, "target")) + util.move_all(self.src, self.dst, rm_src_dir=True) + + self.assertTrue(os.path.isfile(os.path.join(self.dst, "target"))) + self.assertFalse(os.path.exists(os.path.join(self.src))) + self.assertFalse(os.path.isfile(os.path.join(self.src, "target"))) + + @mock.patch("six.moves.urllib.request.urlretrieve") class TestAsLocalFile(PungiTestCase): def test_local_file(self, urlretrieve):