U
    Uw                     @   s2  d Z dZdZddlZddlZddlmZ ddlmZ ddl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mZ dd	lmZ ddlZejjjd
d ddlZddlmZ ddlmZ ddlm Z  e!dd Z"e!dd Z#ej$j%dkZ&G dd dZ'dd Z(e)dkr.e(  dS )z>
This module provides a command line client for the aptdaemon
z%Sebastian Heinlein <devel@glatzor.de>)ConsoleClientmain    N)gettext)ngettext)OptionParser)SourceEntry)GLibT)Zset_as_default   )client)enums)errors   z[1mz[0m   c                   @   s.  e Zd ZdZdLddZdMddZ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d Z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+d, Zd-d. Zd/d0 Zd1d2 ZdNd4d5Zd6d7 Zd8d9 Zd:d; Zd<d= Zd>d? Z d@dA Z!dBdC Z"dDdE Z#dFdG Z$dHdI Z%dJdK Z&d3S )Or   z4
    Command line interface client to aptdaemon
    TFc                 C   s   t  | _t \| _| _g | _ttj	| j
 ttj| j
 ttj| j |  | _g | _d | _d| _d| _d| _|| _|| _|| _d| _d| _d| _d| _d| _t | _d | _t ! | _"d S )NT r   z|/-\)#r
   Z	AptClient_clientptyopenpty	master_fdslave_fd_signalssignalSIGINT_on_cancel_signalSIGQUITSIGWINCH_on_terminal_resize_get_terminal_width_terminal_width	_watchers_old_tty_modeZ_show_status_status_percent_show_terminal_details_allow_unauthenticated_show_progress_status_details_progress_details_spin_elements	_spin_curtime_spin_stamp_transactionr   ZMainLoop_loop)selfshow_terminalallow_unauthenticateddetails r3   3/usr/lib/python3/dist-packages/aptdaemon/console.py__init__=   s.    


zConsoleClient.__init__r   c              
   C   s6   t |}| jj|j|j|j|j|j|| j| j	d dS )z#Add repository to the sources list.reply_handlererror_handlerN)
r   r   add_repositorytypeZuriZdistcompsZcomment_run_transaction_on_exception)r/   lineZsourcesfileentryr3   r3   r4   r9   X   s     zConsoleClient.add_repositoryc                 C   s   | j j|| j| jd dS zInstall repository key file.r6   N)r   add_vendor_key_from_filer<   r=   r/   pathr3   r3   r4   rA   a   s
    z&ConsoleClient.add_vendor_key_from_filec                 C   s   | j j||| j| jd dS r@   )r   add_vendor_key_from_keyserverr<   r=   )r/   Zkeyid	keyserverr3   r3   r4   rD   h   s     z+ConsoleClient.add_vendor_key_from_keyserverc                 C   s   | j j|| j| jd dS )zRemove repository key.r6   N)r   remove_vendor_keyr<   r=   )r/   Zfingerprintr3   r3   r4   rF   o   s    zConsoleClient.remove_vendor_keyc                 C   s   | j j|| j| jd dS )zInstall package file.r6   N)r   install_filer<   r=   rB   r3   r3   r4   rG   u   s    zConsoleClient.install_filec                    s"    fdd} j j| jd dS )z%List the keys of the trusted vendors.c                    s    | D ]}t | q j  d S N)printr.   quit)keyskeyr/   r3   r4   on_done|   s    
z7ConsoleClient.list_trusted_vendor_keys.<locals>.on_doner6   N)r   Zget_trusted_vendor_keysr=   )r/   rN   r3   rM   r4   list_trusted_vendor_keysz   s    z&ConsoleClient.list_trusted_vendor_keysc              
   C   s$   | j j||||||| j| jd dS )zCommit changesr6   N)r   commit_packagesr<   r=   )r/   install	reinstallremovepurgeupgrade	downgrader3   r3   r4   rP      s     zConsoleClient.commit_packagesc                 C   s   | j j| j| jd dS )zFix incomplete installsr6   N)r   fix_incomplete_installr<   r=   rM   r3   r3   r4   rW      s    z$ConsoleClient.fix_incomplete_installc                 C   s   | j j| j| jd dS )zRepair broken dependencies.r6   N)r   fix_broken_dependsr<   r=   rM   r3   r3   r4   rX      s    
