
Y]4                 @   s  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 Z d  d l Z d  d l	 Z	 d  d l
 Z
 d  d l Z  d  d l m Z m Z d  d l	 m Z m Z d d l m Z m Z m Z d d l m Z Gd d   d e  Z Gd	 d
   d
 e  Z Gd d   d e  Z e d k re   Z e   Z e   Z e j d d  e  j   Z x) e j  d d  D] Z! e e! j"   qWe j# e e  e j$ d  e j% d   d S)    N)NoSectionErrorNoOptionError)PIPEPopen   )DistUpgradeViewInstallProgressAcquireProgress)DistUpgradeConfigc               @   s   e  Z d  Z d d   Z d S)NonInteractiveAcquireProgressc             C   st   t  j |  | | | |  | t j k rp t d | |  j t j t |  j   f  t	 j
 j   rp t	 j
 j   d  S)Nzfetched %s (%.2f/100) at %sb/s)r	   update_statusapt_pkgZ	STAT_DONEprintpercentZsize_to_strintZcurrent_cpssysstdoutisattyflush)selfZuriZdescrZ
shortDescrstatus r   K/usr/lib/python3/dist-packages/DistUpgrade/DistUpgradeViewNonInteractive.pyr   +   s    &z+NonInteractiveAcquireProgress.update_statusN)__name__
__module____qualname__r   r   r   r   r   r   *   s   r   c               @   sv   e  Z d  Z 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   Z d S)NonInteractiveInstallProgressz 
    Non-interactive version of the install progress class
    
    This ensures that conffile prompts are handled and that
    hanging scripts are killed after a (long) timeout via ctrl-c
    c             C   s   t  j |   t j d  d t j k r6 d t j d <d t j d <d t j d <t d  |  _ | |  _ d	 |  _	 y/ |  j j
 d
 d d  r t j j d d  Wn t t f k
 r Yn Xd |  _ y |  j j d
 d  |  _ Wn t k
 r Yn Xd  S)Nz*setting up environ for non-interactive useZDEBIAN_FRONTENDZnoninteractiveZnoneZAPT_LISTCHANGES_FRONTEND1ZRELEASE_UPRADER_NO_APPORT.r   NonInteractiveZForceOverwriteFzDPkg::Options::z--force-overwritei`	  ZTerminalTimeout)r   __init__loggingdebugosenvironr
   configlogdirinstall_run_numbergetWithDefaultr   setr   r   timeoutZgetint	Exception)r   r&   r   r   r   r    =   s&    			z&NonInteractiveInstallProgress.__init__c             C   s  t  j d | | f  |  j j d d d  s3 d  St j t j  } d | d <g  } d | k r d } d	 } d
 } d | | | f } n d | k r d } d } d } d | | f } n d | k r d } d } d } d | | | f } nL d | k r"d } d } d } d | | | f } n t d | | f  d  St j j	 |  sat  j d |  d  St
 |  - }	 |	 j   d d   j   j   d }
 Wd  QRXd |
 k sd |
 k rd g } n3 d |
 k rd g } d | d  <n t  j d! |
  t
 |   }	 |	 j   } Wd  QRXd" | k rHd# | d$ <d% | d& <d' }
 d( d g } | j |
  | j |  | j |  | j |  | d	 k rt d) | d* d+ d, t d- d+ j   d } | rf| j | j d. d/  d/ j    n | d k rft j j |  } | j d0  d } t d1 | d* d+ d, t d- d+ j   d } | rf| j | j d. d/  d/ j    t  j d2 | | f  t j | d3 | } t  j d4 | | f  d  S)5Nz)got a error from dpkg for pkg: '%s': '%s'r   ZDebugBrokenScriptsFr"   Z	PYCENTRALzpost-installationz/var/lib/dpkg/info/ZpostinstZ	configurez%s/%s.%szpre-installationz/var/lib/dpkg/tmp.ci/Zpreinstinstallz%s/%szpre-removalZprermremovezpost-removalZpostrmz3UNKNOWN (trigger?) dpkg/script failure for %s (%s) z+can not find failed maintainer script '%s'    r   Zbashz/bin/shz-exZperlz-dzAutoTrace NonStopZPERLDB_OPTSzunknown interpreter: '%s'z. /usr/share/debconf/confmoduleZ	developerZDEBCONF_DEBUGr   ZDEBIAN_HAS_FRONTENDz/usr/share/debconf/frontendZshz%dpkg-query -s %s|grep ^Config-VersionshellTr   Zuniversal_newlines:r   _zdpkg-query -s %s|grep ^Versionzre-running '%s' (%s)envz%s script returned: %s)r!   errorr%   r(   copyr#   r$   r   pathexistsopenreadlinestripsplitwarningreadappendextendr   r   Zcommunicatebasenamer"   
subprocesscall)r   pkgZerrormsgr$   cmdprefixnameZargumentZmaintainer_scriptfZinterpZ
debug_optsZmaintainer_script_textversionZretr   r   r   r3   V   s    	
-	

&#z#NonInteractiveInstallProgress.errorc             C   sr   t  j d |  t j d  y t j |  j d  Wn6 t k
 rm } z t  j d |  WYd  d  } ~ Xn Xd  S)Nz.got a conffile-prompt from dpkg for file: '%s'   zn
z/error '%s' when trying to write to the conffile)	r!   r;   timesleepr#   write	master_fdr+   r3   )r   Zcurrentnewer   r   r   conffile   s    z&NonInteractiveInstallProgress.conffilec             C   s   t  j |   t j   |  _ |  j j d d d  } | r t j j |  j	 d |  j
  } t j d |  t | d  |  _ n t t j d  |  _ |  j j d t j    d  S)Nr   ZDpkgProgressLogFzdpkg-progress.%s.logz!writing dpkg progress log to '%s'wz
%s: Start
)r   start_updaterI   last_activityr%   r(   r#   r5   joinr&   r'   r!   r"   r7   dpkg_progress_logdevnullrK   )r   Zprogress_logZfullpathr   r   r   rQ      s    z*NonInteractiveInstallProgress.start_updatec             C   sG   t  j |   |  j j d t j    |  j j   |  j d 7_ d  S)Nz%s: Finished
r   )r   finish_updaterT   rK   rI   closer'   )r   r   r   r   rV      s    z+NonInteractiveInstallProgress.finish_updatec             C   s*   |  j  j d t j   | | | f  d  S)Nz%s:%s:%s:%s
)rT   rK   rI   )r   rB   r   Z
status_strr   r   r   status_change   s    z+NonInteractiveInstallProgress.status_changec             C   sF  t  j |   |  j d  k r  d  S|  j |  j t j   k  rr t j d |  j |  j f  t	 j
 |  j t d   t j |  j g g  g  d  } x t | d  d k r4t j   |  _ yB t	 j |  j d  } t j j
 d | j t j   d d  Wn t k
 rd  SYn Xt j |  j g g  g  d  } q Wt j j   d  S)	Nz,no activity %s seconds (%s) - sending ctrl-c   g?r   r   z%serrorsignore)r   update_interfaceZstatusfdrR   r*   rI   r!   r;   r   r#   rK   rL   chrselectlenr<   r   r   decodelocalegetpreferredencodingOSErrorr   )r   ressr   r   r   r\      s$    		"z.NonInteractiveInstallProgress.update_interfacec             C   si   t  j d  d t j d <d t j d <t j   \ |  _ |  _ |  j d k rb t  j d |  j  |  j S)Nzdoing a pty.fork()ZdumbZTERMtrueZPAGERr   z
pid is: %s)r!   r"   r#   r$   ptyforkpidrL   )r   r   r   r   rh      s    z"NonInteractiveInstallProgress.forkN)r   r   r   __doc__r    r3   rO   rQ   rV   rX   r\   rh   r   r   r   r   r   5   s   `
r   c               @   s   e  Z d  Z d Z d d d d  Z d d   Z d d   Z d	 d
   Z d d d  Z d d   Z	 d d   Z
 d d d d  Z d d d  Z d d   Z d d d  Z d d   Z d S)DistUpgradeViewNonInteractivez- non-interactive version of the upgrade view Nc             C   s\   t  j |   t d  |  _ t   |  _ t |  |  _ t j	 j
 j   |  _ |  j t _ d  S)Nr   )r   r    r
   r%   r   _acquireProgressr   _installProgressaptZprogressbaseZ
