
    ܖi<                    r   d Z 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m	Z	 ddl
mZ ddlm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  ej4                  e      Z ed      Z e eej>                              Z  e	jB                         dz  Z"ddZ#ddddZ$ G d de      Z%ddddZ&y)z%
Caching middleware for `pip-audit`.
    )annotationsN)Path)NamedTemporaryFile)Any)CacheControl)	FileCache)Version)user_cache_path)ServiceErrorz20.1z.pip-audit-cachec                 `   t         j                  ddddg} 	 t        j                  | dt        j                  t        j
                        }|j                  j                  d      j                  d	      }t        |      d
z  }|S # t        j                  $ r}t        d|        |d }~ww xY w)Nz-mpipcachedirT)checkstdoutstderrz0Failed to query the `pip` HTTP cache directory: zutf-8
http)sys
executable
subprocessrunPIPEDEVNULLCalledProcessErrorr   r   decodestripr   )cmdprocesscpe	cache_dirhttp_cache_dirs        T/var/www/html/content-pipeline/venv/lib/python3.12/site-packages/pip_audit/_cache.py_get_pip_cacher$   $   s     >>4
7C^..DQ[QcQcd
 %%g.44T:I)_v-N (( ^ McUSTZ]]^s   5B B-B((B-Tuse_pipc               B   | | S t        ddd      }t        j                         r"|t        k7  rt        j                  t               |rTt        j                  d      s?t        t        k\  r
t               nd}||S t        j                  dt         d| d	       |S |S )
z
    Returns a directory path suitable for HTTP caching.

    The directory is **not** guaranteed to exist.

    `use_pip` tells the function to prefer `pip`'s pre-existing cache,
    **unless** `PIP_NO_CACHE_DIR` is present in the environment.
    Nz	pip-auditFT)	appauthorensure_existsPIP_NO_CACHE_DIRzpip z3 doesn't support the `cache dir` subcommand, using z instead)r
    _PIP_AUDIT_LEGACY_INTERNAL_CACHEexistsshutilrmtreeosgetenv_PIP_VERSION_MINIMUM_PIP_VERSIONr$   loggerwarning)custom_cache_dirr&   pip_audit_cache_dirpip_cache_dirs       r#   _get_cache_dirr8   3   s     # *+VZ[ 	)//1#CC67 ryy!34,8<P,P(VZ$  NN|n %,-X7 '&""    c                  R     e Zd ZdZd fdZd fdZd	d
dZd fdZd fdZ xZ	S )_SafeFileCachez
    A rough mirror of `pip`'s `SafeFileCache` that *should* be runtime-compatible
    with `pip` (i.e., does not interfere with `pip` when it shares the same
    caching directory as a running `pip` process).
    c                D    d| _         t        | 	  t        |             y )NF)_logged_warningsuper__init__str)self	directory	__class__s     r#   r?   z_SafeFileCache.__init__a   s    $Y(r9   c                    	 t         |   |      S # t        $ r5}| j                  st        j                  d|        d| _        Y d }~y d }~ww xY w)NzBFailed to read from cache directory, performance may be degraded: T)r>   get	Exceptionr=   r3   r4   rA   keyerC   s      r#   rE   z_SafeFileCache.gete   sU    	7;s## 	''XYZX[\ (,$	s    	A+AAc                    	 | j                  ||       y # t        $ r:}| j                  s$t        j	                  d|        d| _        Y d }~y Y d }~y d }~ww xY w)NzAFailed to write to cache directory, performance may be degraded: T)	_set_implrF   r=   r3   r4   )rA   rH   valueexpiresrI   s        r#   setz_SafeFileCache.setp   sX    	,NN3& 	,''WXYWZ[ (,$$	 (	,s    	A+AAc                   t         |   |      }	 t        j                  t        j                  j                  |      | j                         t        dt        j                  j                  |            5 }|j                  |       |j                          t        j                  |j                                d d d        t        j                  j                  |       y # t        $ r Y w xY w# 1 sw Y   9xY w)NF)deleter   )r>   _fnr/   makedirspathdirnamedirmodeOSErrorr   writeflushfsyncfilenoreplacename)rA   rH   rL   r\   iorC   s        r#   rK   z_SafeFileCache._set_implz   s    GK$	KK-t||<  u"''//$2GH 	"BHHUO HHJHHRYY[!	" 	

277D!!  			" 	"s   =C' 9AC6'	C32C36C?c                    	 t         |   |       y # t        $ r:}| j                  s$t        j                  d|        d| _        Y d }~y Y d }~y d }~ww xY w)NzIFailed to delete file from cache directory, performance may be degraded: T)r>   rP   rF   r=   r3   r4   rG   s      r#   rP   z_SafeFileCache.delete   sW    	,GN3 	,''_`a_bc (,$$	 (	,s    	A+AA)rB   r   )rH   r@   return
Any | None)N)rH   r@   rL   bytesrM   r`   r_   None)rH   r@   rL   ra   r_   rb   )rH   r@   r_   rb   )
__name__
__module____qualname____doc__r?   rE   rN   rK   rP   __classcell__)rC   s   @r#   r;   r;   Z   s&    )	,"0, ,r9   r;   Fc          	     z    t        j                         }d|_        t        |t	        t        | |                  S )a  
    Return a `requests` style session, with suitable caching middleware.

    Uses the given `cache_dir` for the HTTP cache.

    `use_pip` determines how the fallback cache directory is determined, if `cache_dir` is None.
    When `use_pip` is `False`, `caching_session` will use a `pip-audit` internal cache directory.
    When `use_pip` is `True`, `caching_session` will attempt to discover `pip`'s cache
    directory, falling back on the internal `pip-audit` cache directory if the user's
    version of `pip` is too old.
       r%   )r   )requestsSessionmax_redirectsr   r;   r8   )r!   r&   inner_sessions      r#   caching_sessionrn      s9     $$&M"#M^IwGH r9   )r_   r   )r5   Path | Noner&   boolr_   r   )r!   ro   r&   rp   r_   zrequests.Session)'rf   
__future__r   loggingr/   r-   r   r   pathlibr   tempfiler   typingr   pip_apirj   cachecontrolr   cachecontrol.cachesr   packaging.versionr	   platformdirsr
   pip_audit._service.interfacer   	getLoggerrc   r3   r2   r@   PIP_VERSIONr1   homer+   r$   r8   r;   rn    r9   r#   <module>r      s    #  	   
  '    % ) % ( 5			8	$ v s7../0#,499;1C#C   FJ $#N@,Y @,F @E r9   