z ConsoleClient.fix_broken_dependsc                 C   s   | j j| j| jd dS )zUpdate cacher6   N)r   update_cacher<   r=   rM   r3   r3   r4   rY      s    
zConsoleClient.update_cachec                 C   s   | j j|| j| jd dS )zUpgrade systemr6   N)r   upgrade_systemr<   r=   )r/   Z	safe_moder3   r3   r4   rZ      s    zConsoleClient.upgrade_systemc                 C   s   | j j||| j| jd dS )zReconfigure packages.r6   N)r   reconfigurer<   r=   )r/   packagespriorityr3   r3   r4   r[      s    
zConsoleClient.reconfigurec                 C   s   | j j| j| jd dS )zClean archives.r6   N)r   cleanr<   r=   rM   r3   r3   r4   r^      s    
zConsoleClient.cleanc                 C   s(   z| j   W n tk
r"   Y nX dS )z%Start the console client application.N)r.   runKeyboardInterruptrM   r3   r3   r4   r_      s    zConsoleClient.runc                 C   s   | j D ]}t| q|| _g | _ | j |d| j | j |d| j | j |d| j | j |d| j	 | j |d| j
 | j |d| j | jr|t| j || j dS )zMonitor the given transactionzterminal-attached-changedzstatus-changedzstatus-details-changedzprogress-changedzprogress-details-changedZfinishedN)r   r   source_remover-   appendZconnect_on_terminal_attached
_on_status_on_status_details_on_progress_on_progress_details_on_exitr#   Zset_terminalosttynamer   Zset_allow_unauthenticatedr%   )r/   transactionZhandlerr3   r3   r4   _set_transaction   s.    
zConsoleClient._set_transactionc                 C   s   |    | jr>dt| jd | jd t|tf }tj	| |tj
krvdtdt|jt|j|jf }t| | j  dS )z.Callback for the exit state of the transactionz[+] 100%% %s %-*.*s%s
	   z%s: %s
%s

%sZERRORN)_detachr&   	ANSI_BOLDr   r   get_exit_string_from_enum
ANSI_RESETsysstderrwriteZEXIT_FAILED_Zget_error_string_from_enumZ
error_codeZget_error_description_from_enumZerror_detailsrI   r.   rJ   )r/   transenumoutputmsgr3   r3   r4   rh      s&    


zConsoleClient._on_exitc                 C   s>   | j r(|r(| js(|   d| _|   n|s:d| _|   dS )zXCallback for the terminal-attachabed-changed signal of the
        transaction.
        FTN)r#   r   _clear_progressr&   _attachrn   )r/   rk   Zattachedr3   r3   r4   rc      s    
z#ConsoleClient._on_terminal_attachedc                 C   s   t || _|   dS )z1Callback for the Status signal of the transactionN)r   Zget_status_string_from_enumr!   _update_progress)r/   rk   statusr3   r3   r4   rd      s    zConsoleClient._on_statusc                 C   s   || _ |   dS )z9Callback for the StatusDetails signal of the transaction.N)r'   r|   )r/   rk   textr3   r3   r4   re      s    z ConsoleClient._on_status_detailsc                 C   sj   |r2|r2t dt|t|t|d | _n,|rXt dt|t|d | _nd| _|   dS )z;Callback for the ProgressDetails signal of the transaction.z0Downloaded %(cur)sB of %(total)sB at %(rate)sB/s)curtotalZratez!Downloaded %(cur)sB of %(total)sB)r   r   r   N)ru   r
   get_size_stringr(   r|   )r/   rk   Z
items_doneZitems_totalZ
bytes_doneZbytes_totalZspeedZetar3   r3   r4   rg      s"    z"ConsoleClient._on_progress_detailsc                 C   s   || _ |   dS )z3Callback for the Progress signal of the transactionN)r"   r|   )r/   rk   percentr3   r3   r4   rf     s    zConsoleClient._on_progressc                 C   s   | j s
dS t| j t }| jr,|d| j 7 }| jr@|d| j 7 }| jd }| jd t k r|| j	d t
| j | _	t | _| j| j	 }| jdkrd}n| j}tjd	| d
|  d|||f  d  dS )zUpdate the progress bar.N z (%s)rm   g?r	   d   ---[%s] %3.3s%% %-*.*s)r&   ro   r!   rq   r'   r(   r   r,   r+   r*   lenr)   r"   rr   rs   rt   )r/   r~   
text_widthspinnerr   r3   r3   r4   r|     s,    


