3
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 j | _tj \| _| _g | _tjtj	| j
 tjtj| j
 tjtj| j | j | _g | _d | _d| _d| _d| _|| _|| _|| _d| _d| _d| _d| _d| _tj | _d | _t j! | _"d S )NT r   z|/-\r	   )#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   r8   X   s    zConsoleClient.add_repositoryc             C   s   | j j|| j| jd dS )zInstall repository key file.)r6   r7   N)r   add_vendor_key_from_filer;   r<   )r/   pathr3   r3   r4   r?   a   s    z&ConsoleClient.add_vendor_key_from_filec             C   s   | j j||| j| jd dS )zInstall repository key file.)r6   r7   N)r   add_vendor_key_from_keyserverr;   r<   )r/   Zkeyid	keyserverr3   r3   r4   rA   h   s    z+ConsoleClient.add_vendor_key_from_keyserverc             C   s   | j j|| j| jd dS )zRemove repository key.)r6   r7   N)r   remove_vendor_keyr;   r<   )r/   Zfingerprintr3   r3   r4   rC   o   s    zConsoleClient.remove_vendor_keyc             C   s   | j j|| j| jd dS )zInstall package file.)r6   r7   N)r   install_filer;   r<   )r/   r@   r3   r3   r4   rD   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$   x| D ]}t | qW  jj  d S )N)printr.   quit)keyskey)r/   r3   r4   on_done|   s    
z7ConsoleClient.list_trusted_vendor_keys.<locals>.on_done)r6   r7   N)r   Zget_trusted_vendor_keysr<   )r/   rI   r3   )r/   r4   list_trusted_vendor_keysz   s    z&ConsoleClient.list_trusted_vendor_keysc          
   C   s$   | j j||||||| j| jd dS )zCommit changes)r6   r7   N)r   commit_packagesr;   r<   )r/   install	reinstallremovepurgeupgrade	downgrader3   r3   r4   rK      s    zConsoleClient.commit_packagesc             C   s   | j j| j| jd dS )zFix incomplete installs)r6   r7   N)r   fix_incomplete_installr;   r<   )r/   r3   r3   r4   rR      s    z$ConsoleClient.fix_incomplete_installc             C   s   | j j| j| jd dS )zRepair broken dependencies.)r6   r7   N)r   fix_broken_dependsr;   r<   )r/   r3   r3   r4   rS      s    
z ConsoleClient.fix_broken_dependsc             C   s   | j j| j| jd dS )zUpdate cache)r6   r7   N)r   update_cacher;   r<   )r/   r3   r3   r4   rT      s    
zConsoleClient.update_cachec             C   s   | j j|| j| jd dS )zUpgrade system)r6   r7   N)r   upgrade_systemr;   r<   )r/   Z	safe_moder3   r3   r4   rU      s    zConsoleClient.upgrade_systemc             C   s   | j j||| j| jd dS )zReconfigure packages.)r6   r7   N)r   reconfigurer;   r<   )r/   packagespriorityr3   r3   r4   rV      s    
zConsoleClient.reconfigurec             C   s   | j j| j| jd dS )zClean archives.)r6   r7   N)r   cleanr;   r<   )r/   r3   r3   r4   rY      s    
zConsoleClient.cleanc             C   s(   y| j j  W n tk
r"   Y nX dS )z%Start the console client application.N)r.   runKeyboardInterrupt)r/   r3   r3   r4   rZ      s    zConsoleClient.runc             C   s   x| j D ]}tj| qW || _g | _ | j j|jd| j | j j|jd| j | j j|jd| j | j j|jd| j	 | j j|jd| j
 | j j|jd| j | jr|jtj| j |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   | jr>dt| jd | jd tj|tf }tjj	| |tj
krvdtdtj|jtj|j|jf }t| | j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_detailsrE   r.   rF   )r/   transenumoutputmsgr3   r3   r4   rc      s"    



zConsoleClient._on_exitc             C   s@   | j r*|r*| j r*| j  d| _| j  n|s<d| _| j  dS )zXCallback for the terminal-attachabed-changed signal of the
        transaction.
        FTN)r#   r   _clear_progressr&   _attachri   )r/   rf   Zattachedr3   r3   r4   r^      s    
