o
    *dC                     @   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re! 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 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   _)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)zWait 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@ =