From 994768806c1e88dca37604bb2f77eaab1abfae9c Mon Sep 17 00:00:00 2001 From: gryf Date: Mon, 2 Jan 2017 20:17:13 +0100 Subject: [PATCH] Parametrize extract and create methods for Archive base class --- fs_uae_wrapper/base.py | 21 ++++++++++++++++----- fs_uae_wrapper/file_archive.py | 13 ++++++++----- fs_uae_wrapper/utils.py | 12 ++++++------ tests/test_base.py | 20 ++++++++++---------- tests/test_utils.py | 11 ++++++----- 5 files changed, 46 insertions(+), 31 deletions(-) diff --git a/fs_uae_wrapper/base.py b/fs_uae_wrapper/base.py index bd0dd81..8571232 100644 --- a/fs_uae_wrapper/base.py +++ b/fs_uae_wrapper/base.py @@ -97,7 +97,9 @@ class Base(object): else: self.arch_filepath = os.path.join(conf_abs_dir, arch) # 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): """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 """ + os.chdir(self.dir) save_path = self._get_saves_dir() if not save_path: return True @@ -148,10 +151,14 @@ class Base(object): if os.path.exists(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') + os.chdir(curdir) return False + os.chdir(curdir) return True def _load_save(self): @@ -163,17 +170,18 @@ class Base(object): curdir = os.path.abspath('.') os.chdir(self.dir) - utils.run_command(['7z', 'x', self.save_filename]) + utils.extract_archive(self.save_filename) os.chdir(curdir) return True 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 - save state dir is set globally - save state dir is set relative to the config file - save state dir doesn't exists + Note, that returned path is relative not absolute """ if not self.all_options.get('save_states_dir'): return None @@ -188,7 +196,10 @@ class Base(object): if not os.path.exists(save_path) or not os.path.isdir(save_path): return None - return save_path + if save.endswith('/'): + save = save[:-1] + + return save def _validate_options(self): """Validate mandatory options""" diff --git a/fs_uae_wrapper/file_archive.py b/fs_uae_wrapper/file_archive.py index 605f861..171c003 100644 --- a/fs_uae_wrapper/file_archive.py +++ b/fs_uae_wrapper/file_archive.py @@ -20,11 +20,13 @@ class Archive(object): self._compess = 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. """ - 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: sys.stderr.write("Unable to create archive `%s'\n" % arch_name) return False @@ -89,7 +91,7 @@ class LzxArchive(Archive): ARCH = 'unlzx' @classmethod - def create(self, arch_name): + def create(self, arch_name, files=None): sys.stderr.write('Cannot create LZX archive. Only extracting is' 'supported\n') return False @@ -98,14 +100,15 @@ class LzxArchive(Archive): class RarArchive(Archive): 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': sys.stderr.write('Cannot create RAR archive. Only extracting is' 'supported by unrar.\n') return False result = subprocess.call([self._compess] + self.ADD + [arch_name] + - sorted(os.listdir('.'))) + files) if result != 0: sys.stderr.write("Unable to create archive `%s'\n" % arch_name) return False diff --git a/fs_uae_wrapper/utils.py b/fs_uae_wrapper/utils.py index d6ab52c..db5e0d6 100644 --- a/fs_uae_wrapper/utils.py +++ b/fs_uae_wrapper/utils.py @@ -57,7 +57,7 @@ def get_config_options(conf): 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 """ @@ -77,31 +77,31 @@ def operate_archive(arch_name, operation, text): res = archiver.extract(arch_name) if operation == 'create': - res = archiver.create(arch_name) + res = archiver.create(arch_name, params) msg.close() return res -def create_archive(arch_name, title=''): +def create_archive(arch_name, title='', params=None): """ Create archive from contents of current directory """ msg = '' if 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 """ msg = '' if 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): diff --git a/tests/test_base.py b/tests/test_base.py index 0fded82..287eb5f 100644 --- a/tests/test_base.py +++ b/tests/test_base.py @@ -143,14 +143,14 @@ class TestBase(TestCase): run.assert_called_once_with(['fs-uae']) @mock.patch('fs_uae_wrapper.base.Base._get_saves_dir') - @mock.patch('fs_uae_wrapper.utils.run_command') - def test_save_save(self, run, saves_dir): + @mock.patch('fs_uae_wrapper.utils.create_archive') + def test_save_save(self, carch, saves_dir): bobj = base.Base('Config.fs-uae', utils.CmdOption(), {}) bobj.dir = self.dirname bobj.save_filename = 'foobar_save.7z' saves_dir.bobj.save_filenamereturn_value = None - run.return_value = True + carch.return_value = True self.assertTrue(bobj._save_save()) @@ -164,16 +164,16 @@ class TestBase(TestCase): os.mkdir(os.path.join(self.dirname, 'fs-uae-save')) self.assertTrue(bobj._save_save()) - run.return_value = False + carch.return_value = False self.assertFalse(bobj._save_save()) - @mock.patch('fs_uae_wrapper.utils.run_command') - def test_load_save(self, run): + @mock.patch('fs_uae_wrapper.utils.extract_archive') + def test_load_save(self, earch): bobj = base.Base('Config.fs-uae', utils.CmdOption(), {}) bobj.dir = self.dirname bobj.save_filename = "foobar_save.7z" - run.return_value = 0 + earch.return_value = 0 # fail to load save is not fatal self.assertTrue(bobj._load_save()) @@ -183,11 +183,11 @@ class TestBase(TestCase): fobj.write('asd') 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 - run.reset_mock() - run.return_value = 1 + earch.reset_mock() + earch.return_value = 1 self.assertTrue(bobj._save_save()) def test_get_saves_dir(self): diff --git a/tests/test_utils.py b/tests/test_utils.py index 24a9e1a..0a86e1a 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -83,13 +83,14 @@ class TestUtils(TestCase): which.return_value = None # 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 with open('unsupported-archive.ace', 'w') as fobj: fobj.write("\n") self.assertFalse(utils.operate_archive('unsupported-archive.ace', - 'foo', '')) + 'foo', '', None)) # archive is known, but extraction will fail - we have an empty # 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: fobj.write("\n") self.assertTrue(utils.operate_archive('supported-archive.7z', - 'extract', '')) + 'extract', '', None)) extract.assert_called_once() extract.reset_mock() self.assertTrue(utils.operate_archive('supported-archive.7z', - 'extract', '')) + 'extract', '', None)) extract.assert_called_once() os.unlink('supported-archive.7z') self.assertTrue(utils.operate_archive('supported-archive.7z', - 'create', 'test')) + 'create', 'test', ['foo'])) create.assert_called_once() show.assert_called_once()