a
    ˆ�Ájù  ã                   @   sò   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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d dlZd dlZe d¡Zg d	¢d
gdgdgg d¢dœZdd„ Zdd„ Zdd„ Zdd„ ZG dd„ dejƒZdS )é    )Úabsolute_import)Úunicode_literals)Úcommands)ÚCleanCommandLock)Ú_ÚP_)ÚmiscNÚdnf)ÚmetadataÚdbcacheúexpire-cacheÚpackagesr   r   )r
   r   r   )r
   r   r   r   Úallc                 c   sN   t  | ¡D ]>\}}}t j || ¡}|D ] }t j ||¡}t j |¡V  q&q
dS )z:Traverse dirpath recursively and yield relative filenames.N)ÚosÚwalkÚpathÚrelpathÚjoinÚnormpath)ÚdirpathÚrootÚdirsÚfilesÚbaseÚfr   © r   ú:/usr/lib/python3.9/site-packages/dnf/cli/commands/clean.pyÚ_tree2   s
    r   c                    s   ‡ fdd„| D ƒS )z5Yield those filenames that match any of the patterns.c                 3   s(   | ] }ˆ D ]}t  ||¡r
|V  q
qd S ©N©ÚreÚmatch)Ú.0r   Úp©Úpatternsr   r   Ú	<genexpr>=   ó    z_filter.<locals>.<genexpr>r   )r   r%   r   r$   r   Ú_filter;   s    r(   c                 C   sH   d}|D ]:}t j | |¡}t tjjtdƒ|¡ t	 
|¡ |d7 }q|S )z(Remove the given filenames from dirpath.r   zRemoving file %sé   )r   r   r   ÚloggerÚlogr	   ÚloggingZDDEBUGr   r   Zunlink_f)r   r   Úcountr   r   r   r   r   Ú_clean@   s    

r.   c                    s0   t jjd ‰ ‡ fdd„| D ƒ}tdd„ |D ƒƒS )z:Return the repo IDs that have some cached metadata around.r
   c                 3   s   | ]}t  ˆ |¡V  qd S r   r   )r"   r   ©Zmetapatr   r   r&   N   r'   z _cached_repos.<locals>.<genexpr>c                 s   s   | ]}|r|  d ¡V  qdS )ZrepoidN)Úgroup)r"   Úmr   r   r   r&   O   r'   )r	   ÚrepoÚCACHE_FILESÚset)r   Zmatchesr   r/   r   Ú_cached_reposK   s    r5   c                   @   s8   e Zd ZdZdZedƒZedd„ ƒZdd„ Z	dd	„ Z
d
S )ÚCleanCommandzSA class containing methods needed by the cli to execute the
    clean command.
    )Zcleanzremove cached datac                 C   s   | j ddt ¡ tdƒd� d S )NÚtypeú+zMetadata type to clean)ÚnargsÚchoicesÚhelp)Úadd_argumentÚ_CACHE_TYPESÚkeysr   )Úparserr   r   r   Úset_argparserZ   s    þzCleanCommand.set_argparserc                 C   s   t j| jj_d S r   )r   ZWRITEZcliZdemandsZclean_command_lock)Úselfr   r   r   Ú	configure`   s    zCleanCommand.configurec              
   C   sÞ  | j jj}tj |d¡}tj |d¡}tj | j jjd¡}�z<|��  |� �ø |�Ò t	dd„ | j
jD ƒƒ}tt|ƒƒ}t tdd |¡ ƒ¡ d|v rÆt|ƒ}| j jj |¡ | d¡ t tdƒ¡ dd	„ |D ƒ}t|t||ƒƒ}	t td
d|	ƒ|	 ¡ W d   ƒ W d   ƒ W d   ƒ W d S 1 �s,0    Y  W d   ƒ n1 �sL0    Y  W d   ƒ n1 �sl0    Y  W q: tjj�yÖ }
 z@| j jj�s¾tdƒ|
j }t |¡ t  d¡ n|
‚W Y d }
~
q:d }
~
0 0 q:d S )NTc                 s   s    | ]}t | D ]
}|V  qqd S r   )r=   )r"   ÚcÚtr   r   r   r&   k   r'   z#CleanCommand.run.<locals>.<genexpr>zCleaning data: ú r   zCache was expiredc                 S   s   g | ]}t jj| ‘qS r   )r	   r2   r3   )r"   rD   r   r   r   Ú
<listcomp>u   r'   z$CleanCommand.run.<locals>.<listcomp>z%d file removedz%d files removedz*Waiting for process with pid %d to finish.é   )!r   ZconfÚcachedirr	   ÚlockZbuild_metadata_lockZbuild_download_lockZbuild_rpmdb_lockZ
persistdirr4   Zoptsr7   Úlistr   r*   Údebugr   r   r5   Z_repo_persistorZexpired_to_addÚupdateÚremoveÚinfor.   r(   r   Ú
exceptionsZ	LockErrorZexit_on_lockÚpidÚtimeÚsleep)rA   rH   Zmd_lockZdownload_lockZ
rpmdb_lockÚtypesr   Zexpiredr%   r-   ÚeÚmsgr   r   r   Úrunc   s0    

€
zCleanCommand.runN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Úaliasesr   ZsummaryÚstaticmethodr@   rB   rV   r   r   r   r   r6   R   s   
r6   )Z
__future__r   r   Zdnf.clir   Zdnf.cli.demandr   Zdnf.i18nr   r   Zdnf.yumr   r	   Zdnf.exceptionsZdnf.lockZdnf.loggingZdnf.repor,   r   r    rQ   Z	getLoggerr*   r=   r   r(   r.   r5   ZCommandr6   r   r   r   r   Ú<module>   s4   
û		