U
    ٪zcA                     @   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 nX esd
Zedd zeejd W n ejk
r   Y nX 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re! Z)e)*  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   s0  t | } | d }| d d }| d d }| d }|dkr<d}ddddd}|dkrftd	d
|| |d< |dkrtdd|| |d< |dkrtdd|| |d< tdd|| |d< |dkr|d dkr|d S td| S |dkr|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 iQ 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   _)ZsecZdaysZhoursZminutesZ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                    sd   t t| |||| |tjkr`td|  |dsB|dr`dD ]}||rFd| _	 q`qFd S )Nz update_status: dlFailed on '%s' zRelease.gpgZRelease)ZhttpZftpZmirrorT)
r   r   update_statusapt_pkgZSTAT_FAILEDloggingZwarningendswith
startswithr#   )r   ZuriZdescrZ
shortDescrZstatusZnetr   r   r   r$      s    

zAcquireProgress.update_statusc                 C   s   dS )NTr   )r   argr   r   r   pulse_items   s    zAcquireProgress.pulse_itemsNc                    sv   t t| | | j| j d t| j| j  | _| j	| j
krP| j
| j	 d | _
| j	dkrr| j| j t| j	 | _dS )Ng      Y@g       @r   T)r   r   pulseZcurrent_bytesZcurrent_itemsfloatZtotal_bytesZtotal_itemsr"   Z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r6|d }|d }tdt|t|f }|S tdt|| j   }|S )z! get the estimated download time r   g      @g       Az[This download will take about %s with a 1Mbit DSL connection and about %s with a 56k modem.z7This download will take about %s with your connection. )r   r   r   )r   Zrequired_downloadZ	timeModemZtimeDSLsr   r   r   estimatedDownloadTime   s    
z%AcquireProgress.estimatedDownloadTime)N)__name__
__module____qualname__r   r    r$   r*   r+   r.   r0   __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 N)aptprogressbaser5   r   Z	master_fdr   r   r   r   r      s    zInstallProgress.__init__c              
   C   s   zt  | jgg g | j W n6 t jk
rP } z|jd tjkr@ W 5 d}~X Y nX |   z&t	| j
tj\}}|| j
kr~W qW q  tk
r } z&|jtjkr |jtjkrW Y qW 5 d}~X Y q X q |S )zWait for child progress to exit.

      The return values is the full status returned from os.waitpid()
      (not only the return code).
      r   N)selectZstatusfdZselect_timeouterrorargserrnoZEINTRZupdate_interfaceoswaitpid	child_pidWNOHANGOSErrorZECHILD)r   epidresr   r   r   
wait_child   s     
zInstallProgress.wait_childc                 C   s   |   }|dkrttjtj z|| j}W nf tk
r } zHtd| t	d t
dd}|t| W 5 Q R X t|j W 5 d }~X Y nX 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_IGNZ
do_installZwritefd	Exceptionprintr&   	exceptionopenwritestrr>   _exitZRESULT_FAILEDr@   WEXITSTATUSrF   )r   ZpmrD   rE   rC   fr   r   r   run   s    


zInstallProgress.runc                 C   s   t jjj| || td||f  d|kr:tj|}|	dd 
 rdd|	ddd }d|krz|	dd }t|| dS )z install error from a package z*got an error from dpkg for pkg: '%s': '%s'/-r   r   Nr   )r7   r8   r9   r5   r;   r&   r>   pathbasenamesplitisdigitjoinr   )r   pkgZerrormsgr   r   r   r;      s    zInstallProgress.errorN)r1   r2   r3   __doc__r   rF   rU   r;   r   r   r   r   r5      s
   r5   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   cmdZhiddenr`   r   r   r   ra      s    zDumbTerminal.callN)F)r1   r2   r3   ra   r   r   r   r   r_      s   r_   c                   @   s$   e Zd Zdd Zdd Zdd ZdS )DummyHtmlViewc                 C   s   d S r6   r   )r   Zurlr   r   r   rO      s    zDummyHtmlView.openc                 C   s   d S r6   r   r   r   r   r   show   s    zDummyHtmlView.showc                 C   s   d S r6   r   r   r   r   r   hide   s    zDummyHtmlView.hideN)r1   r2   r3   rO   rd   re   r   r   r   r   rc      s   rc   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)
r1   r2   r3   ZPREPAREZMODIFY_SOURCESZFETCHZINSTALLZCLEANUPZREBOOTNr   r   r   r   rf     s   rf   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d/d	d
Zdd Zdd Z	dd Z
dd Zdd Zdd Zdd Zd0ddZd1ddZd2d d!Zd"d# Zd3d$d%Zd4d&d'Zd(d) Zd5d+d,Zd-d. ZdS )6DistUpgradeViewz" abstraction for the upgrade view c                 C   s
   d| _ d S )NF)Z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)r7   r8   r9   Z
