
    vKg1                        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e      Zedk(  r% edd      Z edej=                  dd      z         yy)    )TemplateN)gettext)SourcesList)urlopen)	HTTPError   )get_disturl_downloadablecountry_mirror)readlinec                       e Zd ZdZdZdZdej                  v Zd Z	d Z
d Zd Zd	 Z	 ddZd Zd Zd Zd Zd Zd Zd Zd Zy
)DistUpgradeFetcherCorez2 base class (without GUI) for the upgrade fetcher  http://archive.ubuntu.com/ubuntumainDEBUG_UPDATE_MANAGERc                 L    || _         t               | _        || _        g | _        y N)new_distr	   current_dist_name	_progressrun_options)selfr   progresss      D/usr/lib/python3/dist-packages/DistUpgrade/DistUpgradeFetcherCore.py__init__zDistUpgradeFetcherCore.__init__1   s"     !)!    c                 b    | j                   r#t        j                  j                  |dz          yy)z" helper to show debug information 
N)DEBUGsysstderrwrite)r   msgs     r   _debugzDistUpgradeFetcherCore._debug8   s$    ::JJS4Z( r   c                    d| j                   v ry| j                  j                  | j                  | j                  j                        }t	        j
                         }	 t	        j                  d       t        |      }|j                         j                  dd      }t	        j                  |       t                t               t        t        d      d       t               }|j!                         j#                         j%                  t        d            ryy# t        $ r' | j                  t        d      t        d             Y yt        $ r' | j                  t        d	      t        d
             Y yw xY w)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ress         r   showReleaseNotesz'DistUpgradeFetcherCore.showReleaseNotes=   s4   59I9II==((4//$--"?"?@C..0G((+ '%**,33GYG $$W-ea !r*j99;))!C&1  

1FG<=? 

1JKEFHs    A D" "-F ,F ?F c                 0    t        |       t        |       y)z minimal implementation for error display, should be overwriten
            by subclasses that want to more fancy method
        F)r5   )r   summarymessages      r   r2   zDistUpgradeFetcherCore.errorX   s     	ggr   c                 
   | j                   j                  r| j                  dz   t        j                  j                  | j                   j                        z   }| j                  dz   t        j                  j                  | j                   j                        z   }t        t        d      t        j                  j                  |      t        j                  j                  |      dz         | j                  ||      ryy)N/z0authenticate '%(file)s' against '%(signature)s' )file	signatureTF)
r   upgradeToolSigtmpdirospathbasenameupgradeToolr5   r3   gpgauthenticate)r   fsigs      r   authenticatez#DistUpgradeFetcherCore.authenticate`   s    ==''c!BGG$4$4T]]5N5N$OOA++#bgg&6&6,,'. .C!FG((+WW--c2K4 4 5 ##As+r   Nc                     dg}|sd}|d|gz  }|d||gz  }t        j                  |t         j                        }|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   rD   rE   keyringgpgrets         r   rL   z&DistUpgradeFetcherCore.gpgauthenticatel   sP    
 kFGW%%)T**ooc*//:axr   c                    t         j                  j                  | j                  t         j                  j	                  | j
                              }t        t        d      t         j                  j	                  |      z         t         j                  j                  |      sy	 t        j                  | j                  dz   t         j                  j	                  | j
                        z   d      }|D ]  }|j                  |        |j                          y# t        j                  $ r"}t        j                  d|z         Y d }~yd }~ww xY w)Nzextracting '%s'FrC   rzfailed to open tarfile (%s)T)rH   rI   joinrG   rJ   r9   r5   r3   existstarfileopenextractclose	ReadErrorloggingr2   )r   fnametartarinfoes        r   extractDistUpgraderz*DistUpgradeFetcherCore.extractDistUpgrader|   s    T[["''*:*:488*DEa!"RWW%5%5e%<<=ww~~e$	,,t{{S0!ww//9 :;>@C %G$%IIK     	MM7!;<	s   'A5D E0EEc                     | j                   d| j                  j                  x| _        }t        j
                  j                  |      s3| j                  t        d      t        d      dz   t        d      z         S y)NrC   z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)	rG   r   namescriptrH   rI   r[   r2   r3   )r   ri   s     r   verifyDistUpraderz(DistUpgradeFetcherCore.verifyDistUprader   sv    
 +/++t}}7I7IJJfww~~f%::a @A @AEI !A BBC C r   c                    | j                  d| j                  z         t        d      }t               }|j                  D ]  }|j
                  s|j                  s|j                  dk(  s,|j                  |v r;|j                  j                  |      r5|j                  | j                  k(  r| j                  |j                  v r|c S |j                  | j                  k(  sd|j                  v s|j                  |t        |      d z   }t        || j                         r|c S |j                  |j                          | j                  d       y)	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typer9   r8   distDEFAULT_COMPONENTcompslenr
   add)r   r9   default_urisourcesseenre   
