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

Parametrize extract and create methods for Archive base class

This commit is contained in:
2017-01-02 20:17:13 +01:00
parent 19acb789b6
commit 994768806c
5 changed files with 46 additions and 31 deletions

View File

@@ -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"""

View File

@@ -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

View File

@@ -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):