OpProgressr   r   r   r   getOpCacheProgress  s    z"DistUpgradeView.getOpCacheProgressc                 C   s   t  S )z# return an acquire progress object )r   r   r   r   r   getAcquireProgress  s    z"DistUpgradeView.getAcquireProgressNc                 C   s   t  S )z" return a install progress object )r5   )r   cacher   r   r   getInstallProgress   s    z"DistUpgradeView.getInstallProgressc                 C   s   t  S r6   )r_   r   r   r   r   getTerminal#  s    zDistUpgradeView.getTerminalc                 C   s   t  S r6   )rc   r   r   r   r   getHtmlView%  s    zDistUpgradeView.getHtmlViewc                 C   s   dS )z\ update the current status of the distUpgrade based
            on the current view
        Nr   )r   msgr   r   r   updateStatus'  s    zDistUpgradeView.updateStatusc                 C   s   dS )z8 provide a visual feedback that the upgrade was aborted Nr   r   r   r   r   abort,  s    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_telemetryZ	add_stagenamer   stepr   r   r   setStep/  s    	zDistUpgradeView.setStepc                 C   s   dS )z" hide a certain step from the GUI Nr   ry   r   r   r   hideStep:  s    zDistUpgradeView.hideStepc                 C   s   dS )z" show a certain step from the GUI Nr   ry   r   r   r   showStep=  s    zDistUpgradeView.showStepTc                 C   s  d| _ || _g | _g | _g | _g | _g | _g | _|D ]}|jrL| j	| q4|j
r`| j	| q4|jrt| j	| q4|jr|jj|jr| j	| q| j	| q4|jr4| j	| q4| j| j | _| j  | j  | j  | j  | j  d}t| jt| j }	t| jt| j }
t| j}t| jdkrl|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r|||
 |	 dkr||   rj|dtd 7 }n|dtd 7 }n|	dkr|dtd 7 }||
 |	 dk rt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.Z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) ZconfirmChangesMessage	demotionsZ	toInstallZtoReinstallZ	toUpgradeZtoRemoveZtoRemoveAutoZtoDowngradeZmarked_installappendZmarked_upgradeZmarked_reinstallZmarked_deleteZ_pcacheZ	_depcacheZis_auto_installedZ_pkgZmarked_downgradesortlenr   r%   Zsize_to_str
isinstancebytesdecodeENCODINGr   ro   r0   r.   r;   )r   summaryZchangesr   ZdownloadSizeZactionsZremoval_boldr]   rt   Zpkgs_removeZ	pkgs_instZpkgs_upgradeZdownloadSizeStrr   r   r   confirmChanges@  s    











 
zDistUpgradeView.confirmChangesNoc                 C   s   dS )z0 ask a Yes/No question and return True on 'Yes' Nr   r   r   rt   defaultr   r   r   askYesNoQuestion  s    z DistUpgradeView.askYesNoQuestionCancelc                 C   s   dS )z= ask a Cancel/Continue question and return True on 'Continue'Nr   r   r   r   r   askCancelContinueQuestion  s    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   rt   r   r   r   confirmRestart  s    zDistUpgradeView.confirmRestartc                 C   s   dS )z display a error Nr   r   r   rt   Zextended_msgr   r   r   r;     s    zDistUpgradeView.errorc                 C   s   dS )z display a information msgNr   r   r   r   r   information  s    zDistUpgradeView.informationc                 C   s   dS )zQ process gui events (to keep the gui alive during a long
            computation Nr   r   r   r   r   processEvents  s    zDistUpgradeView.processEventsFc                 C   s   dS )zX do a progress pulse (e.g. bounce a bar back and forth, show
          a spinner)
      Nr   )r   Zfinishedr   r   r   pulseProgress  s    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   rt   r   r   r   r   showDemotions  s    zDistUpgradeView.showDemotions)N)NT)r   )r   )N)N)F)r1   r2   r3   r^   r   rn   ro   rq   rr   rs   ru   rv   r{   r|   r}   r   r   r   r   r;   r   r   r   r   r   r   r   r   rm     s.   
   
c




rm   __main__)+ZDistUpgradeGettextr   r   r   Z	telemetryr   rw   r7   enumr   r=   r>   r%   Zlocaler&   rI   r:   ZDistUpgradeApportr   	setlocaleLC_ALLZgetdefaultlocalecoder   rN   putenvr	   r;   infoZ	getlocaler   r8   r9   r   r5   objectr_   rc   rf   rm   r1   fpr+   r   r   r   r   <module>   sV   

D6@ 3
