
    f/                        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  G d de      Zy)    N)PIPEPopen   )get_arch)gettextc                   p   e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Z d Z!d  Z"d! Z#d" Z$d# Z%d$ Z&d% Z'd& Z(d<d'Z)d=d)Z*d* Z+d+ Z,d, Z-d>d-Z.d. Z/d/ Z0d0 Z1d1 Z2d2 Z3d3 Z4d4 Z5d?d5Z6d?d6Z7d7 Z8d8 Z9d9 Z:d: Z;d; Z<y()@DistUpgradeQuirksz
    This class collects the various quirks handlers that can
    be hooked into to fix/work around issues that the individual
    releases have
    c                 2   || _         |j                  | _        || _        t        ddgt        d      j                         d   j                         | _        t               | _	        d| _
        d | _        d| _        d | _        d | _        d | _        d| _        y )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_did_change_font)selfr   r   s      ?/usr/lib/python3/dist-packages/DistUpgrade/DistUpgradeQuirks.py__init__zDistUpgradeQuirks.__init__/   s    $%%
GT?4.244?KM!EEJUW 	
J	 !
$)!! %    c                    | j                   j                  rt        j                  d       y| j                  j                  dd      }| j                  j                  dd      }d|z  }t        | |d      }|t        j                  d|z          |        ||}t        | |d      }|t        j                  d|z          |        d||}t        | |d      }| t        j                  d|z          |        yy)	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 modeNSourcesToFrom%szquirks: running %sfrom_)r   _partialUpgradelogginginfor   getgetattrdebug)r   
quirksName
to_releasefrom_releasefuncnamefuncs         r   runzDistUpgradeQuirks.runB   s    $ ??**LLDE[[__Y5
{{y&9 *$tXt,MM.9:F (4tXt,MM.9:F #/
;tXt,MM.9:F r   c                 N    t        j                  d       | j                          y)z3 run before the apt cache is opened the first time zrunning Quirks.PreCacheOpenN)r'   r+   _add_apport_ignore_listr   s    r   PreCacheOpenzDistUpgradeQuirks.PreCacheOpenp   s    34$$&r   c                    t        j                  d       | j                          | j                          | j	                          | j                          | j                          | j                          | j                  j                  }| j                  |       | j                          d|vrt        j                  d       y|d   j                  rNt        j                  j                  d      st        j                  j                  d      r| j!                          | j"                  r| j%                          yy)z2 run after the apt cache is opened the first time z%running Quirks.noblePostInitialUpdatesnapd'package required for Quirk not in cacheNz/run/snapd.socketz/run/snapd-snap.socket)r'   r+   _get_from_and_to_version_test_and_fail_on_i386_test_and_fail_on_aufs_test_and_fail_on_power8$_test_and_fail_on_bios_with_xfs_boot_test_and_fail_on_armhf_raspir   cache_test_and_warn_if_ros_installed#_maybe_prevent_flatpak_auto_removalis_installedospathexists_checkStoreConnectivityr   _calculateSnapSizeRequirementsr   r?   s     r   noblePostInitialUpdatez(DistUpgradeQuirks.noblePostInitialUpdatev   s     	=>%%'##%##%%%'113**,%%,,U3002%MMCD>&& 34 89((* $$//1 %r   c                 t   t        j                  d       | j                  j                  }d|vrt        j                  d       y |d   j                  r| j
                  r| j                          d|v r|d   j                  r| j                          d|v r!|d   j                  r| j                          y y y )Nzrunning Quirks.noblePostUpgrader7   r8   zubuntu-desktop-raspizlinux-firmware-raspi2)	r'   r+   r   r?   rB   r   _replaceDebsAndSnaps_replace_fkms_overlay_remove_uboot_on_rpirH   s     r   noblePostUpgradez"DistUpgradeQuirks.noblePostUpgrade   s    78%%%MMCD>&&%%'!U*+,99**,"e+,-::))+ ; ,r   c                 N    t        j                  d       | j                          y)z run after cleanup zrunning Quirks.PostCleanupN)r'   r+   _remove_apport_ignore_listr4   s    r   PostCleanupzDistUpgradeQuirks.PostCleanup   s    23'')r   c                    t        j                  d       | j                          | j                          | j	                          | j                          | j                          | j                          | j                          y )Nzrunning Quirks.StartUpgrade)	r'   r+   _applyPatches_removeOldApportCrashes_killUpdateNotifier_killKBluetooth_pokeScreensaver_stopDocvertConverter_set_generic_fontr4   s    r   StartUpgradezDistUpgradeQuirks.StartUpgrade   sc    34$$&  """$ r   c                 n    t        j                  d       | j                          | j                          y)z/ run right before calculating the dist-upgrade z"running Quirks.PreDistUpgradeCacheN)r'   r+   _maybe_remove_gpg_wks_server!_install_t64_replacement_packagesr4   s    r   PreDistUpgradeCachez%DistUpgradeQuirks.PreDistUpgradeCache   s'    :;))+..0r   c                 N    t        j                  d       | j                          y)z( run after calculating the dist-upgrade z#running Quirks.PostDistUpgradeCacheN)r'   r+   _install_linux_metapackager4   s    r   PostDistUpgradeCachez&DistUpgradeQuirks.PostDistUpgradeCache   s    ;<'')r   c                 &   t               }	 t        j                  ddgt        j                  d      }|j                         d   j                  d      D ]'  }|s|j                  |j                         d          ) |S # t        $ r |cY S w xY w)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lines       r   _get_pci_idszDistUpgradeQuirks._get_pci_ids   s    	  '448:A MMOA&,,T2 	+D		$**,q/*	+   	L	s   (B BBc                 ~    t        j                         }	 |j                  d j                  j                  z        j                         d    _        |j                  d j                  j                  z        j                         d    _        y # t        $ r t         fd|j                  d      D         j                  j                        j                         d    _        t         fd|j                  d      D         j                  j                        j                         d    _        Y y w xY w)Nr$   r   c              3   |   K   | ]3  }|j                   j                  j                  k(  r|j                   5 y wN)seriesr   fromDistversion.0rr   s     r   	<genexpr>z=DistUpgradeQuirks._get_from_and_to_version.<locals>.<genexpr>   s2      :qHH 8 88  :   9<objectc              3   |   K   | ]3  }|j                   j                  j                  k(  r|j                   5 y wrp   )rq   r   toDistrs   rt   s     r   rw   z=DistUpgradeQuirks._get_from_and_to_version.<locals>.<genexpr>   s2      8qHH 6 66  8rx   )distro_infoUbuntuDistroInfors   r   rr   ri   r   r{   r   AttributeErrornextget_all)r   dis   ` r   r9   z*DistUpgradeQuirks._get_from_and_to_version   s   ))+	3

4$//":"::;AACAF  

