#! /usr/bin/env python
# -*- coding: iso-8859-1 -*-
#-----------------------------------------------------------------------------
# Modeling Framework: an Object-Relational Bridge for python
#
# Copyright (c) 2001-2004 Sbastien Bigaret <sbigaret@users.sourceforge.net>
# All rights reserved.
#
# This file is part of the Modeling Framework.
#
# This code is distributed under a "3-clause BSD"-style license;
# see the LICENSE file for details.
#-----------------------------------------------------------------------------
"""
TBD docstring
"""
__version__='$Revision: 932 $'[11:-2]
from Modeling import ModelValidation
from Modeling.ModelValidation import NOT_SUPPORTED,ERROR,WARNING,INFO,DEBUG
import getopt, sys
def log(msg):
if not quiet_mode:
sys.stderr.write(msg+'\n')
def tracebackInfoFromStack(exc_traceback):
from traceback import extract_tb,format_list
from Modeling.utils import isListOrTuple
tb=None
str='%s\n%s\n'%sys.exc_info()[:2]
try:
tb=sys.exc_info()[-1]
str+=reduce(lambda a,b: a+b, format_list(extract_tb(tb)))
return str
finally:
del tb
def usage(prgName):
_usage="""
%s [options] model.(xml|py)
Validate a model. Errors and warnings messages are written on sys.stderr.
The model file can be either a xml model file or a PyModel.
Return status:
0 if the model is valid,
1 if the model has errors (no matter whether there are warnings)
2 if some warnings were issued (except with --ignore-warnings)
3 in case the model cannot be loaded
Options:
--------
Default is to print informational, warning and error messages
-q --quiet ignore info, only prints warnings and errors
-w --ignore-warnings only print errors (see also: Return status above)
-Q really quiet: do not print anything
-h --help gives this help
-v --verbose prints extra info. on the runtime steps
""" % prgName
sys.stderr.write(_usage)
def validate_model(model, ignore_levels=None):
"""
"""
errors=MV.ModelValidationException(ignore_levels=ignore_levels)
MV.validateModel(model, errors)
return errors
# Global variable
quiet_mode=0
def main(args):
me=args[0]
try: options, args = getopt.getopt(sys.argv[1:],
'hQqvw',
["help", "quiet", "verbose",
"ignore-warnings",])
except: usage(me); return 1
global quiet_mode
ignore_warnings=verbose=0
ignore_levels=[DEBUG]
for k, v in options:
if k in ('-h', '--help'): usage(me); return 0
if k in ('-Q', ): quiet_mode=1; verbose=0; continue
if k in ('-q', '--quiet'):
ignore_levels.append(INFO)
continue
if k in ('-v', '--verbose'): verbose=1; continue
if k in ('-w', '--ignore_warnings'):
ignore_levels.append(INFO)
ignore_levels.append(WARNING)
ignore_warnings=1
continue
if len(args)!=1: usage(me) ; return 1
model_file=args[0]
# load the model
if verbose: log("Loading the model...")
try:
from Modeling import Model
model=Model.loadModel(model_file)
if not model:
raise RuntimeError, "Abnormal: got no exception but Model is None"
except Exception, exc:
log("Serious: couldn't load the model")
log(tracebackInfoFromStack(exc))
return 3
if verbose: log("Done.")
if verbose: log("Validating...")
errors=validate_model(model, ignore_levels)
if verbose: log("Done.")
log(str(errors))
err_levels=errors.levels_of_errors()
if not errors.levels_of_errors() or err_levels==[INFO]:
return 0
try: err_levels.remove(INFO)
except ValueError: pass
if err_levels==[WARNING]:
if ignore_warnings: return 0
else: return 2
return 1
if __name__ == "__main__":
status = main(sys.argv)
sys.exit(status)
|