3 Dã\Êã@s@dZddlZddlZddlmZddlmZGdd„deƒZdS)a¼ Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. éN)ÚCloudFormationLintRule)Ú RuleMatchc@sHeZdZdZdZdZdZdZddgZdd „Z d d „Z d d „Z dd„Z dS)ÚCircularDependencyz-Check if Resources have a circular dependencyZE3004z&Resource dependencies are not circularzHCheck that Resources are not circularly dependent by Ref, Sub, or GetAttz5https://github.com/aws-cloudformation/cfn-python-lintÚ resourcesZ circularlycCstjdƒ}|j|ƒS)z"Search string for tokenized fieldsz\${([a-zA-Z0-9.]*)})ÚreÚcompileÚfindall)ÚselfÚstringÚregex©r ú„/private/var/folders/pf/wv4htv3x0qs2c2mp0dnn0kchsvlck3/T/pip-install-emcbgzcf/cfn-lint/cfnlint/rules/resources/CircularDependency.pyÚ searchstring s zCircularDependency.searchstringcCs`g}||kr||kr|SgS|j|ƒ}|r\x0|D](}|j|j|||dd…|g|ƒƒq0W|S)zCheck resource association N)ÚgetÚextendÚ_check_circular_dependency)r rZstarting_resourceZfound_resourcesÚ associationÚresultZassociation_resourcesZassociation_resourcer r r r%s   z-CircularDependency._check_circular_dependencyc Cs´g}xª|jƒD]ž\}}g}xR|D]J}t|j|||g|ƒƒ}d}|r x|D]} | |krHd}qHW|s |j|ƒq Wx<|D]4} d} d|g} |jt| | j|djtt| ƒƒƒƒƒqtWqW|S)z.Check circular dependencies one item at a timeFTzGCircular Dependencies for resource {0}. Circular dependency with [{1}]Ú Resourcesz, ) ÚitemsÚsetrÚappendrÚformatÚjoinÚmapÚstr) r rÚmatchesÚ resource_nameZ associationsZresource_resultsrÚresultsÚfoundZresource_resultÚmessageZ resource_treer r r Úcheck_circular_dependencies<s,    z.CircularDependency.check_circular_dependenciescCs<g}|jdƒ}i}xv|D]n}|d }t|tjtjtfƒr|dd…\}}|dkr|jjdiƒj|iƒr|j|ƒsxg||<||j|ƒqW|jdƒ} x†| D]~} | d }t|t ƒs²qšt |ƒdksÀqš|d}| dd…\} } | dkrš|jjdiƒj|iƒrš|j| ƒs g|| <|| j|ƒqšW|jdƒ} xþ| D]ö}g}i}|d }|dd…\} } t|t ƒrŽ|shq.t |ƒdkr~|d}|j |dƒ}nt|tjtjfƒr¬|j |ƒ}| dkr.xj|D]b}||kr¼d |krâ|j d ƒd}|jjdiƒj|iƒr¼|j| ƒsg|| <|| j|ƒq¼Wq.W|j |j|ƒƒ|S) zCheck CloudFormation ResourcesZReféNérz Fn::GetAttrzFn::SubÚ.éÿÿÿÿr%r%)Zsearch_deep_keysÚ isinstanceÚsixÚ text_typeÚ string_typesÚintÚtemplaterrÚlistÚlenrÚsplitrr!)r ZcfnrZref_objsrZref_objÚvalueZref_typeZref_nameZ getatt_objsZ getatt_objZres_typeZres_nameZsub_objsZsub_objZsub_parametersZsub_parameter_valuesZ sub_parameterr r r ÚmatchXsf                 zCircularDependency.matchN) Ú__name__Ú __module__Ú __qualname__Ú__doc__ÚidZ shortdescÚ descriptionZ source_urlÚtagsrrr!r0r r r r rsr)r4rr'Zcfnlintrrrr r r r Ús