Commit e06180e0 authored by Xavier Thompson's avatar Xavier Thompson

slapformat: WIP: add some logs and fix errors

parent d8846274
......@@ -42,6 +42,9 @@ from netifaces import AF_INET, AF_INET6
def do_format(conf):
# starting logs
conf.logStartInfo()
# wrap system calls for tracing and dry-run
with WrappedSystem(conf):
# load configuration
computer = Computer(conf)
......@@ -118,33 +121,45 @@ class FormatConfig(Parameters, Options):
def __init__(self, logger):
self.logger = logger
def wrong(self, fmt, *args):
return self.error(fmt, *args, exc=UsageError)
def error(self, fmt, *args):
message = fmt % tuple(args)
self.logger.error(message)
raise UsageError(message)
raise ValueError(message)
def warn(self, fmt, *args):
self.logger.warning(fmt, *args)
def info(self, fmt, *args):
self.logger.info(fmt, *args)
def debug(self, fmt, *args):
self.logger.debug(fmt, *args)
def parse(self, name, value, t):
if not isinstance(value, str):
if type(value).__name__ != t and value is not None:
self.error("Option %s takes type %s, not %r", name, t, value)
self.wrong("Option %s takes type %s, not %r", name, t, value)
return value
if t == 'int':
try:
return int(value)
except ValueError:
self.error("Option %s takes type %s, not %r", name, t, value)
self.wrong("Option %s takes type %s, not %r", name, t, value)
if t == 'bool':
try:
return bool(('false', 'true').index(value.lower()))
except IndexError:
self.error("Option %r must be 'true' or 'false', not %r", name, value)
self.wrong("Option %r must be 'true' or 'false', not %r", name, value)
return value
def get(self, option):
try:
return getattr(self, option)
except AttributeError:
self.error("Parameter %r is not defined", option)
self.wrong("Parameter %r is not defined", option)
def mergeConfig(self, args, configp):
# args (from command line) override configp (from cfg) options
......@@ -157,14 +172,14 @@ class FormatConfig(Parameters, Options):
for option in self.DEPRECATED:
if option in self.__dict__:
if option == 'computer_xml':
self.error(
self.wrong(
"Option %r is no longer supported\n"
"Use --output_definition_file to migrate existing computer_xml"
", then use the generated file as input_definition_file",
option
)
else:
self.error("Option %r is no longer supported" % option)
self.wrong("Option %r is no longer supported" % option)
# Mandatory parameters
for option, t in Parameters.__annotations__.items():
setattr(self, option, self.parse(option, self.get(option), t))
......@@ -176,15 +191,22 @@ class FormatConfig(Parameters, Options):
path = getattr(self, option)
if path:
if not os.path.exists(path):
self.error("File %r does not exist or is not readable", path)
self.wrong("File %r does not exist or is not readable", path)
setattr(self, option, os.path.abspath(path))
# Paths normalization
for option in self.NORMALIZE_PATHS:
path = getattr(self, option)
if path:
setattr(self, option, os.path.abspath(path))
# XXX Check command line tools
def logStartInfo(self):
if self.dry_run:
self.logger.info("Starting run in dry-run mode")
else:
self.logger.info("Starting run")
# XXX more logs
# XXX Check command line tools + Logs
class Definition(defaultdict):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment