o
    wi                     @   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mZm	Z	 d dl
Z
ddlmZ z	d dlZe
jZW n eyA   e
jZY nw zd dlZejZdZW n eyZ   eZdZY nw dZd	Ze	ejjZe	d
d Ze	dd ZeG dd dZG dd dZdS )    )	dataclassN   )safe_makedirscached_function)__version__z.gz z.zipi  @c                 C   s   t j| }dt||f d}t|}t|d(}|d}|r9|	| |d}|s#W d    |
 S W d    |
 S 1 sFw   Y  |
 S )Nz%d:%sUTF-8rbi  )ospathnormpathlenencodehashlibsha256openreadupdate	hexdigest)filenamer   prefixmfdata r   O/home/ubuntu/sommelier/.venv/lib/python3.10/site-packages/Cython/Build/Cache.py	file_hash"   s    





r   c                  C   sr   dt jv r
t jd S d} t jdkr#tjdkrt jd} nt jd} | r0t j| r0t	| dS t jt	dd	S )
z
    Return the base directory containing Cython's caches.

    Priority:

    1. CYTHON_CACHE_DIR
    2. (OS X): ~/Library/Caches/Cython
       (posix not OS X): XDG_CACHE_HOME/cython if XDG_CACHE_HOME defined
    3. ~/.cython

    CYTHON_CACHE_DIRNposixdarwinz~/Library/CachesXDG_CACHE_HOMEcython~z.cython)
r
   environnamesysplatformr   
expandusergetisdir	join_path)parentr   r   r   get_cython_cache_dir/   s   




r,   c                   @   s:   e Zd ZU dZeed< dZeed< dZeed< dd Z	dS )	FingerprintFlagsclanguageFpy_limited_api
np_pythranc                 C   s   t | j| j| jfS N)strr/   r0   r1   )selfr   r   r   get_fingerprintT   s   z FingerprintFlags.get_fingerprintN)
__name__
__module____qualname__r/   r3   __annotations__r0   boolr1   r5   r   r   r   r   r-   N   s
   
 r-   c                   @   sN   e Zd ZdddZe fddZdd Zdd	 Zd
d Zdd Z	dddZ
dS )CacheNc                 C   sR   |d u rt t d| _n|| _|d ur|nt| _tj| js't| j d S d S )Ncompiler)r*   r,   r   MAX_CACHE_SIZE
cache_sizer
   existsmakedirs)r4   r   r>   r   r   r   __init__Y   s   zCache.__init__c                 C   s   zFt td}|t|d t|D ]}tj	|d dvr-|t|d q||
 d ||
 d | W S  tyP   Y dS w )a;  
        Return a fingerprint of a cython file that is about to be cythonized.

        Fingerprints are looked up in future compilations. If the fingerprint
        is found, the cythonization can be skipped. The fingerprint must
        incorporate everything that has an influence on the generated code.
        r      )z.cz.cppz.hN)r   r   r   r   r   r   sortedr
   r   splitextr5   r   OSError)r4   r   dependenciescompilation_optionsflagsr   xr   r   r   transitive_fingerprintb   s   

zCache.transitive_fingerprintc                 C   s    t | jdtj||f | S )Nz%s-%s)r*   r   r
   basename)r4   cfilefingerprintextr   r   r   fingerprint_file}   s   zCache.fingerprint_filec                 C   sX   t j| jst| j | ||t}t j|r|S | ||t}t j|r*|S d S r2   )r
   r   r?   r   rO   gzip_extzip_ext)r4   c_filerM   gz_fingerprint_filezip_fingerprint_filer   r   r   lookup_cache   s   
zCache.lookup_cachec           	   	   C   s  t j|d }|tkrMt |d  t|d,}t|d}t|| W d    n1 s.w   Y  W d    d S W d    d S 1 sFw   Y  d S |t	krt |d  t j
|}t|}| D ]}||t|| qgW d    d S 1 s~w   Y  d S td| )NrB   r	   wbz"Unsupported cache file extension: )r
   r   rD   rP   utime	gzip_openr   shutilcopyfileobjrQ   dirnamezipfileZipFilenamelistextractr*   
ValueError)	r4   rR   cachedrN   gr   r[   zartifactr   r   r   load_from_cache   s&   ""zCache.load_from_cachec           
   	   C   s   |  }t|dkrD| ||t}t|d%}t|d d}t|| W d    n1 s/w   Y  W d    n1 s>w   Y  n/| ||t}t	
|d dt}|D ]}	||	tj|	 qWW d    n1 snw   Y  t|d | d S )NrB   r	   z.tmprV   w)get_generated_source_filesr   rO   rP   r   rX   rY   rZ   rQ   r\   r]   zipfile_compression_modewriter
   r   rK   rename)
r4   rR   rM   compilation_result	artifactsrO   r   rb   ziprd   r   r   r   store_to_cache   s(   
zCache.store_to_cache333333?c              	   C   s  z/t jdddtj| jgt jd}|j}|jdkr.dt|	 
 d  }|| jk r.W d S W n ttfy:   Y nw d}g }t| jD ]}t| j|}t|}||j7 }||j|j|f qE|| jkrtt|D ]\}	}
}t| ||
8 }|| j| k r d S qmd S d S )Nduz-sz-k)stdoutr   i   )
subprocessrunr
   r   abspathPIPErq   
returncodeintstripsplitr>   rE   r`   listdirr*   statst_sizeappendst_atimereversedrC   unlink)r4   ratiocompleted_processrq   
total_sizeallfiler   stimesizer   r   r   cleanup_cache   s:   





zCache.cleanup_cacher2   )ro   )r6   r7   r8   rA   r-   rJ   rO   rU   re   rn   r   r   r   r   r   r;   X   s    


r;   )dataclassesr   r%   r
   r   rY   rr   Utilsr   r   r\   r   r   zlibZIP_DEFLATEDrh   ImportError
ZIP_STOREDgzipr   rX   rP   rQ   r=   r   joinr*   r   r,   r-   r;   r   r   r   r   <module>   sB    



	