#! /usr/bin/env python # ------------------------------------------------ # help # ------------------------------------------------ """ TTB - TM Test Bench Description Test bench to pevent ttb's (ticking time bombs) in the model. History 2010, Arjo Segers """ # ------------------------------------------------ # main # ------------------------------------------------ if __name__ == "__main__" : # ----------------------------------------------- # external # ----------------------------------------------- # standard modules: import sys import os import logging import optparse import traceback import numpy # ----------------------------------------------- # toolboxes # ----------------------------------------------- import rc import ttb_restart import ttb_change # ----------------------------------------------- # arguments # ----------------------------------------------- # set text for 'usage' help line: usage = "%prog [OPTIONS] " # initialise the option parser: parser = optparse.OptionParser(usage=usage) # define options: parser.add_option( "-v", "--verbose", help="print extra logging messages to standard output", dest="verbose", action="store_true", default=False ) parser.add_option( "-t", "--test-only", help="do not run model, only apply tests to output", dest="test_only", action="store_true", default=False ) parser.add_option( "-n", "--new", help="new build", dest="build_new", action="store_true", default=False ) parser.add_option( "-j", "--jobs", help="number of processes that could be used by make", dest="build_jobs", action="store", default='' ) # now parse the actual arguments; # return an object 'opts' with fields 'verbose' etc, # and the unnamed arguments in the list 'args' : opts,args = parser.parse_args() # only one command ... if len(args) != 1 : if opts.verbose : logger.error( 'single argument rcfile should be specified, found : %i' % len(args) ) parser.print_usage() sys.exit(1) #endif # extract ... rcfile = args[0] # ----------------------------------------------- # logging # ----------------------------------------------- # create logger logger = logging.getLogger('ttb') # log everything: logger.setLevel(logging.DEBUG) # standard format: #logformat = '%(filename)s:%(lineno)s - %(levelname)s - %(message)s' logformat = '[%(levelname)-8s] %(message)s' # create standard output handler: stdout_handler = logging.StreamHandler(sys.stdout) # set level to debug or info: logging_level = logging.INFO if opts.verbose : logging_level = logging.DEBUG stdout_handler.setLevel(logging_level) # set format: stdout_handler.setFormatter(logging.Formatter(logformat)) # add: logger.addHandler(stdout_handler) # ----------------------------------------------- # begin # ----------------------------------------------- # info ... logger.info( '' ) logger.info( 'TTB - TM Test Bench' ) logger.info( '' ) # open rcfile: rcf = rc.RcFile( rcfile ) # replace options: if opts.test_only : rcf.replace( 'ttb.no-run', 'True' ) # tests to be performed: tests = rcf.get( 'ttb.tests' ).split() # no failures yet ... failure = False # loop over tests: for test in tests : # call proper routine: if test == 'restart' : # restart test: try : ttb_restart.Test( rcf ) except ValueError : sys.exit(1) except : for line in traceback.format_exc().split('\n') : logger.error(line) logger.error( 'received exception from test "%s" ...' % test ) failure = True #endif elif test == 'change' : # restart test: try : ttb_change.Test( rcf, build_jobs=opts.build_jobs, build_new=opts.build_new ) except ValueError : sys.exit(1) except : for line in traceback.format_exc().split('\n') : logger.error(line) logger.error( 'received exception from test "%s" ...' % test ) failure = True #endif else : # unknown ... logger.error( 'unsupported test : %s' % test ) sys.exit(1) #endif #endif # failed ? if failure : logger.error( 'Tests failed.' ) sys.exit(1) #endif # info ... logger.info( '' ) logger.info( 'end' ) logger.info( '' ) # ok: #sys.exit(0) #endif # ------------------------------------------------ # end # ------------------------------------------------