From d22b948f693c35074af23094ac8d1808e2f48f0d Mon Sep 17 00:00:00 2001 From: Lubomír Sedlář Date: Mar 07 2017 14:18:41 +0000 Subject: partial-copy: Create destination directory first Otherwise rsync will fail to copy the contents in there and crash. Signed-off-by: Lubomír Sedlář --- diff --git a/compose_utils/copy_compose.py b/compose_utils/copy_compose.py index 189b5d9..29693c2 100644 --- a/compose_utils/copy_compose.py +++ b/compose_utils/copy_compose.py @@ -2,6 +2,7 @@ from __future__ import print_function +import errno import os from kobo import shortcuts @@ -49,6 +50,11 @@ def _run_rsync(sources, destination, dry_run=False, opts=[]): cmd = ['rsync', '-avHh'] + opts + sources + [destination] print(' '.join(cmd)) if not dry_run: + try: + os.makedirs(destination) + except OSError as exc: + if exc.errno == errno.EEXIST: + pass shortcuts.run(cmd, stdout=True) diff --git a/tests/test_copy_compose.py b/tests/test_copy_compose.py index 5cba156..d08bb23 100644 --- a/tests/test_copy_compose.py +++ b/tests/test_copy_compose.py @@ -16,7 +16,7 @@ class ChangelogTest(unittest.TestCase): def setUp(self): self.compose = get_compose('DP-1.0-20160315.t.0') - self.dest = tempfile.mkdtemp() + '/' + self.dest = tempfile.mkdtemp(prefix='compose-copy-dest') + '/' def tearDown(self): shutil.rmtree(self.dest) @@ -41,6 +41,19 @@ class ChangelogTest(unittest.TestCase): self.assertComposeId() @mock.patch('kobo.shortcuts.run') + def test_copy_to_missing_dir(self, mock_run): + self.dest = os.path.join(self.dest, 'missing', 'directory') + '/' + copy_compose(self.compose, self.dest, variants=[], arches=[]) + self.assertItemsEqual( + mock_run.mock_calls, + [mock.call(['rsync', '-avHh', + self.compose.compose_path + '/Client', + self.compose.compose_path + '/Server', + self.dest], + stdout=True)]) + self.assertComposeId() + + @mock.patch('kobo.shortcuts.run') def test_copy_full_with_extra_opts(self, mock_run): copy_compose(self.compose, self.dest, variants=[], arches=[], rsync_opts=['--exclude=repodata']) self.assertItemsEqual(