zConsoleClient._update_progressNc                 C   s   t | t }| jd }|rH| jd t| j | _t | _| j| j }nd}|dkrXd}tj	
d| d|  d|||f  d	  d
S )z5Update the progress bar with a custom status message.rm   r	   +Nr   r   r   r   r   T)ro   rq   r   r*   r   r)   r+   r,   rr   rs   rt   )r/   ry   r   Zspinr~   r   r   r3   r3   r4   _update_custom_progress#  s"    

z%ConsoleClient._update_custom_progressc                 C   s   | j dk	rt| j  dS )z7Stop the spinner which shows non trans status messages.N)_progress_idr   ra   rM   r3   r3   r4   _stop_custom_progress6  s    
z#ConsoleClient._stop_custom_progressc                 C   s   t jd| j| jdf  dS )z%Clear progress information on stderr.z%-*.*sr   N)rr   rs   rt   r   rM   r3   r3   r4   rz   ;  s    zConsoleClient._clear_progressc                 C   s.   | j r | j jtjkr | j   n
| j  dS )zCallback for a cancel signal.N)r-   r}   r   ZSTATUS_SETTING_UPZcancelr.   rJ   r/   Zsignumframer3   r3   r4   r   A  s
    zConsoleClient._on_cancel_signalc                 C   s   |   | _|   dS )z%Callback for a changed terminal size.N)r   r   r|   r   r3   r3   r4   r   I  s    
z!ConsoleClient._on_terminal_resizec                 C   s4   | j D ]}t| q| jr0ttjtj| j dS )z.Dettach the controlling terminal to aptdaemon.N)	r   r   ra   r    ttyZ	tcsetattrr   STDIN_FILENOZ	TCSAFLUSH)r/   Zwidr3   r3   r4   rn   N  s    
zConsoleClient._detachc              	   C   s   zt tj| _t tj W n t jk
r:   d| _Y nX tjtj	B tj
B }| jttjtj|| j| j | jt| jtj|| jtj dS )zSAttach the controlling terminal to aptdaemon.
        Based on pty.spwan()
        N)r   Z	tcgetattrr   r   r    Zsetrawerrorr   IO_INZIO_ERRZIO_HUPr   rb   Zio_add_watchZPRIORITY_HIGH_IDLE_copy_ior   ZSTDOUT_FILENO)r/   flagsr3   r3   r4   r{   V  s*        zConsoleClient._attachc                 C   s8   |t jkr*t|d}|r&t|| dS t| dS )z(Callback to copy data between terminals.i   TF)r   r   ri   readrt   close)r/   sourceZ	conditiontargetdatar3   r3   r4   r   h  s    

zConsoleClient._copy_ioc                 C   s<   z t  dttjtjdd W S  tk
r6   Y dS X dS )z7Return the witdh in characters of the current terminal.hz        r	   P   N)arrayfcntlZioctlrr   rs   termiosZ
TIOCGWINSZIOErrorrM   r3   r3   r4   r   r  s    z!ConsoleClient._get_terminal_widthc                 C   s   |    z|W np tjk
r@   dtdtd| f }Y nB tjk
rn   dtd| | f }Y n   t|}Y nX | j	
  t| dS )zError callback.z	%s %s

