o
    Vh'                     @  s  d dl mZ d dlZd dlZd dlZd dlZd dlZd dlZd dlZd dl	m
Z
 d dlmZ d dlmZ d dlmZ d dlmZ d dlmZ d d	lmZ d d
lmZ d dlmZ d dlmZ d dlmZ d dlmZ er~d dlmZ d dlmZ d dlm Z  dZ!edZ"e#e$Z%d%ddZ&ej'dfej(dfej)dfdZ*ej+ddG dd  d ee" Z,ej+ddG d!d" d"ee" Z-G d#d$ d$Z.dS )&    )annotationsN)defaultdict)Path)TYPE_CHECKING)Any)Generic)TypeVar)overload)decode)encode)get_highest_priority_hash_type)InvalidWheelNameError)Wheel)Callable)Link)Envl   c(	 Tminutesintreturnc                 C  s   t t | d  S )z
    Calculates the time in seconds since epoch that occurs 'minutes' from now.

    :param minutes: The number of minutes to count forward
    <   )roundtime)r    r   f/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/poetry/utils/cache.py_expiration)   s   r            )md5sha1sha256T)frozenc                   @  s4   e Zd ZU dZded< dZded< edd	d
ZdS )	CacheItemz3
    Stores data and metadata for cache items.
    r   dataN
int | Noneexpiresr   boolc                 C  s   | j duot | j kS )zS
        Return true if the cache item has exceeded its expiration period.
        N)r&   r   selfr   r   r   expiredB   s   zCacheItem.expired)r   r'   )__name__
__module____qualname____doc____annotations__r&   propertyr*   r   r   r   r   r#   9   s   
 r#   c                   @  s   e Zd ZU dZded< dZded< d.d	d
Zd/ddZd0ddZd1d2ddZ	d3ddZ
d.ddZ	d1d4d d!Zd/d"d#Zd5d$d%Zd6d)d*Zd7d,d-ZdS )8	FileCachez
    Cachy-compatible minimal file cache. Stores subsequent data in a JSON format.

    :param path: The path that the cache starts at.
    :param hash_type: The hash to use for encoding keys/building directories.
    r   pathr!   str	hash_typer   Nonec                 C  s    | j tvrtd| j  dd S )Nz'FileCache.hash_type is unknown value: 'z'.)r4   _HASHES
ValueErrorr(   r   r   r   __post_init__V   s
   
zFileCache.__post_init__keyT | Nonec                 C  s
   |  |S N)_get_payloadr)   r9   r   r   r   get\   s   
zFileCache.getr'   c                 C  s   |  |duS )z
        Determine if a file exists and has not expired in the cache.
        :param key: The cache key
        :returns: True if the key exists in the cache
        N)r>   r=   r   r   r   has_   s   zFileCache.hasNvaluer   r   r%   c                 C  sv   t ||dur
t|ndd}| |}|jjddd |d}|| | W d   dS 1 s4w   Y  dS )z
        Store an item in the cache.

        :param key: The cache key
        :param value: The cache value
        :param minutes: The lifetime in minutes of the cached value
        N)r&   Tparentsexist_okwb)r#   r   _pathparentmkdiropenwrite
_serialize)r)   r9   r@   r   payloadr2   fr   r   r   putg   s   
"zFileCache.putc                 C  s"   |  |}| r|  dS dS )zS
        Remove an item from the cache.

        :param key: The cache key
        N)rE   existsunlink)r)   r9   r2   r   r   r   forgetw   s   
zFileCache.forgetc                 C  s   t | j dS )z"
        Clear the cache.
        N)shutilrmtreer2   r(   r   r   r   flush   s   zFileCache.flushcallbackT | Callable[[], T]r   c                 C  s6   |  |}|du rt|r| n|}| ||| |S )z
        Get an item from the cache, or use a default from callback.

        :param key: The cache key
        :param callback: Callback function providing default value
        :param minutes: The lifetime in minutes of the cached value
        N)r>   callablerM   )r)   r9   rT   r   r@   r   r   r   remember   s
   

zFileCache.rememberc              	   C  s   |  |}| sd S |d}| }W d    n1 sw   Y  z| |}W n tjtfyB   | | t	
d Y d S w |jrM| | d S |jS )Nrbz/Corrupt cache file was detected and cleaned up.)rE   rN   rH   read_deserializejsonJSONDecodeErrorr7   rP   loggerwarningr*   r$   )r)   r9   r2   rL   file_contentrK   r   r   r   r<      s"   




zFileCache._get_payloadc                   sZ   t | j \}}|t|   fddtdt dD d | }t| jg| R  S )Nc                   s   g | ]
} ||d   qS )r   r   ).0ihr   r   
<listcomp>   s    z#FileCache._path.<locals>.<listcomp>r   r   )r6   r4   r   	hexdigestrangelenr   r2   )r)   r9   r4   parts_countpartsr   rb   r   rE      s   &zFileCache._pathrK   CacheItem[T]bytesc                 C  s(   |j pt}t|j}t|d| S )N010d)r&   MAX_DATEr[   dumpsr$   r   )r)   rK   r&   r$   r   r   r   rJ      s   
zFileCache._serializedata_rawc                 C  s4   t |}t|dd  }t|d d }t||S )N
   )r
   r[   loadsr   r#   )r)   ro   data_strr$   r&   r   r   r   rZ      s   
zFileCache._deserialize)r   r5   )r9   r3   r   r:   )r9   r3   r   r'   r;   )r9   r3   r@   r   r   r%   r   r5   )r9   r3   r   r5   )r9   r3   rT   rU   r   r%   r   r   )r9   r3   r   r   )rK   rj   r   rk   )ro   rk   r   rj   )r+   r,   r-   r.   r/   r4   r8   r>   r?   rM   rP   rS   rW   r<   rE   rJ   rZ   r   r   r   r   r1   J   s    
 








