a
    !cA                     @   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	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)_)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   r5   r   r   r   	configureR   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}|rt| d S | jjrt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_moduleW   s0    z*ModuleCommand.ListSubCommand.run_on_moduleNr.   r/   r0   aliasesr   summaryr9   rE   r   r   r   r   ListSubCommandM   s   rI   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 r3   r4   r8   r   r   r   r9   t   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_infor@   r   rA   rB   r   r   rC   r   r   r   rE   y   s    
z*ModuleCommand.InfoSubCommand.run_on_moduleNrF   r   r   r   r   InfoSubCommando   s   rM   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 r3   r   r5   r6   r7   	resolving	root_userr8   r   r   r   r9      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   rN   r   r   r   rA   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   rE      s    
z,ModuleCommand.EnableSubCommand.run_on_moduleNrF   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 r3   rO   r8   r   r   r   r9      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 rR   )r   r`   r   r   r   rA   rT   r   rU   rV   rW   rX   rY   r<   r   r=   rZ   r   r[   r\   r]   r   r   r   rE      s    
z-ModuleCommand.DisableSubCommand.run_on_moduleNrF   r   r   r   r   DisableSubCommand   s   ra   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 r3   rO   r8   r   r   r   r9      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   rb   r   r   r   rA   rT   r   rU   rV   rW   r   r[   r\   r]   r   r   r   rE      s    
z+ModuleCommand.ResetSubCommand.run_on_moduleNrF   r   r   r   r   ResetSubCommand   s   rc   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|_d S r3   rO   r8   r   r   r   r9      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   rd   r   r   r   rU   rV   r   rA   rT   rW   rX   r   r[   r\   r]   r   r   r   rE      s    
z-ModuleCommand.InstallSubCommand.run_on_moduleNrF   r   r   r   r   InstallSubCommand   s   re   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|_d S r3   rO   r8   r   r   r   r9      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   rE      s    z,ModuleCommand.UpdateSubCommand.run_on_moduleNrF   r   r   r   r   UpdateSubCommand   s   rg   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   r5   Zallow_erasingr6   Zfresh_metadatarP   rQ   r7   r8   r   r   r   r9      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 rtd|}	t|	 q~| jjj|| jjjd q~|sd S ttjj|d d S )Nr   z0Package {} belongs to multiple modules, skipping)Z
clean_deps)rW   )r   rh   r   r   allr*   r-   r   r   r$   ZgetModulePackagessackr%   r?   filtermr   r"   r   r!   Zgoalri   rU   Zclean_requirements_on_remover[   r   rA   rT   )
r   Zskipped_groupsr'   Zremove_names_from_specr(   Z
keep_namesZremove_queryZ
keep_querypkgrD   r   r   r   rE      s.    
z,ModuleCommand.RemoveSubCommand.run_on_moduleNrF   r   r   r   r   RemoveSubCommand   s   	ro   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   s.   | j j}d|_d|_d|_d|_d| jj_d S r3   )	r   r5   r6   r7   rP   rQ   r   rU   Zmodule_stream_switchr8   r   r   r   r9     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)rV   )r   Z	switch_tor   r   r   rU   rV   r   rA   rT   rW   rX   r   r[   r\   r]   r   r   r   rE   "  s    
z.ModuleCommand.SwitchToSubCommand.run_on_moduleNrF   r   r   r   r   SwitchToSubCommand  s   rp   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 r3   r4   r8   r   r   r   r9   0  s    z*ModuleCommand.ProvidesSubCommand.configurec                 C   s    | j | jj}|rt| d S r	   )r   Z_what_providesr   r   r@   rL   r   r   r   rE   5  s    z.ModuleCommand.ProvidesSubCommand.run_on_moduleNrF   r   r   r   r   ProvidesSubCommand+  s   rq   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 r3   r4   r8   r   r   r   r9   ?  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|	 qdt|}
t|
 d S )N)Znevra_strictrj   
)r   r   r   r   r   r&   r-   	availabler?   r   rl   r%   rm   r,   r\   rf   sortedr@   )r   r'   r   r)   r(   Znames_from_specZspec_artifactsZpackage_stringsr%   rn   rC   r   r   r   rE   D  s$    z/ModuleCommand.RepoquerySubCommand.run_on_moduleNrF   r   r   r   r   RepoquerySubCommand:  s   ru   )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>d      z)ModuleCommand.__init__.<locals>.<genexpr>c                 S   s   i | ]}|j D ]
}||qqS r   rG   rv   rw   aliasr   r   r   
<dictcomp>f  s   z*ModuleCommand.__init__.<locals>.<dictcomp>)r
   r   r   SUBCMDSrw   _subcmd_name2obj)r   r   Zsubcmd_objsr   rx   r   r   b  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pd 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	--profilerK   zshow profile contentz--availablers   zshow only available packagesz--allrk   zremove all modular packagesc                 S   s
   | j d S Nr   r{   )xr   r   r   <lambda>~  rz   z-ModuleCommand.set_argparser.<locals>.<lambda>)keyr   z{}: {}r   rw   rS   z<modular command>rr   )nargschoicesmetavarr   r   zmodule-spec*zModule specification)r   r   r   )
Zadd_mutually_exclusive_groupadd_argumentr   rt   r   appendrG   r"   rH   rf   )r   parserZnarrowsZsubcommand_choicesZsubcommand_helprw   r   r   r   set_argparseri  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   rw   r   KeyErrorr   Z	optparserZprint_usager9   r   r   r   r   r9     s    

zModuleCommand.configurec                 C   s   |    | j  d S r	   )check_required_argumentrw   rE   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   rw   r   r   r   r"   r   utilZ	MAIN_PROGZcommand)r   Znot_required_argumentr   r   r   r     s    
z%ModuleCommand.check_required_argument)r.   r/   r0   r   Commandr   rI   rM   r_   ra   rc   re   rg   ro   rp   rq   ru   r   r   rG   r   rH   r   r   r9   r   r   r1   r   r   r   r   r   %   s0   '"%	r   )Z
__future__r   Zdnf.clir   r   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   