o
    tï‰dÉ1  ã                   @   sâ   d dl m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 d dlmZ d dlmZ d dlmZ ddlmZmZmZ dd	lmZ G d
d„ deƒZedkroeddƒZede dd¡ ƒ dS dS )é    )ÚTemplateN)Úgettext)ÚSourcesList)Úurlopen)Ú	HTTPErroré   )Úget_distÚurl_downloadableÚcountry_mirror)Úreadlinec                   @   s–   e Zd ZdZdZdZdejv Zdd„ Z	dd„ Z
d	d
„ Zdd„ Zdd„ Z	d"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S )#ÚDistUpgradeFetcherCorez2 base class (without GUI) for the upgrade fetcher ú http://archive.ubuntu.com/ubuntuÚmainÚDEBUG_UPDATE_MANAGERc                 C   s   || _ tƒ | _|| _g | _d S ©N)Únew_distr   Úcurrent_dist_nameÚ	_progressÚrun_options)Úselfr   Úprogress© r   úD/usr/lib/python3/dist-packages/DistUpgrade/DistUpgradeFetcherCore.pyÚ__init__1   s   
zDistUpgradeFetcherCore.__init__c                 C   s   | j rtj |d ¡ dS dS )z" helper to show debug information Ú
N)ÚDEBUGÚsysÚstderrÚwrite)r   Úmsgr   r   r   Ú_debug8   s   ÿzDistUpgradeFetcherCore._debugc                 C   sò   d| j v rdS | jjd urX|  | jj¡}t ¡ }zt d¡ t|ƒ}| ¡  	dd¡}W n' t
y?   |  tdƒtdƒ¡ Y dS  tyR   |  td	ƒtd
ƒ¡ Y dS w t |¡ tƒ  t|ƒ ttdƒdd� tƒ }| ¡  ¡  tdƒ¡rwdS dS )Nz(--frontend=DistUpgradeViewNonInteractiveTé   zUTF-8Úreplacez'Could not find the release announcementzThe server may be overloaded.Fz+Could not download the release announcementz&Please check your internet connection.zContinue [yN] Ú )ÚendÚy)r   r   ÚreleaseNotesURIÚ
_expandUriÚsocketÚgetdefaulttimeoutÚsetdefaulttimeoutr   ÚreadÚdecoder   ÚerrorÚ_ÚIOErrorÚprintr   ÚstripÚlowerÚ
startswith)r   ÚuriÚtimeoutÚrelease_notesÚnotesÚresr   r   r   ÚshowReleaseNotes=   s8   


ÿ
ÿý
z'DistUpgradeFetcherCore.showReleaseNotesc                 C   s   t |ƒ t |ƒ dS )z� minimal implementation for error display, should be overwriten
            by subclasses that want to more fancy method
        F)r0   )r   ÚsummaryÚmessager   r   r   r-   X   s   zDistUpgradeFetcherCore.errorc                 C   sv   | j jr9| jd tj | j j¡ }| jd tj | j j¡ }ttdƒtj |¡tj |¡dœ ƒ |  	||¡r9dS dS )Nú/z0authenticate '%(file)s' against '%(signature)s' )ÚfileÚ	signatureTF)
