a
    !c$                     @   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
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mZ edZdZG dd	 d	eZG d
d deZdd Zdd Zdd Zdd Zdd ZdS )    )absolute_import)print_function)unicode_literalsN)_dnfzdnf.plugin.dynamicc                   @   s\   e Zd ZdZdZdZedd Zdd Zdd	 Z	d
d Z
dd Zdd Zdd Zdd ZdS )Pluginz5The base class custom plugins must derive from. #:apiz	<invalid>Nc                    s   t j }| jr| jn| j  fdd|jD }|D ]\}tj|r4z|	| W q4 t
y } z$tjtdt| W Y d }~q4d }~0 0 q4|S )Nc                    s   g | ]}d | f qS )z
%s/%s.conf ).0pathnamer   ./usr/lib/python3.9/site-packages/dnf/plugin.py
<listcomp>9       z&Plugin.read_config.<locals>.<listcomp>zParsing file failed: %s)libdnfconfZConfigParserconfig_namer   Zpluginconfpathosr
   isfileread	Exceptionr   
exceptionsZConfigErrorr   str)clsr   parserfilesfileer   r   r   read_config4   s    
0zPlugin.read_configc                 C   s   || _ || _d S N)basecliselfr    r!   r   r   r   __init__B   s    zPlugin.__init__c                 C   s   d S r   r   r#   r   r   r   
pre_configG   s    zPlugin.pre_configc                 C   s   d S r   r   r%   r   r   r   configK   s    zPlugin.configc                 C   s   d S r   r   r%   r   r   r   resolvedO   s    zPlugin.resolvedc                 C   s   d S r   r   r%   r   r   r   sackS   s    zPlugin.sackc                 C   s   d S r   r   r%   r   r   r   pre_transactionW   s    zPlugin.pre_transactionc                 C   s   d S r   r   r%   r   r   r   transaction[   s    zPlugin.transaction)__name__
__module____qualname____doc__r   r   classmethodr   r$   r&   r'   r(   r)   r*   r+   r   r   r   r   r   .   s   
r   c                   @   sv   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd ZdddZ	dd Z
dd Zdd Zdd Zdd Zdd ZdS )Pluginsc                 C   s   g | _ g | _d S r   )
plugin_clspluginsr%   r   r   r   r$   a   s    zPlugins.__init__c              	   C   sv   | j D ]j}zt||  W q tjjy2    Y q tyn   t \}}}t	|||}t
d| Y q0 qd S )N )r3   getattrr   r   Errorr   sysexc_info	tracebackformat_exceptionloggerZcriticaljoin)r#   methodpluginexc_type	exc_valueexc_tracebackZexcept_listr   r   r   _callere   s    
zPlugins._callerc                    st   | j dd D ]`}|j t fdd|D r0q||}|do\|ddo\|dd }|r| j | qdS )zwChecks whether plugins are enabled or disabled in configuration files
           and removes disabled plugins from listNc                 3   s   | ]}t   |V  qd S r   fnmatch)r	   patternr   r   r   	<genexpr>u   r   z)Plugins._check_enabled.<locals>.<genexpr>mainZenabled)r2   r   anyr   Zhas_sectionZ
has_optionZ
getbooleanremove)r#   r   enable_pluginsZplug_clsr   Zdisabledr   r   r   _check_enabledp   s    


zPlugins._check_enabledc                 C   s   t tjv rtdtjt  tjt < }g |_t|j	||}t
|| t dd | _| || t| jdkrtdd | jD }ttdd| dS )z)Dynamically load relevant plugin modules.zload_plugins() called twiceNr   c                 s   s   | ]}|j V  qd S r   r   )r	   r>   r   r   r   rF      r   z Plugins._load.<locals>.<genexpr>zLoaded plugins: %s, )DYNAMIC_PACKAGEr7   modulesRuntimeErrorr   Zpycomp
ModuleType__path___get_plugins_filesZ
pluginpath_import_modules_plugin_classesr2   rK   lensortedr;   debugr   r<   )r#   r   ZskipsrJ   packager   namesr   r   r   _load   s    

zPlugins._loadc                 C   s   |  d d S )Nr&   rB   r%   r   r   r   _run_pre_config   s    zPlugins._run_pre_configc                 C   s   |  d d S )Nr'   r[   r%   r   r   r   _run_config   s    zPlugins._run_configNc                 C   s&   | j D ]}|||}| j| qd S r   )r2   r3   append)r#   r    r!   Zp_clsr>   r   r   r   	_run_init   s    

zPlugins._run_initc                 C   s   |  d d S )Nr)   r[   r%   r   r   r   run_sack   s    zPlugins.run_sackc                 C   s   |  d d S )Nr(   r[   r%   r   r   r   run_resolved   s    zPlugins.run_resolvedc                 C   s   |  d d S )Nr*   r[   r%   r   r   r   run_pre_transaction   s    zPlugins.run_pre_transactionc                 C   s   |  d d S )Nr+   r[   r%   r   r   r   run_transaction   s    zPlugins.run_transactionc                 C   s   t td tjt= d S )NzPlugins were unloaded)r;   rW   r   r7   rN   rM   r%   r   r   r   _unload   s    zPlugins._unloadc                 C   s   |j s
dS t }| jD ]}||t|j< qt| }t }|j D ]}||	|j
 qD|sddS |jD ]}||j
 qj|D ]}| j||  qdS )zH
        Unload plugins that were removed in the `transaction`.
        N)Z
remove_setdictr3   inspectZgetfile	__class__setkeysupdateintersectionr   Zinstall_setdifference_updaterI   )r#   r+   r3   r>   Zplugin_filesZerased_plugin_filesZpkgZplugin_filer   r   r   unload_removed_plugins   s    


zPlugins.unload_removed_plugins)N)r,   r-   r.   r$   rB   rK   rZ   r\   r]   r_   r`   ra   rb   rc   rd   rm   r   r   r   r   r1   `   s   
r1   c                   C   s   t  S r   )r   __subclasses__r   r   r   r   rT      s    rT   c                 C   s   |D ]}t j|\}}| j| t j|\}}d| j|f }zt|}W q t	y } z2t
td|| t
jtjjddd W Y d }~qd }~0 0 qd S )Nz%s.%szFailed loading plugin "%s": %sr4   T)r8   )r   r
   splitrQ   r^   splitextr,   	importlibimport_moduler   r;   errorr   logr   loggingZSUBDEBUG)rX   Zpy_filesfnr
   moduleZextr   r   r   r   r   rS      s    rS   c                 C   s6  g }t |}t |}t  }t  }| D ]}td| D ]}tjtj|\}}	d}
d}|D ]B}t||r^|| d}
|D ]}t||r~d}
|| q~d}q^|s|D ]}t||r|| q|
r6|| q6q$|	|}|rt
tddt| |	|}|r2t
tddt| |S )Nz%s/*.pyTFz=No matches found for the following enable plugin patterns: {}rL   z>No matches found for the following disable plugin patterns: {})rh   globr   r
   rp   basename_plugin_name_matches_patternaddr^   
differencer;   Zwarningr   formatr<   rV   )pathsZdisable_pluginsrJ   r3   Zpattern_enable_foundZpattern_disable_foundprv   plugin_nameZdummyZmatchedZenable_pattern_testedZpattern_skipZpattern_enableZenable_not_foundZdisable_not_foundr   r   r   rR      sH    





rR   c                    s*   t | | ddf}t fdd|D S )z
    Checks plugin name matches the pattern.

    The alternative plugin name using dashes instead of underscores is tried
    in case of original name is not matched.

    (see https://bugzilla.redhat.com/show_bug.cgi?id=1980712)
    r   -c                 3   s   | ]}t  | V  qd S r   rC   )r	   r   rE   r   r   rF     r   z/_plugin_name_matches_pattern.<locals>.<genexpr>)rh   replacerH   )r   rE   Z	try_namesr   r   r   rz      s    
rz   c                    s<    fdd}t t jd tjf| jd d}| _ S )z5A class decorator for automatic command registration.c                    s   |r|   d S r   )register_commandr"   command_classr   r   r$     s    z"register_command.<locals>.__init__r   r   )r$   r   )typer   r,   r   r   aliasesZ_plugin)r   r$   Zplugin_classr   r   r   r     s    r   )Z
__future__r   r   r   rD   rx   rq   rf   ru   operatorr   r7   r9   r   Zdnf.loggingr   Z
dnf.pycompZdnf.utilZdnf.i18nr   Z	getLoggerr;   rM   objectr   r1   rT   rS   rR   rz   r   r   r   r   r   <module>   s2   
2g%