
    9qfOJ                     >   d dl Z  e j                  dde       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Zd dlZd dlZd dlZd dlmZ 	 d d	lmZ d
ZdZedz   Z G d de      Z G d dej<                  j>                        Zy# e$ r dZY =w xY w)    Nignorezapt API not stable yet)	HTTPError)urlopen)urlsplit)BadStatusLine)gettext)	Launchpadz/var/lib/synaptic/preferencesz.https://changelogs.ubuntu.com/changelogs/pool/z%s/%s/%s/%s_%s/%sc                       e Zd ZdZy)HttpsChangelogsUnsupportedErrorzhttps changelogs with credentials are unsupported because of the
    lack of certitifcation validation in urllib2 which allows MITM
    attacks to steal the credentials
    N)__name__
__module____qualname____doc__     </usr/lib/python3/dist-packages/UpdateManager/Core/MyCache.pyr   r   3   s    
 	r   r   c                       e Zd ZdZddZd Zd Zd Zed        Z	ed        Z
d	 Ze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)MyCacheUbuntuNc                    t         j                  j                  | ||       || _        t	        | j
                        dk(  sJ | j                         rJ | j                          i | _        i | _	        i | _
        | j                  j                  dkD  r| j                          | j                  j                  dk(  r| j                  j                  dk(  sJ d | _        t         j"                  j%                  d      }|rt'        j(                  ddj+                  |D cg c]  }d|z   	 c}      z   dz         | _        t/        j0                  ddgd	
      j3                         }t'        j(                  ddj+                  |D cg c]#  }|j5                  d      sd|z   dz   |z   n||z   % c}      z   dz         | _        y d | _        d | _        y c c}w c c}w )Nr   zAPT::VersionedKernelPackages(|^)unamez-rT)universal_newlinesz.*)aptCache__init__rootdirlenreq_reinstall_pkgs_dpkgJournalDirty_initDepCacheall_changesall_newspro_versions	_depcachebroken_countsaveDistUpgrade	del_count	launchpadapt_pkgconfig
value_listrecompilejoinversioned_kernel_pkgs_regexp
subprocesscheck_outputrstrip
startswithrunning_kernel_pkgs_regexp)selfprogressr    apt_versioned_kernel_pkgsprunning_kernel_versions         r   r   zMyCache.__init__@   s   		473 4**+q000 ))+++>>&&*  "NN''1,1I1IQ1N	
N$+NN$=$=*%
! %02

((-FGC!GGHI1D-
 &0%<%<$D&fh # /1jj((
 ";	   !||D1 q4*@@4q8: 	/D+ 15D-.2D+) Hs   G(Gc                     t         j                  j                  t        j                  j                  d            dz   }t        j                  |      D ]  }t        j                  d|      s y y)z`
        test if the dpkg journal is dirty
        (similar to debSystem::CheckUpdates)
        zDir::State::statusz/updatesz[0-9]+TF)	ospathdirnamer-   r.   	find_filelistdirr0   match)r9   dfs      r   r#   zMyCache._dpkgJournalDirtyt   s`     GGOOGNN445IJK 	
 A 	Axx!$	 r   c                     | j                   j                          t        j                  j	                  t
              r| j                   j                  t
               | j                   j                          y N)r(   read_pinfiler?   r@   existsSYNAPTIC_PINFILEinitr9   s    r   r$   zMyCache._initDepCache   sH     	##%77>>*+NN''(89r   c                 $    | j                          y rH   )r$   rM   s    r   clearzMyCache.clear   s    r   c                     t        j                  | j                        }t        j                         }|j	                  || j
                  | j                         |j                  S )z:get the size of the packages that are required to download)r-   PackageManagerr(   Acquireget_archives_list_recordsfetch_needed)r9   pmfetchers      r   required_downloadzMyCache.required_download   sH     ##DNN3//#
