Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
#! /usr/bin/env python3
import sys,os,time,datetime,logging
import argparse
from termcolor import colored

from CalibMuon.DTCalibration.Workflow.DTCalibrationWorker import DTCalibrationWorker


log = logging.getLogger(__name__)
log_choices = [ 'ERROR', 'WARNING', 'INFO', 'DEBUG' ]
date = '%F %H:%M:%S'
log_file_name = 'dtCalibration_'
log_file_name += datetime.datetime.now().strftime( "%Y-%m-%d_%H.%M.%S" ) + '.log'

def main():
    options = parse_command_line()
    # setup logging
    setupLogging(options)

    # echo the command line used to run calibration to the log file
    log.info("DT Calibration started with command:")
    log.info(" ".join(sys.argv))

    start = time.time()
    print(colored("[DT Calibration starting]", 'cyan', attrs = ["bold"]))
    print(f"Running at {options.working_dir}")

    #stdout_original = sys.stdout
    #sys.stdout = logOut

    dtCalibWorker = DTCalibrationWorker(options)
    local_path = dtCalibWorker.run()

    print(f"Job dir is {local_path}")
    print(f"Log file is {log_file_name}")
    # move the log file to the working directory to avoid cluttering up the base directory
    os.rename(log_file_name, local_path+'/'+log_file_name)

    #sys.stdout = stdout_original
    stop = time.time()
    print(colored("[DT Calibration finished]", 'green', attrs = ["bold"]))
    print(f"Time elapsed was {stop-start:.2f} seconds")

def setupLogging(options):
    #setup logging
    format = '%(levelname)s (%(name)s) [%(asctime)s]: %(message)s'
    #logging.basicConfig( level=logging.getLevelName(options.debug), format=format, datefmt=date )
    logging.basicConfig(filename=log_file_name, level=logging.DEBUG, format=format, datefmt=date )
    log.setLevel(logging.getLevelName(options.debug))
    #formatter = logging.Formatter( format )
    #hdlr = logging.FileHandler( log_file_name, mode='w' )
    #hdlr.setFormatter( formatter )
    #log.addHandler( hdlr )
    logging.getLogger('CRAB3').propagate = False  # Avoid any CRAB message to propagate up to the handlers of the root logger.


def parse_command_line():
    ''' Setup command line options using prog command [options] sheme '''
    # This is the main parser instance. This parser will be called to parse the passed args
    descr = "Main script to perform various DT calibration tasks.\n"
    descr += "Choose a workflow, in some cases a workflow mode and a command\n"
    descr += "example dtCalibration ttrig timeboxes submit"
    main_parser = argparse.ArgumentParser(description=descr)
    main_parser.add_argument( '--debug',
        metavar='LEVEL', default='INFO',
        choices=log_choices,
        help='Set the debug level. Allowed values: ' +
            ', '.join( log_choices ) + ' [default: %(default)s]' )
    DTCalibrationWorker.add_arguments( main_parser )

    return main_parser.parse_args()


if __name__ == '__main__':
    main()