3 =\.@sxddlmZddlZddlZddlZddlZddlZddlZddlm Z ddl m Z Gddde Z Gddde ZdS) )print_functionN) OrderedDict)TaskCatExceptionc @sdeZdZdZdddZddd Zd d Zd d ZddZddZ ddZ ddZ ddZ ddZ dS)Loggerz.Wrapper for a logging object that logs in jsonNjsonwarningcriticalcCs ||_||_|j|||ddS)z#Initializes logging with request_id)loglevel botolevelN) request_id log_formatconfig)selfr r r r rf/private/var/folders/pf/wv4htv3x0qs2c2mp0dnn0kchsvlck3/T/pip-install-emcbgzcf/taskcat/taskcat/utils.py__init__szLogger.__init__c Cs tt| jd} tt| jd} tj} | j| tjdj| tjdj| tjdj| tjdj| |jdkrd} n|jd krd } nd } t| jd kr| jtj | jd j tj | tj | i|_ ||_||_||_||_||_||_||_||_d S)aConfigures logging object Args: request_id (str): request id. original_job_id (str): [optional] pipeline job_id from first request in this run. job_id (str): [optional] pipeline job_id for the current invocation (differs from original_job_id if this is a continuation invocation). artifact_revision_id (str): [optional] commit id for current revision. pipeline_execution_id (str): [optional] pipeline execution id (same for all actions/stages in this pipeline run). pipeline_action (str): [optional] pipeline action name. stage_name (str): [optional] pipeline stage name. pipeline_name (str): [optional] pipeline name. loglevel (str): [optional] logging verbosity, defaults to warning. botolevel (str): [optional] boto logging verbosity, defaults to critical. (boto3botocorenose s3transferrzQ{"time_stamp": "%(asctime)s", "log_level": "%(levelname)s", "data": %(message)s} logfilez&%(asctime)s %(levelname)s %(message)s z %(message)s rN)getattrloggingupper getLoggersetLevelr lenhandlers addHandler StreamHandler setFormatter Formatter LoggerAdapterlogr original_job_idjob_idpipeline_execution_idartifact_revision_idpipeline_action stage_name pipeline_name) rr r&r'r)r(r*r+r,r r mainloggerZlogfmtrrrr s2   z Logger.configcCsTtt|jd}tjdj|tjdj|tjdj|tjdj|dS)zsets boto logging level Args: botolevel (str): boto3 logging verbosity (critical|error|warning|info|debug) rrrrrN)rrrrr)rr rrrset_boto_levelKs zLogger.set_boto_levelcCs*|jdkr|j|}nt|}t||S)Nr)r _format_jsonstrprint)rmessagerrr_formatYs   zLogger._formatcCsi}|jr|j|d<|jr$|j|d<|jr4|j|d<|jrD|j|d<|jrT|j|d<|jrd|j|d<|jrt|j|d<|jr|j|d<ytj |}Wn(t k rYnt k rYnXy||d <tj |St k rYn(t k r t ||d <tj |SXd S) zformats log message in json Args: message (str): log message, can be a dict, list, string, or json blob r r&r(r,r+r)r*r'r2N)r r&r(r,r+r)r*r'rloadsr Exceptiondumpsr0)rr2metadatarrrr/as>          zLogger._format_jsoncKs|jj|j|f|dS)zwrapper for logging.debug callN)r%debugr3)rr2kwargsrrrr8sz Logger.debugcKs|jj|j|f|dS)zwrapper for logging.info callN)r%infor3)rr2r9rrrr:sz Logger.infocKs|jj|j|f|dS)z wrapper for logging.warning callN)r%rr3)rr2r9rrrrszLogger.warningcKs|jj|j|f|dS)zwrapper for logging.error callN)r%errorr3)rr2r9rrrr;sz Logger.errorcKs|jj|j|f|dS)z!wrapper for logging.critical callN)r%rr3)rr2r9rrrrszLogger.critical)Nrrr) NNNNNNNNrr)__name__ __module__ __qualname____doc__rr r.r3r/r8r:rr;rrrrrrs  +'rc@sDeZdZdZd ddZeefddZed dd Zed d Z dS)CFNYAMLHandlera^Handles the loading and dumping of CloudFormation YAML templates. Example usage: from taskcat import utils class MyClass(object): def __init__(self): # init MyClass return def my_load_yaml_function(self, template_file): template_data = utils.CFNYAMLHandler.ordered_safe_load(open(template_file, 'rU'), object_pairs_hook=OrderedDict)) return template_data def my_dump_yaml_function(self, template_data, output_file): utils.CFNYAMLHandler.validate_output_dir(output_file) with open(output_file, 'wb') as updated_template: updated_template.write(utils.CFNYAMLHandler.ordered_safe_dump(template_data, indent=2, allow_unicode=True, default_flow_style=False, explicit_start=True, explicit_end=True)) updated_template.close() Nr;cCs|stt|jd}tt|jd}tj}|j|tjdj|tjdj|tjdj|tjdj|t|jdkr|jtjn||_ dS) z_Sets up the logging object Args: logger (obj): a logging instance rrrrrrrN) rrrrrrrr r!logger)rrAr r r-rrrrs zCFNYAMLHandler.__init__csdGdddtj}dd}fdd}dd}|jd ||jtjjj||jd |tj||S) Nc@s eZdZdS)z;CFNYAMLHandler.ordered_safe_load..OrderedSafeLoaderN)r<r=r>rrrrOrderedSafeLoadersrBc Ss@t|j|jdd}y t|dStk r:|j|SXdS)N_ )r0Zconstruct_scalarreplaceint ValueErrorZconstruct_yaml_int)loadernodevaluerrr_construct_int_without_octalss  zGCFNYAMLHandler.ordered_safe_load.._construct_int_without_octalscs|j||j|S)N)construct_mappingZconstruct_pairs)rIrJ)object_pairs_hookrr_construct_mappings z._construct_mappingcSsdj|}t|tjr@|jdkr0dj|d|jSdj||jSn6t|tjrT|j}n"t|tjrh|j }nt dj|t |||fgS)Nz!{}|z {0} {1} {2}z{0} {1}z[ERROR] Unknown tag_suffix: {}) format isinstanceyamlZ ScalarNodestylerKZ SequenceNodeZconstruct_sequenceZ MappingNoderM BaseExceptionr)rIZ tag_suffixrJ constructorrrr_construct_cfn_tags     z._construct_cfn_tagztag:yaml.org,2002:int!)rSZ SafeLoaderZadd_constructorresolver BaseResolverDEFAULT_MAPPING_TAGZadd_multi_constructorload)streamrNrBrLrOrWr)rNrordered_safe_loads   z CFNYAMLHandler.ordered_safe_loadcKsGdddtj}dd}dd}|jt||jdtjdtd |jt|d d |_ tj |fd |i|}tj d |}x:|rtj tj |jd|jdjd|}tj d |}qW|S)Nc@s eZdZdS)z;CFNYAMLHandler.ordered_safe_dump..OrderedSafeDumperN)r<r=r>rrrrOrderedSafeDumpersr_cSs|jtjjj|jS)N)Zrepresent_mappingrSrYrZr[items)dumper_datarrr_dict_representersz;CFNYAMLHandler.ordered_safe_dump.._dict_representercSsptjd|rBtjd|jd}|jt||jdddjddSt|j dkrb|jd|ddS|j |SdS)Nz !\w+\s+\|.+z^!\w+rrP)rTztag:yaml.org,2002:str) rematchsearchgroupZrepresent_scalarr0splitlstripr splitlinesZ represent_str)rarbtagrrr_str_representers  $z:CFNYAMLHandler.ordered_safe_dump.._str_representerztag:yaml.org,2002:intz^[-+]?[0-9][0-9_]*$z -+0123456789cSsdS)NTr)rdatarrrsz2CFNYAMLHandler.ordered_safe_dump..ZDumperz'!.*':?rz'":)rSZ SafeDumperZadd_representerrZadd_implicit_resolverrecompilelistr0Zignore_aliasesdumprgsubescaperhstrip)rnr]kwdsr_rcrmZ yaml_dumpkeywordrrrordered_safe_dumps    &z CFNYAMLHandler.ordered_safe_dumpcCsltjj|rtjj|d}tjj|sBtdj|tj|n&tj|tj shtdj|t j ddS)Nrz;[INFO]: Directory [{}] does not exist. Trying to create it.zD[ERROR]: No write access allowed to output directory [{}]. Aborting.rd) ospathisfileriisdirr1rQmakedirsaccessW_OKsysexit) directoryrrrvalidate_output_dirs   z"CFNYAMLHandler.validate_output_dir)Nr;r;)N) r<r=r>r?r staticmethodrr^rxrrrrrr@s & r@) __future__rrrryrrSre collectionsrZtaskcat.exceptionsrobjectrr@rrrr s