r1   c                   @  s   e Zd Zd1ddZd2d	d
Zd3ddZd4ddZeddd5ddZeddd d6d"dZd#d#d d7d%dZd8d(d)Z	d#d#d*d9d,d-Z
d:d/d0Zd#S );ArtifactCache	cache_dirr   r   r5   c                C  s   || _ ttj| _d S r;   )
_cache_dirr   	threadingLock_archive_locks)r)   rt   r   r   r   __init__   s   
zArtifactCache.__init__linkr   c                 C  sD   d|j i}t|j|j }r|j| ||< |jr|j|d< | |S )Nurlsubdirectory)url_without_fragmentr   hashesfilenamesubdirectory_fragment_get_directory_from_hash)r)   rz   	key_parts	hash_namer   r   r   get_cache_directory_for_link   s   


z*ArtifactCache.get_cache_directory_for_linkr   objectc                 C  sZ   t tj|ddddd }|d d |dd |dd |dd  g}| jj| S )NT),:)	sort_keys
separatorsensure_asciiasciir   r      )hashlibr!   r[   rn   r   re   ru   joinpath)r)   r   r9   	split_keyr   r   r   r      s   ,z&ArtifactCache._get_directory_from_hashr{   r3   refr|   
str | Nonec                 C  s    ||d}|r||d< |  |S )N)r{   r   r|   )r   )r)   r{   r   r|   r   r   r   r   get_cache_directory_for_git   s   

z)ArtifactCache.get_cache_directory_for_git.)envstrictr'   r   
Env | Nonedownload_funcCallable[[str, Path], None]c                C     d S r;   r   r)   rz   r   r   r   r   r   r   get_cached_archive_for_link      z)ArtifactCache.get_cached_archive_for_link)r   r   Path | Nonec                C  r   r;   r   r   r   r   r   r      r   N"Callable[[str, Path], None] | Nonec             	   C  s   |  |}| j|||j|d}|d u r^|r^|d ur^||j }| j| 3 | sF|jddd z||j| W n tyE   |jdd  w W d    |S W d    |S 1 sYw   Y  |S )N)r   r   r   TrA   )
missing_ok)	r   _get_cached_archiver   rx   rN   rG   r{   BaseExceptionrO   )r)   rz   r   r   r   rt   cached_archiver   r   r   r      s.   




	referencer   c                 C  s   |  |||}| j|d|dS )NF)r   r   )r   r   )r)   r{   r   r|   r   rt   r   r   r   get_cached_archive_for_git  s   z(ArtifactCache.get_cached_archive_for_git)r   r   r   c          	   	   C  s   |s|d usJ |r|d usJ |  |}|sd S g }|D ]E}|r+||jkr*|  S q|d us1J |jdkr@|td|f qzt|j}W n	 tyP   Y qw ||sWq|||j	|f q|sgd S t
|d S )Nz.whlinf   )_get_cached_archivesnamesuffixappendfloatr   r   is_supported_by_environmentget_minimum_supported_indexsupported_tagsmin)	r)   rt   r   r   r   archives
candidatesarchivewheelr   r   r   r     s8   	



z!ArtifactCache._get_cached_archive
list[Path]c                 C  s.   g d}g }|D ]}|| d| 7 }q|S )N)whlztar.gzztar.bz2bz2zipz*.)glob)r)   rt   archive_typespathsarchive_typer   r   r   r   N  s
   z"ArtifactCache._get_cached_archives)rt   r   r   r5   )rz   r   r   r   )r   r   r   r   )r{   r3   r   r3   r|   r   r   r   )
rz   r   r   r'   r   r   r   r   r   r   )
rz   r   r   r'   r   r   r   r5   r   r   )
rz   r   r   r'   r   r   r   r   r   r   )
r{   r3   r   r3   r|   r   r   r   r   r   )
rt   r   r   r'   r   r   r   r   r   r   )rt   r   r   r   )r+   r,   r-   ry   r   r   r   r	   r   r   r   r   r   r   r   r   rs      s(    




		
2rs   )r   r   r   r   )/
__future__r   dataclassesr   r[   loggingrQ   rv   r   collectionsr   pathlibr   typingr   r   r   r   r	   poetry.utils._compatr
   r   poetry.utils.helpersr   poetry.utils.wheelr   r   collections.abcr   poetry.core.packages.utils.linkr   poetry.utils.envr   rm   r   	getLoggerr+   r]   r   r   r    r!   r6   	dataclassr#   r1   rs   r   r   r   r   <module>   sJ    




t