Format: application/x-python-code
⬇ Download File (10.4 KB)
   1  o
   2  
   3  2'h�/�@s�ddlZddlZddlZddlZddlZddlZddlZddlZddlZ	ddl
   4  mZddlm
   5  Z
   6  ddlmZddlmZddlmZddlmZdd	ed
   7  e	jfdd�ZGd
   8  d�de�ZdS)�N)�contextmanager)�Path)�sleep)�time)�	BaseCache)�FileSystemSerializer��string�returncCs
   9  t�|�S)z�Don't access ``hashlib.md5`` until runtime. FIPS builds may not include
  10      md5, in which case the import and use as a default would fail before the
  11      developer can configure something else.
  12      )�hashlib�md5)r	�r
  13  �CC:\Users\shubh\Videos\agent\nova\Lib\site-packages\cachelib/file.py�	_lazy_md5s
  14  rc@seZdZdZdZdZee�Ze	�Z
  15  				dAdeded	ed
  16  e
  17  jede
  18  jf
  19  dd
  20  �Zdefdd�Zedefdd��Z	dBde
  21  jede
  22  jeddfdd�Zde
  23  jedefdd�Zdedefdd�Zde
  24  jeddffdd�Zdefdd �Zd!eddfd"d#�Zdefd$d%�ZdCd&d'�Zdefd(d)�Zd*edefd+d,�Z d*ede
  25  jfd-d.�Z!dDd*ede
  26  jde
  27  jedefd/d0�Z"		1dEd*ede
  28  jde
  29  jed2edef
  30  d3d4�Z#dFd*ed2edefd5d6�Z$d*edefd7d8�Z%d9e
  31  j&d:e
  32  jd;e
  33  jde
  34  jfd<d=�Z'e(d>ed
  35  ede
  36  jfd?d@��Z)dS)G�FileSystemCachea1A cache that stores the items on the file system.  This cache depends
  37      on being the only user of the `cache_dir`.  Make absolutely sure that
  38      nobody but this cache stores files there or otherwise the cache will
  39      randomly delete files therein.
  40  
  41      :param cache_dir: the directory where cache files are stored.
  42      :param threshold: the maximum number of items the cache stores before
  43                        it starts deleting some. A threshold value of 0
  44                        indicates no threshold.
  45      :param default_timeout: the default timeout that is used if no timeout is
  46                              specified on :meth:`~BaseCache.set`. A timeout of
  47                              0 indicates that the cache never expires.
  48      :param mode: the file mode wanted for the cache files, default 0600
  49      :param hash_method: Default hashlib.md5. The hash method used to
  50                          generate the filename for cached results.
  51                          Default is lazy loaded and can be overriden by
  52                          seeting  `_default_hash_method`
  53      z.__wz_cacheZ__wz_cache_count���,N�	cache_dir�	threshold�default_timeout�mode�hash_methodc
  54  Cs�t�||�||_||_|j|_|dur||_||_|jdur$|��|_zt�	|j�Wnt
  55  yE}z
  56  |jtjkr;�WYd}~nd}~ww|jdkrY|j
  57  tt|����d�dSdS)Nr��value)r�__init__�_path�
  58  _threshold�_default_hash_method�_hash_method�_mode�_get_compatible_platform_mode�os�makedirs�OSError�errno�EEXIST�
  59  _update_count�len�list�	_list_dir)�selfrrrrr�exr
  60  r
  61  rr8s(
  62  
  63  ���
  64  �zFileSystemCache.__init__r
  65  cCsd}t��dkrtj}|S)Ni��Windows)�platform�system�stat�S_IWRITE)r*rr
  66  r
  67  rr Zsz-FileSystemCache._get_compatible_platform_modecCs|�|j�pdS�Nr)�get�_fs_count_file�r*r
  68  r
  69  r�_file_count`szFileSystemCache._file_count�deltarcCs<|jdkrdS|r|j|}n|pd}|j|j|dd�dS)NrT)�mgmt_element)rr5�setr3)r*r6rZ	new_countr
  70  r
  71  rr&ds
  72  zFileSystemCache._update_count�timeoutcCs*t�||�}|dkrtt��|}t|�Sr1)r�_normalize_timeout�intr)r*r9r
  73  r
  74  rr:psz"FileSystemCache._normalize_timeout�namecCs,|�|j��tj�d}||kp|�|j�S)N�����)�
  75  _get_filenamer3�splitr!�sep�endswith�_fs_transaction_suffix)r*r<Zfshashr
  76  r
  77  r�_is_mgmtvszFileSystemCache._is_mgmtcs�fdd�t��j�D�S)z2return a list of (fully qualified) cache filenamesc3s*�|]}��|�stj��j|�VqdS�N)rCr!�path�joinr)�.0�fnr4r
  78  r�	<genexpr>|s��
  79  �z,FileSystemCache._list_dir.<locals>.<genexpr>)r!�listdirrr4r
  80  r4rr)zs
  81  
  82  �zFileSystemCache._list_dircCs|jdko
  83  |j|jkSr1)rr5r4r
  84  r
  85  r�_over_threshold�szFileSystemCache._over_threshold�nowcCs�|��D]U}z6|�|d��}t�d|�d��d}Wd�n1s#wY|dkr;||kr;t�|�|jdd�WqtyEYqt	t
  86  tjfyYtj
  87  d|dd	�YqwdS)
  88  N�rb�I�rr=�r6�/Exception raised while handling cache file '%s'T��exc_info)r)�_safe_stream_open�struct�unpack�readr!�remover&�FileNotFoundErrorr#�EOFError�error�logging�warning)r*rL�fname�f�expiresr
  89  r
  90  r�_remove_expired�s&�
  91  �
  92  ���zFileSystemCache._remove_expiredcCs(g}|��D]I}z*|�|d��}t�d|�d��d}|�||f�Wd�n1s,wYWqty;Yqtttj	fyOt
  93  jd|dd�Yqwdd	�t|d
  94  d�d�D�}|D]2}z
  95  t
  96  �|�|jd
  97  d�WntywYnty�t
  98  jd|dd�YdSw|��s�dSq_dS)NrMrNrOrrQTrRcss�|]\}}|VqdSrDr
  99  )rG�_r^r
 100  r
 101  rrI�s�
 102  �z0FileSystemCache._remove_older.<locals>.<genexpr>cSs|dSr1r
 103  )�itemr
 104  r
 105  r�<lambda>�sz/FileSystemCache._remove_older.<locals>.<lambda>)�keyr=rPF)r)rTrUrVrW�appendrYr#rZr[r\r]�sortedr!rXr&rK)r*Zexp_fname_tuplesr^r_�	timestampZfname_sortedr
 106  r
 107  r�
 108  _remove_older�sN��
 109  ���
 110  ���zFileSystemCache._remove_oldercCs0|��rt�}|�|�|��r|��dSdSrD)rKrrari)r*rLr
 111  r
 112  r�_prune�s
 113  �zFileSystemCache._prunec	Csxt|���D]-\}}zt�|�WqtyYqty3tjd|dd�|j|d�YdSw|jdd�dS)NrQTrRrPFrr)	�	enumerater)r!rXrYr#r\r]r&)r*�ir^r
 114  r
 115  r�clear�s ��zFileSystemCache.clearrecCsFt|t�r|�d�}|�|���}n	tdt|�����tj�	|j
 116  |�S)Nzutf-8z$Key must be a string, received type )�
 117  isinstance�str�encoder�	hexdigest�	TypeError�typer!rErFr)r*reZbkeyZ	bkey_hashr
 118  r
 119  rr>�s
 120  
 121  
 122  zFileSystemCache._get_filenamec
 123  Cs�|�|�}z=|�|d��,}t�d|�d��d}|dks!|t�kr/|j�|�Wd�WSWd�WdS1s;wYWdStyLYdSt	t
 124  tjfyatj
 125  d|dd�YdSw)NrMrNrOrrQTrR)r>rTrUrVrWr�
 126  serializer�loadrYr#rZr[r\r]�r*re�filenamer_Zpickle_timer
 127  r
 128  rr2�s.
 129  
 130  �������zFileSystemCache.getcCs(|�|�}tj�|�s|�|||�SdS�NF)r>r!rE�existsr8)r*rerr9rwr
 131  r
 132  r�add�s
 133  zFileSystemCache.addFr7cCs
 134  |rd}n|��|�|�}|�|�}tj�|�}zKtj|j|j	d�\}}t�
 135  |d��}	|	�t�
 136  d|��|j�||	�Wd�n1sFwY|�tj||�|�tj||j�t|���j}
 137  Wntyvtjd|dd�YdSw|s�|s�|jd	d
 138  �|
 139  dkS)Nr)�suffix�dir�wbrNrQTrRF�rP)rjr:r>r!rE�isfile�tempfile�mkstemprBr�fdopen�writerU�packrt�dump�_run_safely�replace�chmodrrr/�st_sizer#r\r]r&)r*rerr9r7rw�	overwrite�fd�tmpr_�fsizer
 140  r
 141  rr8�s8
 142  
 143  
 144  ����	zFileSystemCache.setcCs^z
 145  t�|�|��WntyYdSty$tjddd�YdSw|s-|jdd�dS)NTz*Exception raised while handling cache filerRFr=rP)r!rXr>rYr#r\r]r&)r*rer7r
 146  r
 147  r�deletes�zFileSystemCache.deletec
 148  Cs�|�|�}z:|�|d��)}t�d|�d��d}|dks!|t�kr+	Wd�WdS	Wd�WdS1s8wYWdStyIYdStttj	fy^t
 149  jd|dd�YdSw)	NrMrNrOrTFrQrR)r>rTrUrVrWrrYr#rZr[r\r]rvr
 150  r
 151  r�has*s&
 152  �(���zFileSystemCache.hasrH�args�kwargscOs�t��dkr9d}d}d}d}||kr7z||i|��}W	|Sty2t|�||7}|d9}Ynw||ks|S||i|��}|S)ztOn Windows os.replace, os.chmod and open can yield
 153          permission errors if executed by two different processes.r,Ng����MbP?g$@g�)r-r.�PermissionErrorr)r*rHr�r��outputZ	wait_stepZmax_sleep_timeZtotal_sleep_timer
 154  r
 155  rr�=s&����zFileSystemCache._run_safelyrEccs<�|�t||�}|durt�z
 156  |VW|��dS|��wrD)r��openr#�close)r*rEr�fsr
 157  r
 158  rrTTs�z!FileSystemCache._safe_stream_open)rrNN)NN)r
 159  NrDrx)F)*�__name__�
 160  __module__�__qualname__�__doc__rBr3�staticmethodrrrrtror;�_t�Optional�Anyrr �propertyr5r&r:�boolrC�	Generatorr)rK�floatrarirjrmr>r2rzr8r�r��Callabler�rrTr
 161  r
 162  r
 163  rrsz�����
 164  �"���
 165  �
 166  #$
 167  �����
 168  �+"r)r)r$rr\r!r-r/rUr��typingr��
 169  contextlibr�pathlibrrrZ
 170  cachelib.baserZcachelib.serializersr�bytesr�rrr
 171  r
 172  r
 173  r�<module>s"