T]];###r   c                 .    | j                   j                  S rH   )r(   
inst_countrM   s    r   install_countzMyCache.install_count   s    ~~(((r   c                 .    | j                   j                  S rH   )r(   
keep_countrM   s    r   r^   zMyCache.keep_count   s    ~~(((r   c                 .    | j                   j                  S rH   )r(   r+   rM   s    r   r+   zMyCache.del_count   s    ~~'''r   c                     |D ]m  }|sd}|D ]]  }|j                   |j                  j                  k7  s6t        j                  |j
                  |j                  |j
                        r\d}_ |sm y y)z<Return True if any of the dependencies in deps match target.TF)namepackage	shortnamer-   	check_depversionrelation)r9   targetdepsdep_orrD   base_deps         r   _check_dependencieszMyCache._check_dependencies   s|      	FE" "MMV^^%=%==",,(9(98;K;K "E" 	 r   c                 J   |j                   }|sy| D ]  }|j                  }|| j                  ||j                  d            s4| j                  ||j                  d            sVt	        j
                  |j                  j                  d|j                  d        y y)NF	ConflictsReplacesz Conflicts/Replaces z; allowing removalT)	installed	candidaterk   get_dependencieslogginginforb   rc   )r9   pkgrg   cpkgrp   s        r   find_removal_justificationz"MyCache.find_removal_justification   s     	 DI$++I66{C..I66zB LL$,,66G  	  r   c                    | j                   j                  d       | j                   j                  }|dkD  rJ| D cg c]  }|j                  s| }}|t	        |      k(  sJ |D ]  }| j                  |      s|dz  } |dkD  rD| j                          | j                   j                  dk(  r| j                   j                  dk(  sJ | j                   j                  dk(  sJ | j                   j                          |S c c}w )z>this functions mimics a upgrade but will never remove anythingTr      )r(   upgrader+   marked_deleter!   rv   rO   r)   )r9   wouldDeletert   deleted_pkgss       r   r*   zMyCache.saveDistUpgrade   s     	t$nn..?+/EC33D3DCELE#l"3333# %22371$K% ?JJL++q0NN,,12 >>..!333  Fs   DDc                 l    |j                  d      }t        |      dkD  rdj                  |dd       }|S )zstrip of the epoch:rx    N)splitr!   r2   )r9   verstrvers_no_epochs      r   _strip_epochzMyCache._strip_epoch   s7    S)}!WW]12./Fr   c                    | |   }|j                   j                  }d}|j                  j                  j	                  |j
                        j                  }|j                   j                  }	| j                  |	      }
|j                  d      }t        |      dkD  r|d   }|d   }|j                  d      rd|d   z   }|r|}nt        |||||
|fz  }t        |      }|j                  dk(  r|j                  rt!        d      t#        |      }d	}d
t%        j&                  |      z  }	 |j)                         j+                  dd      }|d	k(  r	 |S t%        j,                  ||      }|rt/        |j0                  dd      }|rd|v r|j                  dd      d   }|j3                  d      }|rd|v r|j                  dd      d   }|r|r:t5        j6                  ||      dk  r!	 |S |rt5        j6                  ||      dk(  r	 |S ||z   })z(helper that fetches the file in questionmain/rx   r   lib   httpszKhttps locations with username/password are notsupported to fetch changelogsr   z^%s \((.*)\)(.*)$zUTF-8replacere   Nr~   )rp   source_name_pcacher(   get_candidate_ver_pkgsectionsource_versionr   r   r!   r7   CHANGELOGS_URIr   schemeusernamer   r   r0   escapereadlinedecoderD   getattrro   groupr-   version_compare)r9   ra   fnamestrict_versioningchangelogs_urirt   srcpkgsrc_sectionr   srcver_epochsrcversplit_sectionprefixurires	changelogalllinesregexplinerD   ro   changelogvers                         r   _get_changelog_or_newszMyCache._get_changelog_or_news   s@   
 4j ** ++''99#((CKK }}33""<0c*}!'*K U#VAY&F
  C $ C sm:: S\\10  CL	 &"))F*;<%%'..w	BDrzB A HHVT*E $CMM9dC		!1 )Q 7 :I${{1~C<$7#/#5#5c1#=a#@L %!#33L)L   "#33L)L   $HG r   c                    t         st        j                  d       y| |   j                  }|j                  D ]V  }t        |      j                  dvrt        j                  d|      }|4|j                  d      |j                  d      }} n t        j                  d       y| j                  t        j                  dd	d
      | _        | j                  j                  j                  d|d|      }|t        j                  d       y|j                  |j                   d|j"                        }|st        j                  d       y|d   j%                         S )zbReturn the changelog URI from the Launchpad API

        Return None in case of an error.
        z9Launchpadlib not available, cannot retrieve PPA changelogN)zppa.launchpad.netzppa.launchpadcontent.netzhttp.*/(.*)/(.*)/ubuntu/.*rx      z)Unable to find a valid PPA candidate URL.zupdate-manager
productiondevel)re   ~z/ubuntu/)	referencez6Unable to retrieve the archive from the Launchpad API.T)r   exact_matchre   z;No published sources were retrieved from the Launchpad API.r   )r	   rr   warningrp   urisr   hostnamer0   searchr   errorr,   login_anonymouslyarchivesgetByReferencegetPublishedSourcesr   r   changelogUrl)	r9   ra   cdtr   rD   userppaarchivespphss	            r   _extract_ppa_changelog_uriz"MyCache._extract_ppa_changelog_uriH  s]   
 OOK 4j""88 	C}%% .  II:C@E !KKNEKKNc	 MMEF >>!&88 ,DN ..))88)-s3 9 
 ?MMH ++&& , 

 MM! Qx$$&&r   c                    | |   }|j                   j                  }|y |j                   j                  j                  d      }|sy |j	                  d      d   j                         }d|v r$|j	                  d      d   j                  d      }n|j                   j                  }|j                  d      d   }|d|d|dz   S )	NSourcer   r   rx   r   r   _z
.changelog)	rp   r   recordgetr   stripr6   r   
rpartition)r9   ra   rt   deb_urisrcrecr   r   base_uris           r   +_guess_third_party_changelogs_uri_by_sourcez3MyCache._guess_third_party_changelogs_uri_by_source  s    4j--##?%%))(3c"1%++-&=\\#&q)005F]]11F%%c*1-???r   c                 n    | |   }|j                   j                  }|rd|j                  dd      d   z  S y)z[guess changelogs uri based on ArchiveURI by replacing .deb
        with .changelog
        z%s.changelog.rx   r   N)rp   r   rsplit)r9   ra   rt   r   s       r   +_guess_third_party_changelogs_uri_by_binaryz3MyCache._guess_third_party_changelogs_uri_by_binary  s=     4j--##!GNN3$:1$===r   c                     | j                  |       | j                  |       	 |j                          y # t        $ r Y y w xY wrH   )get_newsget_changelogrelease	Exception)r9   ra   locks      r   get_news_and_changelogzMyCache.get_news_and_changelog  s<    d4 	LLN 		s   5 	A Ac                 n    	 | j                  |dd      }|r|| j                  |<   yy# t        $ r Y yw xY w)z5get the NEWS.Debian file from the changelogs locationzNEWS.DebianTN)r   r   r&   )r9   ra   newss      r   r   zMyCache.get_news  sG    	..t]DID "&DMM$   		s   ( 	44c                 b   d }|D ]1  }|j                   j                  d      s	 | j                  |      } n | j                  |      }| j                  |      }d}|||fD ]1  }|s	 | j                  |dd|      }	| j                  |xx   |	z  cc<   3 | j                  |xx   |z  cc<   y # t        $ r t	        j
                  d       Y w xY w# t        t        f$ r t        d      }Y t        t        t        j                   f$ r# t	        j
                  d       t        d      }Y w xY w)	NzLP-PPA-z'Unable to connect to the Launchpad API.r   r   FzAThis update does not come from a source that supports changelogs.zerror on changelog fetchingOFailed to download the list of changes. 
Please check your Internet connection.)originr7   r   r   rr   	exceptionr   r   r   r%   r   r   r   IOErrorr   socketr   )
r9   ra   originschangelogs_uri_ppar   changelogs_uri_binarychangelogs_uri_sourceerror_messager   r   s
             r   (_fetch_changelog_for_third_party_packagez0MyCache._fetch_changelog_for_third_party_package  s\   ! 	F}}''	2)-)H)H)N&		 <<TB 	 <<TB 	 !!
 	N
  $ ; ;k5.!I $$T*i7*	0 	-/I ! %%A. "#BC $%;%M  = %%&CD$%A%Ms)   B.)+C.CCD..=D.-D.c                 :    |D ]  \  }}}}|| j                   |<    y rH   )r'   )r9   pro_pkgsra   re   _as        r   create_pro_cachezMyCache.create_pro_cache  s*    '/ 	.#T7B&-Dd#	.r   c                 P   | |   j                   j                  }| j                  j                  || |   j                   j                        }t        d      |t        | |   j                  dd      |fz  | j                  |<   | j                  |D cg c]  }|j                   c}vr| j                  ||       y| |   j                   j                  }| |   j                   j                  j                  dd      }	 | j                  |d      }t!        |      dk(  rt        d      ||fz  }| j                  |xx   |z  cc<   yc c}w # t"        $ r t        d	      ||fz  }Y 8t$        t&        t(        j*                  f$ r!}t-        d
|       t        d      }Y d}~qd}~ww xY w)z2get the changelog file from the changelog locationzFChanges for %s versions:
Installed version: %s
Available version: %s

re   Nr~   z%3Ar   r   zThe changelog does not contain any relevant changes.

Please use http://launchpad.net/ubuntu/+source/%s/%s/+changelog
until the changes become available or try again later.zThe list of changes is not available yet.

Please use http://launchpad.net/ubuntu/+source/%s/%s/+changelog
until the changes become available or try again later.zcaught exception: r   )rp   r   r'   r   re   r   r   ro   r%   CHANGELOG_ORIGINr   r   r   r   r   r   r!   r   r   r   r   r   print)	r9   ra   r   re   or   r   r   es	            r   r   zMyCache.get_changelog  s   t*&&..##''d4j.B.B.J.JK!"("

 DJ(()T:
	"
   7(Ca(CC99$Hd%%11Dz++::BB3N	33D+FI9~"M
 \*+	& 	)+9 )D  	'I
 &'I 5 	&*9I	s$   E?0E F%'F%F  F%rH   )FN)r   r   r   r   r   r#   r$   rO   propertyrY   r\   r^   r+   rk   rv   r*   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   <   s    23h $ $ ) )) ( (&&. DHaF5'n@"
',0d.),r   r   ) warningsfilterwarningsFutureWarningr   r-   rr   r?   urllib.errorr   urllib.requestr   urllib.parser   http.clientr   r   r4   r0   DistUpgrade.DistUpgradeCacheDistUpgrader   r   launchpadlib.launchpadr	   ImportErrorrK   CHANGELOGS_POOLr   r   r   DistUpgradeCacher   r   r   r   <module>r      s   .    ":M J 
   	 " " ! %   	 #  0 3 B #66	i 	P,k**22 P,#  Is   B BB