U
    cUh                     @   s   d Z ddlZddlmZ ddlmZ ddlmZmZmZm	Z	 ddl
mZ ddlmZ ddlmZ dd	lmZmZ dd
lmZ eedddZeed dddZG dd deZdS )zHTTP cache implementation.
    N)contextmanager)datetime)BinaryIO	GeneratorOptionalUnion)SeparateBodyBaseCache)SeparateBodyFileCache)Response)adjacent_tmp_filereplace)
ensure_dir)responsereturnc                 C   s   t | ddS )N
from_cacheF)getattr)r    r   [/opt/bot/vods_filmes/script/venv/lib/python3.8/site-packages/pip/_internal/network/cache.pyis_from_cache   s    r   )NNN)r   c                   c   s$   z
dV  W n t k
r   Y nX dS )zvIf we can't access the cache then we can just skip caching and process
    requests as if caching wasn't enabled.
    N)OSErrorr   r   r   r   suppressed_cache_errors   s    
r   c                       s   e Zd ZdZedd fddZeedddZeee d	d
dZ	eeddddZ
deeeeedf ddddZedd	ddZeee d	ddZeeddddZ  ZS )SafeFileCacheaG  
    A file based cache which is safe to use even when the target directory may
    not be accessible or writable.

    There is a race condition when two processes try to write and/or read the
    same entry at the same time, since each entry consists of two separate
    files (https://github.com/psf/cachecontrol/issues/324).  We therefore have
    additional logic that makes sure that both files to be present before
    returning an entry; this fixes the read side of the race condition.

    For the write side, we assume that the server will only ever return the
    same data for the same URL, which ought to be the case for files pip is
    downloading.  PyPI does not have a mechanism to swap out a wheel for
    another wheel, for example.  If this assumption is not true, the
    CacheControl issue will need to be fixed.
    N)	directoryr   c                    s$   |d k	st dt   || _d S )Nz!Cache directory must not be None.)AssertionErrorsuper__init__r   )selfr   	__class__r   r   r   2   s    
zSafeFileCache.__init__)namer   c                 C   s4   t |}t|d d |g }tjj| jf| S )N   )r	   encodelistospathjoinr   )r   r   Zhashedpartsr   r   r   _get_cache_path7   s    
zSafeFileCache._get_cache_path)keyr   c                 C   sv   |  |}|d }tj|r*tj|s.d S t 8 t|d"}| W  5 Q R  W  5 Q R  S Q R X W 5 Q R X d S N.bodyrb)r'   r#   r$   existsr   openread)r   r(   metadata_path	body_pathfr   r   r   get?   s    
zSafeFileCache.get)r$   datar   c              
   C   s   t   ttj| t|b}|| t| jj	d@ dB }tj
tjkr`t
| | ntj
tjkr~tj
|j|dd W 5 Q R X t|j| W 5 Q R X d S )Ni  i  F)follow_symlinks)r   r   r#   r$   dirnamer   writestatr   st_modechmodsupports_fdfilenosupports_follow_symlinksr   r   )r   r$   r3   r1   moder   r   r   _writeI   s    

zSafeFileCache._write)r(   valueexpiresr   c                 C   s   |  |}| || d S )Nr'   r>   )r   r(   r?   r@   r$   r   r   r   set^   s    
zSafeFileCache.setc              	   C   sJ   |  |}t  t| W 5 Q R X t  t|d  W 5 Q R X d S Nr*   )r'   r   r#   remove)r   r(   r$   r   r   r   deleted   s
    
zSafeFileCache.deletec              
   C   sV   |  |}|d }tj|r*tj|s.d S t  t|dW  5 Q R  S Q R X d S r)   )r'   r#   r$   r,   r   r-   )r   r(   r/   r0   r   r   r   get_bodyk   s    
zSafeFileCache.get_body)r(   bodyr   c                 C   s   |  |d }| || d S rC   rA   )r   r(   rG   r$   r   r   r   set_bodyt   s    zSafeFileCache.set_body)N)__name__
__module____qualname____doc__strr   r'   r   bytesr2   r>   r   intr   rB   rE   r   rF   rH   __classcell__r   r   r   r   r       s   
   	r   )rL   r#   
contextlibr   r   typingr   r   r   r   pip._vendor.cachecontrol.cacher   Zpip._vendor.cachecontrol.cachesr	   pip._vendor.requests.modelsr
   pip._internal.utils.filesystemr   r   pip._internal.utils.miscr   boolr   r   r   r   r   r   r   <module>   s   
