
    \NeC                        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 	  ej&                  ej(                  d        ej*                         \  ZZes3d
Z ej2                  dd       	  ej&                  ej4                  d        ej8                  d ej:                         z         d Z G d dej>                  j@                  jB                        Z! G d dej>                  j@                  jD                        Z" G d de#      Z$ G d de#      Z% G d de	      Z& ed       ed       ed       ed       ed      f  G d de#      Z'e(d k(  r e!       Z)e)jU                          yy#   ej0                  d	       d
ZY 6xY w# ej6                  $ r Y w xY w)!   )gettext)ngettext)get    N)Enum)apport_pkgfailure zgetting the encoding failedzutf-8LC_CTYPEzC.UTF-8zlocale: '%s' '%s'c                    t        |       } | dz  }| dz  dz  }| dz  dz  }| dz  }|dk(  rd}ddddd}|dkD  rt        d	d
|      |z  |d<   |dkD  rt        dd|      |z  |d<   |dkD  rt        dd|      |z  |d<   t        dd|      |z  |d<   |dkD  r|d   dk(  r|d   S t        d      |z  S |dkD  r|d   S |dkD  r|d   dk(  r|d   S t        d      |z  S |dkD  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maps         =/usr/lib/python3/dist-packages/DistUpgrade/DistUpgradeView.pyFuzzyTimeToStrr   ;   se    	C#	x$
,
%"WN'"H'\G 	
# 
AXyT:TAC
O
QY
;>FCq[!,}gFPC]GDwN#m 
AX
;2: )*S00qy{qy
=R; ,-33{}	]	    c                   P     e Zd Z fdZ fdZ fdZd Zd fd	Zd Zd Z	 xZ
S )	AcquireProgressc                 8    t         t        |           d| _        y )N        )superr   __init__	est_speedself	__class__s    r   r#   zAcquireProgress.__init__   s    	/4)+DNr   c                 b    t         t        |           d| _        d| _        d| _        d| _        y )Nr!   F)r"   r   startr$   etapercentrelease_file_download_errorr%   s    r   r)   zAcquireProgress.start   s-    	/4&(DNDHDL',D$r   c                    t         t        |   ||||       |t        j                  k(  r]t        j                  d|z         |j                  d      s|j                  d      r"dD ]  }|j                  |      sd| _	         y  y y y )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'   s         r   r2   zAcquireProgress.update_status   s    	/4.sE:vN $$$oo83>?	m	$Y(? + 	C^^C /3D,	 )@ %r   c                      yNT )r&   args     r   pulse_itemszAcquireProgress.pulse_items   s    r   c                    t         t        |   |       | j                  | j                  z   dz  t        | j                  | j                  z         z  | _        | j                  | j                  kD  r!| j                  | j                  z   dz  | _
        | j                  dkD  r4| j                  | j                  z
  t        | j                        z  | _        y)Ng      Y@g       @r   T)r"   r   pulsecurrent_bytescurrent_itemsfloattotal_bytestotal_itemsr+   current_cpsr$   r*   )r&   ownerr'   s     r   rD   zAcquireProgress.pulse   s    	/4&u-((4+=+==F$**T-=-==>?DL$..(t'7'77<dn!##d&8&88(()*dhr   c                      | j                   dk7  S )Nr   )r$   r&   s    r   isDownloadSpeedEstimatedz(AcquireProgress.isDownloadSpeedEstimated   s    NNa r   c                     | j                   dk(  r.|dz  }|dz  }t        d      t        |      t        |      fz  }|S t        d      t        || j                   z        z  }|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ss        r   estimatedDownloadTimez%AcquireProgress.estimatedDownloadTime   sw    ~~#]3i$n5j	
 3 
47Ej7QSabkSl6m
nah	
CD~VghlhvhvVvGwwAHr   N)__name__
__module____qualname__r#   r)   r2   rB   rD   rN   rT   __classcell__)r'   s   @r   r   r      s&    -	!
r   r   c                   (    e Zd ZdZd Zd Zd Zd Zy)InstallProgressz_ Base class for InstallProgress that supports some fancy
      stuff like apport integration
  c                 x    t         j                  j                  j                  j	                  |        d | _        y rU   )aptprogressbaser[   r#   	master_fdrM   s    r   r#   zInstallProgress.__init__   s'    LL%%..t4DNr   c                 <   	 	 t        j                   | j                  gg g | j                         | j                          	 t        j                  | j                  t        j                        \  }}|| j                  k(  r	 |S 	 # t         j                  $ r+}|j                  d   t
        j                  k7  r Y d}~d}~ww xY w# t        $ rK}|j
                  t
        j                  k7  r |j
                  t
        j                  k(  rY d}~S Y d}~d}~ww xY w)zWait for child progress to exit.

      The return values is the full status returned from os.waitpid()
      (not only the return code).
      r   N)selectstatusfdselect_timeouterrorargserrnoEINTRupdate_interfaceoswaitpid	child_pidWNOHANGOSErrorECHILD)r&   epidress       r   