%szERROR:z+You are not allowed to perform this action.z
%s %s - %sN)rn   r   ZPolicyKitErrorru   get_dbus_messagedbusZDBusExceptionget_dbus_namestrr.   rJ   rr   exit)r/   r   ry   r3   r3   r4   r=   {  s     


zConsoleClient._on_exceptionc                    s     |     jjtjtjtjtjtj	fkrdt
d jtd _ jj j fddd n*t
d jtd _ jj j jd dS )	z,Callback which runs a requested transaction.   Queuingc                      s      S rH   r   r3   rM   r3   r4   <lambda>      z0ConsoleClient._run_transaction.<locals>.<lambda>r8   r7   zResolving dependenciesr6   N)rl   r   r-   Zroler   ZROLE_UPDATE_CACHEZROLE_ADD_VENDOR_KEY_FILEZ"ROLE_ADD_VENDOR_KEY_FROM_KEYSERVERZROLE_REMOVE_VENDOR_KEYZROLE_FIX_INCOMPLETE_INSTALLr   timeout_addr   ru   r   r_   r=   Zsimulate_show_changes)r/   rv   r3   rM   r4   r<     s.    



zConsoleClient._run_transactionc                    s   fdd}        jj\}}}}}} jj\}}	}
}}}}|| || || ||
 || ||	 || |}|rttddt|dt|i  || |rttddt|dt|i  || |rttdd	t|dt|i  || |rDttd
dt|dt|i  || |rrttddt|dt|i  || |rttddt|dt|i  ||  jj	rtt
dt jj	   jjdkrtt
dt jj  n( jjdk rtt
dt jj   jjsN jj	sN|sN|sN|sN|sN|sN|rz$trdtt
d}ntt
d}W n tk
r   d}Y nX tttj|s|dkrttj} |d d  j  td t d jt
d _! jj" j# fddd d S )Nc              	      s   d}|    | D ]}z|dddd \}}W n tk
rL   |}d}Y nX  jrf|rfd||f }n|}t|d t|  jkr|dkrt| d}|d| 7 }q|dkrt| dS )	zFormat the pkgs in a nice way.r   =r	   r   r   Nz%s=%sz %s)sortsplit
ValueErrorr$   r   r   rI   )Zpkgsr>   pkgnameversionrx   rM   r3   r4   show_packages  s&    

z2ConsoleClient._show_changes.<locals>.show_packagesz8The following NEW package will be installed (%(count)s):z9The following NEW packages will be installed (%(count)s):countz3The following package will be upgraded (%(count)s):z4The following packages will be upgraded (%(count)s):z2The following package will be REMOVED (%(count)s):z3The following packages will be REMOVED (%(count)s):z5The following package will be DOWNGRADED (%(count)s):z6The following packages will be DOWNGRADED (%(count)s):z6The following package will be reinstalled (%(count)s):z7The following packages will be reinstalled (%(count)s):z5The following package has been kept back (%(count)s):z7The following packages have been kept back (%(count)s):zNeed to get %sB of archives.r   z@After this operation, %sB of additional disk space will be used.zAAfter this operation, %sB of additional disk space will be freed.zDo you want to continue [Y/n]?nr   Fr	   r   r   c                      s      S rH   r   r3   rM   r3   r4   r     r   z-ConsoleClient._show_changes.<locals>.<lambda>r   )$r   rz   r-   r\   ZdependenciesextendrI   r   r   Zdownloadru   r
   r   ZspacePY3KinputZ	raw_inputEOFErrorrematchlocalenl_langinfoYESEXPRr   rp   ZEXIT_CANCELLEDr   r.   rJ   rr   r   r   r   r   r_   r=   )r/   r   ZinstallsZ
reinstallsZremovalsZpurgesZupgradesZ
downgradesZdep_installsZdep_reinstallsZdep_removalsZ
dep_purgesZdep_upgradesZdep_downgradesZ	dep_keptsZkeptsZcontry   r3   rM   r4   r     s     

















zConsoleClient._show_changes)TFF)r   r   )NT)'__name__
__module____qualname____doc__r5   r9   rA   rD   rF   rG   rO   rP   rW   rX   rY   rZ   r[   r^   r_   rl   rh   rc   rd   re   rg   rf   r|   r   r   rz   r   r   rn   r{   r   r   r=   r<   r   r3   r3   r3   r4   r   9   sL     

		

	r   c               	   C   s  t d} ttj| d}|jdddddt dd	 |jdd
