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

Use logging for displaying messages

Also added debug logs
This commit is contained in:
2017-01-08 13:29:23 +01:00
parent a918e4c9ff
commit 152446abbe
4 changed files with 38 additions and 30 deletions

View File

@@ -1,9 +1,10 @@
"""
Base class for all wrapper modules
"""
import logging
import os
import sys
import shutil
import sys
import tempfile
from fs_uae_wrapper import utils
@@ -117,7 +118,7 @@ class Base(object):
curdir = os.path.abspath('.')
if not utils.create_archive(self.save_filename, '', [save_path]):
sys.stderr.write('Error: archiving save state failed\n')
logging.error('Error: archiving save state failed.')
os.chdir(curdir)
return False
@@ -219,21 +220,20 @@ class Base(object):
def _validate_options(self):
"""Validate mandatory options"""
if 'wrapper' not in self.all_options:
sys.stderr.write("Configuration lacks of required "
"`wrapper' option.\n")
logging.error("Configuration lacks of required `wrapper' option.")
return False
if self.all_options.get('wrapper_save_state', '0') == '0':
return True
if 'wrapper_archiver' not in self.all_options:
sys.stderr.write("Configuration lacks of required "
"`wrapper_archiver' option.\n")
logging.error("Configuration lacks of required "
"`wrapper_archiver' option.")
return False
if not path.which(self.all_options['wrapper_archiver']):
sys.stderr.write("Cannot find archiver `%s'." %
self.all_options['wrapper_archiver'])
logging.error("Cannot find archiver `%s'.",
self.all_options['wrapper_archiver'])
return False
return True

View File

@@ -3,8 +3,8 @@ File archive classes
"""
import os
import subprocess
import sys
import re
import logging
from fs_uae_wrapper import path
@@ -25,10 +25,12 @@ class Archive(object):
Create archive. Return True on success, False otherwise.
"""
files = files if files else ['.']
logging.debug("Calling `%s %s %s %s'.", self._compress,
" ".join(self.ADD), arch_name, " ".join(files))
result = subprocess.call([self._compress] + self.ADD + [arch_name]
+ files)
if result != 0:
sys.stderr.write("Unable to create archive `%s'\n" % arch_name)
logging.error("Unable to create archive `%s'.", arch_name)
return False
return True
@@ -37,13 +39,15 @@ class Archive(object):
Extract archive. Return True on success, False otherwise.
"""
if not os.path.exists(arch_name):
sys.stderr.write("Archive `%s' doesn't exists.\n" % arch_name)
logging.error("Archive `%s' doesn't exists.", arch_name)
return False
logging.debug("Calling `%s %s %s'.", self._compress,
" ".join(self.ADD), arch_name)
result = subprocess.call([self._decompress] + self.EXTRACT +
[arch_name])
if result != 0:
sys.stderr.write("Unable to extract archive `%s'\n" % arch_name)
logging.error("Unable to extract archive `%s'.", arch_name)
return False
return True
@@ -55,10 +59,12 @@ class TarArchive(Archive):
def create(self, arch_name, files=None):
files = files if files else sorted(os.listdir('.'))
logging.debug("Calling `%s %s %s %s'.", self._compress,
" ".join(self.ADD), arch_name, " ".join(files))
result = subprocess.call([self._compress] + self.ADD + [arch_name] +
files)
if result != 0:
sys.stderr.write("Unable to create archive `%s'\n" % arch_name)
logging.error("Unable to create archive `%s'.", arch_name)
return False
return True
@@ -101,8 +107,8 @@ class LzxArchive(Archive):
@classmethod
def create(self, arch_name, files=None):
sys.stderr.write('Cannot create LZX archive. Only extracting is'
'supported\n')
logging.error('Cannot create LZX archive. Only extracting is'
'supported.')
return False
@@ -112,14 +118,16 @@ class RarArchive(Archive):
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')
logging.error('Cannot create RAR archive. Only extracting is'
'supported by unrar.')
return False
logging.debug("Calling `%s %s %s %s'.", self._compress,
" ".join(self.ADD), arch_name, " ".join(files))
result = subprocess.call([self._compress] + self.ADD + [arch_name] +
files)
if result != 0:
sys.stderr.write("Unable to create archive `%s'\n" % arch_name)
logging.error("Unable to create archive `%s'.", arch_name)
return False
return True
@@ -174,13 +182,13 @@ def get_archiver(arch_name):
archiver = Archivers.get(ext)
if not archiver:
sys.stderr.write("Unable find archive type for `%s'\n" % arch_name)
logging.error("Unable find archive type for `%s'.", arch_name)
return None
archobj = archiver()
if archobj.archiver is None:
sys.stderr.write("Unable find executable for operating on files"
" `*%s'\n" % ext)
logging.error("Unable find executable for operating on files `*%s'.",
ext)
return None
return archobj

View File

@@ -2,9 +2,9 @@
Misc utilities
"""
from distutils import spawn
import logging
import os
import subprocess
import sys
try:
import configparser
except ImportError:
@@ -115,10 +115,10 @@ def run_command(cmd):
if not isinstance(cmd, list):
cmd = cmd.split()
logging.debug("Executing `%s'.", " ".join(cmd))
code = subprocess.call(cmd)
if code != 0:
sys.stderr.write('Command `{0}` returned non 0 exit '
'code\n'.format(cmd[0]))
logging.error('Command `%s` returned non 0 exit code.', cmd[0])
return False
return True

View File

@@ -62,7 +62,7 @@ def parse_args():
def usage():
"""Print help"""
sys.stdout.write("Usage: %s [conf-file] [fs-uae-option...]\n\n"
sys.stdout.write("Usage: %s [conf-file] [-v] [-q] [fs-uae-option...]\n\n"
% sys.argv[0])
sys.stdout.write("Config file is not required, if `Config.fs-uae' "
"exists in the current\ndirectory, although it might "
@@ -85,14 +85,14 @@ def run():
sys.exit(0)
if not config_file:
sys.stderr.write('Error: Configuration file not found\nSee --help'
' for usage\n')
logging.error('Error: Configuration file not found. See --help'
' for usage')
sys.exit(1)
configuration = utils.get_config_options(config_file)
if configuration is None:
sys.stderr.write('Error: Configuration file have syntax issues\n')
logging.error('Error: Configuration file have syntax issues')
sys.exit(2)
wrapper_module = fsuae_options.get(WRAPPER_KEY)
@@ -106,8 +106,8 @@ def run():
wrapper = importlib.import_module('fs_uae_wrapper.' +
wrapper_module)
except ImportError:
sys.stderr.write("Error: provided wrapper module: `%s' doesn't "
"exists.\n" % wrapper_module)
logging.error("Error: provided wrapper module: `%s' doesn't "
"exists.", wrapper_module)
sys.exit(3)
runner = wrapper.Wrapper(config_file, fsuae_options, configuration)