4$//"8"889??A!D   	3!%:BJJx$8 :(("* +0%'!"5D  $8BJJx$8 8&& ( ). 3D	3s   A<B B%D<;D<c                 
   d}t         j                  j                  |      rdt         j                  vry t	        j
                  g dt        t         j                  d            }|dk7  rt        j                  d       y t	        j
                  |g      }t        j                  d|d	|d
       |dk7  rL| j                  j                  t        d      t        d            }|s| j                  j                          y y y )Nz/usr/lib/nux/unity_support_testDISPLAY)psz-Czunity-panel-servicewr   r   z5_test_and_warn_for_unity_3d_support: no unity runningz%_test_and_warn_for_unity_3d_support 'z' returned ''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?)rC   rD   rE   environrg   callopendevnullr'   r+   r   askYesNoQuestion_r   abort)r   UNITY_SUPPORT_TESTreturn_coderess       r   #_test_and_warn_for_unity_3d_supportz5DistUpgradeQuirks._test_and_warn_for_unity_3d_support   s    >12+ oo/RZZ8MO!MMGI oo'9&:;"K1	2 !**-- " # D EC %%'  r   c                     g d}| j                         }t        |      j                  |      rL| j                  j	                  t        d      t        d            }|s| j                  j                          y y y )N)z	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   r   r   r   r   )r   I8XX_PCI_IDSrc   r   s       r   _test_and_warn_on_i8xxz(DistUpgradeQuirks._test_and_warn_on_i8xx  sy     !!#|))%0**-- & ' > ?	C %%'  1r   c                    | j                  d      r| j                  d      s| j                  j                  t	        d      t	        d            }|s| j
                  j                          g d}t        j                  ddj                  |      z         | j
                  j                  j                  dd      }|D ]  }|j                  |        | j
                  j                  j                  ddd	j                  |             y
y
y
)zi
        Some cards are no longer supported by fglrx. Check if that
        is the case and warn
        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-devlibamdxvba1z	remove %s, DistroPostUpgradePurge,N)_checkVideoDriver_supportInModaliasesr   r   r   r   r   r'   r+   joinr   getlistappendrf   )r   r   removalspostupgradepurgeremoves        r   '_test_and_warn_on_dropped_fglrx_supportz9DistUpgradeQuirks._test_and_warn_on_dropped_fglrx_support  s     ""7+--g6**--    - .
/C %%'H MM+		((;;<#55==" $ # 0 ''/0OO""&&x1C'*xx0@'AC= 7 ,r   c                 N   | j                   dk(  rd}n| j                   dk(  rd}| j                  dk(  rst        j                  d       t	        d      }t	        d| j                   d	d
      }| j
                  j                  ||       | j                  j                          yy)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 i386'Sorry, no more upgrades for this systemziThere will not be any further Ubuntu releases for this system's 'i386' architecture.

Updates for Ubuntu  will continue until .N)r   r   r'   errorr   r   r   r   )r   updates_endsummarymsgs       r   r:   z(DistUpgradeQuirks._test_and_fail_on_i386F  s    
 (&K7*(K99MM45ABG ''6 7C JJWc*OO!!# r   c                     | j                   dk(  rZ| j                         sI| j                  j                  t	        d      t	        d             | j
                  j                          yyy)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
        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   r   r   r   r   r4   s    r   _test_and_fail_on_non_arm_v6z.DistUpgradeQuirks._test_and_fail_on_non_arm_v6Z  s_    
 99$$&

  n% , -. %%' '  r   c                    dD ]  }t         j                  j                  |      s#t        j                  d       t        d      }t        d|z        }| j                  j	                  ||       | j                  j                           y)z
        Test and fail if docker has aufs storage driver available
        as it is deprecated after 20.04. Even if no containers are
        currently running aufs, the upgrade could break existing
        container images.
        )z+/var/snap/docker/common/var-lib-docker/aufsz/var/lib/docker/aufszDocker config uses aufszISorry, this storage driver is not supported in kernels for newer releaseszThere will not be any further Ubuntu releases that provide kernel support for the aufs storage driver.

Please ensure that none of your containers are using the aufs storage driver, remove the directory %s and try again.N)	rC   rD   rE   r'   r   r   r   r   r   )r   aufs_dirr   r   s       r   r;   z(DistUpgradeQuirks._test_and_fail_on_aufsl  s~    1 	(Hx(78 < = ,
 /77 8 

  #.%%'	(r   c                    | j                   dk7  ryd}| j                  dk(  rd}n| j                  dk(  rd}t        d      5 }|j                         }t	        j
                  d	|t        j                        rrt        j                  d
       t        d      }t        d| j                  d|d      }| j                  j                  ||       | j                  j                          ddd       y# 1 sw Y   yxY w)a#  
        Test and fail if running on a POWER8 processor. Starting with Ubuntu
        22.04, ppc64el is compiled for POWER9 processors. Prior to 22.04 it
        was/is compiled for POWER8 processors. Hence POWER8 processors are not
        able to run, install or upgrade to jammy.
        ppc64elN z20.04z
April 2025z21.10z	July 2022/proc/cpuinfoz^cpu\s*:\s*POWER8zPOWER8 processor detectedr   zfThere will not be any further Ubuntu releases for this system's POWER8 processor.

Updates for Ubuntu r   r   )r   r   r   readresearch	MULTILINEr'   r   r   r   r   r   )r   r   fcpuinfor   r   s         r   r<   z*DistUpgradeQuirks._test_and_fail_on_power8  s     99	!(&K7*%K/" 	(affhGyy-wE9:EF ++[: ; 

  #.%%'	( 	( 	(s   B(C22C;c                 B   	 t        j                  dd       y# t        $ r| t        j                  d       | j
                  j                  t        d      t        d            }|s| j                  j                          | j
                  j                          Y yw xY w)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)rC   killrh   r'   warningr   r   r   r   r   processEventsr   r   s     r   _test_and_warn_if_vserverz+DistUpgradeQuirks._test_and_warn_if_vserver  s~    	'GGAqM 	'OOO,**--%& 7 89C %%'JJ$$&	's    BBBc                    t               }dD ])  }|j                  t        j                  d|z               + d}|D ]D  }|r n@|D ]9  }|j	                  |j
                        s|j                  s|j                  rd} D F |rL| j                  j                  t        d      t        d            }|s| j                  j                          yyy)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.
        )catkinzrosboost-cfg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   r   compilematchnamerB   marked_installr   r   r   r   r   )r   r?   ros_package_patternspackage_nameros_is_installedpkgpatternr   s           r   r@   z1DistUpgradeQuirks._test_and_warn_if_ros_installed  s      #u! 	=L !$$

+l:;=	= ! 	C/ ==*''3+=+=+/(			 **--AB 7 89C %%'  r   c                    d}d| j                   j                  vry| j                   j                  d   j                  syt        j                  j                  d      sydD ]V  }t        j                  d|gt        j                        }|j                  j                  d      j                         sTd} n t        j                  d	j                  |rd
nd             |sy| j                   j                  d   j                  d       dD ]  }|| j                   j                  vr| j                   j                  |   j                  s@t        j                  dj                  |             | j                   j                  |   j                  d        | j                   j                  j!                  | j"                  j%                         | j"                  j'                  | j                   j                               y)z
        If flatpak is installed, and there are either active remotes, or
        flatpak apps installed, prevent flatpak's auto-removal on upgrade.
        FflatpakNz/usr/bin/flatpak)remoteslistr   utf-8Tz-flatpak will{}be marked as manually installed z NOT )auto)zplasma-discover-backend-flatpakzgnome-software-plugin-flatpakz'{} will be marked as manually installed)r   r?   rB   rC   rD   rE   rg   r1   r   r   decoder   r'   r+   format	mark_autocommitr   getAcquireProgressgetInstallProgress)r   prevent_auto_removalsubcmdrv   r   s        r   rA   z5DistUpgradeQuirks._maybe_prevent_flatpak_auto_removal  s   
  %DOO111$$Y/<<ww~~01) 	F'0%??A xxw'--/'+$	 	Ev%9cwG	I $i(222>5 
	=C$/////??((-::MMC!6#;(OO!!#&00e0<
	= 	$$JJ))+JJ))$//*?*?@	
