From 1f218202bb5afdada05dec4f8fff52952131531a Mon Sep 17 00:00:00 2001 From: Tomas Kopecek Date: Oct 27 2017 12:36:30 +0000 Subject: [PATCH 1/2] delete build directory if cg_import fails Related: https://pagure.io/koji/issue/584 --- diff --git a/hub/kojihub.py b/hub/kojihub.py index 2579b32..cb0b59a 100644 --- a/hub/kojihub.py +++ b/hub/kojihub.py @@ -5233,8 +5233,12 @@ class CG_Importer(object): # finalize import self.get_build() self.import_brs() - self.import_outputs() - self.import_metadata() + try: + self.import_outputs() + self.import_metadata() + except Exception: + self.check_build_dir(delete=True) + raise koji.plugin.run_callbacks('postImport', type='cg', metadata=metadata, directory=directory, build=self.buildinfo) @@ -5313,11 +5317,15 @@ class CG_Importer(object): self.buildinfo['volume_name'] = vol['name'] - def check_build_dir(self): + def check_build_dir(self, delete=False): """Check that the import directory does not already exist""" path = koji.pathinfo.build(self.buildinfo) if os.path.lexists(path): - raise koji.GenericError("Destination directory already exists: %s" % path) + if delete: + logger.warning("Deleting build directory: %s", path) + koji.util.rmtree(olddir) + else: + raise koji.GenericError("Destination directory already exists: %s" % path) def prep_build(self): From a1a422bc91c1d95aa6875dd904a17320fd0cd03c Mon Sep 17 00:00:00 2001 From: Tomas Kopecek Date: Oct 27 2017 12:53:57 +0000 Subject: [PATCH 2/2] typo fix + tests --- diff --git a/hub/kojihub.py b/hub/kojihub.py index cb0b59a..69927c7 100644 --- a/hub/kojihub.py +++ b/hub/kojihub.py @@ -5323,7 +5323,7 @@ class CG_Importer(object): if os.path.lexists(path): if delete: logger.warning("Deleting build directory: %s", path) - koji.util.rmtree(olddir) + koji.util.rmtree(path) else: raise koji.GenericError("Destination directory already exists: %s" % path) diff --git a/tests/test_hub/test_cg_importer.py b/tests/test_hub/test_cg_importer.py index 5207f6f..ac0e528 100644 --- a/tests/test_hub/test_cg_importer.py +++ b/tests/test_hub/test_cg_importer.py @@ -82,6 +82,32 @@ class TestCGImporter(unittest.TestCase): assert x.buildinfo assert isinstance(x.buildinfo, dict) + @mock.patch('koji.pathinfo.build') + @mock.patch('os.path.lexists') + @mock.patch('koji.util.rmtree') + def test_check_build_dir(self, rmtree, lexists, build): + path = '/random_path/random_dir' + build.return_value = path + + x = kojihub.CG_Importer() + + # directory exists + lexists.return_value = True + with self.assertRaises(koji.GenericError): + x.check_build_dir(delete=False) + rmtree.assert_not_called() + + # directory exists + delete + lexists.return_value = True + x.check_build_dir(delete=True) + rmtree.assert_called_once_with(path) + + # directory doesn't exist + rmtree.reset_mock() + lexists.return_value = False + x.check_build_dir() + rmtree.assert_not_called() + @mock.patch('kojihub.get_build') @mock.patch("koji.pathinfo.work") def test_prep_build_exists(self, work, get_build):