o
    >I¼cÑÓ  ã                   @   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mZmZ ddl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dd„ Zeƒ \ZZG dd„ deƒZG dd„ deƒZG dd„ de jƒZe dkrÈd dl!Z!ddl"m#Z# ddl$m$Z$ e%dƒ ee#dƒe$ƒ dƒZ&e%e& '¡ ƒ e! (¡  e& )¡  e& *¡  e&j+ e& ,dg¡ e%e& -¡ ƒ e& .¡  dS dS )é    N)ÚPopenÚPIPEé   )Úgettext)Úngettext)Úinside_chrootc                   @   ó   e Zd ZdS )ÚCacheExceptionN©Ú__name__Ú
__module__Ú__qualname__© r   r   ú>/usr/lib/python3/dist-packages/DistUpgrade/DistUpgradeCache.pyr	   (   ó    r	   c                   @   r   )ÚCacheExceptionLockingFailedNr
   r   r   r   r   r   ,   r   r   c                   @   r   )ÚCacheExceptionDpkgInterruptedNr
   r   r   r   r   r   0   r   r   c                  C   sÞ   d} d}t  ¡ d }t d| ¡D ]/}|d| kr"|t j |¡7 }q| d¡dks7| d¡dks7| d¡dkr8q| t j |¡7 } q| dkrLt d¡ d	} |dkrWt d
¡ d}| d7 } d}|d |krg|d }||7 }| |fS )zjestimate the amount of space used by the kernel and initramfs in /boot,
    including a safety margin
    r   é   z