r   c                 V   ddg}d}	 t        j                  t         j                  j                  |      d       t	        |d      5 }|D ]  }|j                  | d        	 d d d        y # 1 sw Y   y xY w# t        $ r%}t        j                  d| d	|        Y d }~y d }~ww xY w)
Nz/usr/libexec/tracker-extract-3z!/usr/sbin/update-apt-xapian-index2/etc/apport/blacklist.d/upgrade-quirks-ignore-listTexist_okr   rd   zFailed to create : )	rC   makedirsrD   dirnamer   write	Exceptionr'   r+   )r   ignore_listrD   r   bines         r   r3   z)DistUpgradeQuirks._add_apport_ignore_list  s    ,/

 D	;KK-=dC (A& (CGGse2J'(( ( (  	;MM-dV2aS9::	;s6   A A: A.%A: .A73A: 7A: :	B(B##B(c                     d}	 t        j                  |       y # t        $ r%}t        j                  d| d|        Y d }~y d }~ww xY w)Nr   zFailed to remove r   )rC   r   r   r'   r+   )r   rD   r   s      r   rP   z,DistUpgradeQuirks._remove_apport_ignore_list'  sD    C	;IIdO 	;MM-dV2aS9::	;s    	AAAc                 D   t        j                  d       t        j                  j	                  d      st        j
                  d       yt        d      5 }|j                         }ddd       t        j                  dt        j                        ryy# 1 sw Y   0xY w)zE
        parse /proc/cpuinfo and search for ARMv6 or greater
        zchecking for ARM CPU versionr   zcannot open /proc/cpuinfo ?!?FNz^Processor\s*:\s*ARMv[45]T)r'   r+   rC   rD   rE   r   r   r   r   r   r   )r   r   r   s      r   r   zDistUpgradeQuirks._checkArmCPU/  sy     	45ww~~o.MM9:/" 	affhG	9917\\#	 	s   BBc                     t        j                  d       t        j                  j	                  d      st        j
                  d       yt        d      5 }|j                         }ddd       |S # 1 sw Y   S xY w)z
        read /sys/class/dmi/id/sys_vendor in case there are
        system specific issues with upgrading e.g LP: #1928434
        zchecking for system vendorz/sys/class/dmi/id/sys_vendorz(cannot open /sys/class/dmi/id/sys_vendorr   N)r'   r+   rC   rD   rE   r   r   r   )r   r   vendors      r   _readDMIVendorz DistUpgradeQuirks._readDMIVendor>  sb    
 	23ww~~<=MMDE01 	QVVXF		s   A11A;c                     t         j                  j                  d      r-t        j                  d       t        j                  ddg       yy)z, /etc/init.d/apparmor stop (see bug #559433)z/etc/init.d/apparmorz/etc/init.d/apparmor stopstopNrC   rD   rE   r'   r+   rg   r   r4   s    r   _stopApparmorzDistUpgradeQuirks._stopApparmorK  s7    77>>01MM56OO3V<= 2r   c                     t         j                  j                  d      r-t        j                  d       t        j                  ddg       yy)z5 /etc/init.d/docvert-converter stop (see bug #450569)z/etc/init.d/docvert-converterz"/etc/init.d/docvert-converter stopr   Nr   r4   s    r   rX   z'DistUpgradeQuirks._stopDocvertConverterQ  s7    77>>9:MM>?OO<fEF ;r   c                     t         j                  j                  d      r-t        j                  d       t        j                  g d       yy)zkill update-notifier/usr/bin/killallzkilling update-notifier)killall-qzupdate-notifierNr   r4   s    r   rU   z%DistUpgradeQuirks._killUpdateNotifierW  s4     77>>,-MM34OO@A .r   c                     t         j                  j                  d      r-t        j                  d       t        j                  g d       yy)z3killall kblueplugd kbluetooth (riddel requested it)r   zkilling kblueplugd kbluetooth4)r   r   
kblueplugdkbluetooth4Nr   r4   s    r   rV   z!DistUpgradeQuirks._killKBluetooth^  s2    77>>,-MM:;OOJK .r   c                    t         j                  j                  d      r~t         j                  j	                  d      r^t        j                  d       d}|dz  }|dz  }	 t        j                  |d      | _	        t        j                  | j                         y y y # t        t        f$ r t        j                  d	       Y y w xY w)
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)rC   rD   rE   r   r)   r'   r+   rg   r   r   atexitregister_stopPokeScreensaverrh   
ValueError	exception)r   cmds     r   rW   z"DistUpgradeQuirks._pokeScreensaverd  s    GGNN56

y)MM@ACHHC$$CF'--c>
 9 9: * 7 Z( F!!"DEFs   !;B $CCc                     d}| j                   <	 | j                   j                          | j                   j                         }d | _         |S # t        $ r t	        j
                  d       Y )w xY w)NFzfailed to stop screensaver poke)r   	terminatewaitrh   r'   r  r   s     r   r  z&DistUpgradeQuirks._stopPokeScreensaverq  sg    ::!E

$$&jjoo' DJ
  E!!"CDEs   4A A.-A.c           	         	 dD ]P  }t        j                   d|z        D ]3  }t        j                  d|d|d       t        j                  |       5 R y# t
        $ r"}t        j                  d|z         Y d}~yd}~ww xY w)z: remove old apport crash files and whoopsie control files )z.crashz.uploadz	.uploadedz/var/crash/*%szremoving old z file 'r   z+error during unlink of old crash files (%s)N)globr'   r+   rC   unlinkr   r   )r   extr   r   s       r   rT   z)DistUpgradeQuirks._removeOldApportCrashes|  sz    	O9 !#3c#9: !AMMa"HIIIaL!!  	OOOIAMNN	Os   AA 	B!A>>Bc                    d}t         j                  j                         }d|d<   t        g dt        t        |d      j                         }t        j                  d|d   t        j                        rd| _	        y	t        j                  d
