From ab71e270d89971a3ab03c3f30c6fe53472183b14 Mon Sep 17 00:00:00 2001 From: gryf Date: Mon, 2 Jan 2017 10:00:35 +0100 Subject: [PATCH] Removed archive from utils --- fs_uae_wrapper/utils.py | 41 ++++++++++------------------------------- tests/test_utils.py | 7 +++---- 2 files changed, 13 insertions(+), 35 deletions(-) diff --git a/fs_uae_wrapper/utils.py b/fs_uae_wrapper/utils.py index b11b14c..fbe74a8 100644 --- a/fs_uae_wrapper/utils.py +++ b/fs_uae_wrapper/utils.py @@ -11,18 +11,7 @@ except ImportError: import ConfigParser as configparser from fs_uae_wrapper import message - - -ARCHIVERS = {'.tar': ['tar', 'xf'], - '.tgz': ['tar', 'xf'], - '.tar.gz': ['tar', 'xf'], - '.tar.bz2': ['tar', 'xf'], - '.tar.xz': ['tar', 'xf'], - '.rar': ['unrar', 'x'], - '.7z': ['7z', 'x'], - '.zip': ['7z', 'x'], - '.lha': ['lha', 'x'], - '.lzx': ['unlzx']} +from fs_uae_wrapper import file_archive class CmdOption(dict): @@ -73,16 +62,11 @@ def extract_archive(arch_name, title=''): Extract provided archive to current directory """ - if not os.path.exists(arch_name): - sys.stderr.write("Archive `%s' doesn't exists.\n" % arch_name) - return False + archiver = file_archive.get_archiver(arch_name) - _, ext = os.path.splitext(arch_name) - - cmd = ARCHIVERS.get(ext) - - if cmd is None: - sys.stderr.write("Unable find archive type for `%s'.\n" % arch_name) + if archiver is None: + sys.stderr.write("Unable find archive type for `%s' or executable " + "doesn't exists.\n" % arch_name) return False msg = message.Message("Extracting files for `%s'. Please be " @@ -90,18 +74,13 @@ def extract_archive(arch_name, title=''): if title: msg.show() - try: - subprocess.check_call(cmd + [arch_name]) - except OSError: - sys.stderr.write("Error executing `%s'.\n" % cmd) - msg.close() - return False - except subprocess.CalledProcessError: - sys.stderr.write("Error during extracting archive `%s'.\n" % arch_name) - msg.close() + res = archiver.extract(arch_name) + msg.close() + + if not res: + sys.stderr.write("Error extracting `%s'.\n" % arch_name) return False - msg.close() return True diff --git a/tests/test_utils.py b/tests/test_utils.py index dedf827..1c933f5 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -91,8 +91,8 @@ class TestUtils(TestCase): fobj.write("\n") self.assertFalse(utils.extract_archive('supported-archive.7z')) - @mock.patch('subprocess.check_call') - def test_extract_archive_positive(self, sp_check_call): + @mock.patch('fs_uae_wrapper.file_archive.Archive.extract') + def test_extract_archive_positive(self, arch_extract): os.chdir(self.dirname) # archive is known, and extraction should succeed @@ -100,8 +100,7 @@ class TestUtils(TestCase): with open(arch_name, 'w') as fobj: fobj.write("\n") self.assertTrue(utils.extract_archive(arch_name)) - sp_check_call.assert_called_once_with(utils.ARCHIVERS['.7z'] + - [arch_name]) + arch_extract.assert_called_once_with(arch_name) def test_merge_all_options(self):