/boot/*%s*z/boot/initrd.img-%sz
initrd.imgz.bakz.dpkg-z>estimate_kernel_initrd_size_in_boot() returned '0' for kernel?i   z>estimate_kernel_initrd_size_in_boot() returned '0' for initrd?i  ð
i   gš™™™™™©?)ÚosÚunameÚglobÚpathÚgetsizeÚfindÚloggingÚwarning)ÚkernelÚinitrdÚkverÚfÚinitrd_bufferr   r   r   Ú#estimate_kernel_initrd_size_in_boot4   s4   ÿÿr!   c                   @   s    e Zd ZdZdd„ Zdd„ ZdS )ÚFreeSpaceRequiredzÁ FreeSpaceRequired object:

    This exposes:
    - the total size required (size_total)
    - the dir that requires the space (dir)
    - the additional space that is needed (size_needed)
    c                 C   s   || _ || _|| _d S ©N)Ú
size_totalÚdirÚsize_needed)Úselfr$   r%   r&   r   r   r   Ú__init__`   s   
zFreeSpaceRequired.__init__c                 C   s   d| j | j| jf S )Nz@FreeSpaceRequired Object: Dir: %s size_total: %s size_needed: %s)r%   r$   r&   ©r'   r   r   r   Ú__str__d   s   zFreeSpaceRequired.__str__N)r   r   r   Ú__doc__r(   r*   r   r   r   r   r"   X   s    r"   c                   @   ó   e Zd ZdZdd„ ZdS )ÚNotEnoughFreeSpaceErrorzI
    Exception if there is not enough free space for this operation

    c                 C   s
   || _ d S r#   )Úfree_space_required_list)r'   r.   r   r   r   r(   m   s   
z NotEnoughFreeSpaceError.__init__N©r   r   r   r+   r(   r   r   r   r   r-   h   s    r-   c                   @   sø  e Zd ZdZdZdsdd„Zdd„ Zed	d
„ ƒZdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zedd„ ƒZedd„ ƒZedd„ ƒZedd„ ƒZdd„ Zdd „ Zdtd!d"„Zd#d$„ Zdud%d&„Zdud'd(„Zdud)d*„Zd+d,„ Zd-d.„ Zd/d0„ Zd1d2„ Zd3d4„ Zd5d6„ Zd7d8„ Zdvd:d;„Z dvd<d=„Z!dvd>d?„Z"dvd@dA„Z#dBdC„ Z$dDdE„ Z%dFdG„ Z&dHdI„ Z'dJdK„ Z(dLdM„ Z)dNdO„ Z*dPdQ„ Z+dRdS„ Z,edTdU„ ƒZ-dVdW„ Z.dXdY„ Z/edZd[„ ƒZ0d\d]„ Z1d^d_„ Z2d`da„ Z3dbdc„ Z4eddde„ ƒZ5dfdg„ Z6dhdi„ Z7djdk„ Z8dldm„ Z9dndo„ Z:dwdqdr„Z;dS )xÚMyCacher   é   NTc              
   C   s"  g | _ g | _|| _|| _d| _d| _|| _| j dd¡| _d| _	|rJzt
 ¡  |  ¡  d| _W n tyI } zdt|ƒv rAt|ƒ‚t|ƒ‚d }~ww tj | |¡ | dd¡| _| j | d¡| _tdd	gtdd
� ¡ d  ¡ | _|  ¡  | dd¡dkrˆdtj vrˆt
j !dd¡ t
j !dd¡ d S )NFÚDistroÚMetaPkgséÿÿÿÿTzdpkg --configure -aÚRemovalDenylistFiler   z-r)ÚstdoutÚuniversal_newlinesr   ÚSourcesÚFromÚhardyÚRELEASE_UPGRADE_NO_RECOMMENDSzAPT::Install-RecommendsÚtruez"APT::AutoRemove::SuggestsImportantÚfalse)"Ú
to_installÚ	to_removeÚviewÚquirksÚlockÚpartialUpgradeÚconfigÚgetlistÚmetapkgsÚ
_listsLockÚapt_pkgÚpkgsystem_lockÚlock_lists_dirÚSystemErrorÚstrr   r   ÚaptÚCacher(   ÚgetListFromFileÚremoval_denylistÚ_get_linux_metapackageÚlinux_metapackager   r   ÚcommunicateÚstripr   Ú_initAptLogÚgetr   ÚenvironÚset)r'   rD   r@   rA   ÚprogressrB   Úer   r   r   r(   v   sF   
€ü
ÿÿÿ
zMyCache.__init__c                 C   s2   | D ]}|j rq|jjtjkr|jddd� qdS )z! honor the dselect install state F)Ú	auto_instÚauto_fixN)Úis_installedÚ_pkgÚselected_staterH   ÚSELSTATE_INSTALLÚmark_install©r'   Úpkgr   r   r   Ú_apply_dselect_upgrade�   s   €ûzMyCache._apply_dselect_upgradec                 C   sJ   t ƒ }| D ]}|jr|jjs"|jj| jks|jj| jkr"| |j¡ q|S )zB return the packages not downloadable packages in reqreinst state )	rX   Ú	candidateÚdownloadabler^   Ú
inst_stateÚ	ReInstReqÚHoldReInstReqÚaddÚname)r'   Ú	reqreinstrc   r   r   r   Úreq_reinstall_pkgs¦   s   €zMyCache.req_reinstall_pkgsc                 C   s~   | j }t|ƒdkr=tddt|ƒƒ}tddt|ƒƒd |¡ }| ||¡r=|  ¡  g d¢t|ƒ }| ¡  |¡ |  	¡  dS d	S )
z/ check for reqreinst state and offer to fix it r   zRemove package in bad statezRemove packages in bad statez¡The package '%s' is in an inconsistent state and needs to be reinstalled, but no archive can be found for it. Do you want to remove this package now to continue?z§The packages '%s' are in an inconsistent state and need to be reinstalled, but no archives can be found for them. Do you want to remove these packages now to continue?ú, )z/usr/bin/dpkgz--removez--force-remove-reinstreqTF)
rm   Úlenr   ÚjoinÚaskYesNoQuestionÚrelease_lockÚlistÚgetTerminalÚcallÚget_lock)r'   r@   rl   ÚheaderÚsummaryÚcmdr   r   r   Úfix_req_reinst²   s&   þö
özMyCache.fix_req_reinstc                 C   s¾   | j  ddd¡}tj |¡st |¡ tj  d|¡ tj  dd¡ t tj 	|d¡tj
tjB tjB d¡| _tj ¡ }d	| }t | j| d
¡¡ tj  dd¡ tj  dd¡ tj  dd¡ dS )z init logging, create log fileÚFilesÚLogDirz/var/log/dist-upgradezDir::LogzDir::Log::Terminalzapt-term.logzapt.logi¤  zLog time: %s
zutf-8zDebug::pkgProblemResolverr<   zDebug::pkgDepCache::MarkerzDebug::pkgDepCache::AutoInstallN)rD   ÚgetWithDefaultr   r   ÚexistsÚmakedirsrH   rX   Úopenrp   ÚO_RDWRÚO_CREATÚO_APPENDÚlogfdÚdatetimeÚnowÚwriteÚencode)r'   Úlogdirr†   rw   r   r   r   rU   Í   s    
ÿ
ÿ
zMyCache._initAptLogc                 C   sZ   t | dƒrt | j¡ t | j¡ t d¡| _t d¡| _t | jd¡ t | jd¡ d S )NÚ
old_stdoutr   r   )Úhasattrr   ÚcloserŠ   Ú
old_stderrÚdupÚdup2r„   r)   r   r   r   Ú_startAptResolverLogß   s   
zMyCache._startAptResolverLogc                 C   s4   t  d¡ t  d¡ t  | jd¡ t  | jd¡ d S )Nr   r   )r   Úfsyncr�   rŠ   r�   r)   r   r   r   Ú_stopAptResolverLogç   s   

zMyCache._stopAptResolverLogc                    s   ‡ fdd„}|S )z3 decorator to ensure that the apt output is logged c                     s*   | d   ¡  ˆ | i |¤Ž}| d  ¡  |S ©Nr   )r�   r’   )ÚargsÚkwargsÚres©r   r   r   Úwrapperñ   s   z(MyCache.withResolverLog.<locals>.wrapperr   )r   r˜   r   r—   r   ÚwithResolverLogï   s   zMyCache.withResolverLogc                 C   s,   t  | j¡}t  ¡ }| || j| j¡ |jS )z< get the size of the packages that are required to download )rH   ÚPackageManagerÚ	_depcacheÚAcquireÚget_archivesÚ_listÚ_recordsÚfetch_needed)r'   ÚpmÚfetcherr   r   r   Úrequired_downloadù   s   zMyCache.required_downloadc                 C   s   | j jS )z9 get the size of the additional required space on the fs )r›   Úusr_sizer)   r   r   r   Úadditional_required_space   s   z!MyCache.additional_required_spacec                 C   s    z| j jW S  ty   Y dS w )z< get the extra size needed to install the snap replacements r   )rA   Úextra_snap_spaceÚAttributeErrorr)   r   r   r   Ú#additional_required_space_for_snaps  s
   
ÿz+MyCache.additional_required_space_for_snapsc                 C   s   | j jdkS )z is the cache broken r   )r›   Úbroken_countr)   r   r   r   Ú	is_broken  s   zMyCache.is_brokenc                 C   s:   t j d¡d }t  |¡| _| jdk rd| }t|ƒ‚d S )NzDir::State::ListsrB   r   zCan not lock '%s' )rH   rD   Úfind_dirrv   rG   r   )r'   rk   rZ   r   r   r   rJ     s   
þzMyCache.lock_lists_dirc                 C   s$   | j dkrt | j ¡ d| _ d S d S )Nr   r4   )rG   r   rŒ   r)   r   r   r   Úunlock_lists_dir  s   

þzMyCache.unlock_lists_dirc                 C   sH   |   ¡  tj | |¡}|  ¡  |r|jrttdƒƒ‚|dkr"tdƒ‚dS )zf
        our own update implementation is required because we keep the lists
        dir lock
        zThe server may be overloadedFzAapt.cache.update() returned False, but did not raise exception?!?N)r¬   rM   rN   ÚupdaterJ   Úrelease_file_download_errorÚIOErrorÚ_)r'   Ú	fprogressr–   r   r   r   r­   "  s   
ÿzMyCache.updatec                 C   s,   t  d¡ | jr|  ¡  tj | ||¡ d S )Nzcache.commit())r   ÚinforB   rr   rM   rN   Úcommit)r'   r±   Ú	iprogressr   r   r   r³   1  s   
zMyCache.commitc              
   C   sT   | j r(z
t ¡  d| _ W d S  ty' } zt d| ¡ W Y d }~d S d }~ww d S )NFzfailed to SystemUnLock() (%s) )rB   rH   Úpkgsystem_unlockrK   r   Údebug©r'   ÚpkgSystemOnlyrZ   r   r   r   rr   7  ó   €ÿüzMyCache.release_lockc              
   C   sT   | j s(z
t ¡  d| _ W d S  ty' } zt d| ¡ W Y d }~d S d }~ww d S )NTzfailed to SystemLock() (%s) )rB   rH   rI   rK   r   r¶   r·   r   r   r   rv   ?  r¹   zMyCache.get_lockc                 C   sB   |r
| j  |j¡}n|jj}|dkrt d|j|f ¡ dS |jS )z* check if the given pkg can be downloaded Nz1no version information for '%s' (useCandidate=%s)F)r›   Úget_candidate_verr^   Úcurrent_verr   r   rk   rf   )r'   rc   ÚuseCandidateÚverr   r   r   rf   G  s   zMyCache.downloadablec                 C   s   |j o	| j |j¡S )z$ check if the pkg is auto-removable )r]   r›   Ú
is_garbager^   rb   r   r   r   Úpkg_auto_removableR  s   ÿzMyCache.pkg_auto_removablec                 C   s
   | j  ¡ S )z^ try to fix broken dependencies on the system, may throw
            SystemError when it can't)r›   Ú
fix_brokenr)   r   r   r   rÀ   W  s   
zMyCache.fix_brokenc                 C   sL   g | _ g | _|  ¡ D ]}|js|jr| j  |j¡ |jr#| j |j¡ q
dS )z* create a snapshot of the current changes N)r>   r?   Úget_changesÚmarked_installÚmarked_upgradeÚappendrk   Úmarked_deleterb   r   r   r   Úcreate_snapshot\  s   €üzMyCache.create_snapshotc                 C   s   | j  ¡  d S r#   )r›   Úinitr)   r   r   r   Úclearf  s   zMyCache.clearc                 C   sZ   t  | j¡}| |  ¡  | jD ]
}| | }| ¡  q| jD ]}| | }|jddd� qdS )z restore a snapshot F)r\   r[   N)rH   ÚActionGroupr›   rÈ   r?   Úmark_deleter>   ra   )r'   Úactiongrouprk   rc   r   r   r   Úrestore_snapshoti  s   


þzMyCache.restore_snapshotc                 C   s’   | j  dd¡}|D ]8}|| v r| | jrt d| ¡  dS d}| j  |d¡D ]}||| v o2| | jM }q'|rAt d| ¡  dS q	t d¡ dS )	ac  
        This checks if we run on a desktop or a server install.

        A server install has more freedoms, for a desktop install
        we force a desktop meta package to be install on the upgrade.

        We look for a installed desktop meta pkg and for key
        dependencies, if none of those are installed we assume
        server mode
        r2   r3   z@need_server_mode(): run in 'desktop' mode, (because of pkg '%s')FTÚKeyDependencieszIneed_server_mode(): run in 'desktop' mode, (because of key deps for '%s')z[need_server_mode(): can not find a desktop meta package or key deps, running in server mode)rD   rE   r]   r   r¶   )r'   rF   ÚkeyÚ
deps_foundrc   r   r   r   Úneed_server_modew  s   þ
zMyCache.need_server_modec                 C   sL   | j r$zt d¡ |  ¡  W dS  ty#   | tdƒtdƒ¡ Y dS w dS )zY check if the cache is ok and if the required metapkgs
            are installed
        z$Have broken pkgs, trying to fix themzBroken packagesz’Your system contains broken packages that couldn't be fixed with this software. Please fix them first using synaptic or apt-get before proceeding.FT)rª   r   r¶   rÀ   rK   Úerrorr°   )r'   r@   r   r   r   Úcoherence_check•  s   

	ø
ÿùzMyCache.coherence_checkÚ c                 K   sX   t  d||f ¡ || v r*| | jdi |¤Ž | | js*| | js*t  d| ¡ dS dS )NzInstalling '%s' (%s)z Installing/upgrading '%s' failedFTr   )r   r¶   ra   rÂ   rÃ   rÑ   ©r'   rc   ÚreasonÚflagsr   r   r   ra   §  s   zMyCache.mark_installc                 C   sP   t  d||f ¡ || v r&| | jr&| |  ¡  | | js&t  d| ¡ dS dS )NzUpgrading '%s' (%s)zUpgrading '%s' failedFT)r   r¶   r]   Úmark_upgraderÃ   rÑ   ©r'   rc   rÕ   r   r   r   r×   ±  s   
zMyCache.mark_upgradec                 K   s6   t  d||f ¡ || v r| | jdi |¤Ž d S d S )NzRemoving '%s' (%s)r   )r   r¶   rÊ   rÔ   r   r   r   Úmark_removeº  ó   ÿzMyCache.mark_removec                 C   s6   t  d||f ¡ || v r| j | | jd¡ d S d S )NzPurging '%s' (%s)T)r   r¶   r›   rÊ   r^   rØ   r   r   r   Ú
mark_purge¿  rÚ   zMyCache.mark_purgec                 C   s8   || v r| | j r| | jr|  ||¡ d S d S d S d S r#   )r]   rÅ   ra   )r'   ÚpkgnamerÕ   r   r   r   Ú_keep_installedÄ  s   ÿþýzMyCache._keep_installedc              	   C   s\  | j  dd¡D ]}|  |d¡ q| jD ]"}|| v r5| | js#| | jr5| j  |d¡D ]
}|  |d| ¡ q*q| j  dd¡dkrªt d¡ | j  dd	¡D ] }| D ]}|j	rj|j	j
rj|jrj|j	j|krj|  |jd
| ¡ qOqK| jD ]<}|| v r©| | js| | jr©| j  |d	¡D ]"}| D ]}|j	r§|j	j
r§|jr§|j	j|kr§|  |jd||f ¡ qŠq†qodS dS )z[ run after the dist-upgrade to ensure that certain
            packages are kept installed r2   ÚKeepInstalledPkgszDistro KeepInstalledPkgs rulez%s KeepInstalledPkgs ruleÚOptionsÚwithNetworkÚTruez"Running KeepInstalledSection rulesÚKeepInstalledSectionz$Distro KeepInstalledSection rule: %sz %s KeepInstalledSection rule: %sN)rD   rE   rÝ   rF   r]   rÂ   rV   r   r¶   re   rf   rÅ   Úsectionrk   )r'   rÜ   rÎ   rã   rc   r   r   r   Úkeep_installed_ruleÊ  sH   
ÿ€
ÿ€ü
ÿÿ€ü€ó	zMyCache.keep_installed_rulec                 C   s   | j s| j d¡ dS dS )z. run before the upgrade was done in the cache ÚPreDistUpgradeCacheN)rC   rA   Úrunr)   r   r   r   Úpre_upgrade_ruleí  s   ÿzMyCache.pre_upgrade_rulec                 C   sÀ   d| j fd| jfd| jfd| jffD ]@\}}| j dd| ¡D ]	}||d| ƒ q| jD ]%}|| v rQ| | js<| | jrQ| j |d| ¡D ]}||d||f ƒ qEq,q| j	s^| j
 d	¡ d
S d
S )z- run after the upgrade was done in the cache ÚInstallÚUpgradeÚRemoveÚPurger2   zPostUpgrade%szDistro PostUpgrade%s rulez%s PostUpgrade%s ruleÚPostDistUpgradeCacheN)ra   r×   rÙ   rÛ   rD   rE   rF   r]   rÂ   rC   rA   ræ   )r'   ÚruleÚactionrc   rÎ   r   r   r   Úpost_upgrade_ruleó  s$   ý
ÿ€üÿzMyCache.post_upgrade_rulec              
   C   sD  t  d¡ zddlm} W n ttfy) } zt  d| ¡ W Y d}~dS d}~ww z]|dd�}|jD ]}|| v rF| | jrF|  	|d	¡  n	q3t  
d
¡ W dS | ¡ }t  d| ¡ || vrht  d| ¡ W dS | | js‚| | js…| |  ¡  t  
d| ¡ W dS W dS W dS  ty¡ } zt  d| ¡ W Y d}~dS d}~ww )zR
        this checks for nvidia hardware and checks what driver is needed
        znvidiaUpdate()r   )ÚNvidiaDetectionz%NvidiaDetector can not be imported %sNFz./ubuntu-drivers-obsolete.pkgs)Úobsoletezold nvidia driverz1no old nvidia driver installed, installing no newznv.selectDriver() returned '%s'zno '%s' foundz,installing %s as suggested by NvidiaDetectorTz$NvidiaDetection returned a error: %s)r   r¶   ÚNvidiaDetector.nvidiadetectorrð   ÚImportErrorÚSyntaxErrorrÑ   ÚoldPackagesr]   rÙ   r²   ÚselectDriverr   rÂ   rÃ   ra   Ú	Exception)r'   rð   rZ   ÚnvÚ	oldDriverÚdriverr   r   r   ÚcheckForNvidia  sF   
€ü

€
ýúþ€þzMyCache.checkForNvidiac                 C   s&   | D ]}|j  d¡r|jr dS qdS )Nzlinux-headers-TF)rk   Ú
startswithr]   rb   r   r   r   Ú_has_kernel_headers_installed/  s   ÿ€z%MyCache._has_kernel_headers_installedc              
   C   sˆ   t  d| j ¡ z| j d¡\}}}W n ty- } zt  d| ¡ W Y d}~dS d}~ww tdgtd� ¡ d }d	|v rBt  d
¡ dS )zd check for the running kernel and try to ensure that we have
            an updated version
        zKernel uname: '%s' ú-z/Can't parse kernel uname: '%s' (self compiled?)NFÚdmesg)r6   r   s   WARNING: NR_CPUS limitzUP kernel on SMP system!?!T)	r   r¶   r   Úsplitr÷   r   r   r   rS   )r'   ÚversionÚbuildÚflavourrZ   rÿ   r   r   r   ÚcheckForKernel6  s   €þ
zMyCache.checkForKernelc                 C   sœ   d}| j  dd¡}| D ]@}|jj |j¡}|r%|jdkr%t d|j	 ¡ q|j
rK|j
jrK|jsK|jsK|j	|vrKd|j	vrK|j
j|v rK|  |j	d| ¡ qd S )N)Úrequiredr2   ÚRemoveEssentialOkr   zPackage %s has no priority setú:z;priority in required set '%s' but not scheduled for install)rD   rE   Ú_pcacher›   rº   r^   Úpriorityr   rÑ   rk   re   rf   r]   rÂ   ra   )r'   ÚneedÚremoveEssentialOkrc   r½   r   r   r   ÚcheckPriorityF  s$   ÿÿ

€ôzMyCache.checkPriorityc                 C   sZ   d}|  ¡ r!|d dkr| ¡  | ¡  t d¡ |d7 }|  ¡ s|jdd� | ¡  d S )Nr   é   g{®Gáz”?r   T)Úfinished)ÚlockedÚpulseProgressÚprocessEventsÚtimeÚsleep)r'   r@   rB   Úir   r   r   Ú	updateGUI[  s   
ûzMyCache.updateGUIc              
   C   sr  t  ¡ }| ¡  t j| j| j|fd�}| ¡  zÍzK|  ¡  |  d¡ |  	¡  |  
¡  tƒ r3t d¡ n|  ¡  |  ¡  |  ¡  |  |¡ |sK|  |¡ |  ¡  | jrbttdƒd dd„ | D ƒ¡ ƒ‚W nw tyÚ } zktdƒ}| j d	d
¡dkr|tdƒ7 }n| j d	d
¡dkr—| j d	d¡dkr—|tdƒ7 }|r |tdƒ7 }n|tdƒ7 }|tdƒ7 }|  ¡  | tdƒ|¡ t|ƒ}t d|¡ |  ¡  W Y d }~W | ¡  | ¡  dS d }~ww W | ¡  | ¡  n	| ¡  | ¡  w g }	g }
|  ¡ D ]^}|j rüqö|j!�r:|
 "|j#¡ |j$j%D ].}t& '|j(|j)j*¡dk �r7|j+D ]\}}|j,j- .|¡}|�r5|j/�s5|	 "|j#¡  n�q�q
qö|j0j1}d}|D ]}||j2O }�qB|�sT|	 "|j#¡ qöz| j 3dd¡}|�rht d¡ W dS W n t4j5�yt   Y nw t6|
ƒdk�rŠ|
 7¡  t dd |
¡ ¡ t6|	ƒdk�r·|	 7¡  t dd |	¡ ¡ |  ¡  | tdƒtdƒd |	¡¡ |  ¡  dS dS ) N)Útargetr”   Tz5skipping kernel checks because we run inside a chrootz!Broken packages after upgrade: %srn   c                 s   s"   � | ]}|j s
|jr|jV  qd S r#   )Úis_inst_brokenÚis_now_brokenrk   )Ú.0Úpr   r   r   Ú	<genexpr>”  s   €  z&MyCache.distUpgrade.<locals>.<genexpr>zBAn unresolvable problem occurred while calculating the upgrade.

 rß   ÚforeignPkgsrá   zÓThis was likely caused by:
 * Unofficial software packages not provided by Ubuntu
Please use the tool 'ppa-purge' from the ppa-purge 
package to remove software from a Launchpad PPA and 
try the upgrade again.

ÚFalseÚ
devReleasez†This was caused by:
 * Upgrading to a pre-release version of Ubuntu
This is most likely a transient problem, 
please try again later.
z@This is most likely a transient problem, please try again later.z€If none of this applies, then please report this bug using the command 'ubuntu-bug ubuntu-release-upgrader-core' in a terminal. z©If you want to investigate this yourself the log files in '/var/log/dist-upgrade' will contain details about the upgrade. Specifically, look at 'main.log' and 'apt.log'.zCould not calculate the upgradezDist-upgrade failed: '%s'Fr   r2   ÚAllowUnauthenticatedzAllowUnauthenticated set!z!Packages to downgrade found: '%s'ú z$Unauthenticated packages found: '%s'z"Error authenticating some packagesz®It was not possible to authenticate some packages. This may be a transient network problem. You may want to try again later. See below for a list of unauthenticated packages.Ú
)8Ú	threadingÚLockÚacquireÚThreadr  r@   Ústartrç   Úupgrader  rä   r   r   r   r  rû   rï   Ú_keepBaseMetaPkgsInstalledÚ_installMetaPkgsÚ_verifyChangesrª   rK   r°   rp   rD   rV   r’   rÑ   rL   r�   ÚreleaserÁ   rÅ   Úmarked_downgraderÄ   rk   r^   Úversion_listrH   Úversion_compareÚver_strÚ	installedr  Ú	file_listr  rž   Ú
find_indexÚ
is_trustedre   ÚoriginsÚtrustedÚ
getbooleanÚconfigparserÚNoOptionErrorro   Úsort)r'   r@   Ú
serverModerC   rB   ÚtrZ   ÚdetailsÚ	error_msgÚ	untrustedÚ	downgraderc   r½   ÚverFileIterÚindexÚ	indexfiler4  r5  ÚoriginÚbr   r   r   ÚdistUpgradef  sÊ   


 ÿ€Õý-
ÿ

ÿÿ€€
þÿÿÿ
ûzMyCache.distUpgradec                 C   s  t j d¡}| j dd¡}|  ¡ D ]A}|jr,|  |j¡r,t 	d|j¡ t
tdƒ|j ƒ‚|jrR|jjdkrR|jj|dfv rR|j|vrRt 	d|j¡ t
td	ƒ|j ƒ‚q| j dd
¡}|D ],}| d¡\}}|| v rˆ| | jrˆ| | jj|krˆ| | js€| | jrˆt
tdƒ| ƒ‚q\dS )zy this function tests if the current changes don't violate
            our constraints (deny listed removals etc)
        zAPT::Architecturer2   r  zHThe package '%s' is marked for removal but it's in the removal deny listzJThe package '%s' is marked for removal but it is in the removal deny list.TÚallzDThe package '%s' is marked for removal but it's an ESSENTIAL packagez1The essential package '%s' is marked for removal.ÚBadVersionsr°   z*Trying to install deny listed version '%s')rH   rD   r   rE   rÁ   rÅ   Ú_inRemovalDenylistrk   r   r¶   rK   r°   r^   Ú	essentialr0  Úarchitecturer   re   r  rÂ   rÃ   )r'   Ú	main_archr  rc   ÚbadVersionsÚbvrÜ   r½   r   r   r   r*  û  s2   
€ÿþ€zMyCache._verifyChangesc                 C   sn   |j j |j¡}|du rtd|jƒ dS |jdu r$td| j ¡  ƒ dS |j d¡\}}|j j 	||f¡ dS )a  
        helper to make sure that the pkg._records is pointing to the right
        location - needed because python-apt 0.7.9 dropped the python-apt
        version but we can not yet use the new version because on upgrade
        the old version is still installed
        NzNo candidate ver: FzNo file_list for: %s r   T)
r  r›   rº   r^   Úprintrk   r1  ÚpoprŸ   Úlookup)r'   rc   r½   r   rA  r   r   r   Ú_lookupPkgRecord  s   
zMyCache._lookupPkgRecordc           	      C   sð   i }t ƒ }| D ]C}|  |¡st d|j ¡ q|jjj d¡D ]*}| 	d¡rI|t
dƒd …  d¡D ]}| ¡ }||vr@t ƒ ||< ||  |j¡ q1qq|D ](}d}| j dd¡}||v r^d}|| D ]}| | jsmd} nqb|ru| |¡ qM|S )	Nz&no PkgRecord found for '%s', skipping r!  úTask:ú,Tr2   ÚIgnoredTasksF)rX   rQ  r   r¶   rk   r  rŸ   Úrecordr   rü   ro   rT   rj   rD   rE   r]   )	r'   ÚtasksÚinstalled_tasksrc   ÚlineÚtaskr0  Úignored_tasksrÜ   r   r   r   ÚinstalledTasks)  s:   


€ú
þ
€zMyCache.installedTasksc                 C   s®   t  d¡ | D ]M}|js|jrq|  |¡ t|jjdƒr!|jjjs*t  	d|j
 ¡ q|jjj d¡D ]!}| d¡rS|tdƒd …  d¡D ]}| ¡ }||v rR| ¡  qDq2qdS )Nzrunning installTasksrU  zcan not find Record for '%s'r!  rR  rS  T)r   r¶   rÂ   r]   rQ  r‹   r  rŸ   rU  r   rk   r   rü   ro   rT   ra   )r'   rV  rc   rX  rY  r   r   r   ÚinstallTasksE  s"   


€ûzMyCache.installTasksc                 C   s$   | j  dd¡D ]}|  |d¡ qd S )Nr2   ÚBaseMetaPkgsz%base meta package keep installed rule)rD   rE   rÝ   )r'   r@   rc   r   r   r   r(  V  s   ÿz"MyCache._keepBaseMetaPkgsInstalledc                    sØ  ‡ ‡fdd„}ˆj  dd¡‰ ˆj  dd¡D ]}ˆ|  ¡  qˆ D ]:}z|ˆv r>ˆ| jr>ˆ| jr>t d| ¡ ˆ|  ¡  W q  tyZ } zt 	d||f ¡ W Y d }~q d }~ww |ƒ sÌt dˆ  ¡ ˆ D ]d}d	}ˆj  |d
¡D ]}||ˆv o}ˆ| jM }qr|rËt d| ¡ zˆ|  ¡  W n+ tt
fy¼ } zt d||f ¡ | tdƒ| tdƒ¡ W Y d }~ dS d }~ww t d|ˆ| jf ¡  nqg|ƒ sêd ˆ dd… ¡}| tdƒtdƒ|ˆ d f ¡ dS d	S )Nc                     sV   ˆ D ]&} | ˆv r(ˆ|  }|j r|jrt d|j ¡ |j r |jr%ˆ|  jr( dS qdS )zy
            internal helper that checks if at least one meta-pkg is
            installed or marked install
            z(metapkg '%s' installed but marked_deleteTF)r]   rÅ   r   r¶   rk   rÂ   )rÎ   rc   ©rF   r'   r   r   ÚmetaPkgInstalled\  s   ÿ€z2MyCache._installMetaPkgs.<locals>.metaPkgInstalledr2   r3   r]  zMarking '%s' for upgradez Can't mark '%s' for upgrade (%s)z$none of the '%s' meta-pkgs installedTrÍ   z!guessing '%s' as missing meta-pkgz$failed to mark '%s' for install (%s)zCan't install '%s'z‹It was impossible to install a required package. Please report this as a bug using 'ubuntu-bug ubuntu-release-upgrader-core' in a terminal.Fzmarked_install: '%s' -> '%s'rn   r   r4   zCan't guess meta-packagez×Your system does not contain a %s or %s package and it was not possible to detect which version of Ubuntu you are running.
 Please install one of the packages above first using synaptic or apt-get before proceeding.)rD   rE   ra   r]   Úis_upgradabler   r¶   r×   rK   r   ÚKeyErrorrÑ   r°   rÂ   rp   )r'   r@   r_  rc   rÎ   rZ   rÏ   Ú	meta_pkgsr   r^  r   r)  Z  sd   ÿþ€€ýÿÿ€÷
ñ

ùÿ	zMyCache._installMetaPkgsc                 C   s8   | j D ]}t |¡ |¡rt d||f ¡  dS qdS )Nzdenylist expr '%s' matches '%s'TF)rP   ÚreÚcompileÚmatchr   r¶   )r'   rÜ   Úexprr   r   r   rH  ¦  s   
ÿýzMyCache._inRemovalDenylistc           
   
   C   sÈ  |  | j¡rt d| ¡ dS || jkrt d| ¡ dS |  |¡r+t d| ¡ dS | j dd¡D ]}|| v rO| | jrO| | jj	|krOt d| ¡  dS q2|| vrVdS z	| j 
dd	¡}W n tjyk   d}Y nw t | j¡}| |  ¡  zF| | j|d
� | j ¡  ||v r�W dS |  ¡ D ]*}|j|vs©|j|v s©|  |j¡s©|j| jkr»t d||jf ¡ |  ¡   W dS q‘W dS  ttfyã }	 zt d|t|	ƒ|	f ¡ |  ¡  W Y d }	~	dS d }	~	ww )Nz skipping running kernel pkg '%s'Fz skipping kernel metapackage '%s'z"skipping '%s' (in removalDenylist)r2   râ   z'skipping '%s' (in KeepInstalledSection)TÚPurgeObsoletes)Úpurgez8package '%s' produces an unwanted removal '%s', skippingz3_tryMarkObsoleteForRemoval failed for '%s' (%s: %s))Úendswithr   r   r¶   rR   rH  rD   rE   r0  rã   r6  r7  r8  rH   rÉ   r›   rÆ   rÊ   r@   r  rÁ   rk   rÌ   rK   ra  r   Úrepr)
r'   rÜ   Úremove_candidatesÚforced_obsoletesÚforeign_pkgsrã   rh  rË   rc   rZ   r   r   r   ÚtryMarkObsoleteForRemoval®  s`   

€ÿ



ÿýüü€ýz!MyCache.tryMarkObsoleteForRemovalc                 C   s0   t ƒ }| D ]}|jr|  |¡s| |j¡ q|S ©z1 get all package names that are not downloadable )rX   r]   ÚanyVersionDownloadablerj   rk   )r'   Úobsolete_pkgsrc   r   r   r   Ú_getObsoletesPkgsè  s   
€zMyCache._getObsoletesPkgsc                 C   s   |j jD ]}|jr dS qdS )zA helper that checks if any of the version of pkg is downloadable TF)r^   r-  rf   )r'   rc   r½   r   r   r   rp  õ  s
   ÿzMyCache.anyVersionDownloadablec                 C   s4   t ƒ }| D ]}|jr| j |j¡r| |j¡ q|S ro  )rX   r]   r›   r¾   r^   rj   rk   )r'   Úunused_dependenciesrc   r   r   r   Ú_getUnusedDependenciesü  s   €zMyCache._getUnusedDependenciesc                 C   sº   t ƒ }| j dd¡}tj |¡r5t|ƒ�}|D ]}| d¡s%| | 	¡ ¡ qW d  ƒ n1 s0w   Y  t ƒ }|D ]}|| vrAq:| | }|j
rR| j |j¡sR|jrSq:| |¡ q:t|ƒS )zƒ return list of installed and demoted packages

            If a demoted package is a automatic install it will be skipped
        r2   Ú	Demotionsú#N)rX   rD   rV   r   r   r~   r€   rü   rj   rT   r]   r›   Úis_auto_installedr^   rÅ   rs   )r'   Ú	demotionsÚdemotions_fileÚdemotions_frX  Úinstalled_demotionsÚdemoted_pkgnamerc   r   r   r   Úget_installed_demoted_packages  s.   

€þÿÿþz&MyCache.get_installed_demoted_packagesc                 C   s~   t ƒ }| D ]7}|jr<|  |¡r<|jsqd}|jjD ]}||jv r'|j|kr'd}||jv r3|j|kr3d}q|r<| |j¡ q|S )zm get all packages that are installed from a foreign repo
            (and are actually downloadable)
        TF)	rX   r]   rf   re   r4  ÚarchiverC  rj   rk   )r'   Úallowed_originÚfromDistÚtoDistrm  rc   ÚforeignrC  r   r   r   Ú_getForeignPkgs  s$   



€€zMyCache._getForeignPkgsFc                    sh  G dd„ dt ƒ}‡ fdd„}g ‰ i }i }tdƒ�>}|D ]3}z| ¡ \}}	}
}}}W n tyD } zt d||f ¡ W Y d}~qd}~ww |	ˆ vrNˆ  |	¡ qW d  ƒ n1 sYw   Y  ˆ jtdd	� t	j
 d
¡}d}t| dƒrŒ| j
 ddd¡rŒ| j
 dd¡}tj |¡sŒt |¡ t d| ¡ dddd||ddfD ]R}tj |¡}||ƒ}tj |¡r»t |¡}|j|j }n	t d| ¡ d}||v rÜt d||| f ¡ |||  ||< q�t d||f ¡ |||< ||ƒ||< q�~t d| ¡ d}| D ] }t d|j¡�r|j�s|j�rt d|jtf ¡ |d7 }qü|t |d t  }d}t| dƒ�rX| j
 ddd¡�rXt d ¡ | j
 dd¡}| D ]}|j�sP|j�rV||jj 7 }�qFd}|�r|| D ]}|j!�rs|j�sm|j"�rs||j#j 7 }�q_t d!| ¡ || j$fd| j%fd| j&fd"d|fd#d$||fd|ff	D ]4\}}|dk �r¡�q–tj |¡}t d%|||| || j'f ¡ ||  j'|8  _'||  j(|7  _(�q–i }|D ]G}|| j'dk �rt	 )t*t+|| j'ƒd ƒ¡}t,|t-ƒ�rô| .t/ 0¡ ¡}t	 )|| j(¡}t,|t-ƒ�r	| .t/ 0¡ ¡}t1|||ƒ|ƒ|||ƒ< �qÏt|ƒdk�r2t 2d&d'd(„ |D ƒ ¡ t3t4| 5¡ ƒƒ‚dS ))zã
        this checks if we have enough free space on /var, /boot and /usr
        with the given cache

        Note: this can not be fully accurate if there are multiple
              mountpoints for /usr, /var, /boot
        c                   @   r,   )z)MyCache.checkFreeSpace.<locals>.FreeSpacez@ helper class that represents the free space on each mounted fs c                 S   s   || _ d| _d S r“   )Úfreer
  )r'   ÚinitialFreer   r   r   r(   ?  s   
z2MyCache.checkFreeSpace.<locals>.FreeSpace.__init__Nr/   r   r   r   r   Ú	FreeSpace=  s    r†  c                    s    ˆ D ]}|   |¡r|  S qdS )zŒ return 'id' of a directory so that directories on the
                same filesystem get the same id (simply the mount_point)
            ú/)rü   )ÚdÚmount_point©Úmountedr   r   Ú
make_fs_idC  s
   
ÿz*MyCache.checkFreeSpace.<locals>.make_fs_idz/proc/mountsz-line '%s' in /proc/mounts not understood (%s)NT)rÎ   ÚreversezDir::Cache::archivesz/tmp/rD   ÚAufsÚEnabledFÚRWDirzcache aufs_rw_dir: %sr‡  ú/usrz/varz/bootz/homezdirectory '%s' does not existsr   zDir %s mounted on %szFree space on %s: %szfs_free contains: '%s'z%^linux-(image|image-debug)-[0-9.]*-.*z,%s (new-install) added with %s to boot spacer   g        z*taking aufs overlay into space calculationz&additional space for the snapshots: %s)r‘  i   )z/tmpi  P )r‡  i    z dir '%s' needs '%s' of '%s' (%f)zNot enough free space: %sc                 S   s   g | ]}t |ƒ‘qS r   )rL   )r  r  r   r   r   Ú
<listcomp>Ð  s    z*MyCache.checkFreeSpace.<locals>.<listcomp>)6Úobjectr€   r   Ú
ValueErrorr   r¶   rÄ   r9  ro   rH   rD   r«   r‹   r}   rV   r   r   r~   r   ÚrealpathÚstatvfsÚf_bavailÚf_frsizer   rc  re  rk   rÂ   rÃ   ÚKERNEL_SIZEÚINITRD_SIZEre   Úinstalled_sizer]   rÅ   r0  r£   r¥   r¨   r„  r
  Úsize_to_strÚfloatÚabsÚ
isinstanceÚbytesÚdecodeÚlocaleÚgetpreferredencodingr"   rÑ   r-   rs   Úvalues)r'   Úsnapshots_in_user†  rŒ  Úmnt_mapÚfs_freeÚmountsrX  ÚwhatÚwhereÚfsÚoptionsÚarD  rZ   Ú
archivedirÚaufs_rw_dirrˆ  Úfs_idÚstr„  Úkernel_countrc   Úspace_in_bootÚrequired_for_aufsÚrequired_for_snapshotsr%   ÚsizeÚrequired_listÚfree_at_leastÚfree_neededr   rŠ  r   ÚcheckFreeSpace4  sæ   	
€þ
€ùÿ

ÿ


ÿÿ€
ÿÿ
€ÿÿ€õ
 ÿÿ€zMyCache.checkFreeSpace)NTr#   )T)rÓ   )F)<r   r   r   rh   ri   r(   rd   Úpropertyrm   rz   rU   r�   r’   r™   r£   r¥   r¨   rª   rJ   r¬   r­   r³   rr   rv   rf   r¿   rÀ   rÆ   rÈ   rÌ   rÐ   rÒ   ra   r×   rÙ   rÛ   rÝ   rä   rç   rï   rû   rý   r  r  r  rE  r*  rQ  r[  r\  r(  r)  rH  rn  rr  rp  rt  r}  rƒ  rº  r   r   r   r   r0   q   s„    
'	














	
#*
 
L
9r0   Ú__main__)ÚDistUpgradeConfig)ÚDistUpgradeViewÚfooÚ.zubuntu-desktop)/rM   rH   r   r¢  r   rc  r   r  r…   r"  r7  Ú
subprocessr   r   ÚDistUpgradeGettextr   r°   r   Úutilsr   r÷   r	   r   r   r!   r™  rš  r“  r"   r-   rN   r0   r   ÚsysÚDistUpgradeConfigParserr½  r¾  rN  Úcrº  ÚexitrÈ   rÆ   r[  r\  rÁ   rÌ   r   r   r   r   Ú<module>   s\   
!	        lð