Imported Upstream version 4.0.5
This commit is contained in:
@@ -22,31 +22,27 @@
|
||||
Handles common operations like option parsing and logging
|
||||
"""
|
||||
|
||||
import logging
|
||||
import sys
|
||||
import os
|
||||
import traceback
|
||||
from optparse import OptionGroup # pylint: disable=deprecated-module
|
||||
from optparse import OptionGroup
|
||||
|
||||
from ipapython import version
|
||||
from ipapython import config
|
||||
from ipapython.ipa_log_manager import standard_logging_setup
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
from ipapython import ipa_log_manager
|
||||
|
||||
|
||||
class ScriptError(Exception):
|
||||
class ScriptError(StandardError):
|
||||
"""An exception that records an error message and a return value
|
||||
"""
|
||||
def __init__(self, msg='', rval=1):
|
||||
if msg is None:
|
||||
msg = ''
|
||||
super(ScriptError, self).__init__(msg)
|
||||
self.msg = msg
|
||||
self.rval = rval
|
||||
|
||||
@property
|
||||
def msg(self):
|
||||
return str(self)
|
||||
def __str__(self):
|
||||
return self.msg
|
||||
|
||||
|
||||
class AdminTool(object):
|
||||
@@ -90,6 +86,7 @@ class AdminTool(object):
|
||||
usage = None
|
||||
description = None
|
||||
|
||||
log = None
|
||||
_option_parsers = dict()
|
||||
|
||||
@classmethod
|
||||
@@ -172,7 +169,7 @@ class AdminTool(object):
|
||||
self.ask_for_options()
|
||||
self.setup_logging()
|
||||
return_value = self.run()
|
||||
except BaseException as exception:
|
||||
except BaseException, exception:
|
||||
traceback = sys.exc_info()[2]
|
||||
error_message, return_value = self.handle_error(exception)
|
||||
if return_value:
|
||||
@@ -227,18 +224,11 @@ class AdminTool(object):
|
||||
- a plain print for things that should not be log (for example,
|
||||
interactive prompting)
|
||||
|
||||
To log, use a module-level logger.
|
||||
To log, use `self.log.info()`, `self.log.warning()`, etc.
|
||||
|
||||
Logging to file is only set up after option validation and prompting;
|
||||
before that, all output will go to the console only.
|
||||
"""
|
||||
root_logger = logging.getLogger()
|
||||
for handler in root_logger.handlers:
|
||||
if (isinstance(handler, logging.StreamHandler) and
|
||||
handler.stream is sys.stderr): # pylint: disable=no-member
|
||||
root_logger.removeHandler(handler)
|
||||
break
|
||||
|
||||
self._setup_logging(log_file_mode=log_file_mode)
|
||||
|
||||
def _setup_logging(self, log_file_mode='w', no_file=False):
|
||||
@@ -259,13 +249,14 @@ class AdminTool(object):
|
||||
verbose = False
|
||||
else:
|
||||
verbose = True
|
||||
standard_logging_setup(
|
||||
ipa_log_manager.standard_logging_setup(
|
||||
log_file_name, console_format=console_format,
|
||||
filemode=log_file_mode, debug=debug, verbose=verbose)
|
||||
self.log = ipa_log_manager.log_mgr.get_logger(self)
|
||||
if log_file_name:
|
||||
logger.debug('Logging to %s', log_file_name)
|
||||
self.log.debug('Logging to %s' % log_file_name)
|
||||
elif not no_file:
|
||||
logger.debug('Not logging to a file')
|
||||
self.log.debug('Not logging to a file')
|
||||
|
||||
|
||||
def handle_error(self, exception):
|
||||
@@ -290,20 +281,16 @@ class AdminTool(object):
|
||||
assumed to have run successfully, and the return value is used as the
|
||||
SystemExit code.
|
||||
"""
|
||||
logger.debug('%s was invoked with arguments %s and options: %s',
|
||||
self.command_name, self.args, self.safe_options)
|
||||
logger.debug('IPA version %s', version.VENDOR_VERSION)
|
||||
self.log.debug('%s was invoked with arguments %s and options: %s',
|
||||
self.command_name, self.args, self.safe_options)
|
||||
self.log.debug('IPA version %s' % version.VENDOR_VERSION)
|
||||
|
||||
def log_failure(self, error_message, return_value, exception, backtrace):
|
||||
logger.debug('%s', ''.join(traceback.format_tb(backtrace)))
|
||||
logger.debug('The %s command failed, exception: %s: %s',
|
||||
self.command_name, type(exception).__name__, exception)
|
||||
self.log.debug(''.join(traceback.format_tb(backtrace)))
|
||||
self.log.debug('The %s command failed, exception: %s: %s',
|
||||
self.command_name, type(exception).__name__, exception)
|
||||
if error_message:
|
||||
logger.error('%s', error_message)
|
||||
message = "The %s command failed." % self.command_name
|
||||
if self.log_file_name:
|
||||
message += " See %s for more information" % self.log_file_name
|
||||
logger.error('%s', message)
|
||||
self.log.error(error_message)
|
||||
|
||||
def log_success(self):
|
||||
logger.info('The %s command was successful', self.command_name)
|
||||
self.log.info('The %s command was successful', self.command_name)
|
||||
|
||||
Reference in New Issue
Block a user