3
g
]5                 @   sV  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 ddlmZ G dd	 d	eZG d
d deZG dd deZedkrRe Ze Ze Zej dd e j! Z"x$ej#dd D ]Z$e"e$ j%  qW e"j&ee ej'd ej(d  dS )    N)NoSectionErrorNoOptionError)PIPEPopen   )DistUpgradeViewInstallProgressAcquireProgress)get)DistUpgradeConfigc               @   s   e Zd Zdd ZdS )NonInteractiveAcquireProgressc             C   sT   t j| |||| |tjkrPtd|| jtjt| jf  t	j
j rPt	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               @   sP   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r(dtjd< dtjd< dtjd< td| _|| _d	| _	y"| jj
d
ddrrtjjdd W n ttfk
r   Y nX d| _y| jjd
d| _W n tk
r   Y nX d 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s&d S tjtj}d|d< g }d|krbd}d	}d
}d|||f }n~d|krd}d}d}d||f }n\d|krd}d}d}d|||f }n8d|krd}d}d}d|||f }ntd||f  d S tjj	|s t jd|  d S t
|"}	|	j dd  j j d }
W d Q R X d|
ksDd|
krLdg}n(d|
krfdg}d|d < nt jd!|
  t
|}	|	j }W d Q R X d"|krd#|d$< d%|d&< d'}
d(dg}|j|
 |j| |j| |j| |d	kr&td)| d*td*d+j d }|r|j|jd,d-d- j  n`|dkrtjj|}|jd.d }td/| d*td*d+j d }|r|j|jd,d-d- j  t jd0||f  tj||d1}t jd2||f  d S )3Nz)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-VersionT)shellr   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   r4   W   s    

&









z#NonInteractiveInstallProgress.errorc             C   sn   t jd|  tjd y tj| jd t jd|  W n0 tk
rh } zt jd|  W Y d d }~X nX d S )Nz.got a conffile-prompt from dpkg for file: '%s'   s   n
z0replied no to the conffile-prompt for file: '%s'z/error '%s' when trying to write to the conffile)	r"   r<   timesleepr$   write	master_fdr,   r4   )r   Zcurrentnewer   r   r   conffile   s    
z&NonInteractiveInstallProgress.conffilec             C   s   t j|  tj | _| jjddd}|rZt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_updaterJ   last_activityr&   r)   r$   r6   joinr'   r(   r"   r#   r8   dpkg_progress_logdevnullrL   )r   Zprogress_logZfullpathr   r   r   rR      s    

z*NonInteractiveInstallProgress.start_updatec             C   s:   t j|  | jjdtj   | jj  |  jd7  _d S )Nz%s: Finished
r   )r   finish_updaterU   rL   rJ   closer(   )r   r   r   r   rW      s    

z+NonInteractiveInstallProgress.finish_updatec             C   s    | j jdtj |||f  d S )Nz%s:%s:%s:%s
)rU   rL   rJ   )r   rC   r   Z
status_strr   r   r   status_change   s    z+NonInteractiveInstallProgress.status_changec             C   s   t j|  | jd krd S | j| j tj k rTtjd| j| jf  t	j
| jtd tj| jgg g d}xxt|d dkrtj | _y0t	j| jd}tjj
d|jtj dd  W n tk
r   d S X tj| jgg g d}qjW tjj  d S )	Nz,no activity %s seconds (%s) - sending ctrl-c   g?r   r   z%signore)errors)r   update_interfaceZstatusfdrS   r+   rJ   r"   r<   r   r$   rL   rM   chrselectlenr=   r   r   decodelocalegetpreferredencodingOSErrorr   )r   ressr   r   r   r]      s$    


z.NonInteractiveInstallProgress.update_interfacec             C   sN   t jd dtjd< dtjd< tj \| _| _| jdkrHt 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pidrM   )r   r   r   r   ri      s    



z"NonInteractiveInstallProgress.forkN)r   r   r   __doc__r!   r4   rP   rR   rW   rY   r]   ri   r   r   r   r   r   6   s   `r   c                   s   e Zd ZdZdddZdd Zdd Zd	d
 ZdddZdd Z	 fddZ
dddZd ddZdd Zd!ddZdd Z  ZS )"DistUpgradeViewNonInteractivez- non-interactive version of the upgrade view Nc             C   sL   t j|  t jd td| _t | _t|| _	t
jjj | _| jt_d S )Nr    r   )r   r!   get_telemetryZset_updater_typer   r&   r   _acquireProgressr   _installProgressaptZprogressbaseZ
OpProgress_opProgress
excepthookr   __excepthook__)r   Zdatadirr'   r   r   r   r!      s    


z&DistUpgradeViewNonInteractive.__init__c             C   sH   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_exceptionr4   rT   confirmRestart)r   typevaluetbrv   linesr   r   r   rs     s
    z(DistUpgradeViewNonInteractive.excepthookc             C   s   | j S )z5 return a OpProgress() subclass for the given graphic)rr   )r   r   r   r   getOpCacheProgress  s    z0DistUpgradeViewNonInteractive.getOpCacheProgressc             C   s   | j S )z# return an acquire progress object )rn   )r   r   r   r   getAcquireProgress  s    z0DistUpgradeViewNonInteractive.getAcquireProgressc             C   s   | j S )z" return a install progress object )ro   )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                s   t t| j| 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
        N)superrl   setStep)r   step)	__class__r   r   r     s    z%DistUpgradeViewNonInteractive.setStepTc             C   sf   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S r   )rF   ).0pr   r   r   
<listcomp>,  s    z@DistUpgradeViewNonInteractive.confirmChanges.<locals>.<listcomp>ztoupgrade: '%s'c             S   s   g | ]
}|j qS r   )rF   )r   r   r   r   r   r   -  s    ztoremove: '%s'c             S   s   g | ]
}|j qS r   )rF   )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.confirmChangesNoc             C   s   dS )z0 ask a Yes/No question and return True on 'Yes' Tr   )r   r   r   defaultr   r   r   askYesNoQuestion0  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   rx   7  s    
z,DistUpgradeViewNonInteractive.confirmRestartc             C   s   t jd|||f  dS )z display a error z
%s %s (%s)N)r"   r4   )r   r   r   Zextended_msgr   r   r   r4   <  s    z#DistUpgradeViewNonInteractive.errorc             C   s   t jd d S )Nzview.abort called)r"   r4   )r   r   r   r   abort?  s    z#DistUpgradeViewNonInteractive.abort)NN)N)NT)r   )N)r   r   r   rk   r!   rs   r}   r~   r   r   r   r   r   rx   r4   r   __classcell__r   r   )r   r   rl      s   




rl   __main__zxserver-xorgzpre-installation script failedr/   ))rp   r   r"   rb   rJ   r   r$   rh   r_   rA   r5   Zapt.progressconfigparserr   r   r   r   r   r   r	   Z	telemetryr
   rm   ZDistUpgradeConfigParserr   r   r   rl   r   ZviewZapZipr4   ZCacher   argvrC   Zmark_installZcommitrK   exitr   r   r   r   <module>   s>    IE

