1
0
mirror of https://github.com/gryf/fs-uae-wrapper.git synced 2025-12-18 20:10:26 +01:00

Extract new base class for archive wrapper module

This commit is contained in:
2017-01-07 09:04:12 +01:00
parent 77cf10cee7
commit f1fb43ca64
7 changed files with 166 additions and 113 deletions

View File

@@ -6,13 +6,12 @@ the temporary one.
""" """
import os import os
import shutil import shutil
import sys
from fs_uae_wrapper import base from fs_uae_wrapper import base
from fs_uae_wrapper import utils from fs_uae_wrapper import utils
class Archive(base.Base): class Archive(base.ArchiveBase):
""" """
Class for performing extracting archive, copying emulator files, and Class for performing extracting archive, copying emulator files, and
cleaning it back again cleaning it back again
@@ -30,12 +29,6 @@ class Archive(base.Base):
- copy configuration - copy configuration
- run the emulation - run the emulation
- optionally make archive save state - optionally make archive save state
Params:
conf_file: a relative path to provided configuration file
fsuae_options: is an CmdOption object created out of command line
parameters
configuration: is config dictionary created out of config file
""" """
if not super(Archive, self).run(): if not super(Archive, self).run():
return False return False
@@ -61,17 +54,6 @@ class Archive(base.Base):
return self._make_archive() return self._make_archive()
def _validate_options(self):
validation_result = super(Archive, self)._validate_options()
if 'wrapper_archive' not in self.all_options:
sys.stderr.write("Configuration lacks of required "
"`wrapper_archive' option.\n")
validation_result = False
return validation_result
def _make_archive(self): def _make_archive(self):
""" """
Produce archive and save it back. Than remove old one. Produce archive and save it back. Than remove old one.

View File

@@ -29,7 +29,6 @@ class Base(object):
fsuae_options) fsuae_options)
self.dir = None self.dir = None
self.save_filename = None self.save_filename = None
self.arch_filepath = None
def run(self): def run(self):
""" """
@@ -46,7 +45,6 @@ class Base(object):
return False return False
self.dir = tempfile.mkdtemp() self.dir = tempfile.mkdtemp()
self._set_assets_paths() self._set_assets_paths()
return True return True
@@ -93,12 +91,6 @@ class Base(object):
conf_base = os.path.basename(self.conf_file) conf_base = os.path.basename(self.conf_file)
conf_base = os.path.splitext(conf_base)[0] conf_base = os.path.splitext(conf_base)[0]
arch = self.all_options.get('wrapper_archive')
if arch:
if os.path.isabs(arch):
self.arch_filepath = arch
else:
self.arch_filepath = os.path.join(conf_abs_dir, arch)
# set optional save_state # set optional save_state
arch_ext = utils.get_arch_ext(self.all_options.get('wrapper_archiver')) arch_ext = utils.get_arch_ext(self.all_options.get('wrapper_archiver'))
if arch_ext: if arch_ext:
@@ -112,16 +104,6 @@ class Base(object):
os.path.join(self.dir, 'Config.fs-uae')) os.path.join(self.dir, 'Config.fs-uae'))
return True return True
def _extract(self):
"""Extract archive to temp dir"""
title = self._get_title()
curdir = os.path.abspath('.')
os.chdir(self.dir)
result = utils.extract_archive(self.arch_filepath, title)
os.chdir(curdir)
return result
def _run_emulator(self, fsuae_options): def _run_emulator(self, fsuae_options):
"""execute fs-uae in provided directory""" """execute fs-uae in provided directory"""
curdir = os.path.abspath('.') curdir = os.path.abspath('.')
@@ -232,3 +214,56 @@ class Base(object):
return False return False
return True return True
class ArchiveBase(Base):
"""
Base class for archive based wrapper modules
"""
def __init__(self, conf_file, fsuae_options, configuration):
"""
Params:
conf_file: a relative path to provided configuration file
fsuae_options: is an CmdOption object created out of command line
parameters
configuration: is config dictionary created out of config file
"""
super(ArchiveBase, self).__init__(conf_file, fsuae_options,
configuration)
self.arch_filepath = None
def _set_assets_paths(self):
"""
Set full paths for archive file (without extension) and for save state
archive file
"""
super(ArchiveBase, self)._set_assets_paths()
conf_abs_dir = os.path.dirname(os.path.abspath(self.conf_file))
arch = self.all_options.get('wrapper_archive')
if arch:
if os.path.isabs(arch):
self.arch_filepath = arch
else:
self.arch_filepath = os.path.join(conf_abs_dir, arch)
def _extract(self):
"""Extract archive to temp dir"""
title = self._get_title()
curdir = os.path.abspath('.')
os.chdir(self.dir)
result = utils.extract_archive(self.arch_filepath, title)
os.chdir(curdir)
return result
def _validate_options(self):
validation_result = super(ArchiveBase, self)._validate_options()
if 'wrapper_archive' not in self.all_options:
sys.stderr.write("Configuration lacks of required "
"`wrapper_archive' option.\n")
validation_result = False
return validation_result