mirror_uris          r   mirror_from_sources_listz/DistUpgradeFetcherCore.mirror_from_sources_list   s    	2T5K5KKL%0u 	 AzzQYYaffouu}  -FFd444**agg5
&&D222v7HUUS[)9):%;;
#J<%%	   	%&r   c                    |j                  | j                        r1| j                  d       | j                  || j                        }|r|S t	        |      }t               }|j                  |      }	 t        || j                        st        d|z        	 |S # t        $ r1}| j                  d|z         |j                  d      }Y d}~|S d}~ww xY 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)	r8   DEFAULT_MIRRORr$   r|   r   r   safe_substituter
   	Exception)r   r9   new_uriuri_templatemre   s         r   r,   z!DistUpgradeFetcherCore._expandUri   s     >>$--.KK8933C9L9LMG}..Q.?	E#GT[[9 7' ABB : 	  	EKK:Q>?"222DG		Es   5$B 	C%&CCc                 
   t        j                  d      }|| _        t        j                  |       t        j                  d|z         | j                  dkD  r@t        j                  j                  dd       t        j                  j                  dd       t        j                  | j                        }| j                  j                  H| j                  | j                  j                        }t        j                   ||t#        d      	      }| | j                  j$                  V| j                  | j                  j$                        | _        t        j                   || j&                  t#        d
      	      }| |j)                         }||j*                  k7  rt        j,                  d|z         yt        j.                  j1                  | j                  j                        t        j.                  j1                  | j                  j$                        fD ]]  }t        j.                  j3                  |      r#t        j.                  j5                  |      dkD  rEt        j,                  d|z          y yy)z. download the tarball with the upgrade script zubuntu-release-upgrader-)prefixzusing tmpdir: '%s'r   zDebug::Acquire::http1zDebug::Acquire::ftpzUpgrade tool signature)descrzUpgrade toolzfetch result != continue (%s)Fzfile '%s' missingT)tempfilemkdtemprG   rH   chdirra   debugr   apt_pkgconfigrn   Acquirer   r   rF   r,   AcquireFiler3   rK   r9   runRESULT_CONTINUEwarningrI   rJ   r[   getsize)r   rG   fetcherr9   af1af2resultrM   s           r   fetchDistUpgraderz(DistUpgradeFetcherCore.fetchDistUpgrader   s   !!)CD
*V34::>NN5s;NN4c://$..1==''3//$--">">?C%%g&),-.F,GIC ==$$0t}}'@'@ADH%%g&*hh,-n,=?C [[]F000 ?& HIgg&&t}}'C'CDgg&&t}}'@'@AC !q)bggooa.@1.DOO$7!$;< 	!
 r   c                     | j                   g| j                  z   }t        j                         dk7  rt        j                  dddg|z          y t        j                  | j                   |       y )Nr   z/usr/bin/sudosudoz-E)ri   r   rH   getuidexecv)r   argss     r   runDistUpgraderz&DistUpgradeFetcherCore.runDistUpgrader   sO    }t///99;!HH_vtnt&;<HHT[[$'r   c                 l    t        j                  d       t        j                  | j                         y )Nz..)rH   r   shutilrmtreerG   )r   s    r   cleanupzDistUpgradeFetcherCore.cleanup  s    
dkk"r   c                    | j                         sy | j                         s%| j                  t        d      t        d             y | j	                         s5| j                  t        d      t        d             | j                          y | j                         s%| j                  t        d      t        d             y | j                         s5| j                  t        d      t        d             | j                          y 	 t        j                  | j                  t        j                        s!t        d	| j                  z        }d
|_        || j                          y# t        $ rs}|j                  d
k(  r)| j                  t        d      t        d             Y d }~y| j                  t        d      t        d      |j                  z         Y d }~yd }~ww xY 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)r>   r   r2   r3   rO   r   rf   rj   rH   accessri   X_OKOSErrorerrnor   strerror)r   exre   s      r   r   zDistUpgradeFetcherCore.run  s   $$&%%'JJq*+ % &'   "JJq01 I JK LLN'')JJq,- I JK %%'JJq./ I JK LLN	I 99T[["''23dkkAB  "   		Iww"}

167 A BC 

1679:QZZGI I		Is   AE' '	G#03G(1GG#r   )__name__
__module____qualname____doc__r   rt   rH   environr   r   r$   r>   r2   rO   rL   rf   rj   r|   r,   r   r   r   r    r   r   r   r   *   sj    87N"bjj0E)
6
 !% "<2%N(#-r   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   rH   r   ra   r\   r   r   r-   r    rR   r   r3   aptsources.sourceslistr   urllib.requestr   urllib.errorr   utilsr	   r
   r   DistUpgradeViewTextr   objectr   r   dr5   r|   r   r   r   <module>r      s   .  	       
    . " " = = )JV JZ ztT*A	


$
$:022 3 r   