z#ConsoleClient._on_terminal_attachedc             C   s   t j|| _| j  dS )z1Callback for the Status signal of the transactionN)r   Zget_status_string_from_enumr!   _update_progress)r/   rf   statusr3   r3   r4   r_      s    zConsoleClient._on_statusc             C   s   || _ | j  dS )z9Callback for the StatusDetails signal of the transaction.N)r'   rw   )r/   rf   textr3   r3   r4   r`      s    z ConsoleClient._on_status_detailsc             C   sj   |r2|r2t dtj|tj|tj|d | _n,|rXt dtj|tj|d | _nd| _| j  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)rz   r{   r   N)rp   r
   get_size_stringr(   rw   )r/   rf   Z
items_doneZitems_totalZ
bytes_doneZbytes_totalZspeedZetar3   r3   r4   rb      s    z"ConsoleClient._on_progress_detailsc             C   s   || _ | j  dS )z3Callback for the Progress signal of the transactionN)r"   rw   )r/   rf   percentr3   r3   r4   ra     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j k r|| j	d t
| j | _	tj | _| j| j	 }| jdkrd}n| j}tjjd	| d
|  d|||f  d  dS )zUpdate the progress bar.N z (%s)rh   g?r	   d   z---z[%s] z%3.3s%% z%-*.*s)r&   rj   r!   rl   r'   r(   r   r,   r+   r*   lenr)   r"   rm   rn   ro   )r/   ry   
text_widthspinnerr}   r3   r3   r4   rw     s"    


zConsoleClient._update_progressNc             C   s   t | t }| jd }|rH| jd t| j | _tj | _| j| j }nd}|dkrXd}tj	j
d| d|  d|||f  d	  d
S )z5Update the progress bar with a custom status message.rh   r	   +Nz---z[%s] z%3.3s%% z%-*.*sr   T)rj   rl   r   r*   r   r)   r+   r,   rm   rn   ro   )r/   rt   r}   Zspinry   r   r   r3   r3   r4   _update_custom_progress#  s    

$z%ConsoleClient._update_custom_progressc             C   s   | j dk	rtj| j  dS )z7Stop the spinner which shows non trans status messages.N)_progress_idr   r\   )r/   r3   r3   r4   _stop_custom_progress6  s    
z#ConsoleClient._stop_custom_progressc             C   s   t jjd| j| jdf  dS )z%Clear progress information on stderr.z%-*.*sr~   N)rm   rn   ro   r   )r/   r3   r3   r4   ru   ;  s    zConsoleClient._clear_progressc             C   s.   | j r | j jtjkr | j j  n
| jj  dS )zCallback for a cancel signal.N)r-   rx   r   ZSTATUS_SETTING_UPZcancelr.   rF   )r/   signumframer3   r3   r4   r   A  s    zConsoleClient._on_cancel_signalc             C   s   | j  | _| j  dS )z%Callback for a changed terminal size.N)r   r   rw   )r/   r   r   r3   r3   r4   r   I  s    
z!ConsoleClient._on_terminal_resizec             C   s8   x| j D ]}tj| qW | jr4tjtjtj| j dS )z.Dettach the controlling terminal to aptdaemon.N)	r   r   r\   r    ttyZ	tcsetattrr   STDIN_FILENOZ	TCSAFLUSH)r/   Zwidr3   r3   r4   ri   N  s
    zConsoleClient._detachc             C   s   yt jtj| _t jtj W n t jk
r:   d| _Y nX tjtj	B tj
B }| jjtjtjtj|| j| j | jjtj| 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   r]   Zio_add_watchZPRIORITY_HIGH_IDLE_copy_ior   ZSTDOUT_FILENO)r/   flagsr3   r3   r4   rv   V  s    zConsoleClient._attachc             C   s8   |t jkr*tj|d}|r&tj|| dS tj| dS )z(Callback to copy data between terminals.i   TF)r   r   rd   readro   close)r/   sourceZ	conditiontargetdatar3   r3   r4   r   h  s    

zConsoleClient._copy_ioc             C   s8   yt j dtjtjtjdd S  tk
r2   dS X dS )z7Return the witdh in characters of the current terminal.h    r	   P   Nz        )arrayfcntlZioctlrm   rn   termiosZ
TIOCGWINSZIOError)r/   r3   r3   r4   r   r  s
    z!ConsoleClient._get_terminal_widthc             C   s   | j   y|W np tjk
r@   dtdtd|j f }Y nB tjk
rn   dtd|j |j f }Y n   t|}Y nX | j	j
  tj| dS )zError callback.z	%s %s

