a
    !cl&                     @   s   d dl mZ d dlmZ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e	dZG dd deZG dd dejejZdS )	    )commands)_ucdNdnfc                   @   s   e Zd ZdZdZdZdZdS )ShellDemandSheetTN)__name__
__module____qualname__Zavailable_reposZ	resolvingZ	root_userZsack_activation r
   r
   :/usr/lib/python3.9/site-packages/dnf/cli/commands/shell.pyr   %   s   r   c                   @   s   e Zd ZdZedjejjdZ	ddddddddd	d
d
Z
dd Zedd Zdd Zdd Zdd Zdd Zd)ddZd*ddZd+ddZd,dd Zd!d" Zd-d#d$Zd.d%d&Zd/d'd(ZdS )0ShellCommand)shellshzrun an interactive {prog} shellprogrepoquitZts_runtransactionconfigresolvehelp)
r   
repositoryexitr   runtsr   r   
resolvedepr   c                 C   s$   t j| | tj|  d| _d S )Nz> )r   Command__init__cmdCmdprompt)selfclir
   r
   r   r   =   s    zShellCommand.__init__c                 C   s*   | j ddtdtdjtjjdd d S )Nscript?ZSCRIPTzScript to run in {prog} shellr   )nargsmetavarr   )add_argumentr   formatr   utilMAIN_PROG_UPPER)parserr
   r
   r   set_argparserB   s
    zShellCommand.set_argparserc              
   C   sl   | j j}t | j _t|D ]L}|dr*qzt| j j| W q tyd   t| j j|t|| Y q0 qd S )N__)r"   demandsr   dir
startswithgetattrAttributeErrorsetattr)r!   Zdefault_demandsattrr
   r
   r   	configureH   s    

zShellCommand.configurec                 C   s$   | j jr| | j j n|   d S N)optsr#   _run_scriptZcmdloopr!   r
   r
   r   r   U   s    zShellCommand.runc                 C   s    | j   d | j _| j   d S r6   )baseZ_finalize_base_transaction	fill_sackr9   r
   r
   r   _clean[   s    
zShellCommand._cleanc              
   C   sh  |r|dkrd S |dkrd}zt |}W n   |   Y d S 0 | jjjdd | jj|}|jdkrnd S |j| jv rt	| d| j|j  |dd   n| jj
|j}|d ur\|| j}z| jj||}W n ty   Y d S 0 z&t| jj|j_|  |  W nF tjjyX } z(ttd	d
 t|  W Y d }~d S d }~0 0 n|   d S )N
