mirror of
https://github.com/gryf/fs-uae-wrapper.git
synced 2025-12-19 04:20:23 +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:
@@ -7,6 +7,7 @@ import shutil
|
|||||||
import tempfile
|
import tempfile
|
||||||
|
|
||||||
from fs_uae_wrapper import utils
|
from fs_uae_wrapper import utils
|
||||||
|
from fs_uae_wrapper import path
|
||||||
|
|
||||||
|
|
||||||
class Base(object):
|
class Base(object):
|
||||||
@@ -195,4 +196,15 @@ class Base(object):
|
|||||||
sys.stderr.write("Configuration lacks of required "
|
sys.stderr.write("Configuration lacks of required "
|
||||||
"`wrapper' option.\n")
|
"`wrapper' option.\n")
|
||||||
return False
|
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
|
return True
|
||||||
|
|||||||
@@ -112,6 +112,41 @@ class RarArchive(Archive):
|
|||||||
return True
|
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):
|
def get_archiver(arch_name):
|
||||||
"""Return right class for provided archive file name"""
|
"""Return right class for provided archive file name"""
|
||||||
|
|
||||||
@@ -122,18 +157,10 @@ def get_archiver(arch_name):
|
|||||||
if result:
|
if result:
|
||||||
ext = result.groups()[0]
|
ext = result.groups()[0]
|
||||||
|
|
||||||
archivers = {'.tar': TarArchive,
|
if ext:
|
||||||
'.tgz': TarGzipArchive,
|
ext = ext[1:]
|
||||||
'.tar.gz': TarGzipArchive,
|
|
||||||
'.tar.bz2': TarBzip2Archive,
|
|
||||||
'.tar.xz': TarXzArchive,
|
|
||||||
'.rar': RarArchive,
|
|
||||||
'.7z': SevenZArchive,
|
|
||||||
'.zip': ZipArchive,
|
|
||||||
'.lha': LhaArchive,
|
|
||||||
'.lzx': LzxArchive}
|
|
||||||
|
|
||||||
archiver = archivers.get(ext)
|
archiver = Archivers.get(ext)
|
||||||
if not archiver:
|
if not archiver:
|
||||||
sys.stderr.write("Unable find archive type for `%s'\n" % arch_name)
|
sys.stderr.write("Unable find archive type for `%s'\n" % arch_name)
|
||||||
return None
|
return None
|
||||||
|
|||||||
@@ -217,3 +217,8 @@ def get_config(conf_file):
|
|||||||
config['_base_dir'] = conf_dir
|
config['_base_dir'] = conf_dir
|
||||||
|
|
||||||
return config
|
return config
|
||||||
|
|
||||||
|
|
||||||
|
def get_arch_ext(archiver_name):
|
||||||
|
"""Return extension for the archiver"""
|
||||||
|
return file_archive.Archivers.get_extension_by_name(archiver_name)
|
||||||
|
|||||||
@@ -75,13 +75,15 @@ class TestBase(TestCase):
|
|||||||
bobj = base.Base('Config.fs-uae', utils.CmdOption(), {})
|
bobj = base.Base('Config.fs-uae', utils.CmdOption(), {})
|
||||||
os.chdir(self.dirname)
|
os.chdir(self.dirname)
|
||||||
bobj.conf_file = 'Config.fs-uae'
|
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()
|
bobj._set_assets_paths()
|
||||||
full_path = os.path.join(self.dirname, 'Config_save.7z')
|
full_path = os.path.join(self.dirname, 'Config_save.7z')
|
||||||
self.assertEqual(bobj.save_filename, full_path)
|
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()
|
bobj._set_assets_paths()
|
||||||
full_path = os.path.join(self.dirname, 'Config_save.7z')
|
full_path = os.path.join(self.dirname, 'Config_save.7z')
|
||||||
@@ -214,9 +216,12 @@ class TestBase(TestCase):
|
|||||||
|
|
||||||
os.unlink(path)
|
os.unlink(path)
|
||||||
os.mkdir(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 = base.Base('Config.fs-uae', utils.CmdOption(), {})
|
||||||
bobj.all_options = {}
|
bobj.all_options = {}
|
||||||
@@ -224,6 +229,15 @@ class TestBase(TestCase):
|
|||||||
self.assertFalse(bobj._validate_options())
|
self.assertFalse(bobj._validate_options())
|
||||||
|
|
||||||
bobj.all_options = {'wrapper': 'dummy'}
|
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())
|
self.assertTrue(bobj._validate_options())
|
||||||
|
|
||||||
def test_run_clean(self):
|
def test_run_clean(self):
|
||||||
@@ -233,7 +247,8 @@ class TestBase(TestCase):
|
|||||||
|
|
||||||
self.assertFalse(bobj.run())
|
self.assertFalse(bobj.run())
|
||||||
|
|
||||||
bobj.all_options = {'wrapper': 'dummy'}
|
bobj.all_options = {'wrapper': 'dummy',
|
||||||
|
'wrapper_archiver': 'rar'}
|
||||||
try:
|
try:
|
||||||
self.assertTrue(bobj.run())
|
self.assertTrue(bobj.run())
|
||||||
self.assertTrue(os.path.exists(bobj.dir))
|
self.assertTrue(os.path.exists(bobj.dir))
|
||||||
|
|||||||
@@ -20,6 +20,9 @@ class TestCD32(TestCase):
|
|||||||
self.assertFalse(acd32._validate_options())
|
self.assertFalse(acd32._validate_options())
|
||||||
|
|
||||||
acd32.all_options['wrapper_archive'] = 'fake.tgz'
|
acd32.all_options['wrapper_archive'] = 'fake.tgz'
|
||||||
|
self.assertFalse(acd32._validate_options())
|
||||||
|
|
||||||
|
acd32.all_options['wrapper_archiver'] = 'rar'
|
||||||
self.assertTrue(acd32._validate_options())
|
self.assertTrue(acd32._validate_options())
|
||||||
|
|
||||||
@mock.patch('tempfile.mkdtemp')
|
@mock.patch('tempfile.mkdtemp')
|
||||||
@@ -42,7 +45,8 @@ class TestCD32(TestCase):
|
|||||||
self.assertFalse(acd32.run())
|
self.assertFalse(acd32.run())
|
||||||
|
|
||||||
acd32.all_options = {'wrapper': 'cd32',
|
acd32.all_options = {'wrapper': 'cd32',
|
||||||
'wrapper_archive': 'fake.tgz'}
|
'wrapper_archive': 'fake.tgz',
|
||||||
|
'wrapper_archiver': 'rar'}
|
||||||
|
|
||||||
self.assertFalse(acd32.run())
|
self.assertFalse(acd32.run())
|
||||||
|
|
||||||
|
|||||||
@@ -72,7 +72,7 @@ class TestUtils(TestCase):
|
|||||||
conf = utils.get_config_options(self.fname)
|
conf = utils.get_config_options(self.fname)
|
||||||
self.assertDictEqual(conf, {'wrapper': ''})
|
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.extract')
|
||||||
@mock.patch('fs_uae_wrapper.file_archive.Archive.create')
|
@mock.patch('fs_uae_wrapper.file_archive.Archive.create')
|
||||||
@mock.patch('fs_uae_wrapper.message.Message.close')
|
@mock.patch('fs_uae_wrapper.message.Message.close')
|
||||||
@@ -153,7 +153,7 @@ class TestUtils(TestCase):
|
|||||||
fobj.write("\n")
|
fobj.write("\n")
|
||||||
self.assertFalse(utils.extract_archive('supported-archive.7z'))
|
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')
|
@mock.patch('fs_uae_wrapper.file_archive.Archive.extract')
|
||||||
def test_extract_archive_positive(self, arch_extract, which):
|
def test_extract_archive_positive(self, arch_extract, which):
|
||||||
arch_extract.return_value = True
|
arch_extract.return_value = True
|
||||||
|
|||||||
Reference in New Issue
Block a user