|d   t        j                        rt        j                  d       d}| j                  j                  }d|v rN|d   j                  r?|d   j                  j                   j#                  d      st        j                  d       d}|rMt%        d      }t%        d      }| j&                  j                  ||       | j                  j)                          n| j&                  j+                  t%        d      t%        d            }nd|d   v rDt        j                  d       | j&                  j+                  t%        d      t%        d            }n4d|d   v rd}t        j                  d       nt        j                  d       |s| j                  j)                          y	y	)z: check for connectivity to the snap store to install snapsFzC.UTF-8LANG)snapr+   connectivityT)r   stderrenvr   z	^ \* PASSr   Nz^ \*.*unreachablezNo snap store connectivity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!z-Unhandled error connecting to the snap store.)rC   r   copyr   r   r   r   r   r   r   r'   r   r   r?   rB   	candidaters   
startswithr   r   r   r   )r   r   snap_env	connectedold_lxd_deb_installedr?   r   r   s           r   rF   z)DistUpgradeQuirks._checkStoreConnectivity  s   ::??$$;D!%8-133>;= 	 99\9Q<>(,D%YY+Yq\2<<HMM67$)!OO))E~<,,e..66AA$GMM"45,0)$AB 	. 
 

  #.%%'jj1178 H I &15MM<=**--*+ D EC $y|3CMM01MMIJOO!!# r   c           	      (   dd l }dd l}ddlm} | j	                          | j
                  j                  t        d             | j                  j                         D ]  \  }}|d   dk7  rdd|d   |d	   d
}g |gd}|j                  j                  dt        |j                  |      d            }|j                  dd       |j                  dd       |j                  d| j                         	 |j                  j!                  |      j#                         }	|j%                  |	      }
t'        |
d   d   d   d   d         }| xj0                  |z  c_         y # t(        |t*        f$ r t-        j.                  d|z         Y 3w xY w)Nr   )URLErrorz"Calculating snap size requirementscommandinstallzupgrade-size-checkdownloadsnap-idchannel)zinstance-keyactionr  r   )contextactionsz)https://api.snapcraft.io/v2/snaps/refreshr   encoding)urldatazSnap-Device-Series16zContent-typezapplication/jsonzSnap-Device-Architectureresultsr  sizezFailed fetching size of snap %s)jsonurllib.requesturllib.errorr  _prepare_snap_replacement_datar   updateStatusr   r   itemsrequestRequestbytesdumps
add_headerr   urlopenr   loadsintKeyErrorr  r'   r+   r   )r   r+  urllibr  r  snap_objectr!  r'  reqresponser(   r*  s               r   rG   z0DistUpgradeQuirks._calculateSnapSizeRequirements  s   )
 	++- 	

"F GH!%!6!6!8 	*D+9%2 4$&y1&y1	F "8D ..((?4::d+g> ) @C NN/6NN>+=>NN5tyyA!>>11#6;;=zz(+4	?1-f5jA&IJ !!T)!5	*. h
3 ?$FGs   8AE%%(FFc                    | j                   j                  t        d             | j                  j	                         D ]`  \  }}|d   }|dk(  r2| j                   j                  t        d|z               d|d|d   |g}nc|dk(  r-| j                   j                  t        d	|z               d||g}n1| j                   j                  t        d
|z               d|d|d   |g}	 | j                   j                          t        j                  |t        j                  d      }| j                   j                          |j                  dk(  rt        j                  |d|d       |dk(  s2|d   s9| j                  j                  j                  |d          c y# t        j                  $ r t        j                  |d|d       Y w xY w)z? install a snap and mark its corresponding package for removal zProcessing snap replacementsr  switchzrefreshing snap %sr  z	--channelr   r   zremoving snap %szinstalling snap %sT)r   checkz	 of snap z failedr   z
 succeededr  debN)r   r/  r   r   r0  r   rg   r1   r   CalledProcessErrorr'   r+   
returncoder   forced_obsoletesr   )r   r  r;  r  	popenargsprocs         r   rK   z&DistUpgradeQuirks._replaceDebsAndSnaps  s   

"@ AB "&!6!6!8 	LD+!),G(" 

''*>*E(FG#W(+i*@$H	H$

''*<t*C(DE#Wd3	

''*>*E(FG#W(+i*@$H		

((*!~~%??  

((* !#7DIJ)#E(:0077E8JK9	L, 00 FGs   &AF$$.GGc                    t        j                  d       d}t        d      5 }|j                         }ddd       t	        j
                  dt        j                        rd}|sbt        j                  d       t        d      }t        d	      }| j                  j                  ||       | j                  j                          yy# 1 sw Y   xY w)
z check PAE in /proc/cpuinfo 	_checkPaer   r   Nz^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/EnablingPAE)r'   r+   r   r   r   r   r   r   r   r   r   r   )r   paer   r   r   r   s         r   rH  zDistUpgradeQuirks._checkPae  s     	k"/" 	affhG	99)7BLLACMM34)*G C DC
 JJWc*OO!!# 		 	s   CCc                 f   d}t         j                  j                  |      syt        |      5 }|j	                         }ddd       D ]Z  }|j                  d      d   j                         }|j                         j                  d      sE|j                  d|z        sZ y y# 1 sw Y   ixY w)	z2 check if the given driver is in use in xorg.conf z/etc/X11/xorg.confFN#r   driverz"%s"T)
rC   rD   rE   r   	readlinesri   r   lowerr  endswith)r   r   XORGr   linesrl   ss          r   r   z#DistUpgradeQuirks._checkVideoDriver%  s    #ww~~d#$Z 	"1KKME	" 	D

3"((*A	$$X.JJv}-	 	" 	"s   B''B0c           
      H   t         j                  j                  |      st        j                  d       yt        j
                  |      D ]  }d|vrt        j                  d|z         !t        j                  d|z         |j                  dd      \  }}}|j                  dd      }t        j                  d	|d
|d|d       t         j                  j                  |      st        j                  d|z         t        j                         }t        |d      5 }|j                  |j                                ddd       |j                         |k(  rt        j                  d|z         -|j                         |k7  rt        j                  d|z         Zddlm}	 	  |	|t         j                  j#                  ||      |       t        j$                  d|z          y# 1 sw Y   xY w# t&        $ r t        j(                  d|z         Y w xY w)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 patchdirNr   zskipping '%s' (no '.')z'check if patch '%s' needs to be appliedre   r   /ztarget for 'z' is 'z' -> 'r   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')rC   rD   rE   r'   r+   listdirrsplitreplacehashlibmd5r   updater   	hexdigestr   DistUpgradePatcherrV  r   r(   r   r  )
r   patchdirr   encoded_pathmd5sumresult_md5sumrD   r[  fdrV  s
             r   rS   zDistUpgradeQuirks._applyPatches4  s    ww~~h'MM-(H% $	<A!|6:;MMCaGH45HHS!4D1\6=  ''S1DMM<' ( 77>>$':TAB
 ++-CdD! &R

2779%&}}-/ELMF* J"&!' (1<dBGGLL15}E81<=E$	<*& &  <!!"6":;<s   # G0,A G<0G9	<!H! H!Nc                 j   |s| j                         }|| j                  j                  vs#| j                  j                  |   j                  st	        j
                  d       y| j                  j                  |   }| j                  |j                  j                        D ]w  \  }}|D ]m  }t        j                  d|      }|s|j                  d      d|j                  d      }|j                         |v sTt	        j                  d|z           y y t	        j                  d	|z         y)
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.*r   :re   z%found system pciid '%s' in modaliasesTz4checking for %s support in modaliases but none found)rm   r   r?   r  r'   r   !_parse_modaliases_from_pkg_headerrecordr   r   grouprN  r+   )	r   pkgnamerc   r   module
pciid_listpciidmmatchids	            r   r   z&DistUpgradeQuirks._supportInModaliasesc  s    %%'E4??000OO))'2<<OO89oo##G,66s}}7K7KL		$ VZ# $HH95A)*QWWQZ@G}}%/&M(/'0 1#$		$ 	L  	!r   c           	         d|vrg S g }|d   j                  d      D ]h  }|j                  d      }|s|j                  d      \  }}|j                  ||j                  d      D cg c]  }|j                          c}f       j |S c c}w )z. return a list of (module1, (pciid, ...), ...)
Modaliases)r   (r   )ri   r   r   )r   	pkgrecordmodulesrm  rj  pciidsxs          r   rf  z3DistUpgradeQuirks._parse_modaliases_from_pkg_header  s    y(I<(..s3 	MAA wws|VVNNFS8I$J1QWWY$JKL	M  %Ks   'B
c                 r   t        j                  d       | j                  j                  }d|vrt        j                  d       y |d   j                  s%|d   j
                  st        j                  d       y 	 dd l}|j                  j                         }|D ]'  }d|j                  v st        j                  d        y  t        j                  d       |j                  d	d
| j                  j                  dgd       |j                          y # t        $ r t        j                  d       Y y w xY w)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.listrA  zhttp://extras.ubuntu.com/ubuntumainz!Third party developers repositoryzerror adding extras.ubuntu.com)r'   r+   r   r?   r   	installedaptsources.sourceslistsourceslistSourcesListurir(   rj   r{   saver   r  )r   r?   
aptsourcessourcesentrys        r   rx  z(DistUpgradeQuirks._add_extras_repository  s   ./%%"%/MMIJ-.==-.88MMNO	@) ,,88:G  	&%))3MM"NO	
 NOE#D OO22VH?A  	@>?	@s   ;1D -D AD D65D6c                     	 | j                   j                  }d|v r=|d   j                  r-d|v r(t        j                  d       |j                  dd       yyyy# t        $ r t        j                  d       Y yw xY w)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  rH   s     r   _gutenprint_fixupz#DistUpgradeQuirks._gutenprint_fixup  s    

	:OO))E(E123AA(E1<=""(GI 2 B 2  	:89	:s   AA A;:A;c                     d}t         j                  j                  |      s@	 t        j                  d       t        |d      5 }|j                  d|z         ddd       yy# t        $ r Y 6w xY w# 1 sw Y   yxY w)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)rC   rD   rE   r   rh   r   r   )r   foreign_archcfgr   s       r   _enable_multiarchz#DistUpgradeQuirks._enable_multiarch  s    .ww~~c"34 c3 D13lBCD D #  D Ds   A# A2#	A/.A/2A;c                    t        j                  d      }|j                  |      }|j                  |      }|rL|rJ|j                  d      d|j                  d      }|j                  d      d|j                  d      }t	        j
                  d|d|       t        j                  j                  ||      dkD  S )z# copied from ubuntu-drivers common z(.+)-([0-9]+)-(.+)r   -re   z
Comparing z with r   )	r   r   r   rh  r'   r+   aptapt_pkgversion_compare)r   term1term2r   match1match2s         r   _is_greater_thanz"DistUpgradeQuirks._is_greater_than  s     **12u%u%f%||A%||A0E%||A%||A0E 	u=>{{**5%81<<r   c           	         |xr dxs d}t        j                  d      }t        j                  d      }d}d}|D ]  }d|j                  v sd|j                  vs"|j                  s|j                  s;|j                  |j                        }	|	sY|j                  j                  d   }
|	j                  d      d	|	j                  d
      }| j                  ||      s|}|j                  |
      }d|
v r,|r*d|d	|	j                  d      d	|j                  d      }d|d	|	j                  d      } |S )zb Get the linux headers or linux metapackage
            copied from ubuntu-drivers-common
        z-headersr   zlinux-image-(.+)-([0-9]+)-(.+)z
linux-(.+)zlinux-imageextraSourcer   r  re   z-lts-linux   )
r   r   r   rB   r   r   r  rg  rh  r  )r   r?   headerssuffixr   source_patternmetapackagers   r   r   sourcecurrent_versionmatch_sources                r   _get_linux_metapackagez(DistUpgradeQuirks._get_linux_metapackage  s6    'Z-2**=>L1 #	;C)gSXX.E%%););chh/  ]]11(;F16Q16Q'AO ,,_-46"1'5';';F'C"f, ,2+0;;q>+7+=+=a+@+BK ,2+0;;q>+;KC#	;H r   c                    | j                   j                  }| j                  |d      }||vrt        j                  d|z         y||   j
                  s-t        j                  d|z         d}|j                  ||       yy)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(   rB   r  )r   r?   linux_metapackagereasons       r   r`   z,DistUpgradeQuirks._install_linux_metapackage  s     %% 77uE E)LL?*+ ,&'44LL;*+ ,OF0&9	 5r   c                    d}| j                   j                  }dD ]  }|D ]  \  }}t        j                  d|d|d       ||v s'||   j                  s7|rNt        j                  d|d|d       d	|z  }|j                  ||d
