a
    ˆ�ÁjÂB  ã                   @   sš   d dl mZ d dlmZmZ d dlmZ 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G dd	„ d	ejƒZdS )
é    )Úprint_function)ÚcommandsÚCliError)ÚCleanCommandLock)Ú_)ÚNoModuleException)ÚloggerNc                       s*  e Zd ZG dd„ dejƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG d	d
„ d
eƒZ	G dd„ deƒZ
G dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZeeee	e
eeeeeehZehZdZedƒZ‡ fdd„Zdd„ Zdd „ Zd!d"„ Zd#d$„ Z‡  ZS )%ÚModuleCommandc                       s,   e Zd Z‡ fdd„Zdd„ Zdd„ Z‡  ZS )zModuleCommand.SubCommandc                    s(   t tj| ƒ |¡ tjj | j¡| _d S ©N)	Úsuperr	   Ú
SubCommandÚ__init__ÚdnfÚmoduleÚmodule_baseZ
ModuleBaseÚbase)ÚselfÚcli©Ú	__class__© ú;/usr/lib/python3.9/site-packages/dnf/cli/commands/module.pyr   )   s    z!ModuleCommand.SubCommand.__init__c           	      C   s¬   t ƒ }| jjD ]˜}| j |¡\}}|d u r,q|jr8|jnd}|jrH|jnd}|jr\|jdksb|jrvt	 
tdƒ |¡¡ |jr‚|jnd}| jj ||dd|¡}| |¡ q|S )NÚ éÿÿÿÿzjOnly module name, stream, architecture or profile is used. Ignoring unneeded information in argument: '{}')ÚsetÚoptsÚmodule_specr   Ú_get_modulesÚnameÚstreamÚversionÚcontextr   Úinfor   ÚformatÚarchr   Ú_moduleContainerÚqueryÚupdate)	r   Úmodules_from_specsr   Ú__Znsvcapr   r   r$   Úmodulesr   r   r   Ú#_get_modules_from_name_stream_specs-   s    þz<ModuleCommand.SubCommand._get_modules_from_name_stream_specsc           	      C   s~   t ƒ }t ƒ }|D ](}||vr| jj |¡r| | ¡ ¡ q|D ]6}t |¡}|jtj	gd�D ]}|j
r\| |j
¡ q\q>||fS )N)Zforms)r   r   r%   ZisModuleActiver'   ZgetArtifactsÚhawkeyZSubjectZget_nevra_possibilitiesZ
FORM_NEVRAr   Úadd)	r   Zuse_modulesZskip_modulesZ	artifactsZ	pkg_namesr   ZartifactZsubjZ	nevra_objr   r   r   Ú_get_module_artifact_names?   s    
ÿ
z3ModuleCommand.SubCommand._get_module_artifact_names)Ú__name__Ú
__module__Ú__qualname__r   r+   r.   Ú__classcell__r   r   r   r   r   '   s   r   c                   @   s(   e Zd ZdZedƒZdd„ Zdd„ ZdS )zModuleCommand.ListSubCommand)Úlistz,list all module streams, profiles and statesc                 C   s   | j j}d|_d|_d S ©NT©r   ÚdemandsÚavailable_reposÚsack_activation©r   r6   r   r   r   Ú	configureS   s    z&ModuleCommand.ListSubCommand.configurec                 C   s¬   | j }| jjr&| | jjtjjj¡}nV| jj	rF| | jjtjjj
¡}n6| jjrf| | jjtjjj¡}n| | jjtjjj¡}|rŒt|ƒ d S | jjr¨tdƒ}tj |¡‚d S ©NzNo matching Modules to list)r   r   ÚenabledZ_get_brief_descriptionr   Úlibdnfr   ÚModulePackageContainerZModuleState_ENABLEDÚdisabledZModuleState_DISABLEDÚ	installedZModuleState_INSTALLEDZModuleState_UNKNOWNÚprintr   r   Ú
exceptionsÚError)r   ZmodsÚoutputÚmsgr   r   r   Úrun_on_moduleX   s0    ÿþþÿz*ModuleCommand.ListSubCommand.run_on_moduleN©r/   r0   r1   Úaliasesr   Úsummaryr:   rF   r   r   r   r   ÚListSubCommandN   s   rJ   c                   @   s(   e Zd ZdZedƒZdd„ Zdd„ ZdS )zModuleCommand.InfoSubCommand)r"   z)print detailed information about a modulec                 C   s   | j j}d|_d|_d S r4   r5   r9   r   r   r   r:   u   s    z&ModuleCommand.InfoSubCommand.configurec                 C   sf   | j jr| j | j j¡}n*| j jr4| j | j j¡}n| j | j j¡}|rRt|ƒ nt	j
 tdƒ¡‚d S r;   )r   Úverboser   Z_get_full_infor   ÚprofileZ_get_info_profilesZ	_get_inforA   r   rB   rC   r   ©r   rD   r   r   r   rF   z   s    
