U
    XXb-Ä  ã                   @   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m	Z	m
Z
 ddlmZ ddlmZ G dd„ deƒZdS )é    N)ÚPIPEÚPopené   )Úget_arch)Úgettextc                   @   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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$d%„ Zd&d'„ Zd(d)„ Zd*d+„ Zd,d-„ Zd.d/„ Zd0d1„ Zd2d3„ Zd4d5„ Zd6d7„ Zd8d9„ Zd:d;„ Z d<d=„ Z!d>d?„ Z"d@dA„ Z#d]dCdD„Z$d^dFdG„Z%dHdI„ Z&dJdK„ Z'dLdM„ Z(d_dOdP„Z)dQdR„ Z*dSdT„ Z+dUdV„ Z,dWdX„ Z-dYdZ„ Z.d[d\„ Z/dES )`ÚDistUpgradeQuirksz•
    This class collects the various quirks handlers that can
    be hooked into to fix/work around issues that the individual
    releases have
    c                 C   sd   || _ |j| _|| _tddgtdd ¡ d  ¡ | _tƒ | _	d| _
d | _d| _d | _d | _d | _d S )NÚunamez-rT©ÚstdoutÚuniversal_newlinesr   F)Ú
controllerÚ_viewÚconfigr   r   ÚcommunicateÚstripr   r   ÚarchÚextra_snap_spaceÚ_pokeÚ_snapstore_reachableÚ
_snap_listÚ_from_versionÚ_to_version)Úselfr   r   © r   ú?/usr/lib/python3/dist-packages/DistUpgrade/DistUpgradeQuirks.pyÚ__init__-   s    
ÿÿ
zDistUpgradeQuirks.__init__c                 C   sÎ   | j jrt d¡ dS | j dd¡}| j dd¡}d| }t| |dƒ}|dk	rbt d| ¡ |ƒ  d||f }t| |dƒ}|dk	r–t d| ¡ |ƒ  d	||f }t| |dƒ}|dk	rÊt d| ¡ |ƒ  dS )
a˜  
        Run the specific quirks handler, the follow handlers are supported:
        - PreCacheOpen: run *before* the apt cache is opened the first time
                        to set options that affect the cache
        - PostInitialUpdate: run *before* the sources.list is rewritten but
                             after an initial apt-get update
        - PreDistUpgradeCache: run *right before* the dist-upgrade is
                               calculated in the cache
        - PostDistUpgradeCache: run *after* the dist-upgrade was calculated
                                in the cache
        - StartUpgrade: before the first package gets installed (but the
                        download is finished)
        - PostUpgrade: run *after* the upgrade is finished successfully and
                       packages got installed
        - PostCleanup: run *after* the cleanup (orphaned etc) is finished
        z)not running quirks in partialUpgrade modeNZSourcesZToZFromú%szquirks: running %sz%s%sz	from_%s%s)r   Z_partialUpgradeÚloggingÚinfor   ÚgetÚgetattrÚdebug)r   Z
quirksNameZ
to_releaseZfrom_releaseÚfuncnameÚfuncr   r   r   Úrun?   s(    
zDistUpgradeQuirks.runc                 C   s   t  d¡ dS )z3 run before the apt cache is opened the first time zrunning Quirks.PreCacheOpenN©r   r!   ©r   r   r   r   ÚPreCacheOpenm   s    zDistUpgradeQuirks.PreCacheOpenc                 C   sf   t  d¡ |  ¡  |  ¡  | jj}|  |¡ d|krBt  d¡ dS |d jrT|  ¡  | j	rb|  
¡  dS )z2 run after the apt cache is opened the first time z%running Quirks.focalPostInitialUpdateÚsnapdú'package required for Quirk not in cacheN)r   r!   Ú_get_from_and_to_versionÚ_test_and_fail_on_i386r   ÚcacheÚ_test_and_warn_if_ros_installedÚis_installedÚ_checkStoreConnectivityr   Ú_calculateSnapSizeRequirements©r   r,   r   r   r   ÚfocalPostInitialUpdater   s    



z(DistUpgradeQuirks.focalPostInitialUpdatec                 C   sD   t  d¡ | jj}d|kr(t  d¡ d S |d jr@| jr@|  ¡  d S )Nzrunning Quirks.focalPostUpgrader(   r)   )r   r!   r   r,   r.   r   Ú_replaceDebsAndSnapsr1   r   r   r   ÚfocalPostUpgrade‡   s    


ÿz"DistUpgradeQuirks.focalPostUpgradec                 C   s   t  d¡ dS )z run after cleanup zrunning Quirks.PostCleanupNr%   r&   r   r   r   ÚPostCleanup’   s    zDistUpgradeQuirks.PostCleanupc                 C   sF   t  d¡ |  ¡  |  ¡  |  ¡  |  ¡  |  ¡  |  ¡  |  ¡  d S )Nzrunning Quirks.StartUpgrade)	r   r!   Ú_applyPatchesÚ_removeOldApportCrashesÚ_killUpdateNotifierÚ_killKBluetoothÚ_killScreensaverÚ_pokeScreensaverÚ_stopDocvertConverterr&   r   r   r   ÚStartUpgrade—   s    
zDistUpgradeQuirks.StartUpgradec                 C   s   t  d¡ |  ¡  dS )z/ run right before calculating the dist-upgrade z"running Quirks.PreDistUpgradeCacheN)r   r!   Ú_install_python_is_python2r&   r   r   r   ÚPreDistUpgradeCache£   s    
z%DistUpgradeQuirks.PreDistUpgradeCachec                 C   s   t  d¡ |  ¡  dS )z( run after calculating the dist-upgrade z#running Quirks.PostDistUpgradeCacheN)r   r!   Ú_install_linux_metapackager&   r   r   r   ÚPostDistUpgradeCacheª   s    
z&DistUpgradeQuirks.PostDistUpgradeCachec                 C   sl   t ƒ }ztjddgtjdd}W n tk
r8   | Y S X | ¡ d  d¡D ]}|rL| | ¡ d ¡ qL|S )z8 return a set of pci ids of the system (using lspci -n) Úlspciz-nTr	   r   Ú
é   )ÚsetÚ
subprocessr   r   ÚOSErrorr   ÚsplitÚadd)r   rB   ÚpÚliner   r   r   Ú_get_pci_ids°   s    ÿ

zDistUpgradeQuirks._get_pci_idsc                    s¶   t  ¡ }z<| dˆ jj ¡ ¡ d ˆ _| dˆ jj ¡ ¡ d ˆ _W nl t	k
r°   t
‡ fdd„| d¡D ƒˆ jjƒ ¡ d ˆ _t
‡ fdd„| d¡D ƒˆ jjƒ ¡ d ˆ _Y nX d S )Nr   r   c                 3   s"   | ]}|j ˆ jjkr|jV  qd S ©N)Úseriesr   ÚfromDistÚversion©Ú.0Úrr&   r   r   Ú	<genexpr>Ç   s    ÿz=DistUpgradeQuirks._get_from_and_to_version.<locals>.<genexpr>Úobjectc                 3   s"   | ]}|j ˆ jjkr|jV  qd S rM   )rN   r   ÚtoDistrP   rQ   r&   r   r   rT   Ë   s    ÿ)Údistro_infoZUbuntuDistroInforP   r   rO   rH   r   rV   r   ÚAttributeErrorÚnextZget_all)r   Zdir   r&   r   r*   ½   s&    ÿÿýýýýz*DistUpgradeQuirks._get_from_and_to_versionc                 C   sž   d}t j |¡rdt jkrd S tjdddgtt jdƒd}|dkrPt 	d	¡ d S t |g¡}t 	d
||f ¡ |dkrš| j
 tdƒtdƒ¡}|sš| j ¡  d S )Nz/usr/lib/nux/unity_support_testÚDISPLAYZpsz-Czunity-panel-serviceÚw)r
   r   z5_test_and_warn_for_unity_3d_support: no unity runningz6_test_and_warn_for_unity_3d_support '%s' returned '%s'zBYour graphics hardware may not be fully supported in Ubuntu 14.04.aS  Running the 'unity' desktop environment is not fully supported by your graphics hardware. You will maybe end up in a very slow environment after the upgrade. Our advice is to keep the LTS version for now. For more information see https://wiki.ubuntu.com/X/Bugs/UpdateManagerWarningForUnity3D Do you still want to continue with the upgrade?)ÚosÚpathÚexistsÚenvironrF   ÚcallÚopenÚdevnullr   r!   r   ÚaskYesNoQuestionÚ_r   Úabort)r   ZUNITY_SUPPORT_TESTZreturn_codeÚresr   r   r   Ú#_test_and_warn_for_unity_3d_supportÏ   s8    ÿ 
ÿÿ ÿÿýz5DistUpgradeQuirks._test_and_warn_for_unity_3d_supportc                 C   sP   dddddddg}|   ¡ }t|ƒ |¡rL| j tdƒtd	ƒ¡}|sL| j ¡  d S )
Nz	8086:7121z	8086:7125z	8086:1132z	8086:3577z	8086:2562z	8086:3582z	8086:2572zFYour graphics hardware may not be fully supported in Ubuntu 12.04 LTS.zûThe support in Ubuntu 12.04 LTS for your Intel graphics hardware is limited and you may encounter problems after the upgrade. For more information see https://wiki.ubuntu.com/X/Bugs/UpdateManagerWarningForI8xx Do you want to continue with the upgrade?)rL   rE   Úintersectionr   rc   rd   r   re   )r   ZI8XX_PCI_IDSrB   rf   r   r   r   Ú_test_and_warn_on_i8xxð   s     úý
z(DistUpgradeQuirks._test_and_warn_on_i8xxc                 C   sœ   |   d¡r˜|  d¡s˜| j tdƒtdƒ¡}|s8| j ¡  dddddd	g}t d
d 	|¡ ¡ | jj
 dd¡}|D ]}| |¡ qp| jj
 ddd 	|¡¡ dS )zi
        Some cards are no longer supported by fglrx. Check if that
        is the case and warn
        ZfglrxzhUpgrading may reduce desktop effects, and performance in games and other graphically intensive programs.zµThis computer is currently using the AMD 'fglrx' graphics driver. No version of this driver is available that works with your hardware in Ubuntu 10.04 LTS.

Do you want to continue?zxorg-driver-fglrxzxorg-driver-fglrx-envyzfglrx-kernel-sourcezfglrx-amdccclezxorg-driver-fglrx-devZlibamdxvba1z	remove %sú, ZDistroZPostUpgradePurgeú,N)Ú_checkVideoDriverÚ_supportInModaliasesr   rc   rd   r   re   r   r!   Újoinr   ZgetlistÚappendrE   )r   rf   ZremovalsZpostupgradepurgeÚremover   r   r   Ú'_test_and_warn_on_dropped_fglrx_support  s4    
ÿû
úþÿz9DistUpgradeQuirks._test_and_warn_on_dropped_fglrx_supportc                 C   sh   | j dkrd}n| j dkrd}| jdkrdt d¡ tdƒ}td| j |f ƒ}| j ||¡ | j ¡  d	S )
z}
        Test and fail if the package architecture is i386 as we
        have dropped support for this architecture.
        z18.04z
April 2023z19.04zJanuary 2020Úi386zapt architecture is i386z'Sorry, no more upgrades for this systemzƒThere will not be any further Ubuntu releases for this system's 'i386' architecture.

Updates for Ubuntu %s will continue until %s.N)r   r   r   Úerrorrd   r   r   re   )r   Zupdates_endÚsummaryÚmsgr   r   r   r+   1  s    



ýz(DistUpgradeQuirks._test_and_fail_on_i386c                 C   s6   | j dkr2|  ¡ s2| j tdƒtdƒ¡ | j ¡  dS )z~
        Test and fail if the cpu is not a arm v6 or greater,
        from 9.10 on we do no longer support those CPUs
        ZarmelzNo ARMv6 CPUzþYour system uses an ARM CPU that is older than the ARMv6 architecture. All packages in karmic were built with optimizations requiring ARMv6 as the minimal architecture. It is not possible to upgrade your system to a new Ubuntu release with this hardware.N)r   Ú_checkArmCPUr   rs   rd   r   re   r&   r   r   r   Ú_test_and_fail_on_non_arm_v6E  s    
þ	z.DistUpgradeQuirks._test_and_fail_on_non_arm_v6c                 C   sb   zt  dd¡ W nL tk
r\   t d¡ | j tdƒtdƒ¡}|sN| j 	¡  | j 
¡  Y nX dS )zd
        upstart and vserver environments are not a good match, warn
        if we find one
        r   r   zno init foundzNo init availablea  Your system appears to be a virtualised environment without an init daemon, e.g. Linux-VServer. Ubuntu 10.04 LTS cannot function within this type of environment, requiring an update to your virtual machine configuration first.

Are you sure you want to continue?N)r\   ÚkillrG   r   Úwarningr   rc   rd   r   re   ÚprocessEvents©r   rf   r   r   r   Ú_test_and_warn_if_vserverW  s    
þ
z+DistUpgradeQuirks._test_and_warn_if_vserverc                 C   s   t ƒ }dD ]}| t d| ¡¡ q
d}|D ]6}|r8 qd|D ]$}| |j¡r<|jsX|jr\d} q,q<q,|rŒ| j 	t
dƒt
dƒ¡}|sŒ| j ¡  dS )z¿
        Test and warn if ROS is installed. A given ROS release only
        supports specific Ubuntu releases, and can cause the upgrade
        to fail in an overly-cryptic manner.
        )Zcatkinzrosboost-cfgZroscleanzros-environmentzros-workspacezros-[^\-]+-%sFTz-The Robot Operating System (ROS) is installeda	  It appears that ROS is currently installed. Each ROS release is very strict about the versions of Ubuntu it supports, and Ubuntu upgrades can fail if that guidance isn't followed. Before continuing, please either uninstall ROS, or ensure the ROS release you have installed supports the version of Ubuntu to which you're upgrading.

For ROS 1 releases, refer to REP 3:
https://www.ros.org/reps/rep-0003.html

For ROS 2 releases, refer to REP 2000:
https://www.ros.org/reps/rep-2000.html

Are you sure you want to continue?N)rE   rI   ÚreÚcompileÚmatchÚnamer.   Úmarked_installr   rc   rd   r   re   )r   r,   Zros_package_patternsZpackage_nameZros_is_installedÚpkgÚpatternrf   r   r   r   r-   m  s*    ÿþz1DistUpgradeQuirks._test_and_warn_if_ros_installedc              	   C   sX   t  d¡ tj d¡s$t  d¡ dS tdƒ}| ¡ }W 5 Q R X t 	d|tj
¡rTdS dS )zE
        parse /proc/cpuinfo and search for ARMv6 or greater
        zchecking for ARM CPU versionú/proc/cpuinfozcannot open /proc/cpuinfo ?!?Fz^Processor\s*:\s*ARMv[45]T)r   r!   r\   r]   r^   rs   ra   Úreadr}   ÚsearchÚ	MULTILINE)r   ÚfÚcpuinfor   r   r   rv   œ  s    


ÿzDistUpgradeQuirks._checkArmCPUc                 C   s(   t j d¡r$t d¡ t ddg¡ dS )z, /etc/init.d/apparmor stop (see bug #559433)z/etc/init.d/apparmorz/etc/init.d/apparmor stopÚstopN©r\   r]   r^   r   r!   rF   r`   r&   r   r   r   Ú_stopApparmor«  s    
zDistUpgradeQuirks._stopApparmorc                 C   s(   t j d¡r$t d¡ t ddg¡ dS )z5 /etc/init.d/docvert-converter stop (see bug #450569)z/etc/init.d/docvert-converterz"/etc/init.d/docvert-converter stoprŠ   Nr‹   r&   r   r   r   r<   ±  s    
z'DistUpgradeQuirks._stopDocvertConverterc                 C   s*   t j d¡r&t d¡ t dddg¡ dS )zkill update-notifierú/usr/bin/killallzkilling update-notifierÚkillallú-qzupdate-notifierNr‹   r&   r   r   r   r8   ·  s    
z%DistUpgradeQuirks._killUpdateNotifierc                 C   s,   t j d¡r(t d¡ t ddddg¡ dS )z3killall kblueplugd kbluetooth (riddel requested it)r   zkilling kblueplugd kbluetooth4rŽ   r   Z
kblueplugdZkbluetooth4Nr‹   r&   r   r   r   r9   ¾  s    
z!DistUpgradeQuirks._killKBluetoothc                 C   s*   t j d¡r&t d¡ t dddg¡ dS )zkillall gnome-screensaver r   zkilling gnome-screensaverrŽ   r   zgnome-screensaverNr‹   r&   r   r   r   r:   Ä  s    
z"DistUpgradeQuirks._killScreensaverc              	   C   s~   t j d¡rzt j d¡rzt d¡ d}|d7 }|d7 }z tj|dd| _	t
 | j¡ W n" ttfk
