From 77cf10cee79bcdd2a2f9f86bf6b0ba167266beed Mon Sep 17 00:00:00 2001 From: gryf Date: Fri, 6 Jan 2017 16:53:49 +0100 Subject: [PATCH] Fixed issue with compressing files with tar.* This changeset fixes tar behavior on creating archive, like: tar cf foo.tar . which include also archive file itself, so that tar reports an error `file changed as we read it` during appending archive file to itself. This changest is fixing that by replacing dot with list of items to be added to the archive (similar as in RarArchive.create). --- fs_uae_wrapper/file_archive.py | 9 +++++++ setup.py | 2 +- tests/test_file_archive.py | 46 +++++++++++++++++++++------------- 3 files changed, 39 insertions(+), 18 deletions(-) diff --git a/fs_uae_wrapper/file_archive.py b/fs_uae_wrapper/file_archive.py index 171c003..c165568 100644 --- a/fs_uae_wrapper/file_archive.py +++ b/fs_uae_wrapper/file_archive.py @@ -53,6 +53,15 @@ class TarArchive(Archive): EXTRACT = ['xf'] ARCH = 'tar' + def create(self, arch_name, files=None): + files = files if files else sorted(os.listdir('.')) + result = subprocess.call([self._compess] + self.ADD + [arch_name] + + files) + if result != 0: + sys.stderr.write("Unable to create archive `%s'\n" % arch_name) + return False + return True + class TarGzipArchive(TarArchive): ADD = ['zcf'] diff --git a/setup.py b/setup.py index abe4500..23fe986 100755 --- a/setup.py +++ b/setup.py @@ -7,7 +7,7 @@ from distutils.core import setup setup(name='fs-uae-wrapper', packages=['fs_uae_wrapper'], - version='0.5', + version='0.6', description='Automate archives and state for fs-uae', author='Roman Dobosz', author_email='gryf73@gmail.com', diff --git a/tests/test_file_archive.py b/tests/test_file_archive.py index da85170..cc30983 100644 --- a/tests/test_file_archive.py +++ b/tests/test_file_archive.py @@ -60,61 +60,73 @@ class TestArchive(TestCase): self.assertFalse(arch.extract('foo')) call.assert_called_once_with(['false', 'x', 'foo']) + @mock.patch('os.path.exists') @mock.patch('fs_uae_wrapper.path.which') @mock.patch('subprocess.call') - def test_tar(self, call, which): + def test_tar(self, call, which, exists): with open('foo', 'w') as fobj: fobj.write('\n') which.return_value = 'tar' + exists.return_value = True arch = file_archive.TarArchive() arch.archiver = 'tar' call.return_value = 0 - self.assertTrue(arch.create('foo')) - call.assert_called_once_with(['tar', 'cf', 'foo', '.']) + self.assertTrue(arch.create('foo.tar')) + call.assert_called_once_with(['tar', 'cf', 'foo.tar', 'foo']) call.reset_mock() call.return_value = 1 - self.assertFalse(arch.extract('foo')) - call.assert_called_once_with(['tar', 'xf', 'foo']) + self.assertFalse(arch.extract('foo.tar')) + call.assert_called_once_with(['tar', 'xf', 'foo.tar']) call.reset_mock() arch = file_archive.TarGzipArchive() arch.archiver = 'tar' call.return_value = 0 - self.assertTrue(arch.create('foo')) - call.assert_called_once_with(['tar', 'zcf', 'foo', '.']) + self.assertTrue(arch.create('foo.tgz')) + call.assert_called_once_with(['tar', 'zcf', 'foo.tgz', 'foo']) call.reset_mock() call.return_value = 1 - self.assertFalse(arch.extract('foo')) - call.assert_called_once_with(['tar', 'xf', 'foo']) + self.assertFalse(arch.extract('foo.tgz')) + call.assert_called_once_with(['tar', 'xf', 'foo.tgz']) call.reset_mock() arch = file_archive.TarBzip2Archive() arch.archiver = 'tar' call.return_value = 0 - self.assertTrue(arch.create('foo')) - call.assert_called_once_with(['tar', 'jcf', 'foo', '.']) + self.assertTrue(arch.create('foo.tar.bz2')) + call.assert_called_once_with(['tar', 'jcf', 'foo.tar.bz2', 'foo']) call.reset_mock() call.return_value = 1 - self.assertFalse(arch.extract('foo')) - call.assert_called_once_with(['tar', 'xf', 'foo']) + self.assertFalse(arch.extract('foo.tar.bz2')) + call.assert_called_once_with(['tar', 'xf', 'foo.tar.bz2']) call.reset_mock() arch = file_archive.TarXzArchive() arch.archiver = 'tar' call.return_value = 0 - self.assertTrue(arch.create('foo')) - call.assert_called_once_with(['tar', 'Jcf', 'foo', '.']) + self.assertTrue(arch.create('foo.tar.xz')) + call.assert_called_once_with(['tar', 'Jcf', 'foo.tar.xz', 'foo']) call.reset_mock() call.return_value = 1 - self.assertFalse(arch.extract('foo')) - call.assert_called_once_with(['tar', 'xf', 'foo']) + self.assertFalse(arch.extract('foo.tar.xz')) + call.assert_called_once_with(['tar', 'xf', 'foo.tar.xz']) + + with open('bar', 'w') as fobj: + fobj.write('\n') + + call.reset_mock() + arch = file_archive.TarGzipArchive() + arch.archiver = 'tar' + call.return_value = 0 + self.assertTrue(arch.create('foo.tgz')) + call.assert_called_once_with(['tar', 'zcf', 'foo.tgz', 'bar', 'foo']) @mock.patch('fs_uae_wrapper.path.which') @mock.patch('subprocess.call')