o
    Œé*dÀC  ã                   @   s´  d dl m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mZ ze ejd¡ e ¡ \ZZW n   e d	¡ d
ZY eswd
Ze dd¡ z	e ejd¡ W n
 ejyv   Y nw e de ¡  ¡ dd„ ZG dd„ dejj j!ƒZ!G dd„ dejj j"ƒZ"G dd„ de#ƒZ$G dd„ de#ƒZ%G dd„ de	ƒZ&edƒedƒedƒedƒedƒf G dd „ d e#ƒZ'e(d!krØe!ƒ Z)e) *¡  dS dS )"é   )Úgettext)Úngettext)Úgeté    N)ÚEnum)Úapport_pkgfailureÚ zgetting the encoding failedzutf-8ÚLC_CTYPEzC.UTF-8zlocale: '%s' '%s'c                 C   s*  t | ƒ} | d }| d d }| d d }| d }|dkrd}dddddœ}|dkr3td	d
|ƒ| |d< |dkrAtdd|ƒ| |d< |dkrOtdd|ƒ| |d< tdd|ƒ| |d< |dkrm|d dkrg|d S tdƒ| S |dkru|d S |dkr‰|d dkrƒ|d S tdƒ| S |dkr‘|d S |d S )z; return the time a bit fuzzy (no seconds if time > 60 secs i€Q i  é   é<   r   r   r   )Ústr_daysÚ	str_hoursÚstr_minutesÚstr_secondsz%li dayz%li daysr   z%li hourz	%li hoursr   z
%li minutez%li minutesr   z
%li secondz%li secondsr   z%(str_days)s %(str_hours)sé   z%(str_hours)s %(str_minutes)s)Úintr   Ú_)ÚsecÚdaysÚhoursÚminutesÚsecondsÚmap© r   ú=/usr/lib/python3/dist-packages/DistUpgrade/DistUpgradeView.pyÚFuzzyTimeToStr;   s@   ý

r   c                       sZ   e Zd Z‡ fdd„Z‡ fdd„Z‡ fdd„Zdd„ Zd‡ fd
d„	Zdd„ Zdd„ Z	‡  Z
S )ÚAcquireProgressc                    s   t t| ƒ ¡  d| _d S )Nç        )Úsuperr   Ú__init__Ú	est_speed©Úself©Ú	__class__r   r   r   �   s   
zAcquireProgress.__init__c                    s*   t t| ƒ ¡  d| _d| _d| _d| _d S )Nr   F)r   r   Ústartr    ÚetaÚpercentÚrelease_file_download_errorr!   r#   r   r   r%   „   s
   
zAcquireProgress.startc                    sn   t t| ƒ ||||¡ |tjkr1t d| ¡ | d¡s!| d¡r3dD ]}| |¡r0d| _	 d S q#d S d S d S )Nz update_status: dlFailed on '%s' zRelease.gpgÚRelease)ÚhttpÚftpÚmirrorT)
r   r   Úupdate_statusÚapt_pkgÚSTAT_FAILEDÚloggingÚwarningÚendswithÚ
startswithr(   )r"   ÚuriÚdescrÚ
shortDescrÚstatusÚnetr#   r   r   r-   Š   s   

þúzAcquireProgress.update_statusc                 C   ó   dS ©NTr   )r"   Úargr   r   r   Úpulse_items™   ó   zAcquireProgress.pulse_itemsNc                    sv   t t| ƒ |¡ | j| j d t| j| j ƒ | _| j	| j
kr(| j
| j	 d | _
| j	dkr9| j| j t| j	ƒ | _dS )Ng      Y@g       @r   T)r   r   ÚpulseÚcurrent_bytesÚcurrent_itemsÚfloatÚtotal_bytesÚtotal_itemsr'   Úcurrent_cpsr    r&   )r"   Úownerr#   r   r   r>   ›   s   ÿ

ÿzAcquireProgress.pulsec                 C   s
   | j dkS )Nr   )r    r!   r   r   r   ÚisDownloadSpeedEstimated¥   s   
