Source code for idmtools_platform_comps.utils.ssmt_utils.common
"""idmtools common ssmt tools.Copyright 2021, Bill & Melinda Gates Foundation. All rights reserved."""importjsonimportosimporttracebackfromargparseimportNamespacefromloggingimportgetLogger,DEBUGfromCOMPSimportClientfromidmtools.core.exceptionsimportidmtools_error_handlerlogger=getLogger(__name__)user_logger=getLogger('user')
[docs]defensure_debug_logging():"""Ensure we have debug logging enabled in idmtools."""# set to debug before loading idmtoolsos.environ['IDMTOOLS_LOGGING_LEVEL']='DEBUG'os.environ['IDMTOOLS_LOGGING_CONSOLE']='on'fromidmtools.core.loggingimportsetup_logging,IdmToolsLoggingConfigsetup_logging(IdmToolsLoggingConfig(level=DEBUG,console=True,force=True))# Import idmtools here to enable loggingfromidmtoolsimport__version__logger.debug(f"Using idmtools {__version__}")
[docs]defsetup_verbose(args:Namespace):"""Setup verbose logging for ssmt."""print(args)ifargs.verbose:ensure_debug_logging()logger.debug(f"Args: {args}")
[docs]deflogin_to_env():"""Ensure we are logged in to COMPS client."""# load the work itemclient=Client()iflogger.isEnabledFor(DEBUG):logger.debug(f"Logging into {os.environ['COMPS_SERVER']}")client.login(os.environ['COMPS_SERVER'])returnclient
[docs]defget_error_handler_dump_config_and_error(job_config):""" Define our exception handler for ssmt. This exception handler writes a "error_reason.json" file to the job that contains error info with additional data. Args: job_config: Job config used to execute items Returns: Error handler for ssmt """defssmt_error_handler(exctype,value:Exception,tb):withopen("error_reason.json",'w')aserr_out:output_error=dict(type=exctype.__name__,args=list(value.args),tb=traceback.format_tb(tb),job_config=job_config)output_error['tb']=[t.strip()fortinoutput_error['tb']]ifhasattr(value,'doc_link'):output_error['doc_link']=value.doc_linkjson.dump(output_error,err_out,indent=4,sort_keys=True)idmtools_error_handler(exctype,value,tb)returnssmt_error_handler