1
0
mirror of https://github.com/gryf/fs-uae-wrapper.git synced 2025-12-19 04:20:23 +01:00

Normalize paths for config options

Removing _kickstart_option in favor of _normalize_options method to
detect and replace relative paths with absolute ones. Dictionary with
changed options lands as a commandline switches for fs-uae during
execution.
This commit is contained in:
2017-01-07 12:47:47 +01:00
parent 853dca385e
commit 8892940339
8 changed files with 59 additions and 77 deletions

View File

@@ -24,7 +24,6 @@ class Archive(base.ArchiveBase):
"""
Main function which accepts configuration file for FS-UAE
It will do as follows:
- set needed full path for asset files
- extract archive file
- copy configuration
- run the emulation
@@ -41,10 +40,6 @@ class Archive(base.ArchiveBase):
if not self._copy_conf():
return False
kick_opts = self._kickstart_option()
if kick_opts:
self.fsuae_options.update(kick_opts)
if not self._run_emulator(self.fsuae_options.list()):
return False

View File

@@ -44,6 +44,7 @@ class Base(object):
if not self._validate_options():
return False
self._normalize_options()
self.dir = tempfile.mkdtemp()
self._set_assets_paths()
@@ -55,33 +56,6 @@ class Base(object):
shutil.rmtree(self.dir)
return
def _kickstart_option(self):
"""
This is kind of hack - since we potentially can have a relative path
to kickstart directory, there is a need for getting this option from
configuration files (which unfortunately can be spanned all over the
different places, see https://fs-uae.net/configuration-files) and
check whether or not one of 'kickstarts_dir', 'kickstart_file' or
'kickstart_ext_file' options are set. In either case if one of those
options are set and are relative, they should be set to absolute path,
so that kickstart files can be found by relocated configuration file.
"""
conf = utils.get_config(self.conf_file)
kick = {}
for key in ('kickstart_file', 'kickstart_ext_file', 'kickstarts_dir'):
val = conf.get(key)
if val:
if not os.path.isabs(val):
val = utils.interpolate_variables(val, self.conf_file)
kick[key] = os.path.abspath(val)
else:
kick[key] = val
return kick
def _set_assets_paths(self):
"""
Set full paths for archive file (without extension) and for save state
@@ -193,6 +167,35 @@ class Base(object):
return save
def _normalize_options(self):
"""
Search and replace values for options which starts with $CONFIG with
absolute path for all options, except:
- save_states_dir
Configuration file will be placed in new directory, therefore it is
needed to calculate new paths so that emulator can find assets.
"""
exclude_list = ['save_states_dir']
conf_abs_dir = os.path.dirname(os.path.abspath(self.conf_file))
changed_options = {}
for key, val in utils.get_config(self.conf_file).items():
if val.startswith('/'):
continue
if key in exclude_list:
continue
if val.startswith('$CONFIG'):
abspath = os.path.abspath(val.replace('$CONFIG', conf_abs_dir))
changed_options[key] = abspath
continue
changed_options[key] = os.path.abspath(val)
self.fsuae_options.update(changed_options)
def _validate_options(self):
"""Validate mandatory options"""
if 'wrapper' not in self.all_options:

View File

@@ -39,10 +39,6 @@ class CD32(base.ArchiveBase):
if not method():
return False
kick_opts = self._kickstart_option()
if kick_opts:
self.fsuae_options.update(kick_opts)
if not self._run_emulator(self.fsuae_options.list()):
return False

View File

@@ -39,10 +39,6 @@ class SaveState(base.Base):
if not self._copy_conf():
return False
kick_opts = self._kickstart_option()
if kick_opts:
self.fsuae_options.update(kick_opts)
if not self._run_emulator(self.fsuae_options.list()):
return False

View File

@@ -46,18 +46,15 @@ class TestArchive(TestCase):
@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):
def test_run(self, extract, load_save, copy_conf, run_emulator,
get_save_dir, save_state, make_arch, which, mkdtemp):
extract.return_value = False
load_save.return_value = False
copy_conf.return_value = False
kick_option.return_value = False
run_emulator.return_value = False
get_save_dir.return_value = False
save_state.return_value = False
@@ -82,10 +79,6 @@ class TestArchive(TestCase):
copy_conf.return_value = True
self.assertFalse(arch.run())
kick_option.return_value = {'foo': 'bar'}
self.assertFalse(arch.run())
self.assertDictEqual(arch.fsuae_options, {'foo': 'bar'})
run_emulator.return_value = True
self.assertFalse(arch.run())