r   ÚupgradeToolSigÚtmpdirÚosÚpathÚbasenameÚupgradeToolr0   r.   Úgpgauthenticate)r   ÚfÚsigr   r   r   Úauthenticate`   s   ÿ


þz#DistUpgradeFetcherCore.authenticateNc                 C   s@   dg}|sd}|d|g7 }|d||g7 }t j|t jd�}|dkS )zx authenticated a file against a given signature, if no keyring
            is given use the apt default keyring
        zapt-keyz./usr/share/keyrings/ubuntu-archive-keyring.gpgz	--keyringÚverify)r   r   )Ú
subprocessÚcallÚPIPE)r   r=   r>   ÚkeyringÚgpgÚretr   r   r   rE   l   s   z&DistUpgradeFetcherCore.gpgauthenticatec              
   C   s¼   t j | jt j | j¡¡}ttdƒt j |¡ ƒ t j |¡s!dS z!t	 
| jd t j | j¡ d¡}|D ]}| |¡ q4| ¡  W dS  t	jy] } zt d| ¡ W Y d }~dS d }~ww )Nzextracting '%s'Fr<   Úrzfailed to open tarfile (%s)T)rA   rB   Újoinr@   rC   r4   r0   r.   ÚexistsÚtarfileÚopenÚextractÚcloseÚ	ReadErrorÚloggingr-   )r   ÚfnameÚtarÚtarinfoÚer   r   r   ÚextractDistUpgrader|   s&   ÿÿ
ý€þz*DistUpgradeFetcherCore.extractDistUpgraderc                 C   sH   d| j | jjf  | _}tj |¡s"|  tdƒtdƒd tdƒ ¡S dS )Nz%s/%szCould not run the upgrade toolz.  z…This is most likely a bug in the upgrade tool. Please report it as a bug using the command 'ubuntu-bug ubuntu-release-upgrader-core'.T)	r@   r   ÚnameÚscriptrA   rB   rR   r-   r.   )r   r_   r   r   r   ÚverifyDistUprader�   s   

ÿÿz(DistUpgradeFetcherCore.verifyDistUpraderc                 C   sÖ   |   d| j ¡ tdd�}tƒ }|jD ]P}|js |js |jdks!q|j|v r'q|j 	|¡r=|j
| jkr=| j|jv r=|  S |j
| jkrcd|jv rc|j|t|ƒd…  }t|| j ƒr]|  S | |j¡ q|   d¡ dS )	a  
        try to figure what the mirror is from current sources.list

        do this by looing for matching DEFAULT_COMPONENT, current dist
        in sources.list and then doing a http HEAD/ftp size request
        to see if the uri is available on this server
        zmirror_from_sources_list: %sF)ÚwithMatcherÚdebr   Nzno mirror foundr#   )r    r   r   ÚsetÚlistÚdisabledÚinvalidÚtyper4   r3   ÚdistÚDEFAULT_COMPONENTÚcompsÚlenr	   Úadd)r   r4   Údefault_uriÚsourcesÚseenr\   Ú
mirror_urir   r   r   Úmirror_from_sources_listœ   s(   


€
z/DistUpgradeFetcherCore.mirror_from_sources_listc              
   C   s¦   |  | j¡r|  d¡ |  || j¡}|r|S t|ƒ}tƒ }|j|d�}zt|| jƒs0td| ƒ‚W |S  tyR } z|  d| ¡ |jdd�}W Y d}~|S d}~ww )z†
        expand the uri so that it uses a mirror if the url starts
        with a well known string (like archive.ubuntu.com)
        ztrying to find suitable mirror)Úcountrymirrorzfailed to download %sz url '%s' could not be downloadedr#   N)	r3   ÚDEFAULT_MIRRORr    rq   r   r
   Úsafe_substituter	   Ú	Exception)r   r4   Únew_uriÚuri_templateÚmr\   r   r   r   r'   º   s&   
ÿü€üz!DistUpgradeFetcherCore._expandUric                 C   sF  t jdd�}|| _t |¡ t d| ¡ | jdkr(tj	 
dd¡ tj	 
dd¡ t | j¡}| jjdurG|  | jj¡}tj||td	ƒd
�}| | jjdur¡|  | jj¡| _tj|| jtdƒd
�}| | ¡ }||jkrtt d| ¡ dS tj | jj¡tj | jj¡fD ]}tj |¡r”tj |¡dksžt d| ¡  dS q„dS dS )z. download the tarball with the upgrade script zubuntu-release-upgrader-)Úprefixzusing tmpdir: '%s'r   zDebug::Acquire::httpÚ1zDebug::Acquire::ftpNzUpgrade tool signature)ÚdescrzUpgrade toolzfetch result != continue (%s)Fzfile '%s' missingT)ÚtempfileÚmkdtempr@   rA   ÚchdirrX   Údebugr   Úapt_pkgÚconfigrc   ÚAcquirer   r   r?   r'   ÚAcquireFiler.   rD   r4   ÚrunÚRESULT_CONTINUEÚwarningrB   rC   rR   Úgetsize)r   r@   Úfetcherr4   Úaf1Úaf2ÚresultrF   r   r   r   ÚfetchDistUpgraderÓ   sF   

þþ
ÿþz(DistUpgradeFetcherCore.fetchDistUpgraderc                 C   sD   | j g| j }t ¡ dkrt dddg| ¡ d S t | j |¡ d S )Nr   z/usr/bin/sudoÚsudoz-E)r_   r   rA   ÚgetuidÚexecv)r   Úargsr   r   r   ÚrunDistUpgraderú   s   z&DistUpgradeFetcherCore.runDistUpgraderc                 C   s   t  d¡ t | j¡ d S )Nz..)rA   r~   ÚshutilÚrmtreer@   )r   r   r   r   Úcleanup  s   
zDistUpgradeFetcherCore.cleanupc              
   C   s@  |   ¡ sd S |  ¡ s|  tdƒtdƒ¡ d S |  ¡ s*|  tdƒtdƒ¡ |  ¡  d S |  ¡ s:|  tdƒtdƒ¡ d S |  ¡ sN|  tdƒtdƒ¡ |  ¡  d S zt 	| j
tj¡sctd	| j
 ƒ}d
|_|‚|  ¡  W dS  tyŸ } z*|jd
kr‡|  tdƒtdƒ¡ W Y d }~dS |  tdƒtdƒ|j ¡ W Y d }~dS d }~ww )NzFailed to fetchz=Fetching the upgrade failed. There may be a network problem. zAuthentication failedz_Authenticating the upgrade failed. There may be a problem with the network or with the server. zFailed to extractz[Extracting the upgrade failed. There may be a problem with the network or with the server. zVerification failedz[Verifying the upgrade failed.  There may be a problem with the network or with the server. zCan not execute '%s'é   zCan not run the upgradezyThis usually is caused by a system where /tmp is mounted noexec. Please remount without noexec and run the upgrade again.FzThe error message is '%s'.T)r9   rŒ   r-   r.   rH   r”   r]   r`   rA   Úaccessr_   ÚX_OKÚOSErrorÚerrnor‘   Ústrerror)r   Úexr\   r   r   r   r„     sV   
ÿ
ÿ
ÿ
ÿ
ö

ÿ
ÿ€özDistUpgradeFetcherCore.runr   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__rs   ri   rA   Úenvironr   r   r    r9   r-   rH   rE   r]   r`   rq   r'   rŒ   r‘   r”   r„   r   r   r   r   r   *   s(    

ÿ'r   Ú__main__zgot mirror: '%s'zghttp://archive.ubuntu.com/ubuntu/dists/intrepid-proposed/main/dist-upgrader-all/0.93.34/intrepid.tar.gzr   )Ústringr   rA   r€   rX   rS   r|   r’   r(   r   rJ   r   r.   Úaptsources.sourceslistr   Úurllib.requestr   Úurllib.errorr   Úutilsr   r	   r
   ÚDistUpgradeViewTextr   Úobjectr   rœ   Údr0   rq   r   r   r   r   Ú<module>   s8     
ý
ÿý