o nnh0@sddlmZddlZddlZddlZddlZddlZddlmZddl m Z ddl m Z ddlmZddlmZddlmZmZdd lmZdd lmZdd lmZdd lmZeZGd ddeZGdddeZGddde Z dS)) annotationsN)abstractmethod)MutableMapping) constants) AnsibleError) S_IRWU_RG_RO)to_bytesto_text) AnsiblePlugin) cache_loader)resource_from_fqcr)DisplaycsteZdZeZfddZeddZeddZeddZ ed d Z ed d Z ed dZ eddZ ZS)BaseCacheModulecs tt||j||ddS)N) var_optionsdirect)superr__init__ set_optionsselfargskwargs __class__I/usr/local/lib/python3.10/dist-packages/ansible/plugins/cache/__init__.pyr.szBaseCacheModule.__init__cCdSNrrkeyrrrget2zBaseCacheModule.getcCrrrrrvaluerrrset6r!zBaseCacheModule.setcCrrrrrrrkeys:r!zBaseCacheModule.keyscCrrrrrrrcontains>r!zBaseCacheModule.containscCrrrrrrrdeleteBr!zBaseCacheModule.deletecCrrrr%rrrflushFr!zBaseCacheModule.flushcCrrrr%rrrcopyJr!zBaseCacheModule.copy)__name__ __module__ __qualname__display_displayrrr r$r&r'r(r)r* __classcell__rrrrr)s"       rcseZdZdZfddZddZddZdd Zd d Zd d Z ddZ ddZ ddZ ddZ ddZddZeddZeddZZS)BaseFileCacheModulez8 A caching module backed by file based storage. csztt|j|i|||d|_t|d|_Wnty4|t j |_tt j |_Ynwt |j |_i|_|dS)N_uri_timeout)rr1r_get_cache_connection get_option _cache_dirfloatr3KeyErrorCCACHE_PLUGIN_CONNECTIONCACHE_PLUGIN_TIMEOUTr r, plugin_name_cachevalidate_cache_connectionrrrrrSs   zBaseFileCacheModule.__init__cCs4|rz tjtj|WStyYdSwdSr)ospath expanduser expandvars TypeError)rsourcerrrr4`s z)BaseFileCacheModule._get_cache_connectionc Cs|js td|jtj|js6z t|jWdSttfy5}z td|j|jt |fd}~wwtj tj tj fD]}t |j|sQtd|j|jfq>dS)Nzwerror, '%s' cache plugin requires the 'fact_caching_connection' config option to be set (to a writeable directory path)zCerror in '%s' cache plugin while trying to create cache dir %s : %szeerror in '%s' cache, configured path (%s) does not have necessary permissions (rwx), disabling plugin)r6rr<r?r@existsmakedirsOSErrorIOErrorrR_OKW_OKX_OKaccess)rexrrrr>gs$z-BaseFileCacheModule.validate_cache_connectioncCs4|d}|rd|j||f}|Sd|j|f}|S)N_prefixz%s/%s%sz%s/%s)r5r6)rrprefix cachefilerrr_get_cache_file_namews z(BaseFileCacheModule._get_cache_file_namec Cs||jvrw||s|dkrt||}z ||}||j|<WnUtyE}ztd|j|t |f| |t d|d}~wt t fya}ztd|j|t |ftd}~wtyv}z t d|t |fd}~ww|j|S)z This checks the in memory cache first as the fact was not expired at 'gather time' and it would be problematic if the key did expire after some long running tasks and user gets 'undefined' error in the same play zlerror in '%s' cache plugin while trying to read %s : %s. Most likely a corrupt file, so erasing and failing.zThe cache file %s was corrupt, or did not otherwise contain valid data. It has been removed, so you can re-run your command now.Nz7error in '%s' cache plugin while trying to read %s : %sz*Error while decoding the cache file %s: %s)r= has_expiredr8rR_load ValueErrorr.warningr<rr(rrGrH Exceptionr )rrrQr#rMrrrr s2     zBaseFileCacheModule.getcCs0||j|<||}tjtj|d\}}zpz|||Wn!tt fy@}zt d|j |t |fWYd}~nd}~wwzt||tj|tdWn"tt fyr}zt d|j ||t |fWYd}~nd}~wwWzt|WdStyYdSwzt|WwtyYww)N)dirz=error in '%s' cache plugin while trying to write to '%s' : %s)modezAerror in '%s' cache plugin while trying to move '%s' to '%s' : %s)r=rRtempfilemkstempr?r@dirname_dumprGrHr.rWr<rrenamechmodrunlink)rrr#rQtmpfile_handle tmpfile_pathrMrrrr$s8  & (  zBaseFileCacheModule.setc Cs|jdkrdS||}zt|}Wn/ttfyB}z!|jtjkr*WYd}~dSt d|j |t |fWYd}~dSd}~wwt |j |jkrOdS||jvrX|j|=dS)NrF7error in '%s' cache plugin while trying to stat %s : %sT)r3rRr?statrGrHerrnoENOENTr.rWr<rtimest_mtimer=)rrrQstrMrrrrTs"    zBaseFileCacheModule.has_expiredcCsb|d}t|}g}t|jD]}|ds||sq||d}||s.||q|S)NrO.)r5lenr?listdirr6 startswithrTappend)rrP prefix_lengthr&krrrr&s    zBaseFileCacheModule.keysc Cs||}||jvr dS||rdSzt|WdSttfyJ}z!|jtjkr2WYd}~dSt d|j |t |fWYd}~dSd}~ww)NTFrd) rRr=rTr?rerGrHrfrgr.rWr<r)rrrQrMrrrr's     (zBaseFileCacheModule.containsc CsPz|j|=Wn tyYnwz t||WdSttfy'YdSwr)r=r8r?removerRrGrHrrrrr(s  zBaseFileCacheModule.deletecCs"i|_|D]}||qdSr)r=r&r(rrrrr)s  zBaseFileCacheModule.flushcCs&t}|D] }||||<q|Sr)dictr&r )rretrrrrr*s zBaseFileCacheModule.copycCdS)a Read data from a filepath and return it as a value :arg filepath: The filepath to read from. :returns: The value stored in the filepath This method reads from the file on disk and takes care of any parsing and transformation of the data before returning it. The value returned should be what Ansible would expect if it were uncached data. .. note:: Filehandles have advantages but calling code doesn't know whether this file is text or binary, should be decoded, or accessed via a library function. Therefore the API uses a filepath and opens the file inside of the method. Nr)rfilepathrrrrUszBaseFileCacheModule._loadcCru)z Write data to a filepath :arg value: The value to store :arg filepath: The filepath to store it at Nr)rr#rvrrrr^szBaseFileCacheModule._dump)r+r,r-__doc__rr4r>rRr r$rTr&r'r(r)r*rrUr^r0rrrrr1Os$    r1c@seZdZdZd(ddZddZddZd d Zd d Zd dZ ddZ ddZ ddZ d)ddZ ddZddZddZddZd d!Zd"d#Zd$d%Zd&d'ZdS)*CachePluginAdjudicatorzC Intermediary between a cache dictionary and a CacheModule memorycKs<i|_i|_tj|fi||_|jstd|||_dS)Nz%Unable to load the cache plugin (%s).)r= _retrievedr r _pluginr _plugin_name)rr<rrrrr s   zCachePluginAdjudicator.__init__cCs|j|jkr |dSdSr)rzr= set_cacher%rrrupdate_cache_if_changed*s  z.CachePluginAdjudicator.update_cache_if_changedcCs6|jD] }|j||j|qt|j|_dSr)r=r&r{r$r*deepcopyrz)rtop_level_cache_keyrrrr}.sz CachePluginAdjudicator.set_cachecCs&|jD] }|j||j|<qdSr)r{r&r r=rrrrload_whole_cache3sz'CachePluginAdjudicator.load_whole_cachecCs t|jSr)r r=r%rrr__repr__7 zCachePluginAdjudicator.__repr__cC t|Sr)iterr&r%rrr__iter__: zCachePluginAdjudicator.__iter__cCrr)rlr&r%rrr__len__=rzCachePluginAdjudicator.__len__cCs6d}t||jv||jv|jdk|j|grd}|S)NFryT)allr=rzr|r{r')rrloadrrr _do_load_key@s z#CachePluginAdjudicator._do_load_keycCsN||r"z |j||j|<Wn tyYn w|j||j|<|j|Srrr{r r=r8rzrrrr __getitem__Ks   z"CachePluginAdjudicator.__getitem__Nc Csd||r+z |j||j|<Wnty"}zWYd}~n d}~ww|j||j|<|j||Srr)rrdefaultrMrrrr Us  zCachePluginAdjudicator.getcC |jSr)r=itemsr%rrrr_rzCachePluginAdjudicator.itemscCrr)r=valuesr%rrrrbrzCachePluginAdjudicator.valuescCrr)r=r&r%rrrr&erzCachePluginAdjudicator.keyscGs"|r |j||dS|j|S)Nr)r=pop)rrrrrrrhs zCachePluginAdjudicator.popcCs |j|=dSrr=rrrr __delitem__mrz"CachePluginAdjudicator.__delitem__cCs||j|<dSrrr"rrr __setitem__psz"CachePluginAdjudicator.__setitem__cCs|ji|_dSr)r{r)r=r%rrrr)ss  zCachePluginAdjudicator.flushcCs|j|dSr)r=update)rr#rrrrwszCachePluginAdjudicator.update)ryr)r+r,r-rwrr~r}rrrrrrr rrr&rrrr)rrrrrrxs(     rx)! __future__rr*rfr?r[rhabcrcollections.abcransiblerr9ansible.errorsr ansible.module_utils.common.filer+ansible.module_utils.common.text.convertersrr ansible.pluginsr ansible.plugins.loaderr ansible.utils.collection_loaderr ansible.utils.displayr r.rr1rxrrrrs*          &N