From 9bbe39b0593c44a6e02fcf13a7a862fe25fcbfbb Mon Sep 17 00:00:00 2001 From: gryf Date: Sun, 25 Dec 2016 16:21:57 +0100 Subject: [PATCH] Extracted some methods to utils module --- fs_uae_wrapper/plain.py | 10 ++++--- fs_uae_wrapper/utils.py | 61 +++++++++++++++++++++++++++++++++++++++ fs_uae_wrapper/wrapper.py | 4 ++- 3 files changed, 70 insertions(+), 5 deletions(-) create mode 100644 fs_uae_wrapper/utils.py diff --git a/fs_uae_wrapper/plain.py b/fs_uae_wrapper/plain.py index 3853773..da11661 100644 --- a/fs_uae_wrapper/plain.py +++ b/fs_uae_wrapper/plain.py @@ -11,17 +11,19 @@ import sys class Plain(object): """Class for run fs-uae""" - def run(self, conf, fs_uae_options): + def run(self, conf_file, fs_uae_options): """ Run the emulation. - conf is a path to the configuration, + conf_file is a path to the configuration, fs_uae_options is a list contains tokenized options to be passed to fs-uae """ try: - subprocess.call(['fs-uae'] + [conf] + fs_uae_options) + subprocess.call(['fs-uae'] + [conf_file] + fs_uae_options) except subprocess.CalledProcessError: sys.stderr.write('Warning: fs-uae returned non 0 exit code\n') + return False + return True def clean(self): """In this class, do nothing on exit""" @@ -33,6 +35,6 @@ def run(config_file, fs_uae_options, _, unused): runner = Plain() try: - runner.run(config_file, fs_uae_options) + return runner.run(config_file, fs_uae_options) finally: runner.clean() diff --git a/fs_uae_wrapper/utils.py b/fs_uae_wrapper/utils.py new file mode 100644 index 0000000..a5a5a77 --- /dev/null +++ b/fs_uae_wrapper/utils.py @@ -0,0 +1,61 @@ +""" +Misc utilities +""" +import os +import subprocess +import sys + +from fs_uae_wrapper.wrapper import WRAPPER_KEY + + +ARCHIVERS = {'.tar': ['tar', 'xf'], + '.tgz': ['tar', 'xf'], + '.tar.gz': ['tar', 'xf'], + '.tar.bz2': ['tar', 'xf'], + '.tar.xz': ['tar', 'xf'], + '.rar': ['unrar', 'x'], + '.7z': ['7z', 'x'], + '.zip': ['7z', 'x'], + '.lha': ['lha', 'x'], + '.lzx': ['unlzx']} + + +def extract_archive(arch_name): + """ + Extract provided archive to current directory + """ + + if not os.path.exists(arch_name): + sys.stderr.write("Archive `%s' doesn't exists.\n" % arch_name) + return False + + _, ext = os.path.splitext(arch_name) + + cmd = ARCHIVERS.get(ext) + + if cmd is None: + sys.stderr.write("Unable find archive type for `%s'.\n" % arch_name) + return False + + try: + subprocess.check_call(cmd + [arch_name]) + except subprocess.CalledProcessError: + sys.stderr.write("Error during extracting archive `%s'.\n" % arch_name) + return False + + return True + + +def merge_options(configuration, wrapper_options): + """ + Merge dictionaries with wrapper options into one. Commandline options + have precedence. + """ + options = {} + for key, val in configuration: + if WRAPPER_KEY in key: + options[key] = val + + options.update(wrapper_options) + + return options diff --git a/fs_uae_wrapper/wrapper.py b/fs_uae_wrapper/wrapper.py index 3d2bd45..62dc83c 100644 --- a/fs_uae_wrapper/wrapper.py +++ b/fs_uae_wrapper/wrapper.py @@ -122,7 +122,9 @@ def run(): "exists.\n" % wrapper_module) sys.exit(3) - wrapper.run(config_file, fs_uae_options, wrapper_options, configuration) + if not wrapper.run(config_file, fs_uae_options, wrapper_options, + configuration): + sys.exit(4) if __name__ == "__main__":