o
    td1                     @   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.  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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|rtj|dkstd|   dS qd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     

