a
    !cn                     @   s   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)_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_tree1   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%   M   r&   z _cached_repos.<locals>.<genexpr>c                 s   s   | ]}|r| d V  qdS )ZrepoidN)group)r!   mr   r   r   r%   N   r&   )r   repoCACHE_FILESset)r   Zmatchesr   r.   r   _cached_reposJ   s    r4   c                   @   s0   e Zd ZdZdZedZe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_argparserY   s    zCleanCommand.set_argparserc              
   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 rt|}| 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st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%   g   r&   z#CleanCommand.run.<locals>.<genexpr>zCleaning data:  r   zCache was expiredc                 S   s   g | ]}t jj| qS r   )r   r1   r2   )r!   rA   r   r   r   
<listcomp>q   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
persistdirr3   Zoptsr6   listr   r)   debugr   r   r4   Z_repo_persistorZexpired_to_addupdateremoveinfor-   r'   r   
exceptionsZ	LockErrorZexit_on_lockpidtimesleep)selfrE   Zmd_lockZdownload_lockZ
rpmdb_locktypesr   Zexpiredr$   r,   emsgr   r   r   run_   s0    


zCleanCommand.runN)
__name__
__module____qualname____doc__aliasesr   Zsummarystaticmethodr?   rT   r   r   r   r   r5   Q   s   
r5   )Z
__future__r   r   Zdnf.clir   Zdnf.i18nr   r   Zdnf.yumr   r   Zdnf.exceptionsZdnf.lockZdnf.loggingZdnf.repor+   r   r   rN   Z	getLoggerr)   r<   r   r'   r-   r4   ZCommandr5   r   r   r   r   <module>   s2   
		