rx   t d	¡ Y nX d S )
Nz/usr/bin/xdg-screensaverrZ   z$setup poke timer for the screensaverzwhile true;z3 do /usr/bin/xdg-screensaver reset >/dev/null 2>&1;z sleep 30; doneT)Úshellz failed to setup screensaver poke)r\   r]   r^   r_   r   r   r!   rF   r   r   ÚatexitÚregisterÚ_stopPokeScreensaverrG   Ú
ValueErrorÚ	exception)r   Úcmdr   r   r   r;   Ê  s    
ÿ
z"DistUpgradeQuirks._pokeScreensaverc                 C   sP   d}| j d k	rLz| j  ¡  | j  ¡ }W n tk
rD   t d¡ Y nX d | _ |S )NFzfailed to stop screensaver poke)r   Z	terminateÚwaitrG   r   r•   r{   r   r   r   r“   ×  s    

z&DistUpgradeQuirks._stopPokeScreensaverc              
   C   st   z>dD ]4}t   d| ¡D ] }t d||f ¡ t |¡ qqW n0 tk
rn } zt d| ¡ W 5 d}~X Y nX dS )z: remove old apport crash files and whoopsie control files )z.crashz.uploadz	.uploadedz/var/crash/*%szremoving old %s file '%s'z+error during unlink of old crash files (%s)N)Úglobr   r!   r\   ÚunlinkÚ	Exceptionry   )r   Zextrˆ   Úer   r   r   r7   â  s    z)DistUpgradeQuirks._removeOldApportCrashesc                 C   sR  d}t j ¡ }d|d< tdddgtt|dd ¡ }t d	|d
 tj¡rPd| _	dS t d|d
 tj¡ròt
 d¡ d}| jj}d|kr¬|d jr¬|d jj d¡s¬t
 d¡ d}|rÚtdƒ}tdƒ}| j ||¡ | j ¡  n| j tdƒtdƒ¡}nLd|d kr"t
 d¡ | j tdƒtdƒ¡}nd|d kr>d}t
 d¡ |sN| j ¡  dS )z: check for connectivity to the snap store to install snapsFzC.UTF-8ZLANGÚsnapr!   ZconnectivityT)r
   ÚstderrÚenvr   z	^ \* PASSr   Nz^ \*.*unreachablezNo snap store connectivityZlxdz1:zlxd is installedz#Connection to the Snap Store faileda  You have the package lxd installed but your system is unable to reach the Snap Store. lxd is now provided via a snap and the release upgrade will fail if snapd is not functional. Please make sure you're connected to the Internet and update any firewall or proxy settings as needed so that you can reach api.snapcraft.io. If you are an enterprise with a firewall setup you may want to configure a Snap Store proxy.zConnection to Snap Store failedzÅYour system does not have a connection to the Snap Store. For the best upgrade experience make sure that your system can connect to api.snapcraft.io.
Do you still want to continue with the upgrade?zerror: unknown commandr   z snap debug command not availablezOutdated snapd packagez»Your system does not have the latest version of snapd. Please update the version of snapd on your system to improve the upgrade experience.
Do you still want to continue with the upgrade?zerror: access deniedzNot running as root!)r\   r_   Úcopyr   r   r   r}   r†   r‡   r   r   rs   r   r,   r.   Ú	candidaterP   Ú
startswithrd   r   re   rc   )r   rf   Zsnap_envZ	connectedZold_lxd_deb_installedr,   rt   ru   r   r   r   r/   ì  sR    
 þ


ÿ
þ
þ
z)DistUpgradeQuirks._checkStoreConnectivityc                 C   s8  dd l }dd l}ddlm} |  ¡  | j tdƒ¡ | j 	¡ D ]ô\}}|d dkrTq>dd|d |d	 d
œ}g |gdœ}|j
jdt| |¡ddd}| dd¡ | dd¡ | d| j¡ z:|j
 |¡ ¡ }	| |	¡}
t|
d d d d d ƒ}W n. t|tfk
r"   t d| ¡ Y q>Y nX |  j|7  _q>d S )Nr   )ÚURLErrorz"Calculating snap size requirementsÚcommandÚinstallzupgrade-size-checkZdownloadúsnap-idÚchannel)zinstance-keyÚactionr¥   r¦   )ÚcontextZactionsz)https://api.snapcraft.io/v2/snaps/refreshzutf-8)Úencoding)ZurlÚdatazSnap-Device-SeriesZ16zContent-typezapplication/jsonzSnap-Device-ArchitectureZresultsrœ   ÚsizezFailed fetching size of snap %s)ÚjsonZurllib.requestZurllib.errorr¢   Ú_prepare_snap_replacement_datar   ÚupdateStatusrd   r   ÚitemsZrequestZRequestÚbytesÚdumpsZ
add_headerr   Zurlopenr…   ÚloadsÚintÚKeyErrorr”   r   r!   r   )r   r¬   Zurllibr¢   rœ   Úsnap_objectr§   rª   ZreqZresponser   r«   r   r   r   r0   *  s>    üþþ
 
z0DistUpgradeQuirks._calculateSnapSizeRequirementsc              	   C   sJ  | j  tdƒ¡ | j ¡ D ](\}}|d }|dkr\| j  td| ƒ¡ d|d|d |g}nN|dkr„| j  td	| ƒ¡ d||g}n&| j  td
| ƒ¡ d|d|d |g}z*| j  ¡  tj|tjdd}| j  ¡  W n. tj	k
r   t
 d||f ¡ Y qY nX |jdkr"t
 d||f ¡ |dkr|d r| jj |d ¡ qdS )z? install a snap and mark its corresponding package for removal zProcessing snap replacementsr£   Úrefreshzrefreshing snap %srœ   z	--channelr¦   rp   zremoving snap %szinstalling snap %sT)r
   Zcheckz%s of snap %s failedr   z%s of snap %s succeededr¤   ÚdebN)r   r®   rd   r   r¯   rz   rF   r$   r   ZCalledProcessErrorr   r!   Ú
returncoder   Zforced_obsoletesro   )r   rœ   rµ   r£   Z	popenargsÚprocr   r   r   r3   R  sD      ÿ  ÿ
ý
z&DistUpgradeQuirks._replaceDebsAndSnapsc              	   C   sx   t  d¡ d}tdƒ}| ¡ }W 5 Q R X t d|tj¡r>d}|stt  d¡ tdƒ}tdƒ}| j	 ||¡ | j
 ¡  d	S )
z check PAE in /proc/cpuinfo Ú	_checkPaer   r„   z^flags\s+:.* pae r   zno pae in /proc/cpuinfozPAE not enabledzôYour system uses a CPU that does not have PAE enabled. Ubuntu only supports non-PAE systems up to Ubuntu 12.04. To upgrade to a later version of Ubuntu, you must enable PAE (if this is possible) see:
http://help.ubuntu.com/community/EnablingPAEN)r   r!   ra   r…   r}   r†   r‡   rs   rd   r   r   re   )r   Zpaerˆ   r‰   rt   ru   r   r   r   rº   s  s    


zDistUpgradeQuirks._checkPaec              	   C   sr   d}t j |¡sdS t|ƒ}| ¡ }W 5 Q R X |D ]8}| d¡d  ¡ }| ¡  d¡r4| 	d| ¡r4 dS q4dS )z2 check if the given driver is in use in xorg.conf z/etc/X11/xorg.confFú#r   Zdriverz"%s"T)
r\   r]   r^   ra   Ú	readlinesrH   r   Úlowerr¡   Úendswith)r   r€   ZXORGrˆ   ÚlinesrK   Úsr   r   r   rl   ‡  s    
ÿz#DistUpgradeQuirks._checkVideoDriverú	./patchesc           
   
   C   sf  t j |¡st d¡ dS t  |¡D ]:}d|krBt d| ¡ q$t d| ¡ | dd¡\}}}| dd¡}t d	|||f ¡ t j |¡sžt d
| ¡ q$t 	¡ }t
|dƒ}| | ¡ ¡ W 5 Q R X | ¡ |krèt d| ¡ q$n| ¡ |krt d| ¡ q$ddlm}	 z(|	|t j ||¡|ƒ t d| ¡ W q$ tk
r^   t d| ¡ Y q$X q$dS )z¯
        helper that applies the patches in patchdir. the format is
        _path_to_file.md5sum and it will apply the diff to that file if the
        md5sum matches
        zno patchdirNÚ.zskipping '%s' (no '.')z'check if patch '%s' needs to be appliedrD   rd   ú/ztarget for '%s' is '%s' -> '%s'ztarget '%s' does not existÚrbz%already at target hash, skipping '%s'z(unexpected target md5sum, skipping: '%s'r   )Úpatchzapplied '%s' successfullyzed failed for '%s')r\   r]   r^   r   r!   ÚlistdirÚrsplitÚreplaceÚhashlibÚmd5ra   Úupdater…   Z	hexdigestry   ZDistUpgradePatcherrÅ   rn   r   rš   r•   )
r   Zpatchdirrˆ   Zencoded_pathZmd5sumZresult_md5sumr]   rÊ   ÚfdrÅ   r   r   r   r6   –  sF    
  ÿÿzDistUpgradeQuirks._applyPatchesNc           	      C   s¾   |s|   ¡ }|| jjks&| jj| js4t d¡ dS | jj| }|  |jj¡D ]\\}}|D ]N}t 	d|¡}|rZd| 
d¡| 
d¡f }| ¡ |krZt d| ¡   dS qZqNt d	| ¡ dS )
zß
        Check if pkgname will work on this hardware

        This helper will check with the modaliasesdir if the given
        pkg will work on this hardware (or the hardware given
        via the lspci argument)
        zcan not find '%s' in cacheFzpci:v0000(.+)d0000(.+)sv.*z%s:%sr   rD   z%found system pciid '%s' in modaliasesTz4checking for %s support in modaliases but none found)rL   r   r,   r    r   ry   Ú!_parse_modaliases_from_pkg_headerÚrecordr}   r   Úgroupr½   r!   )	r   ZpkgnamerB   r‚   ÚmoduleZ
pciid_listZpciidÚmZmatchidr   r   r   rm   Å  s.    	ÿ
ÿ
ÿÿz&DistUpgradeQuirks._supportInModaliasesc                 C   sd   d|krg S g }|d   d¡D ]@}| d¡}|s2q|  d¡\}}| |dd„ |  d¡D ƒf¡ q|S )z. return a list of (module1, (pciid, ...), ...)Z
Modaliasesú)rj   ú(c                 S   s   g | ]}|  ¡ ‘qS r   )r   )rR   Úxr   r   r   Ú
<listcomp>ï  s     zGDistUpgradeQuirks._parse_modaliases_from_pkg_header.<locals>.<listcomp>rk   )rH   r   ro   )r   Z	pkgrecordÚmodulesrÑ   rÐ   Zpciidsr   r   r   rÍ   ä  s    
 z3DistUpgradeQuirks._parse_modaliases_from_pkg_headerc                 C   sÐ   t  d¡ | jj}d|kr(t  d¡ d S |d jsJ|d jsJt  d¡ d S zbdd l}|j ¡ }|D ]}d|j	krbt  d¡  qªqbt  
d¡ | d	d
| jjdgd¡ | ¡  W n tk
rÊ   t  d¡ Y nX d S )NÚ_add_extras_repositoryzubuntu-extras-keyringz-no ubuntu-extras-keyring, no need to add repoz2ubuntu-extras-keyring not installed/marked_installr   zextras.ubuntu.comz*found extras.ubuntu.com, no need to add itz/no extras.ubuntu.com, adding it to sources.listr·   zhttp://extras.ubuntu.com/ubuntuÚmainz!Third party developers repositoryzerror adding extras.ubuntu.com)r   r!   r   r,   r   Z	installedZaptsources.sourceslistZsourceslistZSourcesListZurir   rI   rV   Zsaverš   r•   )r   r,   Z
aptsourcesZsourcesÚentryr   r   r   r×   ò  s4    


ÿ




 þz(DistUpgradeQuirks._add_extras_repositoryc                 C   s`   z<| j j}d|kr:|d jr:d|kr:t d¡ | dd¡ W n tk
rZ   t d¡ Y nX dS )zÂ foomatic-db-gutenprint get removed during the upgrade,
            replace it with the compressed ijsgutenprint-ppds
            (context is foomatic-db vs foomatic-db-compressed-ppds)
        zfoomatic-db-gutenprintzijsgutenprint-ppdszinstalling ijsgutenprint-ppdsz1foomatic-db-gutenprint -> ijsgutenprint-ppds rulez_gutenprint_fixup failedN)r   r,   Úmarked_deleter   r   Úmark_installrš   r•   r1   r   r   r   Ú_gutenprint_fixup  s    ÿþ
þz#DistUpgradeQuirks._gutenprint_fixuprr   c              	   C   s\   d}t j |¡sXzt  d¡ W n tk
r2   Y nX t|dƒ}| d| ¡ W 5 Q R X dS )z5 enable multiarch via /etc/dpkg/dpkg.cfg.d/multiarch z/etc/dpkg/dpkg.cfg.d/multiarchz/etc/dpkg/dpkg.cfg.d/r[   zforeign-architecture %s
N)r\   r]   r^   ÚmakedirsrG   ra   Úwrite)r   Zforeign_archZcfgrˆ   r   r   r   Ú_enable_multiarch  s    z#DistUpgradeQuirks._enable_multiarchc                 C   sv   t  d¡}| |¡}| |¡}|rRd| d¡| d¡f }d| d¡| d¡f }t d||f ¡ tj ||¡dkS )z# copied from ubuntu-drivers common z(.+)-([0-9]+)-(.+)ú%s-%sr   rD   zComparing %s with %sr   )	r}   r~   r   rÏ   r   r!   ÚaptÚapt_pkgZversion_compare)r   Zterm1Zterm2rƒ   Zmatch1Zmatch2r   r   r   Ú_is_greater_than(  s    



ÿ
ÿz"DistUpgradeQuirks._is_greater_thanc                 C   sÞ   |rdp
d}t  d¡}t  d¡}d}d}|D ]¬}d|jkr,d|jkr,|jsP|jr,| |j¡}	|	r,|jjd }
d|	 d	¡|	 d
¡f }|  	||¡r,|}| |
¡}d|
krÆ|rÆd||	 d¡| d	¡f }q,d||	 d¡f }q,|S )zb Get the linux headers or linux metapackage
            copied from ubuntu-drivers-common
        z-headersÚ zlinux-image-(.+)-([0-9]+)-(.+)z
linux-(.+)zlinux-imageZextraZSourcerà   r   rD   z-lts-zlinux%s-%s-%sé   z
linux%s-%s)
r}   r~   r€   r.   r   r   r    rÎ   rÏ   rã   )r   r,   ZheadersÚsuffixrƒ   Zsource_patternZmetapackagerP   r‚   r   ÚsourceZcurrent_versionZmatch_sourcer   r   r   Ú_get_linux_metapackage8  sB    

ÿÿ
ÿÿ
	ýþz(DistUpgradeQuirks._get_linux_metapackagec                 C   sZ   | j j}|  |d¡}||kr.t d| ¡ dS || jsVt d| ¡ d}| ||¡ dS )zo Ensure the linux metapackage is installed for the newest_kernel
            installed. (LP: #1509305)
        Fz$linux metapackage (%s) not availableNz installing linux metapackage: %sz8linux metapackage may have been accidentally uninstalled)r   r,   rè   r   r   r.   rÛ   )r   r,   Zlinux_metapackageÚreasonr   r   r   r@   h  s    ÿ
ÿz,DistUpgradeQuirks._install_linux_metapackagec                    sÂ  d}| j j‰ dD ]ª}|D ] \}}t d||f ¡ |ˆ krˆ | jr|r‚t d||f ¡ d| }ˆ j||d|ds‚t d| ¡ t d	||f ¡ d
| }ˆ j||ddst d|¡ qqtj ˆ ¡}|D ]À\}}|ˆ krÌˆ | jrÌ|rp|ˆ krT‡ fdd„|D ƒ}t	dƒ}t	d||d 
|¡f ƒ}	t |¡ ˆ  ¡  | j ||	¡ | j  ¡  n| ˆ | ¡ | ˆ | ¡ | ˆ | ¡ | ˆ | ¡ qÌd}
|
ˆ kr¶ˆ |
 jr¶| ˆ |
 ¡ | ¡  dS )zÌ
        Ensure python is removed, before it can produce a conflict with any
        other package and the python-is-python2 package is installed instead,
        if python-minimal was installed.
        ))z
python-dbgzpython2-dbg)z
python-doczpython2-doc)zpython-minimalzpython-is-python2)z
python-devzpython-dev-is-python2)zlibpython-devN)zlibpython-stdlibN)zlibpython-dbgN)FTzchecking for %s (auto_inst=%s)z&installing %s because %s was installedz%s was installed on the systemF)Úauto_fixÚ	auto_instzfailed to install %sz)removing %s because %s is being installedz#%s is being installed on the system)rê   zfailed to remove %sc                    s&   g | ]\}}|ˆ krˆ | j r|‘qS r   )r.   )rR   ÚoldÚnew©r,   r   r   rÕ   ¥  s     ÿ
ÿz@DistUpgradeQuirks._install_python_is_python2.<locals>.<listcomp>zuniverse component not enabledaÿ  You have the package %s installed which is a python2 package. python2 has been deprecated in Ubuntu 20.04 LTS and is now available from the universe component of the Ubuntu archive. To ensure any existing third party code you have installed continues to work the release upgrade process would install %s for you. However, the universe component of the archive is not enabled on this system. For the upgrade process to proceed you should either enable the universe component in /etc/apt/sources.list or remove %srj   ÚpythonN)r   r,   r   r   r.   rÛ   Zmark_removerá   ZProblemResolverrd   rn   Z_stopAptResolverLogr   rs   re   ÚclearZprotectrp   rÚ   Zresolve)r   Zreplacementsrë   rì   rí   ré   ZsolverZold_instrt   ru   Úpyr   rî   r   r>   {  sZ    ÿÿ
ÿÿ
ó
z,DistUpgradeQuirks._install_python_is_python2c                 C   s@   | j js<tjj d¡s<d}|d7 }t |¡ tjj dd¡ dS )z] ensure that on a desktop install recommends are installed
            (LP: #759262)
        zApt::Install-Recommendsz%Apt::Install-Recommends was disabled,z! enabling it just for the upgradeÚ1N)	r   Z
serverModerá   râ   r   Zfind_br   ry   rE   )r   ru   r   r   r   Ú+ensure_recommends_are_installed_on_desktopsÊ  s    
z=DistUpgradeQuirks.ensure_recommends_are_installed_on_desktopsc              
   C   s°  ddl }i | _d| j }d| j }i }i }z,tj tj t¡¡}t	|d dƒ}| 
|¡}| ¡  |d D ]t}	|d |	 }
|
 dd¡}|| jjkr”qj|r¬| jj| jdkr¬qj|
 d	d¡}|
 d
|¡}|
 d|¡}|||f||	< qj|d D ]l}	|d |	 }| d	d¡}| dd¡}|| jjkr qè|r<| jj| jdkr<qè| d
|¡}||f||	< qèW n2 tk
rŠ } zt d| ¡ W 5 d}~X Y nX d}tjddgdtjd ¡ }|rdd„ |d  d¡dd… D ƒ}|D ].}	|	|ksÔ|	|kròqÔnd||f||	< qÔ| j tdƒ¡ | ¡ D ]$\}	\}}}i }tjdd|	gdtjd ¡ }| j ¡  t d|d tj¡r°t  d|	 ¡ t d| |d tj¡s¦t  d|	 ¡ qd|d< n~| jj}|rê||ksÔ|| jsêt  d ||	f ¡ qt d!|d ¡}|st  d"|	 ¡ qd#|d< ||d	< |d |d$< ||d%< || j|	< q| ¡ D ]Z\}	\}}i }tjdd|	gdtjd ¡ }| j ¡  t d|d tj¡rLt  d|	 ¡ t d| |d tj¡sÔt  d|	 ¡ qLd&|d< tjdd'|	gdtjd ¡ }| j ¡  |d  d¡D ]Š}| ¡ }t!|ƒd(kr.q|d }|d) }| "|	d* ¡r| d*¡d }|d+kr||krt  d,|	|f ¡ d|d< d-|d%<  qœq|| j|	< qL| jS ).z» Helper function fetching all required info for the deb-to-snap
            migration: version strings for upgrade (from and to) and the list
            of snaps (with actions).
        r   Nzstable/ubuntu-%sz/deb2snap.jsonrS   ZseededÚmetapkgFr·   Úfrom_channelÚ
to_channelZunseededz%error reading deb2snap.json file (%s)rä   rœ   ÚlistT)r   r
   c                 S   s   g | ]}|  ¡ d  ‘qS )r   )rH   )rR   rK   r   r   r   rÕ   	  s   ÿzDDistUpgradeQuirks._prepare_snap_replacement_data.<locals>.<listcomp>rC   r   éÿÿÿÿzChecking for installed snapsr   z^installed: zSnap %s is installedz^tracking:.*%sz+Snap %s is not tracking the release channelr¶   r£   zFDeb package %s is not installed. Skipping snap package %s installationzsnap-id:\s*(\w*)z'Could not parse snap-id for the %s snapr¤   r¥   r¦   rp   Zconnectionsé   rD   ú:ú-z9Snap %s is being used by %s. Switching it to stable trackZstable)#r¬   r   r   r   r\   r]   ÚdirnameÚabspathÚ__file__ra   ÚloadÚcloser   r   r,   r.   rš   r   ry   rF   r   r   r   rH   r   r®   rd   r¯   rz   r}   r†   r‡   r!   Úlenr¡   )r   r¬   rõ   rö   Zseeded_snapsZunseeded_snapsZcurrent_pathZd2s_fileZd2srœ   Zseedrô   r·   Z	from_chanZto_chanZunseedr›   Z	snap_listZsnaps_installedrµ   Z	snap_infor,   r   ZconnsZconnZ	conn_colsZplugZslotZ	plug_snapr   r   r   r­   Õ  s   


ÿÿ 
þ
ÿþ


 ÿÿ
ÿÿþ


 ÿÿþ


ÿþ
z0DistUpgradeQuirks._prepare_snap_replacement_data)rÁ   )N)rr   )0Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r$   r'   r2   r4   r5   r=   r?   rA   rL   r*   rg   ri   rq   r+   rw   r|   r-   rv   rŒ   r<   r8   r9   r:   r;   r“   r7   r/   r0   r3   rº   rl   r6   rm   rÍ   r×   rÜ   rß   rã   rè   r@   r>   ró   r­   r   r   r   r   r   &   sZ   .!)/
>(!
/

0Or   )rá   r‘   rW   r˜   r   r\   r}   rÉ   rF   r   r   Zutilsr   ZDistUpgradeGettextr   rd   rU   r   r   r   r   r   Ú<module>   s   