wait_childzInstallProgress.wait_child   s     mmT]]ORT5H5HI 


!::dnnbjjAjsC& j '   ekk) *  EKK'ELL(j )s0   -B A C C!B??C	D;DDc                 n   | j                         }|dk(  rct        j                  t        j                  t        j                         	 |j	                  | j
                        }t        j                         || _        t        j"                  | j%                               }|S # t        $ r}t        d|       t        j                  d       t        dd      5 }|j                  t        |             d d d        n# 1 sw Y   nxY wt        j                  |j                         Y d }~d }~ww xY w)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printr5   	exceptionopenwritestrrj   _exitRESULT_FAILEDrl   WEXITSTATUSrs   )r&   pmrq   rr   rp   fs         r   runzInstallProgress.run   s    
))+C
ax mmFNN6>>2#mmDLL) 	hhsmDN
..*
+CJ  #115;<B3G 	1GGCFO	 	 	
!!""#s0   	B% %	D4.-D/C?6	D/?D	&D//D4c                    t         j                  j                  j                  j	                  | ||       t        j                  d|d|d       d|v rt        j                  j                  |      }|j                  d      d   j                         r#dj                  |j                  d      dd       }d	|v r|j                  d	      d   }t        ||       y)
z install error from a package z!got an error from dpkg for pkg: 'z': ''/-r   r   Nr   )r]   r^   r_   r[   re   r5   rj   pathbasenamesplitisdigitjoinr   )r&   pkgerrormsgs      r   re   zInstallProgress.error   s    LL%%++D#x@MM#xPQ
czGGS!c
yy~a  "JJsyy~ab)*c
czIIcN1cc8$r   N)rV   rW   rX   __doc__r#   rs   r   re   r@   r   r   r[   r[      s    0&%r   r[   c                       e Zd ZddZy)DumbTerminalc                 .    ddl }|j                  |       y)z7 expects a command in the subprocess style (as a list) r   N)
subprocesscall)r&   cmdhiddenr   s       r   r   zDumbTerminal.call   s    r   NF)rV   rW   rX   r   r@   r   r   r   r      s    r   r   c                       e Zd Zd Zd Zd Zy)SampleHtmlViewc                      y rU   r@   )r&   urls     r   r   zSampleHtmlView.open   s    r   c                      y rU   r@   rM   s    r   showzSampleHtmlView.show       
r   c                      y rU   r@   rM   s    r   hidezSampleHtmlView.hide   r   r   N)rV   rW   rX   r   r   r   r@   r   r   r   r      s    r   r   c                   (    e Zd ZdZdZdZdZdZdZdZ	y)	Stepr      r               N)
rV   rW   rX   PREPAREMODIFY_SOURCESFETCHINSTALLCLEANUPREBOOTNr@   r   r   r   r     s%    GNEGGF	Ar   r   zPreparing to upgradezGetting new software channelszGetting new packageszInstalling the upgradeszCleaning upc                       e Zd ZdZd Zd Zd ZddZd Zd Z	d	 Z
d
 Zd Zd Zd Z	 ddZddZddZd Zd Zd ZddZddZd ZddZd Zy)DistUpgradeViewz" abstraction for the upgrade view c                     d| _         y )NF)needs_screenrM   s    r   r#   zDistUpgradeView.__init__  s    !r   c                 R    t         j                  j                  j                         S )z5 return a OpProgress() subclass for the given graphic)r]   r^   r_   
OpProgressrM   s    r   getOpCacheProgressz"DistUpgradeView.getOpCacheProgress  s    ||  ++--r   c                     t               S )z# return an acquire progress object )r   rM   s    r   getAcquireProgressz"DistUpgradeView.getAcquireProgress        r   Nc                     t               S )z" return a install progress object )r[   )r&   caches     r   getInstallProgressz"DistUpgradeView.getInstallProgress   r   r   c                     t               S rU   )r   rM   s    r   getTerminalzDistUpgradeView.getTerminal#  s
    ~r   c                     t               S rU   )r   rM   s    r   getHtmlViewzDistUpgradeView.getHtmlView%  s    r   c                      y)z\ update the current status of the distUpgrade based
            on the current view
        Nr@   )r&   msgs     r   updateStatuszDistUpgradeView.updateStatus'  s     	r   c                      y)z8 provide a visual feedback that the upgrade was aborted Nr@   rM   s    r   abortzDistUpgradeView.abort,      r   c                 J    t               j                  |j                         y)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&   steps     r   setStepzDistUpgradeView.setStep/  s     	!!$)),r   c                      y)z" hide a certain step from the GUI Nr@   r   s     r   hideStepzDistUpgradeView.hideStep:  r   r   c                      y)z" show a certain step from the GUI Nr@   r   s     r   showStepzDistUpgradeView.showStep=  r   r   c                 T   d| _         || _        g | _        g | _        g | _        g | _        g | _        g | _        |D ]  }|j                  r| j                  j                  |       ,|j                  r| j                  j                  |       T|j                  r| j                  j                  |       ||j                  rg|j                  j                  j                  |j                         r| j                  j                  |       | j
                  j                  |       |j"                  s| j                  j                  |        | j                  | j                  z   | _        | j                  j%                          | j                  j%                          | j
                  j%                          | j                  j%                          | j                  j%                          d}t'        | j
                        t'        | j                        z   }	t'        | j                        t'        | j                        z   }