z(AcquireProgress.isDownloadSpeedEstimatedc                 C   sP   | j dkr|d }|d }tdƒt|ƒt|ƒf }|S tdƒt|| j  ƒ }|S )z! get the estimated download time r   g    Ð#Ag    ÐSAz_This download will take about %s with a 40Mbit connection and about %s with a 5Mbit connection.z7This download will take about %s with your connection. )r    r   r   )r"   Úrequired_downloadÚ	time5MbitÚ
time40MbitÚsr   r   r   ÚestimatedDownloadTime§   s   
ÿz%AcquireProgress.estimatedDownloadTime©N)Ú__name__Ú
__module__Ú__qualname__r   r%   r-   r<   r>   rF   rK   Ú__classcell__r   r   r#   r   r      s    
r   c                   @   s0   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
S )ÚInstallProgressz_ Base class for InstallProgress that supports some fancy
      stuff like apport integration
  c                 C   s   t jjj | ¡ d | _d S rL   )ÚaptÚprogressÚbaserQ   r   Ú	master_fdr!   r   r   r   r   ¹   s   
zInstallProgress.__init__c              
   C   sÖ   	 zt   | jgg g | j¡ W n t jy* } z|jd tjkr ‚ W Y d}~nd}~ww |  ¡  zt 	| j
tj¡\}}|| j
krBW |S W n& tyi } z|jtjkrR‚ |jtjkr_W Y d}~|S W Y d}~nd}~ww q)z�Wait for child progress to exit.

      The return values is the full status returned from os.waitpid()
      (not only the return code).
      Tr   N)ÚselectÚstatusfdÚselect_timeoutÚerrorÚargsÚerrnoÚEINTRÚupdate_interfaceÚosÚwaitpidÚ	child_pidÚWNOHANGÚOSErrorÚECHILD)r"   ÚeÚpidÚresr   r   r   Ú
