3
g
]@                 @   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 yejejd ej \ZZW n   ejd	 d
ZY nX esd
Zejdd yejejd W n ejk
r   Y nX ejdej   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)j*  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 <      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)si  iQ i  )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| j  d| _d S )Ng        )superr   __init__	est_speed)self)	__class__r   r   r      s    zAcquireProgress.__init__c                s*   t t| j  d| _d| _d| _d| _d S )Ng        F)r   r   startr   etapercentrelease_file_download_error)r   )r   r   r   r      s
    zAcquireProgress.startc                sf   t t| j|||| |tjkrbtjd|  |jdsB|jdrbxdD ]}|j|rHd| _	P qHW d S )	Nz update_status: dlFailed on '%s' zRelease.gpgZReleasehttpftpmirrorT)r!   r"   r#   )
r   r   update_statusapt_pkgZSTAT_FAILEDloggingZwarningendswith
startswithr    )r   ZuriZdescrZ
shortDescrZstatusZnet)r   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| 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   owner)r   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   8   i      z[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. i   g      @i   g       A)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.   r2   __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j|  d | _d S )N)aptprogressbaser7   r   Z	master_fd)r   r   r   r   r      s    zInstallProgress.__init__c             C   s   xyt j | jgg g | j W n6 t jk
rR } z|jd tjkrB W Y dd}~X nX | j  y$tj	| j
tj\}}|| j
kr~P W q tk
r } z"|jtjkr |jtjkrP W Y dd}~X qX qW |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   | j  }|dkrtjtjtj y|j| j}W nf tk
r } zJtd| tj	d t
dd}|jt| W d Q R X tj|j W Y d d }~X nX tj| || _tj| j }|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_FAILEDrA   WEXITSTATUSrG   )r   ZpmrE   rF   rD   fr   r   r   run   s    


zInstallProgress.runc             C   s^   t jjjj| || tjd||f  d|kr:tjj|}d|krP|j	dd }t
|| dS )z install error from a package z*got an error from dpkg for pkg: '%s': '%s'/r   r   N)r8   r9   r:   r7   r<   r&   r?   pathbasenamesplitr   )r   pkgZerrormsgr   r   r   r<      s    zInstallProgress.errorN)r3   r4   r5   __doc__r   rG   rV   r<   r   r   r   r   r7      s
   r7   c               @   s   e Zd ZdddZdS )DumbTerminalFc             C   s   ddl }|j| dS )z7 expects a command in the subprocess style (as a list) r   N)
subprocesscall)r   cmdZhiddenr^   r   r   r   r_      s    zDumbTerminal.callN)F)r3   r4   r5   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 )DummyHtmlViewc             C   s   d S )Nr   )r   Zurlr   r   r   rP      s    zDummyHtmlView.openc             C   s   d S )Nr   )r   r   r   r   show   s    zDummyHtmlView.showc             C   s   d S )Nr   )r   r   r   r   hide   s    zDummyHtmlView.hideN)r3   r4   r5   rP   rb   rc   r   r   r   r   ra      s   ra   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)
r3   r4   r5   ZPREPAREZMODIFY_SOURCESZFETCHINSTALLZCLEANUPZREBOOTNr   r   r   r   rd     s   rd   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d-ddZd.ddZdd  Zd/d!d"Zd0d#d$Zd%d& Zd1d(d)Zd*d+ ZdS )2DistUpgradeViewz" abstraction for the upgrade view c             C   s
   d| _ d S )NF)Zneeds_screen)r   r   r   r   r     s    zDistUpgradeView.__init__c             C   s   t jjj S )z5 return a OpProgress() subclass for the given graphic)r8   r9   r:   Z
OpProgress)r   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 )r7   )r   cacher   r   r   getInstallProgress  s    z"DistUpgradeView.getInstallProgressc             C   s   t  S )N)r]   )r   r   r   r   getTerminal!  s    zDistUpgradeView.getTerminalc             C   s   t  S )N)ra   )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|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   )r   rx   r   r   r   hideStep8  s    zDistUpgradeView.hideStepc             C   s   dS )z" show a certain step from the GUI Nr   )r   rx   r   r   r   showStep;  s    zDistUpgradeView.showStepTc             C   s  d| _ || _g | _g | _g | _g | _g | _g | _x|D ]}|jrN| jj	| q6|j
rb| jj	| q6|jrv| jj	| q6|jr|jjj|jr| jj	| q| jj	| q6|jr6| jj	| q6W | j| j | _| jj  | jj  | jj  | jj  | jj  d}t| jt| j }	t| jt| j }
t| j}t| jdkrp|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j|}t|tr|jt}|td| 7 }|| j j|7 }||
 dkr||
 |	 dkr| j j rn|dtd 7 }n|dtd 7 }n|	dkr|dtd 7 }||
 |	 dk rtd}td}| j|| 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   rn   r2   r.   r<   )r   summaryZchangesr   ZdownloadSizeZactionsZremoval_boldr[   rs   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   rs   defaultr   r   r   askYesNoQuestion  s    z DistUpgradeView.askYesNoQuestionc             C   s   t d}t d}| j||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   rs   r   r   r   confirmRestart  s    zDistUpgradeView.confirmRestartc             C   s   dS )z display a error Nr   )r   r   rs   extended_msgr   r   r   r<     s    zDistUpgradeView.errorc             C   s   dS )z display a information msgNr   )r   r   rs   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   | j ||dj| dS )zr
      show demoted packages to the user, default implementation
      is to just show a information dialog
      r|   N)r   join)r   r   rs   r   r   r   r   showDemotions  s    zDistUpgradeView.showDemotions)N)NT)r   )N)N)F)r3   r4   r5   r\   r   rm   rn   rp   rq   rr   rt   ru   ry   rz   r{   r   r   r   r<   r   r   r   r   r   r   r   r   rl     s(   

b



rl   __main__)+ZDistUpgradeGettextr   r   r   Z	telemetryr   rv   r8   enumr   r>   r?   r%   Zlocaler&   rJ   r;   ZDistUpgradeApportr   	setlocaleLC_ALLZgetdefaultlocalecoder   rO   putenvr	   r<   infoZ	getlocaler   r9   r:   r   r7   objectr]   ra   rd   rl   r3   fpr+   r   r   r   r   <module>   sT   

D6>
 0