ZEOFr   F)Zreset_usager   r      Error: )shlexsplit_helpr"   	optparserr   Zparse_main_argsZcommandMAPPINGr1   Zcli_commandsgetZparse_command_args
SystemExitcopyZdeepcopyr.   r5   r   r   
exceptionsErrorloggererrorr   r   )r!   lineZs_liner7   Zcmd_clsr   er
   r
   r   onecmd`   s<    
$

zShellCommand.onecmdNc           	      C   s   dd }|rt |dkr&| d d S |d }t |dkrB|d nd }|d}|dkr|d | }||d d  }| jj|}|D ]}|||| q|sttd	| n|||| jj	 d S )
Nc                 S   sP   |rt || | n:ztd| t|t|  W n   ttd Y n0 d S )Nz{}: {}zUnsupported key value.)r3   printr(   r1   strrL   warningr   )keyvalconfr
   r
   r   print_or_set   s    z*ShellCommand._config.<locals>.print_or_set   r   r   r?   .zCould not find repository: %s)
lenrD   findr:   reposget_matchingrL   rS   r   rV   )	r!   argsrW   rT   rU   ZperiodZ	repo_namer]   r   r
   r
   r   _config   s$    	


zShellCommand._configc                 C   s   t |trt|dkr|d n|}d}|r|dkrBtd|}n|dkrZtd|}nv|dv rrtd|}n^|d	krtd
|}nF|dv rtd|}n.|dkrtd|}n|dv rtd|}|s| jj  td}td|  dS )zOutput help information.

        :param args: the command to output help information about. If
           *args* is an empty, general help will be output.
        r   Nr   z{} arg [value]
  arg: debuglevel, errorlevel, obsoletes, gpgcheck, assumeyes, exclude,
        repo_id.gpgcheck, repo_id.exclude
    If no value is given it prints the current value.
    If value is given it sets that value.r   z{} [command]
    print help)r   r   z{} arg [option]
  list: lists repositories and their status. option = [all | id | glob]
  enable: enable repositories. option = repository id
  disable: disable repositories. option = repository idr   z"{}
    resolve the transaction set)r   r   zy{} arg
  list: lists the contents of the transaction
  reset: reset (zero-out) the transaction
  run: run the transactionr   z{}
    run the transaction)r   r   z{}
    exit the shella  Shell specific arguments:

config                   set config options
help                     print help
repository (or repo)     enable, disable or list repositories
resolvedep               resolve the transaction set
transaction (or ts)      list, reset or run the transaction set
run                      resolve and run the transaction set
exit (or quit)           exit the shellr>   )	
isinstancelistr[   r   r(   r"   rE   Z
print_helprQ   )r!   r_   argmsgr
   r
   r   rD      sF    "
zShellCommand._helpc                 C   s   |r|d nd }|dv r6|  dd|dd    n|dv r| jjj}d}|dd  D ]L}||}|r|t||  d}qXtt	d	d t	d
 | jj
j| qX|r| j  d | j_n
| d d S )Nr   rb   Nz	repolist rA   r?   )ZenableZdisableFTr@   zUnknown repo: '%s'r   )rP   joinr"   r:   r]   r^   r1   rL   Zcriticalr   outputtermboldr<   Z_compsrD   )r!   r_   r   r]   r<   r   rr
   r
   r   _repo   s$    



zShellCommand._repoc              
   C   sN   z| j j| j jj W n0 tjjyH } zt| W Y d }~n
d }~0 0 d S r6   )	r"   r:   r   r.   Zallow_erasingr   rJ   ZDepsolveErrorrQ   )r!   r_   rO   r
   r
   r   _resolve   s    zShellCommand._resolvec              	   C   s   zTt |d6}| }|D ]}|ds| | qW d    n1 sH0    Y  W n8 ty   ttd| jj	j
| td Y n0 d S )Nrj   #z!Error: Cannot open %s for readingr?   )open	readlinesr0   rP   IOErrorrL   infor   r:   rg   rh   ri   sysr   )r!   filefdlinesrN   r
   r
   r   r8      s    
.zShellCommand._run_scriptc              
   C   s   |r|d nd }|dkr$|    d S |   |dv rZ| jjr| jj| jj}t| n||dkrz| j  W nB t	j
jy } z&ttdd t|  W Y d }~nd }~0 0 ttd |    n
| d d S )	Nr   resetre   r   r@   rA   z	Complete!r   )r=   rl   r:   r;   rg   Zlist_transactionrL   rq   Zdo_transactionr   rJ   rK   rM   r   r   rD   )r!   r_   r   outrO   r
   r
   r   r;   	  s"    0
zShellCommand._transactionc                 C   s   |  dg d S )Nr   )r;   r!   r_   r
   r
   r   _ts_run"  s    zShellCommand._ts_runc                 C   s   t td td d S )NzLeaving Shellr   )rL   rq   r   rr   r   rx   r
   r
   r   _quit%  s    zShellCommand._quit)N)N)N)N)N)N)N)r   r   r	   aliasesr   r(   r   r)   r*   ZsummaryrF   r   staticmethodr,   r5   r   r=   rP   r`   rD   rk   rl   r8   r;   ry   rz   r
   r
   r
   r   r   ,   s8   
&

;



r   )Zdnf.clir   Zdnf.i18nr   r   Zdnf.utilr   r   rI   ZloggingrB   rr   Z	getLoggerrL   objectr   r   r   r   r
   r
   r
   r   <module>   s   