dddt dd	 |jdddddt dd	 |jddddddt dd |jddddddt dd |jddddddt dd |jdddddd t d!d |jd"d#dddd$t dd |jdd%dddd&t d'd |jdd(dd)t d*d+ |jdd,dd)t d-d+ |jdd.dd/t d0d+ |jdd1dddd2t d3d |jdd4dddt d5d6 |jdd7dddt d8d6 |jdd9dddd:t d;d |jdd<dddd=t d>d? |jdd@dddAt dBd	 |jddCddddDt dEd |jddFddGt dHd+ |jddIddddJt dKd |jddLdMdddNt dOd |jddPddQt dRd+ |jddSddTdUt dVdW |jdXdYddZt d[d+ | \}}t|j |j|j	d\}t
d]|jt d^|_|jr|d_ n|jr|dU n|jr$|  nv|jrD||j |j nV|jrX|  nB|jrl|  n.|jr|  n|jr|jd`r||j n|js|js|js|j s|j!s|j"r|#|j |j |j |j  |j! |j"  n|j$r(|$|j$|j% nr|j&r>|'|j& n\|j(r`|j)r`|(|j(|j) n:|j*rv|*|j* n$|j+r|+  n|,  t-.da |/  dbS )cz'Run a command line client for aptdaemonziTo operate on more than one package put the package names in quotation marks:
aptdcon --install "foo bar")r   epilogz-cz	--refreshr   
store_truerefreshzRefresh the cache)defaultactiondesthelpz--fix-dependsfix_dependszoTry to resolve broken dependencies. Potentially dangerous operation since it could try to remove many packages.z--fix-installfix_installz1Try to finish a previous incompleted installationz-iz	--installZstorestringrQ   zInstall the given packages)r   r   r:   r   r   z--reinstallrR   zReinstall the given packagesz-rz--removerS   zRemove the given packagesz-pz--purgerT   z7Remove the given packages including configuration filesz-uz	--upgraderU   z--downgraderV   zDowngrade the given packagesz--upgrade-systemsafe_upgradez%Deprecated: Please use --safe-upgrade)r   r   r   z--safe-upgradez Upgrade the system in a safe wayz--full-upgradefull_upgradez=Upgrade the system, possibly installing and removing packagesz--add-vendor-keyadd_vendor_keyz"Add the vendor to the trusted onesz--add-vendor-key-from-keyserverz-Add the vendor keyid (also needs --keyserver))r   r   r:   r   z--keyserverz+Use the given keyserver for looking up keysz--add-repositoryr9   z*Add new repository from the given deb-linez--sources-filesources_filezQSpecify an alternative sources.list.d file to which repositories should be added.)r   r   r:   r   r   z--list-trusted-vendorsrO   zList trusted vendor keysz--remove-vendor-keyrF   z/Remove the trusted key of the given fingerprintz--cleanr^   zRemove downloaded package filesz--reconfigurer[   z]Reconfigure installed packages. Optionally the minimum priority of questions can be specifiedz
--priorityr   r]   z8The minimum debconf priority of question to be displayedz--hide-terminalhide_terminalz!Do not attach to the apt terminalz--allow-unauthenticatedr1   Fz+Allow packages from unauthenticated sources)r   r   r   r   z-dz--show-detailsr2   zQShow additional information about the packages. Currently only the version number)r0   r1   r2   r   zWaiting for authenticationTz.debr	   N)0ru   r   	aptdaemon__version__Z
add_option
parse_argsr   r   r1   r2   r   r   r   r   r   rZ   r   r   rY   r[   r   r]   r^   r   rW   r   rX   rQ   endswithrG   rR   rS   rT   rU   rV   rP   r9   r   r   rA   rD   rE   rF   rO   Z
print_helprr   r   r_   )r   parserZoptionsargsZconr3   r3   r4   r     s   
 
 
 
  
  
  
  
  
     
  
 
 
   
 
   
  
     


r   __main__)*r   
__author____all__r   r   r   ru   r   r   Zoptparser   ri   r   r   r   r+   r   r   rr   Zaptsources.sourceslistr   Zgi.repositoryr   Zdbus.mainloop.glibr   ZmainloopZglibZDBusGMainLoopr   r   r
   r   r   chrro   rq   version_infomajorr   r   r   r   r3   r3   r3   r4   <module>   sF      i 