|      st        j                  d|z         t        j                  d|d|d       d|z  }|j                  ||d
      rt        j                  d|         t        j                  j                  |      }|D ]#  \  }}||v s||   j                  s|r||vr|D cg c]  \  }}||v s||   j                  r| }}}t        d      }	t        ddddj                  |            }
t        j                  |	       |j                          | j                  j                  |	|
       | j                   j                          n(|j                  ||          |j!                  ||          |j                  ||          |j#                  ||          & |j%                          yc c}}w )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 z (auto_inst=rq  zinstalling z	 because z was installedz%s was installed on the systemFauto_fix	auto_instzfailed to install %sz	removing z is being installedz#%s is being installed on the systemr  zfailed to remove %szuniverse component not enabledzYou have the package a   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 z 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 r   N)r   r?   r'   r(   rB   r  mark_remover  ProblemResolverr   r   _stopAptResolverLogr   r   r   clearprotectr   resolve)r   replacementsr?   r  oldnewr  solverold_instr   r   s              r   _install_python_is_python2z,DistUpgradeQuirks._install_python_is_python2  sD   1 %% % 	AI( AS!9. /%<E#J$;$;&)3&0 1!AC!G$11#v<E  2  G#LL)?#)EFLL"%s", -BSHF ,,S&5,I%:C@A	A$ **51$  	*HCe|c
 7 7%'8D $JHC',I-16s1H1H %( $J $J"#$D"E $'TYYx-@!B "  W- 113

((#6--/U3Z0uSz2U3Z(eCj)A 	*D 	=$Js   ?IIc                    | j                   j                  sqt        j                  j                  j                  d      sGd}|dz  }t        j                  |       t        j                  j                  j                  dd       yyy)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   
serverModer  r  r   find_br'   r   rf   )r   r   s     r   +ensure_recommends_are_installed_on_desktopsz=DistUpgradeQuirks.ensure_recommends_are_installed_on_desktopsc  sl     ));;%%,,-FG=::$""&&'@#F	 H *r   c                     |j                  dd      }t        |t              s|g}|D ]D  }|| j                  j                  vr|r&| j                  j                  |   j
                  du rD y y)z Helper function that checks if the given deb2snap entry
            has at least one metapkg which is installed on the system.
        metapkgNFT)r)   
isinstancer   r   r?   rB   )r   deb2snap_entrymetapkg_listr  s       r   _is_deb2snap_metapkg_installedz0DistUpgradeQuirks._is_deb2snap_metapkg_installedn  sv     &)))T:,-(>L# 	Gdoo333OO))'2??5H	 r   c           	         ddl }i | _        d| j                  z  }d| j                  z  }i }i }	 t        j
                  j                  t        j
                  j                  t                    }t        |dz   d      }|j                  |      }|j                          |d   D ]Z  }	|d   |	   }