View File

@@ -48,27 +48,32 @@ class TestBase(TestCase):
self.assertFalse(os.path.exists(self.dirname))
@mock.patch('fs_uae_wrapper.utils.get_config')
def test_kickstart_option(self, get_config):
def test_normalize_options(self, get_config):
bobj = base.Base('Config.fs-uae', utils.CmdOption(), {})
get_config.return_value = {'foo': 'bar'}
self.assertDictEqual(bobj._kickstart_option(), {})
get_config.return_value = {'kickstarts_dir': '/some/path'}
self.assertDictEqual(bobj._kickstart_option(),
{'kickstarts_dir': '/some/path'})
bobj._normalize_options()
self.assertDictEqual(bobj.fsuae_options, {})
os.chdir(self.dirname)
get_config.return_value = {'kickstarts_dir': '../some/path'}
get_config.return_value = {'foo': 'bar'}
bobj._normalize_options()
self.assertDictEqual(bobj.fsuae_options,
{'foo': os.path.join(self.dirname, 'bar')})
get_config.return_value = {'floppies_dir': '../some/path'}
bobj.fsuae_options = utils.CmdOption()
result = os.path.abspath(os.path.join(self.dirname, '../some/path'))
self.assertDictEqual(bobj._kickstart_option(),
{'kickstarts_dir': result})
bobj._normalize_options()
self.assertDictEqual(bobj.fsuae_options, {'floppies_dir': result})
bobj.conf_file = os.path.join(self.dirname, 'Config.fs-uae')
get_config.return_value = {'kickstarts_dir': '$CONFIG/../path'}
get_config.return_value = {'cdroms_dir': '$CONFIG/../path'}
bobj.fsuae_options = utils.CmdOption()
result = os.path.abspath(os.path.join(self.dirname, '../path'))
self.assertDictEqual(bobj._kickstart_option(),
{'kickstarts_dir': result})
bobj._normalize_options()
self.assertDictEqual(bobj.fsuae_options, {'cdroms_dir': result})
def test_set_assets_paths(self):
@@ -240,6 +245,12 @@ class TestBase(TestCase):
'wrapper_archiver': '7z'}
self.assertTrue(bobj._validate_options())
which.return_value = None
bobj.all_options = {'wrapper': 'dummy',
'wrapper_save_state': '1',
'wrapper_archiver': '7z'}
self.assertFalse(bobj._validate_options())
@mock.patch('fs_uae_wrapper.path.which')
def test_run_clean(self, which):

View File

@@ -16,17 +16,15 @@ class TestCD32(TestCase):
@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):
def test_run(self, extract, load_save, copy_conf, run_emulator,
get_save_dir, save_state, which, mkdtemp):
extract.return_value = False
copy_conf.return_value = False
load_save.return_value = False
kick_option.return_value = {}
run_emulator.return_value = False
get_save_dir.return_value = False
save_state.return_value = False
@@ -50,10 +48,6 @@ class TestCD32(TestCase):
load_save.return_value = True
self.assertFalse(acd32.run())
kick_option.return_value = {'foo': 'bar'}
self.assertFalse(acd32.run())
self.assertDictEqual(acd32.fsuae_options, {'foo': 'bar'})
run_emulator.return_value = True
self.assertTrue(acd32.run())

View File

@@ -12,7 +12,7 @@ from fs_uae_wrapper import savestate
from fs_uae_wrapper import utils
class TestArchive(TestCase):
class TestSaveState(TestCase):
def setUp(self):
self.dirname = mkdtemp()
@@ -31,14 +31,12 @@ class TestArchive(TestCase):
@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')
def test_run(self, load_save, copy_conf, kick_option, run_emulator,
get_save_dir, save_state, which, mkdtemp):
def test_run(self, load_save, copy_conf, run_emulator, get_save_dir,
save_state, which, mkdtemp):
copy_conf.return_value = False
kick_option.return_value = False
run_emulator.return_value = False
get_save_dir.return_value = False
save_state.return_value = False
@@ -58,10 +56,6 @@ class TestArchive(TestCase):
copy_conf.return_value = True
self.assertFalse(arch.run())
kick_option.return_value = {'foo': 'bar'}
self.assertFalse(arch.run())
self.assertDictEqual(arch.fsuae_options, {'foo': 'bar'})
run_emulator.return_value = True
self.assertTrue(arch.run())