U
    ٪zc-                     @   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
m
Z d dlmZ ddlmZmZmZ G dd deZed	kreddZed
edd  dS )    )TemplateN)gettext)SourcesList   )get_disturl_downloadablecountry_mirrorc                   @   s   e Zd ZdZdZdZdejk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mainZDEBUG_UPDATE_MANAGERc                 C   s   || _ t | _|| _g | _d S )N)new_distr   current_dist_name	_progressrun_options)selfr   Zprogress r   D/usr/lib/python3/dist-packages/DistUpgrade/DistUpgradeFetcherCore.py__init__-   s    zDistUpgradeFetcherCore.__init__c                 C   s   | j rtj|d  dS )z" helper to show debug information 
N)DEBUGsysstderrwrite)r   msgr   r   r   _debug4   s    zDistUpgradeFetcherCore._debugc                 C   s   dS )NTr   r   r   r   r   showReleaseNotes9   s    z'DistUpgradeFetcherCore.showReleaseNotesc                 C   s   t | t | dS )z dummy implementation for error display, should be overwriten
            by subclasses that want to more fancy method
        F)print)r   Zsummarymessager   r   r   error<   s    zDistUpgradeFetcherCore.errorc                 C   sv   | j jrr| jd tj| j j }| jd tj| j j }ttdtj|tj|d  | 	||rrdS dS )N/z0authenticate '%(file)s' against '%(signature)s' )file	signatureTF)
r   upgradeToolSigtmpdirospathbasenameupgradeToolr   _gpgauthenticate)r   fZsigr   r   r   authenticateD   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	--keyringZverify)r   r   )
subprocesscallPIPE)r   r!   r"   ZkeyringZgpgZretr   r   r   r*   P   s    z&DistUpgradeFetcherCore.gpgauthenticatec              
   C   s   t j| jt j| j}ttdt j|  t j|sBdS z@t	
| jd t j| j d}|D ]}|| qh|  W n8 t	jk
r } ztd|  W Y dS d }~X Y nX dS )Nzextracting '%s'Fr    rzfailed to open tarfile (%s)T)r%   r&   joinr$   r'   urir   r)   existstarfileopenextractcloseZ	ReadErrorloggingr   )r   fnameZtarZtarinfoer   r   r   extractDistUpgrader`   s"    z*DistUpgradeFetcherCore.extractDistUpgraderc                 C   sH   d| j | jjf  | _}tj|sD| tdtdd td S dS )Nz%s/%szCould not run the upgrade toolz.  zThis 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scriptr%   r&   r3   r   r)   )r   r=   r   r   r   verifyDistUpraderq   s    

z(DistUpgradeFetcherCore.verifyDistUpraderc                 C   s   |  d| j  tdd}t }|jD ]}|js&|js&|jdksBq&|j|krNq&|j	|rz|j
| jkrz| j|jkrz|  S |j
| jkr&d|jkr&|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)ZwithMatcherZdebr   Nzno mirror found )r   r   r   setlistZdisabledZinvalidtyper2   
startswithZdistDEFAULT_COMPONENTcompslenr   add)r   r2   Zdefault_uriZsourcesseenr:   Z
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s`td| W n< tk
r } z| d|  |jdd}W 5 d}~X Y nX |S )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)Zcountrymirrorzfailed to download %sz url '%s' could not be downloadedr?   N)	rC   DEFAULT_MIRRORr   rI   r   r   Zsafe_substituter   	Exception)r   r2   Znew_uriZuri_templatemr:   r   r   r   
_expandUri   s    
z!DistUpgradeFetcherCore._expandUric                 C   sN  t jdd}|| _t| td|  | jdkrPtj	
dd tj	
dd t| j}| jjdk	r| | jj}tj||td	d
}| | jjdk	rJ| | jj| _tj|| jtdd
}| | }||jkrt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)ZdescrzUpgrade toolzfetch result != continue (%s)Fzfile '%s' missingT)tempfileZmkdtempr$   r%   chdirr8   debugr   apt_pkgZconfigr@   ZAcquirer   r   r#   rM   ZAcquireFiler)   r(   r2   runZRESULT_CONTINUEZwarningr&   r'   r3   getsize)r   r$   Zfetcherr2   Zaf1Zaf2resultr+   r   r   r   fetchDistUpgrader   sD    


 
z(DistUpgradeFetcherCore.fetchDistUpgraderc                 C   sB   | j g| j }t dkr0tdddg|  nt| j | d S )Nr   z/usr/bin/sudoZsudoz-E)r=   r   r%   getuidexecv)r   argsr   r   r   runDistUpgrader   s    z&DistUpgradeFetcherCore.runDistUpgraderc                 C   s   t d t| j d S )Nz..)r%   rQ   shutilZrmtreer$   r   r   r   r   cleanup   s    
zDistUpgradeFetcherCore.cleanupc              
   C   s>  |   sd S |  s,| tdtd d S |  sT| tdtd |   d S |  st| tdtd d S |  s| tdtd |   d S z4t	| j
tjstd	| j
 }d
|_||   W nh tk
r8 } zH|jd
kr| tdtd W Y "dS | tdtd|j  W 5 d }~X Y nX dS )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)r   rW   r   r)   r,   r]   r;   r>   r%   accessr=   X_OKOSErrorerrnor[   strerror)r   exr:   r   r   r   rT      sP    






zDistUpgradeFetcherCore.run)N)__name__
__module____qualname____doc__rJ   rD   r%   environr   r   r   r   r   r,   r*   r;   r>   rI   rM   rW   r[   r]   rT   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   r%   rS   r8   r4   rP   r\   r   r-   r   r)   Zaptsources.sourceslistr   Zutilsr   r   r   objectr	   re   dr   rI   r   r   r   r   <module>   s*    v