t'        | j                        }t'        | j                        dkD  r@|t)        ddt'        | j                              dt'        | j                        iz  z  }|dz  }|	dkD  r|t)        dd	|	      |	z  z  }|d
z  }|
dkD  r|t)        dd|
      |
z  z  }|d
z  }|dkD  r|t)        dd|      |z  z  }|d
z  }|dkD  rmt+        j,                  |      }t/        |t0              r|j3                  t4              }|t7        d      |z  z  }|| j9                         j;                  |      z  }||
z   dkD  rM||
z   |	z   dkD  rB| j9                         j=                         r|dt7        d      z  z  }n(|dt7        d      z  z  }n|	dkD  r|dt7        d      z  z  }||
z   |	z   dk  r)t7        d      }t7        d      }| j?                  ||       y|| _         y)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   r3   size_to_str
isinstancebytesdecodeENCODINGr   r   rT   rN   re   )r&   summarychangesr   downloadSizeactionsremoval_boldr   r   pkgs_remove	pkgs_instpkgs_upgradedownloadSizeStrs                r   confirmChangeszDistUpgradeView.confirmChanges@  s   
 &("" 	+C!!nn##C(##nn##C(%%%%c*""&&88B!!((-$$S)%%%%c*	+ $*:*:: $--(3t/@/@+AA'#d.>.>*??	4>>*t~~"
<G!
 &.DNN0C#EF F# -#? >@%')45 5# *#q=
 '''02 5>> ># *#!
?A&(*67 7# )#!#//=// / 6 6x @o
=> #
((*@@N
N#I%*,2J[2X\_1_$$&??A8a "Q R R RC 8a "Q R R RC 38a "< = = =C 9${2a7DE' 6 7#
**Wc
"%("r   c                      y)z0 ask a Yes/No question and return True on 'Yes' Nr@   r&   r   r   defaults       r   askYesNoQuestionz DistUpgradeView.askYesNoQuestion  r   r   c                      y)z= ask a Cancel/Continue question and return True on 'Continue'Nr@   r   s       r   askCancelContinueQuestionz)DistUpgradeView.askCancelContinueQuestion  r   r   c                 R    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   r   s      r   confirmRestartzDistUpgradeView.confirmRestart  s1    %&   $$Wc22r   c                 j    t        d      }t        d      }t        d      }| j                  |||      S )NzAction requiredz9Exit all other instances of Ubuntu WSL before continuing.z'Unsaved progress may otherwise be lost.r   informationr&   r   r   extendeds       r   adviseExitOtherWSLz"DistUpgradeView.adviseExitOtherWSL  s8    %&KL>?h77r   c                 j    t        d      }t        d      }t        d      }| j                  |||      S )NzWSL restart requiredz!Exit this instance of Ubuntu WSL.z"The upgrade will then be complete.r  r  s       r   adviseRestartWSLz DistUpgradeView.adviseRestartWSL  s8    *+349:h77r   c                      y)z display a error Nr@   r&   r   r   extended_msgs       r   re   zDistUpgradeView.error  r   r   c                      y)z display a information msgNr@   r  s       r   r  zDistUpgradeView.information  r   r   c                      y)zQ process gui events (to keep the gui alive during a long
            computation Nr@   rM   s    r   processEventszDistUpgradeView.processEvents  s     	r   c                      y)zX do a progress pulse (e.g. bounce a bar back and forth, show
          a spinner)
      Nr@   )r&   finisheds     r   pulseProgresszDistUpgradeView.pulseProgress  s     r   c                 H    | j                  ||dj                  |             y)zr
      show demoted packages to the user, default implementation
      is to just show a information dialog
      r   N)r  r   )r&   r   r   r   s       r   showDemotionszDistUpgradeView.showDemotions  s    
 wTYYy%9:r   rU   r?   )No)Cancelr   )rV   rW   rX   r   r#   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r
  r  re   r  r  r  r  r@   r   r   r   r     sz    (.!! 

 37aF38
8

;r   r   __main__)+DistUpgradeGettextr   r   r   	telemetryr   r   r]   enumr   rg   rj   r3   localer5   rw   rb   DistUpgradeApportr   	setlocaleLC_ALLgetdefaultlocalecoder   r~   putenvr
   re   info	getlocaler   r^   r_   r   r[   objectr   r   r   r   rV   fprD   r@   r   r   <module>r)     s  , - ( + 
   	      0FV]]B'.v..0T8 HBIIj)$)4  #36#3#3#55 6AH2cll''77 2l>%cll''77 >%@6 V 
4 
 #$M	z;f z;x z"((* QG34H << s    1F 	F* F'*F=<F=