| j                  |
      s|
j                  dd      }|
j                  d|      }|
j                  d	|      }|||f||	<   \ |d
   D ]G  }	|d
   |	   }|j                  dd      }| j                  |      s/|j                  d|      }||f||	<   I 	 d}t#        j$                  ddgdt"        j&                        j)                         }|rM|d   j+                  d      dd D cg c]  }|j+                         d    }}|D ]  }	|	|v s|	|v rd||f||	<    | j,                  j/                  t1        d             |j3                         D ]  \  }	\  }}}i }t#        j$                  dd|	gdt"        j&                        j)                         }| j,                  j5                          t7        j8                  d|d   t6        j:                        rbt        j<                  d|	z         t7        j8                  d|z  |d   t6        j:                        st        j<                  d|	z         d|d<   n| j>                  j@                  }|r1||vs||   jB                  st        j<                  d|d|	d       1t7        j8                  d|d         }|st        j<                  d |	z         fd!|d<   ||d<   |d   |d"<   ||d#<   || j                  |	<    |j3                         D ]  \  }	\  }}i }t#        j$                  dd|	gdt"        j&                        j)                         }| j,                  j5                          t7        j8                  d|d   t6        j:                        st        j<                  d|	z         t7        j8                  d|z  |d   t6        j:                        st        j<                  d|	z         d$|d<   t#        j$                  dd%|	gdt"        j&                        j)                         }| j,                  j5                          |d   j+                  d      D ]  }|j+                         }tE        |      d&k7  r"|d   }|d'   }|jG                  |	d(z         sA|j+                  d(      d   }|d)k7  s[||vs`t        j<                  d*|	d+|d,       d|d<   d-|d#<    n || j                  |	<    | j                  S # t        $ r#}t        j                   d|z         Y d}~td}~ww xY wc c}w ).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.jsonrv   seededrA  from_channel
to_channelunseededz%error reading deb2snap.json file (%s)r   r  r   T)r   r   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  zDeb package z) is not installed. Skipping snap package z installationzsnap-id:\s*(\w*)z'Could not parse snap-id for the %s snapr  r  r   r   connections   re   re  r  zSnap z is being used by z. Switching it to stable trackstable)$r+  r   r   r   rC   rD   r   abspath__file__r   loadcloser  r)   r   r'   r   rg   r   r   r   ri   r   r/  r   r0  r   r   r   r   r+   r   r?   rB   lenr  )r   r+  r  r  seeded_snapsunseeded_snapscurrent_pathd2s_filed2sr  seedrA  	from_chanto_chanunseedr   	snap_listrl   snaps_installedr;  	snap_infor?   r   connsconn	conn_colsplugslot	plug_snaps                                r   r.  z0DistUpgradeQuirks._prepare_snap_replacement_data  s   
 	)D,>,>>'$*:*::
	I77??277??8+DELL+;;SAH))H%CNNH ?8}T*::4@hhud+ HH^\B	((<<&)9g%>T"? J 8Z.jj-::6B"JJ~|D	(+Y'7t$8 	$$ff%58<,6OO==H[] 	  ,5Q<+=+=d+CAb+IK#'  $zz|A KO K ( J<'4>+A*.j)IL&	J 	

"@ AB5A5G5G5I !	01D13jK"((&&$)?<@0:AAL  JJ$$&yy)A,E4t;<yy!2\!A!*1r||=MM"O$(#) *)1I& --S-U3Z5L5LMMDG#O P		"5y|DMM"K$(#) *)2I&%(E").qI&%/K	"$/DOOD!C!	0D *8)=)=)? *	4%D%3K"((&&$)?<@0:AAL  JJ$$&yy)A,E4t;<yy!2\!A!*1r||=MM"O$(#) *)1I& #((&-)F<@0:AAL  

((*!!HNN40 "D $

I9~* $Q<D$Q<Dtcz2$(JJsOA$6	$+$N:#MM-19+> ? 6>K	25=K	2!!"$ )4%U*	4V C  	IOOCaGHH	IKs   D!V $W	W
'WW
c                 P   d}	 t         j                  j                  |d      }t        |dd      5 }|j	                         }d d d        dj                  d j                  d	
      D              }||k(  rt        j                  d|       y 	 t         j                  j                  |d      }t        |dd      5 }|j                  |       d d d        	 t        |dd      5 }|j                  |       d d d        y # 1 sw Y   xY w# t
        $ r t        j                  d|       Y y w xY w# 1 sw Y   exY w# t        $ r"}t        j                  d||       Y d }~y d }~ww xY w# 1 sw Y   y xY w# t        $ r"}t        j                  d|||       Y d }~y d }~ww xY w)NzfYou may need to replace the vc4-fkms-v3d overlay with vc4-kms-v3d in config.txt on your boot partition
config.txtrv   r   r$  +failed to open boot configuration in %s; %sr   c              3      K   | ]X  }|j                  d       rd|z   |j                  d d      z   n*|j                  d      s|j                         dk(  rd|z   n| Z yw)zdtoverlay=vc4-fkms-v3dz0# changed by do-release-upgrade (LP: #1923673)
#zdtoverlay=vc4-kms-v3dzgpu_mem=z	start_x=1z1# disabled by do-release-upgrade (LP: #1923673)
#N)r  rY  rstripru   rl   s     r   rw   z:DistUpgradeQuirks._replace_fkms_overlay.<locals>.<genexpr>  ss      
  78 @$FLL13JKL z*dkkm{.J A4G	
s   AA T)keependsz3no fkms overlay or camera firmware line found in %szconfig.txt.distUpgrader   0unable to write boot config backup to %s: %s; %s-unable to write new boot config to %s: %s; %s)rC   rD   r   r   r   FileNotFoundErrorr'   r   
splitlinesr   r   IOError)	r   boot_dirfailure_actionboot_config_filenamer   boot_config
new_configboot_backup_filenameexcs	            r   rL   z'DistUpgradeQuirks._replace_fkms_overlay  s   ? 		#%77<<,#G *C'B 'affh' WW 
 $...=
 

  $OO $%9;	#%77<<2$4 *C'B %a$%	E*C'B $a
#$ $M' '  	MMG.@	:% % 	MML.^E	
$ $ 	EMMI.^E E	Es   .D DD .E  >D4E  E: 'E.9E: DD  D10D14D=9E   	E+	E&&E+.E73E: 7E: :	F%F  F%c                    ddd}|j                        }ddd	 t        j                  j                  d      }t	        |d	d
      5 }|j                         }d d d        fd}fd}j                         D cg c]  }|j                          }	} | ||	            }	dj                  d |	D              }