wait_child½   s4   ÿ€ÿ
ù
þ€ýõzInstallProgress.wait_childc                 C   sÐ   |   ¡ }|dkr\t tjtj¡ z| | j¡}W n> tyV } z2td|ƒ t 	d¡ t
ddƒ�}| t|ƒ¡ W d   ƒ n1 sAw   Y  t |j¡ W Y d }~nd }~ww t |¡ || _t |  ¡ ¡}|S )Nr   z!Exception during pm.DoInstall(): zException during pm.DoInstall()z./var/run/ubuntu-release-upgrader-apt-exceptionÚw)ÚforkÚsignalÚSIGPIPEÚSIG_IGNÚ
do_installÚwritefdÚ	ExceptionÚprintr0   Ú	exceptionÚopenÚwriteÚstrr^   Ú_exitÚRESULT_FAILEDr`   ÚWEXITSTATUSrg   )r"   Úpmre   rf   rd   Úfr   r   r   ÚrunÕ   s$   

ÿ€û
zInstallProgress.runc                 C   sˆ   t jjj | ||¡ t d||f ¡ d|v rtj |¡}| 	d¡d  
¡ r2d | 	d¡dd… ¡}d|v r=| 	d¡d }t||ƒ dS )z install error from a package z*got an error from dpkg for pkg: '%s': '%s'ú/ú-r   r   Nr   )rR   rS   rT   rQ   rY   r0   r^   ÚpathÚbasenameÚsplitÚisdigitÚjoinr   )r"   ÚpkgÚerrormsgr   r   r   rY   è   s   zInstallProgress.errorN)rM   rN   rO   Ú__doc__r   rg   rz   rY   r   r   r   r   rQ   µ   s    rQ   c                   @   s   e Zd Zddd„ZdS )ÚDumbTerminalFc                 C   s   ddl }| |¡ dS )z7 expects a command in the subprocess style (as a list) r   N)Ú
subprocessÚcall)r"   ÚcmdÚhiddenr†   r   r   r   r‡   ö   s   zDumbTerminal.callN©F)rM   rN   rO   r‡   r   r   r   r   r…   õ   s    r…   c                   @   s$   e Zd Zdd„ Zdd„ Zdd„ ZdS )ÚSampleHtmlViewc                 C   ó   d S rL   r   )r"   Úurlr   r   r   rr   ü   r=   zSampleHtmlView.openc                 C   rŒ   rL   r   r!   r   r   r   Úshowþ   r=   zSampleHtmlView.showc                 C   rŒ   rL   r   r!   r   r   r   Úhide   r=   zSampleHtmlView.hideN)rM   rN   rO   rr   rŽ   r�   r   r   r   r   r‹   û   s    r‹   c                   @   s(   e Zd ZdZdZdZdZdZdZdZ	dS )	ÚStepr   é   r   é   é   é   é   N)
rM   rN   rO   ÚPREPAREÚMODIFY_SOURCESÚFETCHÚINSTALLÚCLEANUPÚREBOOTÚNr   r   r   r   r�     s    r�   zPreparing to upgradezGetting new software channelszGetting new packageszInstalling the upgradeszCleaning upc                   @   sÐ   e Zd ZdZdd„ Zdd„ Zdd„ Zd3d	d
„Zdd„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ Z	d4dd„Zd5dd„Zd6d d!„Zd"d#„ Zd$d%„ Zd&d'„ Zd3d(d)„Zd3d*d+„Zd,d-„ Zd7d/d0„Zd1d2„ ZdS )8ÚDistUpgradeViewz" abstraction for the upgrade view c                 C   s
   d| _ d S )NF)Úneeds_screenr!   r   r   r   r     s   zDistUpgradeView.__init__c                 C   s   t jj ¡ S )z5 return a OpProgress() subclass for the given graphic)rR   rS   rT   Ú
OpProgressr!   r   r   r   ÚgetOpCacheProgress  s   z"DistUpgradeView.getOpCacheProgressc                 C   ó   t ƒ S )z# return an acquire progress object )r   r!   r   r   r   ÚgetAcquireProgress  ó   z"DistUpgradeView.getAcquireProgressNc                 C   r¡   )z" return a install progress object )rQ   )r"   Úcacher   r   r   ÚgetInstallProgress   r£   z"DistUpgradeView.getInstallProgressc                 C   r¡   rL   )r…   r!   r   r   r   ÚgetTerminal#  ó   zDistUpgradeView.getTerminalc                 C   r¡   rL   )r‹   r!   r   r   r   ÚgetHtmlView%  r§   zDistUpgradeView.getHtmlViewc                 C   r9   )z\ update the current status of the distUpgrade based
            on the current view
        Nr   )r"   Úmsgr   r   r   ÚupdateStatus'  ó   zDistUpgradeView.updateStatusc                 C   r9   )z8 provide a visual feedback that the upgrade was aborted Nr   r!   r   r   r   Úabort,  ó   zDistUpgradeView.abortc                 C   s   t ƒ  |j¡ dS )zé we have 6 steps current for a upgrade:
        1. Analyzing the system
        2. Updating repository information
        3. fetch packages
        3. Performing the upgrade
        4. Post upgrade stuff
        5. Complete
        N)Úget_telemetryÚ	add_stageÚname©r"   Ústepr   r   r   ÚsetStep/  s   	zDistUpgradeView.setStepc                 C   r9   )z" hide a certain step from the GUI Nr   r±   r   r   r   ÚhideStep:  r­   zDistUpgradeView.hideStepc                 C   r9   )z" show a certain step from the GUI Nr   r±   r   r   r   ÚshowStep=  r­   zDistUpgradeView.showStepTc                 C   sÈ  d| _ || _g | _g | _g | _g | _g | _g | _|D ]B}|jr&| j 	|¡ q|j
r0| j 	|¡ q|jr:| j 	|¡ q|jrS|jj |j¡rL| j 	|¡ q| j 	|¡ q|jr\| j 	|¡ q| j| j | _| j ¡  | j ¡  | j ¡  | j ¡  | j ¡  d}t| jƒt| jƒ }	t| jƒt| jƒ }
t| jƒ}t| jƒdkrµ|tddt| jƒƒdt| jƒi 7 }|d7 }|	dkrÇ|tdd	|	ƒ|	 7 }|d
7 }|
dkrÙ|tdd|
ƒ|
 7 }|d
7 }|dkrë|tdd|ƒ| 7 }|d
7 }|dk�rt |¡}t|tƒrÿ| t¡}|tdƒ| 7 }||  ¡  |¡7 }||
 dk�r9||
 |	 dk�r9|  ¡  ¡ �r0|dtdƒ 7 }n|dtdƒ 7 }n|	dk�rF|dtdƒ 7 }||
 |	 dk �r_tdƒ}tdƒ}|  ||¡ dS || _ dS )zm display the list of changed packages (apt.Package) and
            return if the user confirms them
        r   Ú
r   zo%(amount)d installed package is no longer supported by Canonical. You can still get support from the community.zq%(amount)d installed packages are no longer supported by Canonical. You can still get support from the community.Úamountz

z"%d package is going to be removed.z$%d packages are going to be removed.ú z(%d new package is going to be installed.z*%d new packages are going to be installed.z#%d package is going to be upgraded.z%%d packages are going to be upgraded.z&

You have to download a total of %s. éd   z

%sznInstalling the upgrade can take several hours. Once the download has finished, the process cannot be canceled.z{Fetching and installing the upgrade can take several hours. Once the download has finished, the process cannot be canceled.z.Removing the packages can take several hours. r   z,The software on this computer is up to date.zRThere are no upgrades available for your system. The upgrade will now be canceled.FT) ÚconfirmChangesMessageÚ	demotionsÚ	toInstallÚtoReinstallÚ	toUpgradeÚtoRemoveÚtoRemoveAutoÚtoDowngradeÚmarked_installÚappendÚmarked_upgradeÚmarked_reinstallÚmarked_deleteÚ_pcacheÚ	_depcacheÚis_auto_installedÚ_pkgÚmarked_downgradeÚsortÚlenr   r.   Úsize_to_strÚ
isinstanceÚbytesÚdecodeÚENCODINGr   r¢   rK   rF   rY   )r"   ÚsummaryÚchangesr»   ÚdownloadSizeÚactionsÚremoval_boldr‚   r©   Úpkgs_removeÚ	pkgs_instÚpkgs_upgradeÚdownloadSizeStrr   r   r   ÚconfirmChanges@  s¬   €





ûûþþýýþþ



ÿ 
zDistUpgradeView.confirmChangesÚNoc                 C   r9   )z0 ask a Yes/No question and return True on 'Yes' Nr   ©r"   rÓ   r©   Údefaultr   r   r   ÚaskYesNoQuestion£  r­   z DistUpgradeView.askYesNoQuestionÚCancelc                 C   r9   )z= ask a Cancel/Continue question and return True on 'Continue'Nr   rÞ   r   r   r   ÚaskCancelContinueQuestion¦  r­   z)DistUpgradeView.askCancelContinueQuestionc                 C   s   t dƒ}t dƒ}|  ||¡S )z2 generic ask about the restart, can be overridden zReboot requiredzMThe upgrade is finished and a reboot is required. Do you want to do this now?)r   rà   )r"   rÓ   r©   r   r   r   ÚconfirmRestart©  s   zDistUpgradeView.confirmRestartc                 C   ó&   t dƒ}t dƒ}t dƒ}|  |||¡S )NzAction requiredz9Exit all other instances of Ubuntu WSL before continuing.z'Unsaved progress may otherwise be lost.©r   Úinformation©r"   rÓ   r©   Úextendedr   r   r   ÚadviseExitOtherWSL±  ó   z"DistUpgradeView.adviseExitOtherWSLc                 C   rä   )NzWSL restart requiredz!Exit this instance of Ubuntu WSL.z"The upgrade will then be complete.rå   rç   r   r   r   ÚadviseRestartWSL¶  rê   z DistUpgradeView.adviseRestartWSLc                 C   r9   )z display a error Nr   ©r"   rÓ   r©   Úextended_msgr   r   r   rY   »  r­   zDistUpgradeView.errorc                 C   r9   )z display a information msgNr   rì   r   r   r   ræ   ¾  r­   zDistUpgradeView.informationc                 C   r9   )zQ process gui events (to keep the gui alive during a long
            computation Nr   r!   r   r   r   ÚprocessEventsÁ  s   zDistUpgradeView.processEventsFc                 C   r9   )zX do a progress pulse (e.g. bounce a bar back and forth, show
          a spinner)
      Nr   )r"   Úfinishedr   r   r   ÚpulseProgressÅ  r«   zDistUpgradeView.pulseProgressc                 C   s   |   ||d |¡¡ dS )zr
      show demoted packages to the user, default implementation
      is to just show a information dialog
      r¶   N)ræ   r�   )r"   rÓ   r©   r»   r   r   r   ÚshowDemotionsÊ  s   zDistUpgradeView.showDemotionsrL   r:   )rÝ   )rá   rŠ   )rM   rN   rO   r„   r   r    r¢   r¥   r¦   r¨   rª   r¬   r³   r´   rµ   rÜ   rà   râ   rã   ré   rë   rY   ræ   rî   rð   rñ   r   r   r   r   r�     s2    

ÿ
c



r�   Ú__main__)+ÚDistUpgradeGettextr   r   r   Ú	telemetryr   r®   rR   Úenumr   r[   r^   r.   Úlocaler0   rj   rV   ÚDistUpgradeApportr   Ú	setlocaleÚLC_ALLÚgetdefaultlocaleÚcoderÒ   rq   Úputenvr	   rY   ÚinfoÚ	getlocaler   rS   rT   r   rQ   Úobjectr…   r‹   r�   r�   rM   Úfpr>   r   r   r   r   Ú<module>   s\   
ÿD6@ü =þ