1
0
mirror of https://github.com/gryf/fs-uae-wrapper.git synced 2025-12-19 04:20:23 +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 Base class for all wrapper modules
""" """
import logging
import os import os
import sys
import shutil import shutil
import sys
import tempfile import tempfile
from fs_uae_wrapper import utils from fs_uae_wrapper import utils
@@ -117,7 +118,7 @@ class Base(object):
curdir = os.path.abspath('.') curdir = os.path.abspath('.')
if not utils.create_archive(self.save_filename, '', [save_path]): 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) os.chdir(curdir)
return False return False
@@ -219,20 +220,19 @@ class Base(object):
def _validate_options(self): def _validate_options(self):
"""Validate mandatory options""" """Validate mandatory options"""
if 'wrapper' not in self.all_options: if 'wrapper' not in self.all_options:
sys.stderr.write("Configuration lacks of required " logging.error("Configuration lacks of required `wrapper' option.")
"`wrapper' option.\n")
return False return False
if self.all_options.get('wrapper_save_state', '0') == '0': if self.all_options.get('wrapper_save_state', '0') == '0':
return True return True
if 'wrapper_archiver' not in self.all_options: if 'wrapper_archiver' not in self.all_options:
sys.stderr.write("Configuration lacks of required " logging.error("Configuration lacks of required "
"`wrapper_archiver' option.\n") "`wrapper_archiver' option.")
return False return False
if not path.which(self.all_options['wrapper_archiver']): if not path.which(self.all_options['wrapper_archiver']):
sys.stderr.write("Cannot find archiver `%s'." % logging.error("Cannot find archiver `%s'.",
self.all_options['wrapper_archiver']) self.all_options['wrapper_archiver'])
return False return False

View File

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

View File

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

View File

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