|
|k(  rt        j                  d|       y 	 |dz   }t	        |dd
      5 }|j                  |       d d d        	 t	        |dd
      5 }|j                  |
       d d d        y # 1 sw Y   xY w# t        $ r t        j                  d|       Y y w xY wc c}w # 1 sw Y   jxY w# t        $ r"}t        j                  d||       Y d }~y d }~ww xY w# 1 sw Y   y xY w# t        $ r"}t        j                  d|||       Y d }~y d }~ww xY w)Nzkernel=vmlinuzz!initramfs initrd.img followkernelzYou may need to replace u_boot_* with vmlinuz, and add {initramfs_line!r} to config.txt on your boot partition; see LP: #1936401 for further details)initramfs_linez0# commented by do-release-upgrade (LP: #1936401)z,# added by do-release-upgrade (LP: #1936401)z5# merged from {} by do-release-upgrade (LP: #1936401)r  rv   r   r$  r  c                 V   g }dx}}| D ]  }|dk(  rJ|j                  |       |r|j                         |j                         |j                         d}R|j                  d      r&|j                         |j                  d|z          |j                  d      r(d}|j                         |j                  d|z          |j                  |        |rH|sD|j                         |j                  d       |j                         |j                         |S | S )NFz[all]Tzdevice_tree_address=rK  zkernel=uboot_rpi_)r   r  )	rQ  resultremoved_ubootadded_kernelrl   added_prefixchange_prefixr  kernel_lines	        r   replace_ubootz=DistUpgradeQuirks._remove_uboot_on_rpi.<locals>.replace_ubootN  s   F+00ML (7? MM$''l3k2n5'+__%;< MM-0MM#*-__%89 %)MMM-0MM#*-MM$'1(> #MM,/MM'*MM+.MM.1r   c                    g }d}d}| D ]  }|j                  d      r|rd}|dv rd}|j                  d      d   }|j                  
j                  |             t        j
                  j                  	|      }d}t        |dd	      5 }|D ]7  }|j                  d      r|rd}|j                  |j                                9 	 d d d        |d
z   }	 t	        j                  ||       |j                  |        |r|S | S # 1 sw Y   ?xY w# t        $ r#}t        j                  d|||       Y d }~3d }~ww xY w)NTFrK  )zinclude syscfg.txtzinclude usercfg.txtr   )maxsplitrv   r   r$  .distUpgradez7failed to move included configuration from %s to %s; %s)r  ri   r   r   rC   rD   r   r   r  renamer  r'   r   )rQ  r  skip_commentsfound_includesrl   included_filenamer   target_filenamer  r  merge_prefixs            r   merge_includesz>DistUpgradeQuirks._remove_uboot_on_rpi.<locals>.merge_includesz  s^   F M"N  ( ??3'M %HH
 &*N(,

A
(>q(A%MM,"5"56G"HI(*X5F)H%$(M/wG 91$% 9D#s3 (,1M"MM$++-8	99 '8.&HO<		"3_E MM$'A (F )9 9 # < ':;L&5s< <<s$   =D
D
D		ED==Er   c              3   &   K   | ]	  }|d z     yw)rd   N r  s     r   rw   z9DistUpgradeQuirks._remove_uboot_on_rpi.<locals>.<genexpr>  s     ;TTD[;s   z!no u-boot removal performed in %sr   r   r  r  )r   rC   rD   r   r   r   r  r'   r   r  r  r   r   r  )r   r  r  r  r   r  r  r  rl   rQ  r  r  r  r  r  r  r  r  s    `           @@@@@r   rM   z&DistUpgradeQuirks._remove_uboot_on_rpi9  s   &<> 	 (..n.MJEN	#%77<<,#G *C'B 'affh'*	X*	X ,7+A+A+CD4DDnU34WW;U;;
$OO?02	#7.#H *C'B %a$%	E*C'B $a
#$ $c' '  	MMG.@	z E% % 	MML.^E	
$ $ 	EMMI.^E E	Es   .E E %E E2)F <E7F F= %F17F=  E	E  E/.E/7F <F 	F.F))F.1F:6F= :F= =	G(G##G(c                 &	   d}| j                   ryt        j                  dd      dv ryddhj                  t        j                        st        j                  d       y	 t        t        j                  dt        j                  d                  }t        j                  |      }d
}t        j                  dd      j                  d      }d|v sd|v rd}nd|v rd}t        t        g d      t        |      z        }t        j                  ddd|j                    ddddddd| dgt        j"                  d      }|j$                  j'                  d       j)                  d!      \  }}	}
|xs d"}	 t        |
       t        j                  d$| d!|
 d%| d!|
 d&	       t        j                  ddd|j                    ddddddd'| dd(| d!|
 d(g      }|j*                  d)k7  rt        j                  d*| d!|
        yd+| _         t        j,                  j/                  |j0                  d,      }t        j2                  t        j,                  j5                  |      d+-       t7        j8                  |      j;                  d./       t        j<                  ||j>                  |j@                         |rod0}t        j2                  t        j,                  j5                  |      d+-       d1}tC        |d2      5 }|jE                  d3| d4| d5| d!|
 d6| d7       ddd       yd8}t        j2                  t        j,                  j5                  |      d+-       tC        |d2      5 }|jE                  d9| d:| d!|
 d;       ddd       t        j                  d<dd|j                    dd=g       t        j                  d<dd|j                    dd>t        j,                  jG                  |      g      }|j*                  d)k7  r7t        j                  d?t        j,                  jG                  |       d@       yy# t        $ r t        j                  d	       Y yw xY w# t        $ r d#}
Y w xY w# 1 sw Y   yxY w# 1 sw Y   xY w)Az Due to changes to the Ubuntu font we enable a generic font
            (in practice DejaVu or Noto) during the upgrade.
            See https://launchpad.net/bugs/2034986
        SansNXDG_SESSION_TYPEr   )r   ttySUDO_UID
PKEXEC_UIDzFCannot determine how root privileges were gained, will not change fontz3Cannot determine non-root UID, will not change fontzorg.gnome.desktop.interfaceXDG_CURRENT_DESKTOPre  MATEUKUIzorg.mate.interface
X-Cinnamonzorg.cinnamon.desktop.interface)BudgieLXQtr  r  r  XFCEzsystemd-runz--userz-Mz@.hostz--waitz--piper   z--z/usr/bin/gsettingsr)   z	font-namer   )r   r%  z'
r   Ubuntu11zSetting generic font z& during the upgrade. Original font is r   rf   "r   zFailed to change font to Tz!.config/upgrade-need-font-restorer   i  )modez//etc/xdg/autostart/upgrade-restore-font.desktopz'$HOME/.config/upgrade-need-font-restorer   z[Desktop Entry]
Name=Restore font after upgrade
Comment=Auto-generated by ubuntu-release-upgrader
Type=Application
Exec=sh -c 'if [ -e "z" ]; then gsettings set z font-name "z	";rm -f "z"; fi'
NoDisplay=true
z2/usr/lib/systemd/user/upgrade-restore-font.servicez# Auto-generated by ubuntu-release-upgrader
[Unit]
Description=Restore font after upgrade
After=graphical-session.target dconf.service
ConditionPathExists=%h/.config/upgrade-need-font-restore

[Service]
Type=oneshot
ExecStart=/usr/bin/gsettings set z font-name 'zm'
ExecStart=/usr/bin/rm -f %h/.config/upgrade-need-font-restore

[Install]
WantedBy=graphical-session.target
	systemctlzdaemon-reloadenablezFailed to enable z%. Font will not be restored on reboot)$r   rC   getenvr   r   r'   r+   r8  pwdgetpwuidr  ri   boolrf   rg   r1   pw_namer   r   r   
rpartitionrC  rD   r   pw_dirr   r   pathlibPathtouchchownpw_uidpw_gidr   r   basename)r   	temp_fontuidpwuidschemadesktopsuse_autostartrv   fontr   r*  need_font_restore_fileautostart_fileflagr   	unit_files                   r   rY   z#DistUpgradeQuirks._set_generic_font  s   
 	  99',; L)66rzzBMM" 	bii
