U
    ٪zc                     @   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                   @   s  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#dadCdDZ$dbdFdGZ%dHdI Z&dJdK Z'dLdM Z(dcdOdPZ)dQdR Z*dSdT Z+dUdV Z,dWdX Z-dYdZ Z.d[d\ Z/d]d^ Z0d_d` Z1dES )d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	rtd|  |  d	||f }t| |d}|dk	rt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   _replaceDebsAndSnapsr0   r   r   r   focalPostUpgrade   s    


z"DistUpgradeQuirks.focalPostUpgradec                 C   s   t d |   dS )z run after cleanup zrunning Quirks.PostCleanupN)r   r!   _fipsLibgcryptHmacCleanupr%   r   r   r   PostCleanup   s    
zDistUpgradeQuirks.PostCleanupc                 C   sN   t d |   |   |   |   |   |   |   | 	  d S )Nzrunning Quirks.StartUpgrade)
r   r!   _applyPatches_removeOldApportCrashes_killUpdateNotifier_killKBluetooth_killScreensaver_pokeScreensaver_stopDocvertConverter_fipsLibgcryptDivertr%   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   rC   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 rN   )rO   r   toDistrQ   rR   r%   r   r   rU      s    )distro_infoZUbuntuDistroInforQ   r   rP   rI   r   rW   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environrG   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.zThe 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?)rM   rF   intersectionr   rd   re   r   rf   )r   ZI8XX_PCI_IDSrC   rg   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.zThis 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   rd   re   r   rf   r   r!   joinr   ZgetlistappendrF   )r   rg   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 systemzThere 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   errorre   r   r   rf   )r   Zupdates_endsummarymsgr   r   r   r*   3  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 CPUzYour 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   rt   re   r   rf   r%   r   r   r   _test_and_fail_on_non_arm_v6G  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]   killrH   r   warningr   rd   re   r   rf   processEventsr   rg   r   r   r   _test_and_warn_if_vserverY  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)rF   rJ   recompilematchnamer-   marked_installr   rd   re   r   rf   )r   r+   Zros_package_patternsZpackage_nameZros_is_installedpkgpatternrg   r   r   r   r,   o  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_   rt   rb   readr~   search	MULTILINE)r   fcpuinfor   r   r   rw     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!   rG   ra   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-screensaverr[   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!   rG   r   r   atexitregister_stopPokeScreensaverrH   
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waitrH   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	Exceptionrz   )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rt
d d}| jj}d|kr|d jr|d jjdst
d d}|rt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 failedzYour 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 packagezYour 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   rt   r   r+   r-   	candidaterQ   
startswithre   r   rf   rd   )r   rg   Zsnap_envZ	connectedZold_lxd_deb_installedr+   ru   rv   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   updateStatusre   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   r/   ,  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   rq   zremoving snap %szinstalling snap %sT)r
   Zcheckz%s of snap %s failedr   z%s of snap %s succeededr   debN)r   r   re   r   r   r{   rG   r$   r   ZCalledProcessErrorr   r!   
returncoder   Zforced_obsoletesrp   )r   r   r   r   Z	popenargsprocr   r   r   r2   T  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 enabledzYour 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!   rb   r   r~   r   r   rt   re   r   r   rf   )r   Zpaer   r   ru   rv   r   r   r   r   u  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_   rb   	readlinesrI   r   lowerr   endswith)r   r   ZXORGr   linesrL   sr   r   r   rm     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|std
|  q$t	 }t
|d}||  W 5 Q R X | |krt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 appliedrE   re   /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md5rb   updater   Z	hexdigestrz   ZDistUpgradePatcherr   ro   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   rE   z%found system pciid '%s' in modaliasesTz4checking for %s support in modaliases but none found)rM   r   r+   r   r   rz   !_parse_modaliases_from_pkg_headerrecordr~   r   groupr   r!   )	r   ZpkgnamerC   r   moduleZ
pciid_listZpciidmZmatchidr   r   r   rn     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)rk   (c                 S   s   g | ]}|  qS r   )r   )rS   xr   r   r   
<listcomp>  s     zGDistUpgradeQuirks._parse_modaliases_from_pkg_header.<locals>.<listcomp>rl   )rI   r   rp   )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  qqbt 
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   	installedZaptsources.sourceslistZsourceslistZSourcesListZurir   rJ   rW   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   r0   r   r   r   _gutenprint_fixup  s    
z#DistUpgradeQuirks._gutenprint_fixuprs   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_   makedirsrH   rb   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   rE   zComparing %s with %sr   )	r~   r   r   r   r   r!   aptapt_pkg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|rd||	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   rE   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metapackagerQ   r   r   sourceZcurrent_versionZmatch_sourcer   r   r   _get_linux_metapackage:  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   rA   j  s    
z,DistUpgradeQuirks._install_linux_metapackagec                    s  d}| j j dD ]}|D ]\}}td||f  | kr | jr|rtd||f  d| } j||d|dst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| |  | |  | |  | |  qd}
|
 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-   )rS   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 %srk   pythonN)r   r+   r   r   r-   r   Zmark_remover   ZProblemResolverre   ro   Z_stopAptResolverLogr   rt   rf   clearZprotectrq   r   Zresolve)r   Zreplacementsr   r   r   r   ZsolverZold_instru   rv   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   rz   rF   )r   rv   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rqj|r| jj| jdkrqj|
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||	< qW 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qnd||f||	< q| jtd | D ]$\}	\}}}i }tjdd|	gdtjd }| j  td|d tjrt d|	  td| |d tjst d|	  qd|d< n~| jj}|r||ks|| jst 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st 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.jsonrT   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   )rI   )rS   rL   r   r   r   r     s   zDDistUpgradeQuirks._prepare_snap_replacement_data.<locals>.<listcomp>rD   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   rq   Zconnections   rE   :-z9Snap %s is being used by %s. Switching it to stable trackZstable)#r   r   r   r   r]   r^   dirnameabspath__file__rb   loadcloser   r   r+   r-   r   r   rz   rG   r   r   r   rI   r   r   re   r   r{   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_datac              
   C   s   t d | jj}d}d}d|kr0t d dS |d jrHt d dS |d jrd	}|d j}|D ](}tj||j	d