z*ModuleCommand.InfoSubCommand.run_on_moduleNrG   r   r   r   r   ÚInfoSubCommandp   s   rN   c                   @   s(   e Zd ZdZedƒZdd„ Zdd„ ZdS )zModuleCommand.EnableSubCommand)Úenablezenable a module streamc                 C   s$   | j j}d|_d|_d|_d|_d S r4   ©r   r6   r7   r8   Ú	resolvingÚ	root_userr9   r   r   r   r:   ‹   s
    z(ModuleCommand.EnableSubCommand.configurec              
   C   sˆ   z| j  | jj¡ W nn tjjy‚ } zR| jjj	r`|j
s>|jrB|‚|jr`|jd tjjjkr`|‚t t|ƒ¡ W Y d }~n
d }~0 0 d S ©Né   )r   rO   r   r   r   rB   ÚMarkingErrorsr   ÚconfÚstrictÚno_match_group_specsÚerror_group_specsÚmodule_depsolv_errorsr=   r   r>   Ú!ModuleErrorType_ERROR_IN_DEFAULTSr   ÚerrorÚstr©r   Úer   r   r   rF   ’   s    
ÿz,ModuleCommand.EnableSubCommand.run_on_moduleNrG   r   r   r   r   ÚEnableSubCommand†   s   r`   c                   @   s(   e Zd ZdZedƒZdd„ Zdd„ ZdS )zModuleCommand.DisableSubCommand)Údisablez%disable a module with all its streamsc                 C   s$   | j j}d|_d|_d|_d|_d S r4   rP   r9   r   r   r   r:   £   s
    z)ModuleCommand.DisableSubCommand.configurec              
   C   sˆ   z| j  | jj¡ W nn tjjy‚ } zR| jjj	r`|j
s>|jrB|‚|jr`|jd tjjjkr`|‚t t|ƒ¡ W Y d }~n
d }~0 0 d S rS   )r   ra   r   r   r   rB   rU   r   rV   rW   rX   rY   rZ   r=   r   r>   r[   r   r\   r]   r^   r   r   r   rF   ª   s    
ÿz-ModuleCommand.DisableSubCommand.run_on_moduleNrG   r   r   r   r   ÚDisableSubCommandž   s   rb   c                   @   s(   e Zd ZdZedƒZdd„ Zdd„ ZdS )zModuleCommand.ResetSubCommand)Úresetzreset a modulec                 C   s$   | j j}d|_d|_d|_d|_d S r4   rP   r9   r   r   r   r:   »   s
    z'ModuleCommand.ResetSubCommand.configurec              
   C   sd   z| j  | jj¡ W nJ tjjy^ } z.| jjj	r<|j
r<|‚t t|ƒ¡ W Y d }~n
d }~0 0 d S r
   )r   rc   r   r   r   rB   rU   r   rV   rW   rX   r   r\   r]   r^   r   r   r   rF   Â   s    
z+ModuleCommand.ResetSubCommand.run_on_moduleNrG   r   r   r   r   ÚResetSubCommand¶   s   rd   c                   @   s(   e Zd ZdZedƒZdd„ Zdd„ ZdS )zModuleCommand.InstallSubCommand)Úinstallz/install a module profile including its packagesc                 C   s,   | j j}d|_d|_d|_d|_tj|_d S r4   ©	r   r6   r7   r8   rQ   rR   r   ÚREADÚclean_command_lockr9   r   r   r   r:   Ð   s    z)ModuleCommand.InstallSubCommand.configurec              
   C   sr   z| j  | jj| jjj¡ W nP tjj	yl } z4| jjjrJ|j
sF|jrJ|‚t t|ƒ¡ W Y d }~n
d }~0 0 d S r
   )r   re   r   r   r   rV   rW   r   rB   rU   rX   rY   r   r\   r]   r^   r   r   r   rF   Ø   s    
z-ModuleCommand.InstallSubCommand.run_on_moduleNrG   r   r   r   r   ÚInstallSubCommandË   s   ri   c                   @   s(   e Zd ZdZedƒZdd„ Zdd„ ZdS )zModuleCommand.UpdateSubCommand)r'   z0update packages associated with an active streamc                 C   s,   | j j}d|_d|_d|_d|_tj|_d S r4   rf   r9   r   r   r   r:   æ   s    z(ModuleCommand.UpdateSubCommand.configurec                 C   s&   | j  | jj¡}|r"td |¡ƒ‚d S )Nz, )r   Zupgrader   r   r   Újoin)r   Zmodule_specsr   r   r   rF   î   s    z,ModuleCommand.UpdateSubCommand.run_on_moduleNrG   r   r   r   r   ÚUpdateSubCommandá   s   rk   c                   @   s(   e Zd ZdZedƒZdd„ Zdd„ ZdS )zModuleCommand.RemoveSubCommand)ÚremoveÚerasez3remove installed module profiles and their packagesc                 C   s0   | j j}d|_d|_d|_d|_d|_d|_d S )NTF)r   r6   Zallow_erasingr7   Zfresh_metadatarQ   rR   r8   r9   r   r   r   r:   ø   s    z(ModuleCommand.RemoveSubCommand.configurec           
      C   sÞ   | j  | jj¡}| jjr¾|  ¡ }|  |tƒ ¡\}}|  | jj	 
¡ |¡\}}| jj ¡  ¡ j|d�}| jj ¡  ¡ j|d�}|D ]>}||v r¤tdƒ |¡}	t |	¡ q~| jjj|| jjjd� q~|sÆd S t tjj|d�¡ d S )N©r   z0Package {} belongs to multiple modules, skipping)Z
clean_deps)rX   )r   rl   r   r   Úallr+   r.   r   r   r%   ZgetModulePackagesÚsackr&   r@   Úfiltermr   r#   r   r"   Zgoalrm   rV   Zclean_requirements_on_remover\   r   rB   rU   )
r   Zskipped_groupsr(   Zremove_names_from_specr)   Z
keep_namesZremove_queryZ
keep_queryÚpkgrE   r   r   r   rF     s.    ÿÿÿ
ÿz,ModuleCommand.RemoveSubCommand.run_on_moduleNrG   r   r   r   r   ÚRemoveSubCommandó   s   	rs   c                   @   s(   e Zd ZdZedƒZdd„ Zdd„ ZdS )z ModuleCommand.SwitchToSubCommand)z	switch-toz7switch a module to a stream and distrosync rpm packagesc                 C   s6   | j j}d|_d|_d|_d|_tj|_d| j	j
_d S r4   )r   r6   r7   r8   rQ   rR   r   rg   rh   r   rV   Zmodule_stream_switchr9   r   r   r   r:     s    z*ModuleCommand.SwitchToSubCommand.configurec              
   C   st   z| j j| jj| jjjd� W nP tjj	yn } z4| jjjrL|j
sH|jrL|‚t t|ƒ¡ W Y d }~n
d }~0 0 d S )N)rW   )r   Z	switch_tor   r   r   rV   rW   r   rB   rU   rX   rY   r   r\   r]   r^   r   r   r   rF   &  s    
z.ModuleCommand.SwitchToSubCommand.run_on_moduleNrG   r   r   r   r   ÚSwitchToSubCommand  s   	rt   c                   @   s(   e Zd ZdZedƒZdd„ Zdd„ ZdS )z ModuleCommand.ProvidesSubCommand)Zprovideszlist modular packagesc                 C   s   | j j}d|_d|_d S r4   r5   r9   r   r   r   r:   4  s    z*ModuleCommand.ProvidesSubCommand.configurec                 C   s    | j  | jj¡}|rt|ƒ d S r
   )r   Z_what_providesr   r   rA   rM   r   r   r   rF   9  s    z.ModuleCommand.ProvidesSubCommand.run_on_moduleNrG   r   r   r   r   ÚProvidesSubCommand/  s   ru   c                   @   s(   e Zd ZdZedƒZdd„ Zdd„ ZdS )z!ModuleCommand.RepoquerySubCommand)Z	repoqueryz#list packages belonging to a modulec                 C   s   | j j}d|_d|_d S r4   r5   r9   r   r   r   r:   C  s    z+ModuleCommand.RepoquerySubCommand.configurec                 C   sØ   t ƒ }| jjD ]}| j |¡\}}| |¡ q|  |t ƒ ¡\}}t ƒ }| jjsV| jjs†| j	j
 ¡  ¡ j|d�}|D ]}	| t|	ƒ¡ qr| jjr¾| j	j
 ¡  ¡ j|d�}|D ]}	| t|	ƒ¡ qªd t|ƒ¡}
t|
ƒ d S )N)Znevra_strictrn   Ú
)r   r   r   r   r   r'   r.   Ú	availabler@   r   rp   r&   rq   r-   r]   rj   ÚsortedrA   )r   r(   r   r*   r)   Znames_from_specZspec_artifactsZpackage_stringsr&   rr   rD   r   r   r   rF   H  s$    ÿz/ModuleCommand.RepoquerySubCommand.run_on_moduleNrG   r   r   r   r   ÚRepoquerySubCommand>  s   ry   )r   zInteract with Modules.c                    s>   t t| ƒ ˆ ¡ ‡ fdd„| jD ƒ}d | _dd„ |D ƒ| _d S )Nc                 3   s   | ]}|ˆ ƒV  qd S r
   r   )Ú.0Úsubcmd©r   r   r   Ú	<genexpr>h  ó    z)ModuleCommand.__init__.<locals>.<genexpr>c                 S   s   i | ]}|j D ]
}||“qqS r   ©rH   ©rz   r{   Úaliasr   r   r   Ú
<dictcomp>j  s   z*ModuleCommand.__init__.<locals>.<dictcomp>)r   r	   r   ÚSUBCMDSr{   Ú_subcmd_name2obj)r   r   Zsubcmd_objsr   r|   r   r   f  s    ÿzModuleCommand.__init__c                 C   s  |  ¡ }|jdddtdƒd� |jdddtdƒd� |jd	d
dtdƒd� |jdddtdƒd� |jdddtdƒd� |jdddtdƒd� g }g }t| jdd„ d�D ]2}| |jd ¡ | d |jd |jpÒd¡¡ q¦|jdd|dd 	|¡d� |jd d!d"td#ƒd$� d S )%Nz	--enabledr<   Ú
store_truezshow only enabled modules)ÚdestÚactionÚhelpz
--disabledr?   zshow only disabled modulesz--installedr@   z'show only installed modules or packagesz	--profilerL   zshow profile contentz--availablerw   zshow only available packagesz--allro   zremove all modular packagesc                 S   s
   | j d S ©Nr   r   )Úxr   r   r   Ú<lambda>‚  r~   z-ModuleCommand.set_argparser.<locals>.<lambda>)Úkeyr   z{}: {}r   r{   rT   z<modular command>rv   )ÚnargsÚchoicesÚmetavarrˆ   r   zmodule-specÚ*zModule specification)r�   r�   rˆ   )
Zadd_mutually_exclusive_groupÚadd_argumentr   rx   rƒ   ÚappendrH   r#   rI   rj   )r   ÚparserZnarrowsZsubcommand_choicesZsubcommand_helpr{   r   r   r   Úset_argparserm  sH    þþþþ
ÿþ 
þ
ÿzModuleCommand.set_argparserc              	   C   sX   z| j | jjd  | _W n& ttfy>   | jj ¡  t‚Y n0 | j| j_| j ¡  d S r‰   )	r„   r   r{   r   ÚKeyErrorr   Z	optparserZprint_usager:   ©r   r   r   r   r:   ‹  s    

zModuleCommand.configurec                 C   s   |   ¡  | j ¡  d S r
   )Úcheck_required_argumentr{   rF   r–   r   r   r   Úrun”  s    zModuleCommand.runc                 C   sR   dd„ | j D ƒ}| jjd |vrN| jjsNttdƒ tjj	| jj
| jjd ¡ƒ‚d S )Nc                 S   s   g | ]}|j D ]}|‘qqS r   r   r€   r   r   r   Ú
<listcomp>™  s   
þz9ModuleCommand.check_required_argument.<locals>.<listcomp>r   z{} {} {}: too few arguments)ÚSUBCMDS_NOT_REQUIRED_ARGr   r{   r   r   r   r#   r   ÚutilZ	MAIN_PROGZcommand)r   Znot_required_argumentr   r   r   r—   ˜  s    ÿ
þÿz%ModuleCommand.check_required_argument)r/   r0   r1   r   ÚCommandr   rJ   rN   r`   rb   rd   ri   rk   rs   rt   ru   ry   rƒ   rš   rH   r   rI   r   r”   r:   r˜   r—   r2   r   r   r   r   r	   &   s0   '"%þ	r	   )Z
__future__r   Zdnf.clir   r   Zdnf.cli.demandr   Zdnf.i18nr   Zdnf.module.exceptionsr   Zdnf.utilr   r   ÚsysÚosr,   r=   Zdnf.module.module_baseZdnf.exceptionsrœ   r	   r   r   r   r   Ú<module>   s   