BIIl,CDECLL%E /992B7==cB X8!3)FX%5F HI(m

 NNHdu}}oV,Dxt!5x+' ??
 ((..0;;C@q$x	I 	-i[$ @337&$qB 	C NN8Temm_F+Chd %F81YKqa0	
  <<1MM5i[$HI $ "$LL="
 	BGGOO$:;dK+,222>
'u||DNNKK7$G<Dnc* 
a- .2F2Jhl4&$ 8"V $''	
  I	
BGGOOI.>)S! 	QGG4 (-vQtf 566	& 	du}}oV4
 	
 NNdu}}oV4bgg&&y1
 
 <<1MM-bgg.>.>y.I-J K@ @ A m  	MME 		H  	D	>
 	 	s=   !AQ 7Q) "Q;.RQ&%Q&)Q87Q8;RRc                    t         j                  j                  d      ryd}d}t        d      5 }|j	                         D ]^  }	 |j                         j                         \  }}}}}	}
|j                         }|j                         }|dk(  r|}n|dk(  r|}|s[|s^ n ddd       |dk(  s|d|dk(  r^t        j                  d	       | j                  j                  t        d
      t        d             | j                  j                          yyy# t        $ r t        j                  d| d       Y w xY w# 1 sw Y   xY w)zy
        If this system is BIOS and has XFS on /boot, do not allow the upgrade
        to happen (LP: #2039172).
        z/sys/firmware/efiNz/proc/mountszline 'z ' in /proc/mounts not understoodrT  z/bootxfsz-Cannot upgrade system with BIOS and XFS /bootzCannot upgrade this systemzrDue to a bug in grub, this system cannot be safely upgraded at this time.

See https://launchpad.net/bugs/2039172.)rC   rD   isdirr   rM  r   ri   rN  r  r'   r+   r   r   r   r   r   )r   boot_fsroot_fsr   rl   whatwherefsoptsabs              r   r=   z6DistUpgradeQuirks._test_and_fail_on_bios_with_xfs_bootP  sJ   
 77==,-.! 	Q 48JJL4F4F4H1T5"dAq!KKMEB C< Gg% Gw#	( eGu4D MMIJJJ./ < = OO!!# 5E " MM!$'HI 		 	s<   D>AD
D>D>D>"D;8D>:D;;D>>Ec                 6   | j                   j                  j                  }	 |d   j                  sy|d   j                  }|D cg c]  \  }}}|j
                  j                  r|  }}}t        |      st        j                  d       | j                   j                  d   j                  d       t        j                  | j                   j                        j                  | j                   j                  d          yyc c}}w # t        $ r Y yw xY w)z}
        Prevent postfix from being unnecessarily installed, and leading to a
        debconf prompt (LP: #2060578).
        zgpg-wks-serverNzmail-transport-agentzENo mail-transport-agent installed, marking gpg-wks-server for removalFr  )r   r?   _cachecurrent_verprovides_list
parent_pkgr9  anyr'   r(   mark_deleter  r  r  )r   r?   provides_mtar   verinstalled_mtas         r   r\   z.DistUpgradeQuirks._maybe_remove_gpg_wks_server|  s    %%,,	)*66 !78FFL%1!1c>>-- M  =!LL5
 OO!!"23???O 5 56>>%%&67 "  		s(   D D #D)D D 	DDc                    | j                   dk7  ryd| j                  j                  v r#| j                  j                  d   j                  rt	        j
                  d       t        j                         }|j                  d      D ci c]  }|j                  | }}|j                  | j                  j                        }|8|j                  j                  d      }t        d|j                   d| d	      }nd
}| j                   j                  t        d      t        d      |z          | j                  j#                          yyyc c}w )z
        Test and fail if running armhf on a Raspberry Pi. Starting with Ubuntu
        24.04, armhf is not well supported on Raspberry Pi. Hence, prevent
        upgrades on such platforms. LP: #2060220.
        armhfNzlinux-image-raspiz%Detected Raspberry Pi kernel on armhfry   z%B %YzUpdates for Ubuntu r   r   r   z.Sorry, cannot upgrade this system to 24.04 LTSzWThe Raspberry Pi kernel for Ubuntu 24.04 LTS does not support the armhf architecture.

)r   r   r?   rB   r'   r   r|   r}   r   rq   r)   rr   eolstrftimer   rs   r   r   )r   r   rv   releasesr.   r   updates_end_msgs          r   r>   z/DistUpgradeQuirks._test_and_fail_on_armhf_raspi  sL    99  4??#8#88OO!!"56CCMMAB--/B H- !H  $<<(@(@AL'*..77@"#),*>*>)? @&&1]!5# #%JJD C $	$		 OO!!#A D 9s   	Ec                    | j                   j                  D ]  }|j                  s|j                  r|j                  j                  r4t        j                  d|j                          |j                  j                  D cg c]  }|j                  |j                  f }}|j                  j                  D cg c]  \  }}}|
 }}}|D ]  }d|f|vr
d|f|vrd|f|vrt        j                  | j                   j                  |j                        }|j                  j                   |k7  ret        j                  d|j                   d|j                          |j#                  dd       |j%                  d	       t        j&                  | j                   j                        j)                  |         t        j                  d
|j                           yc c}w c c}}w )z
        LP: #2061175
        zSearching for replacement for ReplacesBreaks	ConflictszFound replacement z for Fr  r  z!Failed to find a replacement for N)r   r?   rB   r  downloadabler'   r+   r   _pkgrev_depends_listdep_type_untranslated
parent_verrD  r  PackagerE  _candr  rG  r  r  )r   packagedepreverse_depends_listr   rI  rD  replacements           r   r]   z3DistUpgradeQuirks._install_t64_replacement_packages  s    ,, 5	G''  W%6%6%C%C
 MM:7<<.IJ #<<88$ **CNN;$  $
 3:,,2L2LMYQ3SMMM$ "$,@@ sO+?? #&.BB!kkOO))NN
 ((..#5()9)9(: ;"<<.*
 ((%5(I##U#3##DOO$9$9:BB7K 9"@ 7~Fg5	$
 Ns   G,?G1)z	./patchesrp   )r   )z/boot/firmware)=__name__
__module____qualname____doc__r   r1   r5   rI   rN   rQ   rZ   r^   ra   rm   r9   r   r   r   r:   r   r;   r<   r   r@   rA   r3   rP   r   r   r   rX   rU   rV   rW   r  rT   rF   rG   rK   rH  r   rS   r   rf  rx  r  r  r  r  r`   r  r  r  r.  rL   rM   rY   r=   r\   r>   r]   r	  r   r   r	   r	   (   sG   &&+\'2:,"*!1*3$(B(0'CR$(($(,(:',-(^0
d;";>GBLF	O>$@&*P!LF$(-<^>@4:"	D= .`:&HT	G&CJ1EfCEJPAd*$X!F+$Z9r   r	   )r  r   r|   r
  r'   rC   r  r   rZ  rg   r$  r   r   utilsr   DistUpgradeGettextr   r   ry   r	   r	  r   r   <module>rf     s?   ,      	 
 	    "  ,] ]r   