%szERROR:z+You are not allowed to perform this action.z
%s %s - %sN)ri   r   ZPolicyKitErrorrp   get_dbus_messagedbusZDBusExceptionget_dbus_namestrr.   rF   rm   exit)r/   r   rt   r3   r3   r4   r<   {  s    
zConsoleClient._on_exceptionc                s    j |  j   jjtjtjtjtjtj	gkrdt
jd jtd _ jj j fddd n*t
jd jtd _ jj j jd dS )	z,Callback which runs a requested transaction.   Queuingc                  s    j  S )N)r   r3   )r/   r3   r4   <lambda>  s    z0ConsoleClient._run_transaction.<locals>.<lambda>)r7   r6   zResolving dependencies)r6   r7   N)rg   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   rp   r   rZ   r<   Zsimulate_show_changes)r/   rq   r3   )r/   r4   r;     s$    



zConsoleClient._run_transactionc                s   fdd} j    j   jj\}}}}}} jj\}}	}
}}}}|j| |j| |j| |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jdkrtt
dtj jj  n( jjdk rtt
dtj jj   jjsN jj	sN|sN|sN|sN|sN|sN|ry$trdtt
d}ntt
d}W n tk
r   d}Y nX tjtjtj| r|dkrtjtj} j|d d  jj  tjd tj d jt
d _! jj" j# fddd d S )Nc                s   d}| j   x| D ]}y|jdddd \}}W n tk
rN   |}d}Y nX  jrh|rhd||f }n|}t|d t|  jkr|dkrt| d}|d| 7 }qW |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   rE   )Zpkgsr=   pkgnameversionrs   )r/   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    j  S )N)r   r3   )r/   r3   r4   r     s    z-ConsoleClient._show_changes.<locals>.<lambda>)r7   r6   )$r   ru   r-   rW   ZdependenciesextendrE   r   r   Zdownloadrp   r
   r|   ZspacePY3KinputZ	raw_inputEOFErrorrematchlocalenl_langinfoYESEXPRr   rk   ZEXIT_CANCELLEDr   r.   rF   rm   r   r   r   r   rZ   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contrt   r3   )r/   r4   r     s    











zConsoleClient._show_changes)TFF)r   r   )NT)'__name__
__module____qualname____doc__r5   r8   r?   rA   rC   rD   rJ   rK   rR   rS   rT   rU   rV   rY   rZ   rg   rc   r^   r_   r`   rb   ra   rw   r   r   ru   r   r   ri   rv   r   r   r<   r;   r   r3   r3   r3   r4   r   9   sJ    

		

	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+ |j \}}t|j |j|j	d\}t
jd]|jt d^|_|jr|jd_ n|jr|jdU n|jr$|j  nv|jrD|j|jj |j nV|jrX|j  nB|jrl|j  n.|jr|j  n|jr|jjd`r|j|j n|js|js|js|j s|j!s|j"r|j#|jj |jj |jj |j j |j!j |j"j  n|j$r(|j$|j$|j% nr|j&r>|j'|j& n\|j(r`|j)r`|j(|j(|j) n:|j*rv|j*|j* n$|j+r|j+  n|j,  t-j.da |j/  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stringrL   zInstall the given packages)r   r   r9   r   r   z--reinstallrM   zReinstall the given packagesz-rz--removerN   zRemove the given packagesz-pz--purgerO   z7Remove the given packages including configuration filesz-uz	--upgraderP   z--downgraderQ   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   r9   r   z--keyserverz+Use the given keyserver for looking up keysz--add-repositoryr8   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   r9   r   r   z--list-trusted-vendorsrJ   zList trusted vendor keysz--remove-vendor-keyrC   z/Remove the trusted key of the given fingerprintz--cleanrY   zRemove downloaded package filesz--reconfigurerV   z]Reconfigure installed packages. Optionally the minimum priority of questions can be specifiedz
--priorityr   rX   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)0rp   r   	aptdaemon__version__Z
add_option
parse_argsr   r   r1   r2   r   r   r   r   r   rU   r   r   rT   rV   r   rX   rY   r   rR   r   rS   rL   endswithrD   rM   rN   rO   rP   rQ   rK   r8   r   r   r?   rA   rB   rC   rJ   Z
print_helprm   r   rZ   )r   parserZoptionsargsZconr3   r3   r4   r     s    





















__main__)r   r   )*r   
__author____all__r   r   r   rp   r   r   Zoptparser   rd   r   r   r   r+   r   r   rm   Zaptsources.sourceslistr   Zgi.repositoryr   Zdbus.mainloop.glibr   ZmainloopZglibZDBusGMainLoopr   r   r
   r   r   chrrj   rl   version_infomajorr   r   r   r   r3   r3   r3   r4   <module>   sF      i 
