1
0
mirror of https://github.com/gryf/fs-uae-wrapper.git synced 2025-12-19 12:28:12 +01:00

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.
This commit is contained in:
2017-01-02 20:08:50 +01:00
parent 5f98e9b794
commit 19acb789b6
6 changed files with 82 additions and 19 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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)

View File

@@ -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))

View File

@@ -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())

View File

@@ -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