mirror of
https://github.com/gryf/fs-uae-wrapper.git
synced 2025-12-19 12:28:12 +01:00
Parametrize extract and create methods for Archive base class
This commit is contained in:
@@ -97,7 +97,9 @@ class Base(object):
|
|||||||
else:
|
else:
|
||||||
self.arch_filepath = os.path.join(conf_abs_dir, arch)
|
self.arch_filepath = os.path.join(conf_abs_dir, arch)
|
||||||
# set optional save_state
|
# set optional save_state
|
||||||
self.save_filename = os.path.join(conf_abs_dir, conf_base + '_save.7z')
|
arch_ext = utils.get_arch_ext(self.all_options['wrapper_archiver'])
|
||||||
|
self.save_filename = os.path.join(conf_abs_dir, conf_base + '_save' +
|
||||||
|
arch_ext)
|
||||||
|
|
||||||
def _copy_conf(self):
|
def _copy_conf(self):
|
||||||
"""copy provided configuration as Config.fs-uae"""
|
"""copy provided configuration as Config.fs-uae"""
|
||||||
@@ -141,6 +143,7 @@ class Base(object):
|
|||||||
"""
|
"""
|
||||||
Get the saves from emulator and store it where configuration is placed
|
Get the saves from emulator and store it where configuration is placed
|
||||||
"""
|
"""
|
||||||
|
os.chdir(self.dir)
|
||||||
save_path = self._get_saves_dir()
|
save_path = self._get_saves_dir()
|
||||||
if not save_path:
|
if not save_path:
|
||||||
return True
|
return True
|
||||||
@@ -148,10 +151,14 @@ class Base(object):
|
|||||||
if os.path.exists(self.save_filename):
|
if os.path.exists(self.save_filename):
|
||||||
os.unlink(self.save_filename)
|
os.unlink(self.save_filename)
|
||||||
|
|
||||||
if not utils.run_command(['7z', 'a', self.save_filename, save_path]):
|
curdir = os.path.abspath('.')
|
||||||
|
|
||||||
|
if not utils.create_archive(self.save_filename, '', [save_path]):
|
||||||
sys.stderr.write('Error: archiving save state failed\n')
|
sys.stderr.write('Error: archiving save state failed\n')
|
||||||
|
os.chdir(curdir)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
os.chdir(curdir)
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def _load_save(self):
|
def _load_save(self):
|
||||||
@@ -163,17 +170,18 @@ class Base(object):
|
|||||||
|
|
||||||
curdir = os.path.abspath('.')
|
curdir = os.path.abspath('.')
|
||||||
os.chdir(self.dir)
|
os.chdir(self.dir)
|
||||||
utils.run_command(['7z', 'x', self.save_filename])
|
utils.extract_archive(self.save_filename)
|
||||||
os.chdir(curdir)
|
os.chdir(curdir)
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def _get_saves_dir(self):
|
def _get_saves_dir(self):
|
||||||
"""
|
"""
|
||||||
Return full path to save state directory or None in cases:
|
Return path to save state directory or None in cases:
|
||||||
- there is no save state dir set relative to config file
|
- there is no save state dir set relative to config file
|
||||||
- save state dir is set globally
|
- save state dir is set globally
|
||||||
- save state dir is set relative to the config file
|
- save state dir is set relative to the config file
|
||||||
- save state dir doesn't exists
|
- save state dir doesn't exists
|
||||||
|
Note, that returned path is relative not absolute
|
||||||
"""
|
"""
|
||||||
if not self.all_options.get('save_states_dir'):
|
if not self.all_options.get('save_states_dir'):
|
||||||
return None
|
return None
|
||||||
@@ -188,7 +196,10 @@ class Base(object):
|
|||||||
if not os.path.exists(save_path) or not os.path.isdir(save_path):
|
if not os.path.exists(save_path) or not os.path.isdir(save_path):
|
||||||
return None
|
return None
|
||||||
|
|
||||||
return save_path
|
if save.endswith('/'):
|
||||||
|
save = save[:-1]
|
||||||
|
|
||||||
|
return save
|
||||||
|
|
||||||
def _validate_options(self):
|
def _validate_options(self):
|
||||||
"""Validate mandatory options"""
|
"""Validate mandatory options"""
|
||||||
|
|||||||
@@ -20,11 +20,13 @@ class Archive(object):
|
|||||||
self._compess = self.archiver
|
self._compess = self.archiver
|
||||||
self._decompess = self.archiver
|
self._decompess = self.archiver
|
||||||
|
|
||||||
def create(self, arch_name):
|
def create(self, arch_name, files=None):
|
||||||
"""
|
"""
|
||||||
Create archive. Return True on success, False otherwise.
|
Create archive. Return True on success, False otherwise.
|
||||||
"""
|
"""
|
||||||
result = subprocess.call([self._compess] + self.ADD + [arch_name, '.'])
|
files = files if files else ['.']
|
||||||
|
result = subprocess.call([self._compess] + self.ADD + [arch_name]
|
||||||
|
+ files)
|
||||||
if result != 0:
|
if result != 0:
|
||||||
sys.stderr.write("Unable to create archive `%s'\n" % arch_name)
|
sys.stderr.write("Unable to create archive `%s'\n" % arch_name)
|
||||||
return False
|
return False
|
||||||
@@ -89,7 +91,7 @@ class LzxArchive(Archive):
|
|||||||
ARCH = 'unlzx'
|
ARCH = 'unlzx'
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def create(self, arch_name):
|
def create(self, arch_name, files=None):
|
||||||
sys.stderr.write('Cannot create LZX archive. Only extracting is'
|
sys.stderr.write('Cannot create LZX archive. Only extracting is'
|
||||||
'supported\n')
|
'supported\n')
|
||||||
return False
|
return False
|
||||||
@@ -98,14 +100,15 @@ class LzxArchive(Archive):
|
|||||||
class RarArchive(Archive):
|
class RarArchive(Archive):
|
||||||
ARCH = ['rar', 'unrar']
|
ARCH = ['rar', 'unrar']
|
||||||
|
|
||||||
def create(self, arch_name):
|
def create(self, arch_name, files=None):
|
||||||
|
files = files if files else sorted(os.listdir('.'))
|
||||||
if self.archiver == 'unrar':
|
if self.archiver == 'unrar':
|
||||||
sys.stderr.write('Cannot create RAR archive. Only extracting is'
|
sys.stderr.write('Cannot create RAR archive. Only extracting is'
|
||||||
'supported by unrar.\n')
|
'supported by unrar.\n')
|
||||||
return False
|
return False
|
||||||
|
|
||||||
result = subprocess.call([self._compess] + self.ADD + [arch_name] +
|
result = subprocess.call([self._compess] + self.ADD + [arch_name] +
|
||||||
sorted(os.listdir('.')))
|
files)
|
||||||
if result != 0:
|
if result != 0:
|
||||||
sys.stderr.write("Unable to create archive `%s'\n" % arch_name)
|
sys.stderr.write("Unable to create archive `%s'\n" % arch_name)
|
||||||
return False
|
return False
|
||||||
|
|||||||
@@ -57,7 +57,7 @@ def get_config_options(conf):
|
|||||||
for key, val in parser.items(section)}
|
for key, val in parser.items(section)}
|
||||||
|
|
||||||
|
|
||||||
def operate_archive(arch_name, operation, text):
|
def operate_archive(arch_name, operation, text, params):
|
||||||
"""
|
"""
|
||||||
Create archive from contents of current directory
|
Create archive from contents of current directory
|
||||||
"""
|
"""
|
||||||
@@ -77,31 +77,31 @@ def operate_archive(arch_name, operation, text):
|
|||||||
res = archiver.extract(arch_name)
|
res = archiver.extract(arch_name)
|
||||||
|
|
||||||
if operation == 'create':
|
if operation == 'create':
|
||||||
res = archiver.create(arch_name)
|
res = archiver.create(arch_name, params)
|
||||||
|
|
||||||
msg.close()
|
msg.close()
|
||||||
|
|
||||||
return res
|
return res
|
||||||
|
|
||||||
|
|
||||||
def create_archive(arch_name, title=''):
|
def create_archive(arch_name, title='', params=None):
|
||||||
"""
|
"""
|
||||||
Create archive from contents of current directory
|
Create archive from contents of current directory
|
||||||
"""
|
"""
|
||||||
msg = ''
|
msg = ''
|
||||||
if title:
|
if title:
|
||||||
msg = "Creating archive for `%s'. Please be patient" % title
|
msg = "Creating archive for `%s'. Please be patient" % title
|
||||||
return operate_archive(arch_name, 'create', msg)
|
return operate_archive(arch_name, 'create', msg, params)
|
||||||
|
|
||||||
|
|
||||||
def extract_archive(arch_name, title=''):
|
def extract_archive(arch_name, title='', params=None):
|
||||||
"""
|
"""
|
||||||
Extract provided archive to current directory
|
Extract provided archive to current directory
|
||||||
"""
|
"""
|
||||||
msg = ''
|
msg = ''
|
||||||
if title:
|
if title:
|
||||||
msg = "Extracting files for `%s'. Please be patient" % title
|
msg = "Extracting files for `%s'. Please be patient" % title
|
||||||
return operate_archive(arch_name, 'extract', msg)
|
return operate_archive(arch_name, 'extract', msg, params)
|
||||||
|
|
||||||
|
|
||||||
def run_command(cmd):
|
def run_command(cmd):
|
||||||
|
|||||||
@@ -143,14 +143,14 @@ class TestBase(TestCase):
|
|||||||
run.assert_called_once_with(['fs-uae'])
|
run.assert_called_once_with(['fs-uae'])
|
||||||
|
|
||||||
@mock.patch('fs_uae_wrapper.base.Base._get_saves_dir')
|
@mock.patch('fs_uae_wrapper.base.Base._get_saves_dir')
|
||||||
@mock.patch('fs_uae_wrapper.utils.run_command')
|
@mock.patch('fs_uae_wrapper.utils.create_archive')
|
||||||
def test_save_save(self, run, saves_dir):
|
def test_save_save(self, carch, saves_dir):
|
||||||
|
|
||||||
bobj = base.Base('Config.fs-uae', utils.CmdOption(), {})
|
bobj = base.Base('Config.fs-uae', utils.CmdOption(), {})
|
||||||
bobj.dir = self.dirname
|
bobj.dir = self.dirname
|
||||||
bobj.save_filename = 'foobar_save.7z'
|
bobj.save_filename = 'foobar_save.7z'
|
||||||
saves_dir.bobj.save_filenamereturn_value = None
|
saves_dir.bobj.save_filenamereturn_value = None
|
||||||
run.return_value = True
|
carch.return_value = True
|
||||||
|
|
||||||
self.assertTrue(bobj._save_save())
|
self.assertTrue(bobj._save_save())
|
||||||
|
|
||||||
@@ -164,16 +164,16 @@ class TestBase(TestCase):
|
|||||||
os.mkdir(os.path.join(self.dirname, 'fs-uae-save'))
|
os.mkdir(os.path.join(self.dirname, 'fs-uae-save'))
|
||||||
self.assertTrue(bobj._save_save())
|
self.assertTrue(bobj._save_save())
|
||||||
|
|
||||||
run.return_value = False
|
carch.return_value = False
|
||||||
self.assertFalse(bobj._save_save())
|
self.assertFalse(bobj._save_save())
|
||||||
|
|
||||||
@mock.patch('fs_uae_wrapper.utils.run_command')
|
@mock.patch('fs_uae_wrapper.utils.extract_archive')
|
||||||
def test_load_save(self, run):
|
def test_load_save(self, earch):
|
||||||
|
|
||||||
bobj = base.Base('Config.fs-uae', utils.CmdOption(), {})
|
bobj = base.Base('Config.fs-uae', utils.CmdOption(), {})
|
||||||
bobj.dir = self.dirname
|
bobj.dir = self.dirname
|
||||||
bobj.save_filename = "foobar_save.7z"
|
bobj.save_filename = "foobar_save.7z"
|
||||||
run.return_value = 0
|
earch.return_value = 0
|
||||||
|
|
||||||
# fail to load save is not fatal
|
# fail to load save is not fatal
|
||||||
self.assertTrue(bobj._load_save())
|
self.assertTrue(bobj._load_save())
|
||||||
@@ -183,11 +183,11 @@ class TestBase(TestCase):
|
|||||||
fobj.write('asd')
|
fobj.write('asd')
|
||||||
|
|
||||||
self.assertTrue(bobj._load_save())
|
self.assertTrue(bobj._load_save())
|
||||||
run.assert_called_once_with(['7z', 'x', bobj.save_filename])
|
earch.assert_called_once_with(bobj.save_filename)
|
||||||
|
|
||||||
# failure in searching for archiver are also non fatal
|
# failure in searching for archiver are also non fatal
|
||||||
run.reset_mock()
|
earch.reset_mock()
|
||||||
run.return_value = 1
|
earch.return_value = 1
|
||||||
self.assertTrue(bobj._save_save())
|
self.assertTrue(bobj._save_save())
|
||||||
|
|
||||||
def test_get_saves_dir(self):
|
def test_get_saves_dir(self):
|
||||||
|
|||||||
@@ -83,13 +83,14 @@ class TestUtils(TestCase):
|
|||||||
which.return_value = None
|
which.return_value = None
|
||||||
|
|
||||||
# No config
|
# No config
|
||||||
self.assertFalse(utils.operate_archive('non-existend.7z', 'foo', ''))
|
self.assertFalse(utils.operate_archive('non-existend.7z', 'foo', '',
|
||||||
|
None))
|
||||||
|
|
||||||
# Archive type not known
|
# Archive type not known
|
||||||
with open('unsupported-archive.ace', 'w') as fobj:
|
with open('unsupported-archive.ace', 'w') as fobj:
|
||||||
fobj.write("\n")
|
fobj.write("\n")
|
||||||
self.assertFalse(utils.operate_archive('unsupported-archive.ace',
|
self.assertFalse(utils.operate_archive('unsupported-archive.ace',
|
||||||
'foo', ''))
|
'foo', '', None))
|
||||||
|
|
||||||
# archive is known, but extraction will fail - we have an empty
|
# archive is known, but extraction will fail - we have an empty
|
||||||
# archive and there is no guarantee, that 7z exists on system where
|
# archive and there is no guarantee, that 7z exists on system where
|
||||||
@@ -99,17 +100,17 @@ class TestUtils(TestCase):
|
|||||||
with open('supported-archive.7z', 'w') as fobj:
|
with open('supported-archive.7z', 'w') as fobj:
|
||||||
fobj.write("\n")
|
fobj.write("\n")
|
||||||
self.assertTrue(utils.operate_archive('supported-archive.7z',
|
self.assertTrue(utils.operate_archive('supported-archive.7z',
|
||||||
'extract', ''))
|
'extract', '', None))
|
||||||
extract.assert_called_once()
|
extract.assert_called_once()
|
||||||
|
|
||||||
extract.reset_mock()
|
extract.reset_mock()
|
||||||
self.assertTrue(utils.operate_archive('supported-archive.7z',
|
self.assertTrue(utils.operate_archive('supported-archive.7z',
|
||||||
'extract', ''))
|
'extract', '', None))
|
||||||
extract.assert_called_once()
|
extract.assert_called_once()
|
||||||
|
|
||||||
os.unlink('supported-archive.7z')
|
os.unlink('supported-archive.7z')
|
||||||
self.assertTrue(utils.operate_archive('supported-archive.7z',
|
self.assertTrue(utils.operate_archive('supported-archive.7z',
|
||||||
'create', 'test'))
|
'create', 'test', ['foo']))
|
||||||
create.assert_called_once()
|
create.assert_called_once()
|
||||||
show.assert_called_once()
|
show.assert_called_once()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user