a )g}9@sddlmZmZmZeZddlmZddlZddl m m m Z ddlmZGdddeZGdddeZGd d d eZGd d d Zd#d dZddZddZddZddZddZddZddZddZdd Z d$d!d"Z!dS)%)absolute_importdivisionprint_function) defaultdictN)timeoutc@seZdZdZdS)CycleFoundInFactDepsa Indicates there is a cycle in fact collector deps If collector-B requires collector-A, and collector-A requires collector-B, that is a cycle. In that case, there is no ordering that will satisfy B before A and A and before B. That will cause this error to be raised. N)__name__ __module__ __qualname____doc__r r H/usr/lib/python3.9/site-packages/ansible/module_utils/facts/collector.pyr)src@s eZdZdS)UnresolvedFactDepNrr r r r r r r4src@s eZdZdS)CollectorNotFoundErrorNrr r r r r8src@sZeZdZeZdZdZeZdddZe ddZ ddZ d d Z dd d Z dd dZdS)BaseFactCollectorGenericNcCs0|pg|_||_t|jg|_|j|jdS)zzBase class for things that collect facts. 'collectors' is an optional list of other FactCollectors for composing.N) collectors namespacesetnameZfact_idsupdate _fact_ids)selfrrr r r __init__Cs zBaseFactCollector.__init__cCs|dd|jkr|SdS)Nsystem)get _platform)cls platform_infor r r platform_matchPsz BaseFactCollector.platform_matchcCs|jr|j|S|SN)rZ transform)rZkey_namer r r _transform_nameVs z!BaseFactCollector._transform_namecCs.t|D]}||}||||<q |S)zKupdate a dicts keys to use new names as transformed by self._transform_name)listkeysr"pop)rZ fact_dictZold_keyZnew_keyr r r _transform_dict_keys[s z&BaseFactCollector._transform_dict_keyscCs"|j||d}|jr||}|S)N)modulecollected_facts)collectrr&rr'r(Z facts_dictr r r collect_with_namespacees z(BaseFactCollector.collect_with_namespacecCsi}|S)aBdo the fact collection 'collected_facts' is a object (a dict, likely) that holds all previously facts. This is intended to be used if a FactCollector needs to reference another fact (for ex, the system arch) and should not be modified (usually). Returns a dict of facts. r r*r r r r)ls zBaseFactCollector.collect)NN)NN)NN)rr r rrrrrequired_factsr classmethodr r"r&r+r)r r r r r<s   rc CsL|pdg}|pt}|pt}|p(tt}t}t}dg}||t}|D]} | } | dkrl||qP| dkr||qP| dr| dd} | dkr||qP| dkr|||qPd} nd} | r||| t|| qP| |vrtd| d t |f|| || qP|s:||| |||S) aEreturn a set of FactCollector names based on gather_subset spec. gather_subset is a spec describing which facts to gather. valid_subsets is a frozenset of potential matches for gather_subset ('all', 'network') etc minimal_gather_subsets is a frozenset of matches to always use, even for gather_subset='!all' allmin!NTFzHBad subset '%s' given to Ansible. gather_subset options allowed: all, %sz, ) frozensetrrextendr startswithradd TypeErrorjoinsorteddifference_update) valid_subsetsminimal_gather_subset gather_subset aliases_maprZadditional_subsetsZexclude_subsetsZgather_subset_with_minZexplicitly_addedZsubsetZ subset_idZexcluder r r get_collector_nameszsP               r>cCs\t}t}|D]F}d}|D]8}||}|s0q|j}||vr||||jqq|Sr!)rr rr5)all_collector_classescompat_platformsZfound_collectorsZfound_collectors_namesZcompat_platformr Zall_collector_class primary_namer r r find_collectors_for_platforms  rBcCs^tt}tt}|D]@}|j}||||jD] }||||||q2q||fSr!)rr#rrappendrr5)collectors_for_platformZfact_id_to_collector_mapr=collector_classrAZfact_idr r r build_fact_id_to_collector_maps rFcCsJt}g}|D]6}||g}|D] }||vr"||||q"q|Sr!)rrrCr5)collector_namesall_fact_subsetsZseen_collector_classesselected_collector_classescollector_namecollector_classesrEr r r select_collector_classess  rLcCsLt}z ||}Wnty0td|Yn0|D]}||jq6|S)NzFact collector "%s" not found)rKeyErrorrrr,)rJrHr,rKrEr r r _get_requires_by_collector_names  rNcCs:t}|D]*}t||}|D]}||vr||qq |S)zFind any collector names that have unresolved requires Returns a list of collector names that correspond to collector classes whose .requires_facts() are not in collector_names. )rrNr5)rGrH unresolvedrJr,Z required_factr r r find_unresolved_requires s rPcCsLt}g}|D]"}||vr&||q||q|rHtdd||S)Nzunresolved fact dep %s,)rr5rCrr7)Zunresolved_requiresrH new_namesZfailedrOr r r resolve_requiress  rScCsHtt}|D]6}t}||D]}|jD]}||q(q|||<q |Sr!)rrr,r5)rGrHdep_maprJZcollector_depsZ collectorZdepr r r build_dep_data+s   rUcCsng}|}|rjd}t|D]6\}}|D]}||vr,q q,d}||=|||fq |s td|q |S)NFTz?Unable to tsort deps, there was a cycle in the graph. sorted=%s)copyr#itemsrCr)rTZ sorted_listZ unsorted_mapZacyclicZnodeZedgesZedger r r tsort6srXcCs@|}|}t||}|tkr&qz8collector_classes_from_gather_subset..)r2platformrrZDEFAULT_GATHER_TIMEOUTZGATHER_TIMEOUTrrrBrFr$r>rYrUrXrL)r?r:r;r<Zgather_timeoutrr=r@rDrHZall_valid_subsetsrGZcomplete_collector_namesrTZ ordered_depsZordered_collector_namesrIr r r $collector_classes_from_gather_subset[s2          r_)NNNNN)NNNNNN)"Z __future__rrrtypeZ __metaclass__ collectionsrr^Z"ansible.module_utils.compat.typingZ module_utilscompattypingtZansible.module_utils.factsr Exceptionr ValueErrorrrMrrr>rBrFrLrNrPrSrUrXrYr_r r r r s>   > O