diff --git a/fs_uae_wrapper/archive.py b/fs_uae_wrapper/archive.py index d70b975..d536af5 100644 --- a/fs_uae_wrapper/archive.py +++ b/fs_uae_wrapper/archive.py @@ -6,13 +6,12 @@ the temporary one. """ import os import shutil -import sys from fs_uae_wrapper import base from fs_uae_wrapper import utils -class Archive(base.Base): +class Archive(base.ArchiveBase): """ Class for performing extracting archive, copying emulator files, and cleaning it back again @@ -30,12 +29,6 @@ class Archive(base.Base): - copy configuration - run the emulation - 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(): return False @@ -61,17 +54,6 @@ class Archive(base.Base): 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): """ Produce archive and save it back. Than remove old one. diff --git a/fs_uae_wrapper/base.py b/fs_uae_wrapper/base.py index 5dc44d8..60cf49a 100644 --- a/fs_uae_wrapper/base.py +++ b/fs_uae_wrapper/base.py @@ -29,7 +29,6 @@ class Base(object): fsuae_options) self.dir = None self.save_filename = None - self.arch_filepath = None def run(self): """ @@ -46,7 +45,6 @@ class Base(object): return False self.dir = tempfile.mkdtemp() - self._set_assets_paths() return True @@ -93,12 +91,6 @@ class Base(object): conf_base = os.path.basename(self.conf_file) 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 arch_ext = utils.get_arch_ext(self.all_options.get('wrapper_archiver')) if arch_ext: @@ -112,16 +104,6 @@ class Base(object): os.path.join(self.dir, 'Config.fs-uae')) 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): """execute fs-uae in provided directory""" curdir = os.path.abspath('.') @@ -232,3 +214,56 @@ class Base(object): return False 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 diff --git a/fs_uae_wrapper/cd32.py b/fs_uae_wrapper/cd32.py index 62e8a91..a2b7b5d 100644 --- a/fs_uae_wrapper/cd32.py +++ b/fs_uae_wrapper/cd32.py @@ -8,12 +8,10 @@ used as a base for save state (it will append '_save.7z' to the archive file name. """ -import sys - from fs_uae_wrapper import base -class CD32(base.Base): +class CD32(base.ArchiveBase): """ Class for performing extracting archive, copying emulator files, and cleaning it back again @@ -53,16 +51,6 @@ class CD32(base.Base): 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): """Run fs-uae with provided config file and options""" diff --git a/setup.py b/setup.py index 23fe986..5c8f508 100755 --- a/setup.py +++ b/setup.py @@ -2,7 +2,7 @@ """ Setup for the fs-uae-wrapper """ -from distutils.core import setup +from setuptools import setup setup(name='fs-uae-wrapper', diff --git a/tests/test_archive.py b/tests/test_archive.py index 736e572..841747c 100644 --- a/tests/test_archive.py +++ b/tests/test_archive.py @@ -43,13 +43,13 @@ class TestArchive(TestCase): @mock.patch('tempfile.mkdtemp') @mock.patch('fs_uae_wrapper.path.which') @mock.patch('fs_uae_wrapper.archive.Archive._make_archive') - @mock.patch('fs_uae_wrapper.base.Base._save_save') - @mock.patch('fs_uae_wrapper.base.Base._get_saves_dir') - @mock.patch('fs_uae_wrapper.base.Base._run_emulator') - @mock.patch('fs_uae_wrapper.base.Base._kickstart_option') - @mock.patch('fs_uae_wrapper.base.Base._copy_conf') - @mock.patch('fs_uae_wrapper.base.Base._load_save') - @mock.patch('fs_uae_wrapper.base.Base._extract') + @mock.patch('fs_uae_wrapper.base.ArchiveBase._save_save') + @mock.patch('fs_uae_wrapper.base.ArchiveBase._get_saves_dir') + @mock.patch('fs_uae_wrapper.base.ArchiveBase._run_emulator') + @mock.patch('fs_uae_wrapper.base.ArchiveBase._kickstart_option') + @mock.patch('fs_uae_wrapper.base.ArchiveBase._copy_conf') + @mock.patch('fs_uae_wrapper.base.ArchiveBase._load_save') + @mock.patch('fs_uae_wrapper.base.ArchiveBase._extract') def test_run(self, extract, load_save, copy_conf, kick_option, run_emulator, get_save_dir, save_state, make_arch, which, mkdtemp): @@ -102,8 +102,8 @@ class TestArchive(TestCase): @mock.patch('os.unlink') @mock.patch('shutil.rmtree') @mock.patch('fs_uae_wrapper.utils.create_archive') - @mock.patch('fs_uae_wrapper.base.Base._get_title') - @mock.patch('fs_uae_wrapper.base.Base._get_saves_dir') + @mock.patch('fs_uae_wrapper.base.ArchiveBase._get_title') + @mock.patch('fs_uae_wrapper.base.ArchiveBase._get_saves_dir') def test_make_archive(self, sdir, title, carch, rmt, unlink, rename): sdir.return_value = None diff --git a/tests/test_base.py b/tests/test_base.py index ce2346e..dd84973 100644 --- a/tests/test_base.py +++ b/tests/test_base.py @@ -99,34 +99,6 @@ class TestBase(TestCase): self.assertTrue(os.path.exists(os.path.join(self.dirname, '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') def test_run_emulator(self, run): @@ -247,12 +219,15 @@ class TestBase(TestCase): self.assertTrue(bobj._validate_options()) bobj.all_options = {'wrapper': 'dummy', - 'wrapper_archiver': 'myarchiver'} + 'wrapper_save_state': '0'} self.assertTrue(bobj._validate_options()) bobj.all_options = {'wrapper': 'dummy', - 'wrapper_save_state': '1', - 'wrapper_archiver': 'myarchiver'} + 'wrapper_archiver': 'rar'} + self.assertTrue(bobj._validate_options()) + + bobj.all_options = {'wrapper': 'dummy', + 'wrapper_save_state': '1'} self.assertFalse(bobj._validate_options()) which.return_value = '7z' @@ -276,9 +251,96 @@ class TestBase(TestCase): self.assertFalse(bobj.run()) bobj.all_options = {'wrapper': 'dummy', - 'wrapper_archiver': 'rar'} + 'wrapper_archiver': 'rar', + 'wrapper_archive': 'foo.7z'} try: self.assertTrue(bobj.run()) self.assertTrue(os.path.exists(bobj.dir)) finally: 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()) diff --git a/tests/test_cd32.py b/tests/test_cd32.py index c9b90bb..4747034 100644 --- a/tests/test_cd32.py +++ b/tests/test_cd32.py @@ -11,29 +11,15 @@ from fs_uae_wrapper import utils 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('fs_uae_wrapper.path.which') - @mock.patch('fs_uae_wrapper.base.Base._save_save') - @mock.patch('fs_uae_wrapper.base.Base._get_saves_dir') - @mock.patch('fs_uae_wrapper.base.Base._run_emulator') - @mock.patch('fs_uae_wrapper.base.Base._kickstart_option') - @mock.patch('fs_uae_wrapper.base.Base._copy_conf') - @mock.patch('fs_uae_wrapper.base.Base._load_save') - @mock.patch('fs_uae_wrapper.base.Base._extract') + @mock.patch('fs_uae_wrapper.base.ArchiveBase._save_save') + @mock.patch('fs_uae_wrapper.base.ArchiveBase._get_saves_dir') + @mock.patch('fs_uae_wrapper.base.ArchiveBase._run_emulator') + @mock.patch('fs_uae_wrapper.base.ArchiveBase._kickstart_option') + @mock.patch('fs_uae_wrapper.base.ArchiveBase._copy_conf') + @mock.patch('fs_uae_wrapper.base.ArchiveBase._load_save') + @mock.patch('fs_uae_wrapper.base.ArchiveBase._extract') def test_run(self, extract, load_save, copy_conf, kick_option, run_emulator, get_save_dir, save_state, which, mkdtemp):