krdt d  dS qdt d| d |  zt
ddd||g W n8 tk
r } zt d| d|   W 5 d}~X Y nX dS )z add a dpkg-divert to correct placement of libgcrypt.so.20.hmac on
            non-usrmerged systems when the fips libgcrypt package is installed.
            (LP: #1982534)
        r=   +/lib/x86_64-linux-gnu/.libgcrypt.so.20.hmac//usr/lib/x86_64-linux-gnu/.libgcrypt.so.20.hmaclibgcrypt20-hmacz1System is not fips enabled, no dpkg-divert neededNusrmergez0System is usrmerged, bug has been worked around.1.8.5-5ubuntu1.fips.1.5r   z\Fixed libgcrypt20-hmac will be installed as part of upgrade process, no need for dpkg-divertzBroken libgcrypt20-hmac will be installed, likely due to using certified fips pocket instead of fips-updates. Installing dpkg-divert for incorrect placement of z to the correct zdpkg-divertz--addz--divertzFailed to add dpkg-divert for z(%s))r   r!   r   r+   r-   Zversionsr   r   r   rQ   rG   Z
check_callr   r   )r   r+   r   r   fixed_versionZcandidate_versionsr   r   r   r   r   r=   d  sN    







 
z&DistUpgradeQuirks._fipsLibgcryptDivertc              
   C   sJ  t d | jj}d}d}d|kr0t d dS |d jrHt d dS |d jrFd	}|d jj}tj	||d
krt d dS t
j|st d| d  dS t
j|st d| d  dS t
j|rt
j||krt d dS t d zt
| W n: tk
rD } zt d| d|   W 5 d}~X Y nX dS )z cleanup the old libgcrypt.so.20.hmac from Bionic in /lib at the
            end of the upgrade, and take care to not remove the new file from
            Focal in /usr/lib by checking to ensure system is not usrmerged.
            (LP: #1982534)
        r4   r  r  r  z1System is not fips enabled, no need for hmac tidyNr  z\System is usrmerged, bug has been worked around. Exiting to avoid removal of Focal hmac filer	  r   z^Fixed libgcrypt20-hmac was installed as part of upgrade process, no need for hmac file cleanupzMissing hmac file: z
 aborting.zYhmac files are identical, system is possibly usrmerged or symlink present. Skip removal. z*Removing old hmac file from Bionic versionzFailed to remove z (%s))r   r!   r   r+   r-   r   rQ   r   r   r   r]   r^   r_   islinkrealpathr   r   r   )r   r+   r   r   r
  Zinstalled_versionr   r   r   r   r4     sD    






z+DistUpgradeQuirks._fipsLibgcryptHmacCleanup)r   )N)rs   )2__name__
__module____qualname____doc__r   r$   r&   r1   r3   r5   r>   r@   rB   rM   r)   rh   rj   rr   r*   rx   r}   r,   rw   r   r<   r8   r9   r:   r;   r   r7   r.   r/   r2   r   rm   r6   rn   r   r   r   r   r   r   rA   r?   r   r   r=   r4   r   r   r   r   r   &   s`   .!)/
>(!
/

0O 'r   )r   r   rX   r   r   r]   r~   r   rG   r   r   Zutilsr   ZDistUpgradeGettextr   re   rV   r   r   r   r   r   <module>   s   