From 19acb789b61270f34b96b9b08c0a645ad46177c6 Mon Sep 17 00:00:00 2001 From: gryf Date: Mon, 2 Jan 2017 20:08:50 +0100 Subject: [PATCH] Added wrapper_archiver option To give user a possibility to choose what archiver he can use another options was introduced for cd32 and archive wrapper modules. This option will indicate what archiver should be used for compressing the save state directories. --- fs_uae_wrapper/base.py | 12 +++++++++ fs_uae_wrapper/file_archive.py | 49 ++++++++++++++++++++++++++-------- fs_uae_wrapper/utils.py | 5 ++++ tests/test_base.py | 25 +++++++++++++---- tests/test_cd32.py | 6 ++++- tests/test_utils.py | 4 +-- 6 files changed, 82 insertions(+), 19 deletions(-) diff --git a/fs_uae_wrapper/base.py b/fs_uae_wrapper/base.py index 705ca1b..bd0dd81 100644 --- a/fs_uae_wrapper/base.py +++ b/fs_uae_wrapper/base.py @@ -7,6 +7,7 @@ import shutil import tempfile from fs_uae_wrapper import utils +from fs_uae_wrapper import path class Base(object): @@ -195,4 +196,15 @@ class Base(object): sys.stderr.write("Configuration lacks of required " "`wrapper' option.\n") return False + + if 'wrapper_archiver' not in self.all_options: + sys.stderr.write("Configuration lacks of required " + "`wrapper_archiver' option.\n") + return False + + if not path.which(self.all_options['wrapper_archiver']): + sys.stderr.write("Cannot find archiver `%s'." % + self.all_options['wrapper_archiver']) + return False + return True diff --git a/fs_uae_wrapper/file_archive.py b/fs_uae_wrapper/file_archive.py index 60007e0..605f861 100644 --- a/fs_uae_wrapper/file_archive.py +++ b/fs_uae_wrapper/file_archive.py @@ -112,6 +112,41 @@ class RarArchive(Archive): return True +class Archivers(object): + """Archivers class""" + archivers = [{'arch': TarArchive, 'name': 'tar', 'ext': ['tar']}, + {'arch': TarGzipArchive, 'name': 'tgz', + 'ext': ['tar.gz', 'tgz']}, + {'arch': TarBzip2Archive, 'name': 'tar.bz2', + 'ext': ['tar.bz2']}, + {'arch': TarXzArchive, 'name': 'tar.xz', 'ext': ['tar.xz']}, + {'arch': RarArchive, 'name': 'rar', 'ext': ['rar']}, + {'arch': SevenZArchive, 'name': '7z', 'ext': ['7z']}, + {'arch': ZipArchive, 'name': 'zip', 'ext': ['zip']}, + {'arch': LhaArchive, 'name': 'lha', 'ext': ['lha', 'lzh']}, + {'arch': LzxArchive, 'name': 'lzx', 'ext': ['lzx']}] + + @classmethod + def get(cls, extension): + """ + Get the archive class or None + """ + for arch in cls.archivers: + if extension in arch['ext']: + return arch['arch'] + return None + + @classmethod + def get_extension_by_name(cls, name): + """ + Get the first defined extension for the archive format + """ + for arch in cls.archivers: + if name == arch['name']: + return '.' + arch['ext'][0] + return None + + def get_archiver(arch_name): """Return right class for provided archive file name""" @@ -122,18 +157,10 @@ def get_archiver(arch_name): if result: ext = result.groups()[0] - archivers = {'.tar': TarArchive, - '.tgz': TarGzipArchive, - '.tar.gz': TarGzipArchive, - '.tar.bz2': TarBzip2Archive, - '.tar.xz': TarXzArchive, - '.rar': RarArchive, - '.7z': SevenZArchive, - '.zip': ZipArchive, - '.lha': LhaArchive, - '.lzx': LzxArchive} + if ext: + ext = ext[1:] - archiver = archivers.get(ext) + archiver = Archivers.get(ext) if not archiver: sys.stderr.write("Unable find archive type for `%s'\n" % arch_name) return None diff --git a/fs_uae_wrapper/utils.py b/fs_uae_wrapper/utils.py index 08be081..d6ab52c 100644 --- a/fs_uae_wrapper/utils.py +++ b/fs_uae_wrapper/utils.py @@ -217,3 +217,8 @@ def get_config(conf_file): config['_base_dir'] = conf_dir return config + + +def get_arch_ext(archiver_name): + """Return extension for the archiver""" + return file_archive.Archivers.get_extension_by_name(archiver_name) diff --git a/tests/test_base.py b/tests/test_base.py index 989e5b8..0fded82 100644 --- a/tests/test_base.py +++ b/tests/test_base.py @@ -75,13 +75,15 @@ class TestBase(TestCase): bobj = base.Base('Config.fs-uae', utils.CmdOption(), {}) os.chdir(self.dirname) bobj.conf_file = 'Config.fs-uae' - bobj.all_options = {'wrapper_archive': 'foo.7z'} + bobj.all_options = {'wrapper_archive': 'foo.7z', + 'wrapper_archiver': '7z'} bobj._set_assets_paths() full_path = os.path.join(self.dirname, 'Config_save.7z') self.assertEqual(bobj.save_filename, full_path) - bobj.all_options = {'wrapper_archive': '/home/user/foo.7z'} + bobj.all_options = {'wrapper_archive': '/home/user/foo.7z', + 'wrapper_archiver': '7z'} bobj._set_assets_paths() full_path = os.path.join(self.dirname, 'Config_save.7z') @@ -214,9 +216,12 @@ class TestBase(TestCase): os.unlink(path) os.mkdir(path) - self.assertEqual(bobj._get_saves_dir(), path) + self.assertEqual(bobj._get_saves_dir(), 'saves') - def test_validate_options(self): + @mock.patch('fs_uae_wrapper.path.which') + def test_validate_options(self, which): + + which.return_value = None bobj = base.Base('Config.fs-uae', utils.CmdOption(), {}) bobj.all_options = {} @@ -224,6 +229,15 @@ class TestBase(TestCase): self.assertFalse(bobj._validate_options()) bobj.all_options = {'wrapper': 'dummy'} + self.assertFalse(bobj._validate_options()) + + bobj.all_options = {'wrapper': 'dummy', + 'wrapper_archiver': 'myarchiver'} + self.assertFalse(bobj._validate_options()) + + which.return_value = '7z' + bobj.all_options = {'wrapper': 'dummy', + 'wrapper_archiver': '7z'} self.assertTrue(bobj._validate_options()) def test_run_clean(self): @@ -233,7 +247,8 @@ class TestBase(TestCase): self.assertFalse(bobj.run()) - bobj.all_options = {'wrapper': 'dummy'} + bobj.all_options = {'wrapper': 'dummy', + 'wrapper_archiver': 'rar'} try: self.assertTrue(bobj.run()) self.assertTrue(os.path.exists(bobj.dir)) diff --git a/tests/test_cd32.py b/tests/test_cd32.py index 295319d..240a75f 100644 --- a/tests/test_cd32.py +++ b/tests/test_cd32.py @@ -20,6 +20,9 @@ class TestCD32(TestCase): self.assertFalse(acd32._validate_options()) acd32.all_options['wrapper_archive'] = 'fake.tgz' + self.assertFalse(acd32._validate_options()) + + acd32.all_options['wrapper_archiver'] = 'rar' self.assertTrue(acd32._validate_options()) @mock.patch('tempfile.mkdtemp') @@ -42,7 +45,8 @@ class TestCD32(TestCase): self.assertFalse(acd32.run()) acd32.all_options = {'wrapper': 'cd32', - 'wrapper_archive': 'fake.tgz'} + 'wrapper_archive': 'fake.tgz', + 'wrapper_archiver': 'rar'} self.assertFalse(acd32.run()) diff --git a/tests/test_utils.py b/tests/test_utils.py index bdd70e2..24a9e1a 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -72,7 +72,7 @@ class TestUtils(TestCase): conf = utils.get_config_options(self.fname) self.assertDictEqual(conf, {'wrapper': ''}) - @mock.patch('fs_uae_wrapper.file_archive.which') + @mock.patch('fs_uae_wrapper.path.which') @mock.patch('fs_uae_wrapper.file_archive.Archive.extract') @mock.patch('fs_uae_wrapper.file_archive.Archive.create') @mock.patch('fs_uae_wrapper.message.Message.close') @@ -153,7 +153,7 @@ class TestUtils(TestCase): fobj.write("\n") self.assertFalse(utils.extract_archive('supported-archive.7z')) - @mock.patch('fs_uae_wrapper.file_archive.which') + @mock.patch('fs_uae_wrapper.path.which') @mock.patch('fs_uae_wrapper.file_archive.Archive.extract') def test_extract_archive_positive(self, arch_extract, which): arch_extract.return_value = True