
    3i                    @   U d Z ddlmZ ddlZddlZddlmZmZ ddlm	Z	 ddl
mZmZmZmZ erddlmZ ddlmZ  ej&                  e      Zd	ed
<   e G d de             Ze G d de             Z G d d      Z G d d      Z G d de      Z G d de      Zg dZy)zMCache Protocol and built-in implementations for Python interpreter discovery.    )annotationsN)contextmanagersuppress)sha256)TYPE_CHECKINGFinalProtocolruntime_checkable)	Generator)PathzFinal[logging.Logger]_LOGGERc                  B    e Zd ZdZddZd	dZd
dZddZedd       Z	y)ContentStorez/A store for reading and writing cached content.c                     y)z)Return whether the cached content exists.N selfs    J/var/www/opsc/venv/lib/python3.12/site-packages/python_discovery/_cache.pyexistszContentStore.exists           c                     y)z?Read the cached content, or ``None`` if unavailable or corrupt.Nr   r   s    r   readzContentStore.read   r   r   c                     y)zi
        Persist *content* to the store.

        :param content: interpreter metadata to cache.
        Nr   r   contents     r   writezContentStore.write        	r   c                     y)zDelete the cached content.Nr   r   s    r   removezContentStore.remove&   r   r   c                     y)z>Context manager that acquires an exclusive lock on this store.Nr   r   s    r   lockedzContentStore.locked*   s     	r   Nreturnboolr$   zdict | Noner   dictr$   Noner$   r)   r$   zGenerator[None]
__name__
__module____qualname____doc__r   r   r   r    r   r"   r   r   r   r   r      s-    9  r   r   c                       e Zd ZdZddZddZy)PyInfoCachez3Cache interface for Python interpreter information.c                     y)
        Return the content store for the interpreter at *path*.

        :param path: absolute path to a Python executable.
        Nr   r   paths     r   py_infozPyInfoCache.py_info4   r   r   c                     y)*Remove all cached interpreter information.Nr   r   s    r   py_info_clearzPyInfoCache.py_info_clear<   r   r   N)r6   r   r$   r   r*   r-   r.   r/   r0   r7   r:   r   r   r   r2   r2   0   s    =r   r2   c                  \    e Zd ZdZd
dZedd       ZddZddZddZ	ddZ
edd       Zy	)DiskContentStorez0JSON file-based content store with file locking.c                     || _         || _        y N_folder_key)r   folderkeys      r   __init__zDiskContentStore.__init__D   s    	r   c                :    | j                   | j                   dz  S )N.jsonr@   r   s    r   _filezDiskContentStore._fileH   s    ||5111r   c                6    | j                   j                         S r?   )rH   r   r   s    r   r   zDiskContentStore.existsL   s    zz  ""r   c                   d\  }}	 t        j                  | j                  j                  d            }t        j                  d| j                         |S # t        $ r d}Y n0t        $ r% t        j                  d| j                  d       Y nw xY w|r5t        t              5  | j                          d d d        y # 1 sw Y   y xY wy )NNFutf-8encodingzgot python info from %sTzfailed to read %s)exc_info)
jsonloadsrH   	read_textr   debug
ValueErrorOSErrorr   r    )r   data
bad_formats      r   r   zDiskContentStore.readO   s    &j	::djj22G2DED MM3TZZ@K  	J 	JMM-tzzDMI	J
 '" ts#   /A B%*BB&C  C	c                    | j                   j                  dd       | j                  j                  t	        j
                  |dd      d       t        j                  d| j                         y )NTparentsexist_ok   )	sort_keysindentrL   rM   zwrote python info at %s)rA   mkdirrH   
write_textrP   dumpsr   rS   r   s     r   r   zDiskContentStore.write_   sQ    4$7

djjDKV]^/<r   c                    t        t              5  | j                  j                          d d d        t        j                  d| j                         y # 1 sw Y   *xY w)Nzremoved python info at %s)r   rU   rH   unlinkr   rS   r   s    r   r    zDiskContentStore.removed   sB    g 	 JJ	 14::>	  	 s   AAc              #     K   ddl m} | j                  | j                   dz  }|j                  j                  dd        |t        |            5  d  d d d        y # 1 sw Y   y xY ww)Nr   )FileLockz.lockTrY   )filelockre   rA   rB   parentr_   str)r   re   	lock_paths      r   r"   zDiskContentStore.lockedi   s^     %LLdii[#66	td;c)n% 		 	 	s   AA-A!	A-!A*&A-N)rC   r   rD   rh   r$   r)   r$   r   r#   r&   r'   r*   r+   )r-   r.   r/   r0   rE   propertyrH   r   r   r   r    r   r"   r   r   r   r=   r=   A   sF    : 2 2# =
?
  r   r=   c                  :    e Zd ZdZddZedd       Zd	dZd
dZy)	DiskCachez
    File-system based Python interpreter info cache (``<root>/py_info/4/<sha256>.json``).

    :param root: root directory for the on-disk cache.
    c                    || _         y r?   _root)r   roots     r   rE   zDiskCache.__init__z   s	    
r   c                &    | j                   dz  dz  S )Nr7   4ro   r   s    r   _py_info_dirzDiskCache._py_info_dir}   s    zzI%++r   c                    t        t        |      j                  d            j                         }t	        | j
                  |      S )r4   rL   )r   rh   encode	hexdigestr=   rt   )r   r6   rD   s      r   r7   zDiskCache.py_info   s9     SY%%g./99; 1 1377r   c                    | j                   }|j                         rN|j                         D ]:  }|j                  dk(  st	        t
              5  |j                          ddd       < yy# 1 sw Y   HxY w)r9   rG   N)rt   r   iterdirsuffixr   rU   rc   )r   rC   entrys      r   r:   zDiskCache.py_info_clear   sh    ""==?) '<<7*!'* '' '' ' 's   A,,A5	N)rq   r   r$   r)   rj   )r6   r   r$   r=   r*   )	r-   r.   r/   r0   rE   rk   rt   r7   r:   r   r   r   rm   rm   s   s*     , ,8'r   rm   c                  B    e Zd ZdZddZd	dZd
dZddZedd       Z	y)NoOpContentStorezDContent store that does nothing -- implements ContentStore protocol.c                     yrK   r   r   s    r   r   zNoOpContentStore.exists   s    r   c                     y r?   r   r   s    r   r   zNoOpContentStore.read   s    r   c                     y r?   r   r   s     r   r   zNoOpContentStore.write       r   c                     y r?   r   r   s    r   r    zNoOpContentStore.remove   r   r   c              #     K   d  y wr?   r   r   s    r   r"   zNoOpContentStore.locked   s	     s   Nr#   r&   r'   r*   r+   r,   r   r   r   r}   r}      s-    N  r   r}   c                       e Zd ZdZddZddZy)	NoOpCachez;Cache that does nothing -- implements PyInfoCache protocol.c                    t               S r?   )r}   r5   s     r   r7   zNoOpCache.py_info   s    !!r   c                     y r?   r   r   s    r   r:   zNoOpCache.py_info_clear   r   r   N)r6   r   r$   r}   r*   r;   r   r   r   r   r      s    E"r   r   )r   rm   r=   r   r}   r2   )r0   
__future__r   rP   logging
contextlibr   r   hashlibr   typingr   r   r	   r
   collections.abcr   pathlibr   	getLoggerr-   r   __annotations__r   r2   r=   rm   r}   r   __all__r   r   r   <module>r      s    S "   /  D D)!2!2!28!<	 < 8  : (   / /d' 'B| ( r   