View File

@@ -8,12 +8,10 @@ used as a base for save state (it will append '_save.7z' to the archive file
name. name.
""" """
import sys
from fs_uae_wrapper import base from fs_uae_wrapper import base
class CD32(base.Base): class CD32(base.ArchiveBase):
""" """
Class for performing extracting archive, copying emulator files, and Class for performing extracting archive, copying emulator files, and
cleaning it back again cleaning it back again
@@ -53,16 +51,6 @@ class CD32(base.Base):
return True return True
def _validate_options(self):
validation_result = super(CD32, self)._validate_options()
if 'wrapper_archive' not in self.all_options:
sys.stderr.write("Configuration lacks of required "
"`wrapper_archive' option.\n")
validation_result = False
return validation_result
def run(config_file, fsuae_options, configuration): def run(config_file, fsuae_options, configuration):
"""Run fs-uae with provided config file and options""" """Run fs-uae with provided config file and options"""

View File

@@ -2,7 +2,7 @@
""" """
Setup for the fs-uae-wrapper Setup for the fs-uae-wrapper
""" """
from distutils.core import setup from setuptools import setup
setup(name='fs-uae-wrapper', setup(name='fs-uae-wrapper',

View File

@@ -43,13 +43,13 @@ class TestArchive(TestCase):
@mock.patch('tempfile.mkdtemp') @mock.patch('tempfile.mkdtemp')
@mock.patch('fs_uae_wrapper.path.which') @mock.patch('fs_uae_wrapper.path.which')
@mock.patch('fs_uae_wrapper.archive.Archive._make_archive') @mock.patch('fs_uae_wrapper.archive.Archive._make_archive')
@mock.patch('fs_uae_wrapper.base.Base._save_save') @mock.patch('fs_uae_wrapper.base.ArchiveBase._save_save')
@mock.patch('fs_uae_wrapper.base.Base._get_saves_dir') @mock.patch('fs_uae_wrapper.base.ArchiveBase._get_saves_dir')
@mock.patch('fs_uae_wrapper.base.Base._run_emulator') @mock.patch('fs_uae_wrapper.base.ArchiveBase._run_emulator')
@mock.patch('fs_uae_wrapper.base.Base._kickstart_option') @mock.patch('fs_uae_wrapper.base.ArchiveBase._kickstart_option')
@mock.patch('fs_uae_wrapper.base.Base._copy_conf') @mock.patch('fs_uae_wrapper.base.ArchiveBase._copy_conf')
@mock.patch('fs_uae_wrapper.base.Base._load_save') @mock.patch('fs_uae_wrapper.base.ArchiveBase._load_save')
@mock.patch('fs_uae_wrapper.base.Base._extract') @mock.patch('fs_uae_wrapper.base.ArchiveBase._extract')
def test_run(self, extract, load_save, copy_conf, kick_option, def test_run(self, extract, load_save, copy_conf, kick_option,
run_emulator, get_save_dir, save_state, make_arch, which, run_emulator, get_save_dir, save_state, make_arch, which,
mkdtemp): mkdtemp):
@@ -102,8 +102,8 @@ class TestArchive(TestCase):
@mock.patch('os.unlink') @mock.patch('os.unlink')
@mock.patch('shutil.rmtree') @mock.patch('shutil.rmtree')
@mock.patch('fs_uae_wrapper.utils.create_archive') @mock.patch('fs_uae_wrapper.utils.create_archive')
@mock.patch('fs_uae_wrapper.base.Base._get_title') @mock.patch('fs_uae_wrapper.base.ArchiveBase._get_title')
@mock.patch('fs_uae_wrapper.base.Base._get_saves_dir') @mock.patch('fs_uae_wrapper.base.ArchiveBase._get_saves_dir')
def test_make_archive(self, sdir, title, carch, rmt, unlink, rename): def test_make_archive(self, sdir, title, carch, rmt, unlink, rename):
sdir.return_value = None sdir.return_value = None

View File

@@ -99,34 +99,6 @@ class TestBase(TestCase):
self.assertTrue(os.path.exists(os.path.join(self.dirname, self.assertTrue(os.path.exists(os.path.join(self.dirname,
'Config.fs-uae'))) 'Config.fs-uae')))
@mock.patch('fs_uae_wrapper.utils.extract_archive')
def test_extract(self, utils_extract):
bobj = base.Base('Config.fs-uae', utils.CmdOption(), {})
bobj.arch_filepath = self.fname
bobj.dir = self.dirname
utils_extract.return_value = False
# message for the gui is taken from title in fs-uae conf or, if there
# is no such entry, use archive name, which is mandatory to provide
bobj.all_options = {'title': 'foo_game', 'wrapper_gui_msg': '1'}
self.assertFalse(bobj._extract())
utils_extract.assert_called_once_with(self.fname, 'foo_game')
utils_extract.reset_mock()
bobj.all_options = {'wrapper_archive': 'arch.tar',
'wrapper_gui_msg': '1'}
self.assertFalse(bobj._extract())
utils_extract.assert_called_once_with(self.fname, 'arch.tar')
# lets pretend, the extracting has failed
utils_extract.reset_mock()
bobj.all_options = {'wrapper_gui_msg': '0'}
utils_extract.return_value = False
self.assertFalse(bobj._extract())
utils_extract.assert_called_once_with(self.fname, '')
@mock.patch('fs_uae_wrapper.utils.run_command') @mock.patch('fs_uae_wrapper.utils.run_command')
def test_run_emulator(self, run): def test_run_emulator(self, run):
@@ -247,12 +219,15 @@ class TestBase(TestCase):
self.assertTrue(bobj._validate_options()) self.assertTrue(bobj._validate_options())
bobj.all_options = {'wrapper': 'dummy', bobj.all_options = {'wrapper': 'dummy',
'wrapper_archiver': 'myarchiver'} 'wrapper_save_state': '0'}
self.assertTrue(bobj._validate_options()) self.assertTrue(bobj._validate_options())
bobj.all_options = {'wrapper': 'dummy', bobj.all_options = {'wrapper': 'dummy',
'wrapper_save_state': '1', 'wrapper_archiver': 'rar'}
'wrapper_archiver': 'myarchiver'} self.assertTrue(bobj._validate_options())
bobj.all_options = {'wrapper': 'dummy',
'wrapper_save_state': '1'}
self.assertFalse(bobj._validate_options()) self.assertFalse(bobj._validate_options())
which.return_value = '7z' which.return_value = '7z'
@@ -276,9 +251,96 @@ class TestBase(TestCase):
self.assertFalse(bobj.run()) self.assertFalse(bobj.run())
bobj.all_options = {'wrapper': 'dummy', bobj.all_options = {'wrapper': 'dummy',
'wrapper_archiver': 'rar'} 'wrapper_archiver': 'rar',
'wrapper_archive': 'foo.7z'}
try: try:
self.assertTrue(bobj.run()) self.assertTrue(bobj.run())
self.assertTrue(os.path.exists(bobj.dir)) self.assertTrue(os.path.exists(bobj.dir))
finally: finally:
bobj.clean() bobj.clean()
class TestArchiveBase(TestCase):
def setUp(self):
fd, self.fname = mkstemp()
self.dirname = mkdtemp()
self.confdir = mkdtemp()
os.close(fd)
self._argv = sys.argv[:]
sys.argv = ['fs-uae-wrapper']
self.curdir = os.path.abspath(os.curdir)
def tearDown(self):
os.chdir(self.curdir)
try:
shutil.rmtree(self.dirname)
except OSError:
pass
try:
shutil.rmtree(self.confdir)
except OSError:
pass
os.unlink(self.fname)
sys.argv = self._argv[:]
def test_set_assets_paths(self):
bobj = base.ArchiveBase('Config.fs-uae', utils.CmdOption(), {})
os.chdir(self.dirname)
bobj.conf_file = 'Config.fs-uae'
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',
'wrapper_archiver': '7z'}
bobj._set_assets_paths()
full_path = os.path.join(self.dirname, 'Config_save.7z')
self.assertEqual(bobj.save_filename, full_path)
@mock.patch('fs_uae_wrapper.utils.extract_archive')
def test_extract(self, utils_extract):
bobj = base.ArchiveBase('Config.fs-uae', utils.CmdOption(), {})
bobj.arch_filepath = self.fname
bobj.dir = self.dirname
utils_extract.return_value = False
# message for the gui is taken from title in fs-uae conf or, if there
# is no such entry, use archive name, which is mandatory to provide
bobj.all_options = {'title': 'foo_game', 'wrapper_gui_msg': '1'}
self.assertFalse(bobj._extract())
utils_extract.assert_called_once_with(self.fname, 'foo_game')
utils_extract.reset_mock()
bobj.all_options = {'wrapper_archive': 'arch.tar',
'wrapper_gui_msg': '1'}
self.assertFalse(bobj._extract())
utils_extract.assert_called_once_with(self.fname, 'arch.tar')
# lets pretend, the extracting has failed
utils_extract.reset_mock()
bobj.all_options = {'wrapper_gui_msg': '0'}
utils_extract.return_value = False
self.assertFalse(bobj._extract())
utils_extract.assert_called_once_with(self.fname, '')
def test_validate_options(self):
bobj = base.ArchiveBase('Config.fs-uae', utils.CmdOption(), {})
bobj.all_options = {}
self.assertFalse(bobj._validate_options())
bobj.all_options = {'wrapper': 'dummy'}
self.assertFalse(bobj._validate_options())
bobj.all_options = {'wrapper': 'dummy',
'wrapper_archive': 'myarchive.7z'}
self.assertTrue(bobj._validate_options())

View File

@@ -11,29 +11,15 @@ from fs_uae_wrapper import utils
class TestCD32(TestCase): class TestCD32(TestCase):
@mock.patch('fs_uae_wrapper.path.which')
def test_validate_options(self, which):
which.return_value = 'rar'
acd32 = cd32.CD32('Config.fs-uae', utils.CmdOption(), {})
self.assertFalse(acd32._validate_options())
acd32.all_options['wrapper'] = 'cd32'
self.assertFalse(acd32._validate_options())
acd32.all_options['wrapper_archive'] = 'fake.tgz'
self.assertTrue(acd32._validate_options())
@mock.patch('tempfile.mkdtemp') @mock.patch('tempfile.mkdtemp')
@mock.patch('fs_uae_wrapper.path.which') @mock.patch('fs_uae_wrapper.path.which')
@mock.patch('fs_uae_wrapper.base.Base._save_save') @mock.patch('fs_uae_wrapper.base.ArchiveBase._save_save')
@mock.patch('fs_uae_wrapper.base.Base._get_saves_dir') @mock.patch('fs_uae_wrapper.base.ArchiveBase._get_saves_dir')
@mock.patch('fs_uae_wrapper.base.Base._run_emulator') @mock.patch('fs_uae_wrapper.base.ArchiveBase._run_emulator')
@mock.patch('fs_uae_wrapper.base.Base._kickstart_option') @mock.patch('fs_uae_wrapper.base.ArchiveBase._kickstart_option')
@mock.patch('fs_uae_wrapper.base.Base._copy_conf') @mock.patch('fs_uae_wrapper.base.ArchiveBase._copy_conf')
@mock.patch('fs_uae_wrapper.base.Base._load_save') @mock.patch('fs_uae_wrapper.base.ArchiveBase._load_save')
@mock.patch('fs_uae_wrapper.base.Base._extract') @mock.patch('fs_uae_wrapper.base.ArchiveBase._extract')
def test_run(self, extract, load_save, copy_conf, kick_option, def test_run(self, extract, load_save, copy_conf, kick_option,
run_emulator, get_save_dir, save_state, which, mkdtemp): run_emulator, get_save_dir, save_state, which, mkdtemp):