a
    ©!cV  ã                   @   sš   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Zd dl	Z	d dl
Z
d dlZd dlZe
 d¡ZG dd„ deƒZG dd	„ d	eƒZG d
d„ deƒZdS )é    )Úabsolute_import)Úunicode_literals)Ú_NÚdnfc                   @   s,   e Zd Zdd„ Zg fdd„Zedd„ ƒZdS )ÚJSONDBc                 C   s0   t j |¡s,tj t j |¡¡ |  |g ¡ d S ©N)ÚosÚpathÚisfiler   ÚutilZ
ensure_dirÚdirnameÚ_write_json_db)ÚselfÚ	json_path© r   ú1/usr/lib/python3.9/site-packages/dnf/persistor.pyÚ_check_json_db*   s    zJSONDB._check_json_dbc              
   C   sš   t |dƒ}| ¡ }W d   ƒ n1 s(0    Y  |dkrXt tdƒ|¡ |  ||¡ n>zt |¡}W n. ty” } zt |¡ W Y d }~n
d }~0 0 |S )NÚrÚ z%s is empty file)	ÚopenÚreadÚloggerÚwarningr   r   ÚjsonÚloadsÚ
ValueError)r   r   ÚdefaultÚfÚcontentÚer   r   r   Ú_get_json_db0   s    & zJSONDB._get_json_dbc                 C   s:   t | dƒ}t ||¡ W d   ƒ n1 s,0    Y  d S )NÚw)r   r   Údump)r   r   r   r   r   r   r   >   s    zJSONDB._write_json_dbN)Ú__name__Ú
__module__Ú__qualname__r   r    Ústaticmethodr   r   r   r   r   r   (   s   r   c                   @   s<   e Zd ZdZdd„ Zedd„ ƒZdd„ Zdd	„ Zd
d„ Z	dS )ÚRepoPersistorzePersistent data kept for repositories.

    Is arch/releasever specific and stores to cachedir.

    c                 C   s*   || _ tj | j d¡| _tƒ | _d| _d S )Nzexpired_repos.jsonF)Úcachedirr   r	   ÚjoinÚdb_pathÚsetÚexpired_to_addÚreset_last_makecache©r   r(   r   r   r   Ú__init__K   s    zRepoPersistor.__init__c                 C   s   t j | jd¡S )NZlast_makecache)r   r	   r)   r(   ©r   r   r   r   Ú_last_makecache_pathQ   s    z"RepoPersistor._last_makecache_pathc              
   C   sZ   z|   | j¡ t|  | j¡ƒW S  tyT } zt tdƒ|¡ W Y d }~d S d }~0 0 d S )Nz&Failed to load expired repos cache: %s)r   r*   r+   r    ÚOSErrorr   r   r   ©r   r   r   r   r   Úget_expired_reposU   s    zRepoPersistor.get_expired_reposc              
   C   sž   z$|   | j¡ |  | jt| jƒ¡ W n6 tyZ } zt tdƒ|¡ W Y d }~dS d }~0 0 | j	ršzt
j | j¡ W dS  ty˜   t tdƒ¡ Y dS 0 d S )Nz'Failed to store expired repos cache: %sFTz#Failed storing last makecache time.)r   r*   r   Úlistr,   r2   r   r   r   r-   r   r   Útouchr1   ÚIOErrorr3   r   r   r   Úsave]   s    zRepoPersistor.savec                 C   s<   zt tj | j¡ƒW S  ty6   t tdƒ¡ Y d S 0 d S )Nz'Failed determining last makecache time.)	Úintr   r   Zfile_ager1   r2   r   r   r   r0   r   r   r   Úsince_last_makecachel   s
    z"RepoPersistor.since_last_makecacheN)
r#   r$   r%   Ú__doc__r/   Úpropertyr1   r4   r8   r:   r   r   r   r   r'   D   s   
r'   c                   @   s,   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	S )
ÚTempfilePersistorc                 C   s"   t j |d¡| _tƒ | _d| _d S )Nztempfiles.jsonF)r   r	   r)   r*   r+   Útempfiles_to_addÚ_emptyr.   r   r   r   r/   v   s    zTempfilePersistor.__init__c                 C   s   |   | j¡ |  | j¡S r   )r   r*   r    r0   r   r   r   Úget_saved_tempfiles{   s    z%TempfilePersistor.get_saved_tempfilesc                 C   sl   | j s| jsd S |  | j¡ | j r4|  | jg ¡ d S | jrht|  | j¡ƒ}| | j¡ |  | jt|ƒ¡ d S r   )	r?   r>   r   r*   r   r+   r    Úupdater5   )r   Údatar   r   r   r8      s    zTempfilePersistor.savec                 C   s
   d| _ d S )NT)r?   r0   r   r   r   Úempty‹   s    zTempfilePersistor.emptyN)r#   r$   r%   r/   r@   r8   rC   r   r   r   r   r=   t   s   r=   )Z
__future__r   r   Zdnf.i18nr   Zdnf.utilr   ÚerrnoÚfnmatchr   Zloggingr   ÚreZ	getLoggerr   Úobjectr   r'   r=   r   r   r   r   Ú<module>   s   
0