OpProgress_opProgress
excepthookr   __excepthook__)r   Zdatadirr&   r   r   r   r       s    z&DistUpgradeViewNonInteractive.__init__c             C   s`   d d l  } t j d | | f  | j | | |  } t j d d j |   |  j   d S)z2 on uncaught exceptions -> print error and reboot r   Nzgot exception '%s': %s znot handled exception:
%s )	tracebackr!   Z	exceptionformat_exceptionr3   rS   confirmRestart)r   typevaluetbrt   linesr   r   r   rq     s
    z(DistUpgradeViewNonInteractive.excepthookc             C   s   |  j  S)z5 return a OpProgress() subclass for the given graphic)rp   )r   r   r   r   getOpCacheProgress  s    z0DistUpgradeViewNonInteractive.getOpCacheProgressc             C   s   |  j  S)z# return an acquire progress object )rl   )r   r   r   r   getAcquireProgress  s    z0DistUpgradeViewNonInteractive.getAcquireProgressc             C   s   |  j  S)z" return a install progress object )rm   )r   cacher   r   r   getInstallProgress  s    z0DistUpgradeViewNonInteractive.getInstallProgressc             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*DistUpgradeViewNonInteractive.updateStatusc             C   s   d S)z we have 5 steps current for a upgrade:
        1. Analyzing the system
        2. Updating repository information
        3. Performing the upgrade
        4. Post upgrade stuff
        5. Complete
        Nr   )r   stepr   r   r   setStep  s    z%DistUpgradeViewNonInteractive.setStepTc             C   s   t  j |  | | | | |  t j d d d   |  j D  t j d d d   |  j D  t j d d d   |  j D  d S)	Nztoinstall: '%s'c             S   s   g  |  ] } | j   q Sr   )rE   ).0pr   r   r   
<listcomp>&  s   	 z@DistUpgradeViewNonInteractive.confirmChanges.<locals>.<listcomp>ztoupgrade: '%s'c             S   s   g  |  ] } | j   q Sr   )rE   )r   r   r   r   r   r   '  s   	 ztoremove: '%s'c             S   s   g  |  ] } | j   q Sr   )rE   )r   r   r   r   r   r   (  s   	 T)r   confirmChangesr!   r"   Z	toInstallZ	toUpgradeZtoRemove)r   summaryZchangesZ	demotionsZdownloadSizeZactionsZremoval_boldr   r   r   r   "  s    
!!!z,DistUpgradeViewNonInteractive.confirmChangesZNoc             C   s   d S)z0 ask a Yes/No question and return True on 'Yes' Tr   )r   r   r   defaultr   r   r   askYesNoQuestion*  s    z.DistUpgradeViewNonInteractive.askYesNoQuestionc             C   s#   t  j d  |  j j d d d  S)z2 generic ask about the restart, can be overridden zconfirmRestart() calledr   Z
RealRebootF)r!   r"   r%   r(   )r   r   r   r   rv   1  s    z,DistUpgradeViewNonInteractive.confirmRestartc             C   s   t  j d | | | f  d S)z display a error z
%s %s (%s)N)r!   r3   )r   r   r   Zextended_msgr   r   r   r3   6  s    z#DistUpgradeViewNonInteractive.errorc             C   s   t  j d  d  S)Nzview.abort called)r!   r3   )r   r   r   r   abort9  s    z#DistUpgradeViewNonInteractive.abort)r   r   r   rj   r    rq   r{   r|   r~   r   r   r   r   rv   r3   r   r   r   r   r   rk      s   
rk   __main__zxserver-xorgzpre-installation script failedr.   )&rn   r   r!   ra   rI   r   r#   rg   r^   r@   r4   Zapt.progressconfigparserr   r   r   r   r   r   r	   ZDistUpgradeConfigParserr
   r   r   rk   r   ZviewZapZipr3   ZCacher}   argvrB   Zmark_installZcommitrJ   exitr   r   r   r   <module>   s:   C			