Ë
    tPêj<  ã                  ó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        úU/home/oem/CyberBrain26-unified/.venv/lib/python3.12/site-packages/pip_audit/_cache.pyÚ_get_pip_cacher$   $   s¤   € ô �>‰>˜4 ¨°Ð
7€Cð^Ü—.‘. ¨D¼¿¹ÔQ[×QcÑQcÔdˆð
 —‘×%Ñ% gÓ.×4Ñ4°TÓ:€IÜ˜)“_ vÑ-€NØÐøô ×(Ñ(ò ^ô ÐMÈcÈUÐSÓTÐZ]Ð]ûð^ú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ñ ”r—y‘yÐ!3Ô4Ü,8Ô<PÒ,PœÔ(ÐVZˆØÐ$Ø Ð ä�N‰NØ”|�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Ó#Ð#øÜò 	Ø×'Ò'Ü—‘ØXÐYZÐX[Ð\ôð (,�Ô$Üûð	ú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   € ð	,Ø�N‰N˜3 Õ&øÜò 	,Ø×'Ò'Ü—‘ØWÐXYÐWZÐ[ôð (,�×$Ñ$ô	 (ûð	,ú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µ   ø€ Ü‘G‘K Ó$ˆð	Ü�K‰KœŸ™Ÿ™¨Ó-¨t¯|©|Ô<ô   u´"·'±'·/±/À$Ó2GÔHð 	"ÈBØ�H‰H�UŒOð �H‰HŒJÜ�H‰H�R—Y‘Y“[Ô!÷	"ô 	�
‰
�2—7‘7˜DÕ!øô! ò 	Ùð	ú÷	"ð 	"ú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   ø€ ð	,Ü‰G‰N˜3ÕøÜò 	,Ø×'Ò'Ü—‘Ø_Ð`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ÔäØÜœ^¨I¸wÔ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“Ð á‘s˜7×.Ñ.Ó/Ó0€à#, 4§9¡9£;Ð1CÑ#CÐ  óð FJõ $#ôN@,�Yô @,ðF @Eö r9   