o
    ïØ{fÅI  ã                   @   sZ  d dl mZmZ d dlZe dde¡ d dlZd dlZd dlZd dl	Z	zd dl
mZ d dlmZ d dlmZ W n eyN   d dlmZmZ d dlmZ Y nw zd d	lmZ W n eyf   d d	lmZ Y nw d dlZd dlZd dlZd dlZd d
lmZ zd dlmZ W n ey�   dZY nw dZdZ e d Z!G dd„ de"ƒZ#G dd„ dej$j%ƒZ%dS )é    )Úabsolute_importÚprint_functionNÚignorezapt API not stable yet)Ú	HTTPError)Úurlopen)Úurlsplit)r   r   )ÚBadStatusLine)Úgettext)Ú	Launchpadz/var/lib/synaptic/preferencesz.https://changelogs.ubuntu.com/changelogs/pool/z%s/%s/%s/%s_%s/%sc                   @   s   e Zd ZdZdS )ÚHttpsChangelogsUnsupportedErrorz¶ https 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__© r   r   ú</usr/lib/python3/dist-packages/UpdateManager/Core/MyCache.pyr   9   s    r   c                   @   sÌ   e Zd ZdZd.dd„Zdd„ Zdd„ Zd	d
„ Zedd„ ƒZ	edd„ ƒZ
dd„ Ze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 )0ÚMyCacheÚUbuntuNc                    s
  t j | ||¡ || _t| jƒdksJ ‚|  ¡ rJ ‚|  ¡  i | _i | _	i | _
| jjdkr1|  ¡  | jjdkr=| jjdks?J ‚d | _tj d¡}|r}t dd dd„ |D ƒ¡ d ¡| _tjdd	gd
d� ¡ ‰ t dd ‡ fdd„|D ƒ¡ d ¡| _d S d | _d | _d S )Nr   zAPT::VersionedKernelPackagesú(ú|c                 S   s   g | ]}d | ‘qS )ú^r   ©Ú.0Úpr   r   r   Ú
<listcomp>`   s    z$MyCache.__init__.<locals>.<listcomp>ú)Úunamez-rT)Úuniversal_newlinesc                    s.   g | ]}|  d ¡sd| d  ˆ  nˆ | ‘qS )z.*r   )Ú
startswithr   ©Úrunning_kernel_versionr   r   r   d   s    ÿÿÿ)Ú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Úrunning_kernel_pkgs_regexp)ÚselfÚprogressr$   Úapt_versioned_kernel_pkgsr   r   r   r#   E   sH   ÿ
ÿÿÿÿ

þÿý
zMyCache.__init__c                 C   s>   t j tj d¡¡d }t  |¡D ]}t d|¡r dS qdS )z`
        test if the dpkg journal is dirty
        (similar to debSystem::CheckUpdates)
        zDir::State::statusz/updatesz[0-9]+TF)	ÚosÚpathÚdirnamer1   r2   Ú	find_fileÚlistdirr4   Úmatch)r<   ÚdÚfr   r   r   r'   k   s   
ÿÿÿzMyCache._dpkgJournalDirtyc                 C   s0   | j  ¡  tj t¡r| j  t¡ | j  ¡  d S ©N)r,   Úread_pinfiler?   r@   ÚexistsÚSYNAPTIC_PINFILEÚinit©r<   r   r   r   r(   w   s   
zMyCache._initDepCachec                 C   s   |   ¡  d S rG   )r(   rL   r   r   r   Úclear€   s   zMyCache.clearc                 C   s,   t  | j¡}t  ¡ }| || j| j¡ |jS )z< get the size of the packages that are required to download )r1   ÚPackageManagerr,   ÚAcquireÚget_archivesÚ_listÚ_recordsÚfetch_needed)r<   ÚpmÚfetcherr   r   r   Úrequired_downloadƒ   s   zMyCache.required_downloadc                 C   ó   | j jS rG   )r,   Ú
inst_countrL   r   r   r   Úinstall_count‹   ó   zMyCache.install_countc                 C   rW   rG   )r,   Ú
keep_countrL   r   r   r   r[   �   s   zMyCache.keep_countc                 C   rW   rG   )r,   r/   rL   r   r   r   r/   ’   rZ   zMyCache.del_countc                 C   sR   |D ]$}|sqd}|D ]}|j |jjkst |j|j|j¡s d}q|r& dS qdS )z<Return True if any of the dependencies in deps match target.TF)ÚnameÚpackageÚ	shortnamer1   Ú	check_depÚversionÚrelation)r<   ÚtargetÚdepsÚdep_orrD   Úbase_depr   r   r   Ú_check_dependencies–   s    ÿÿ€ÿzMyCache._check_dependenciesc                 C   sl   |j }|sdS | D ]*}|j}|d ur3|  || d¡¡r3|  || d¡¡r3t d|jj|jf ¡  dS q	dS )NFÚ	ConflictsÚReplacesz*%s Conflicts/Replaces %s; allowing removalT)Ú	installedÚ	candidaterf   Úget_dependenciesÚloggingÚinfor]   r^   )r<   Úpkgrb   Úcpkgrj   r   r   r   Úfind_removal_justification¦   s*   
ÿ
ÿþ
ÿÿ€z"MyCache.find_removal_justificationc                 C   s¢   | j  d¡ | j j}|dkr+dd„ | D ƒ}|t|ƒksJ ‚|D ]}|  |¡r*|d8 }q|dkrB|  ¡  | j jdkr?| j jdksAJ ‚n| j jdksJJ ‚| j  ¡  |S )z@ this functions mimics a upgrade but will never remove anything Tr   c                 S   s   g | ]}|j r|‘qS r   )Úmarked_delete)r   rn   r   r   r   r   ½   s    z+MyCache.saveDistUpgrade.<locals>.<listcomp>é   )r,   Úupgrader/   r%   rp   rM   r-   )r<   ÚwouldDeleteÚdeleted_pkgsrn   r   r   r   r.   ·   s    
€
zMyCache.saveDistUpgradec                 C   s,   |  d¡}t|ƒdkrd |dd… ¡}|S )z strip of the epoch ú:rr   Ú N)Úsplitr%   r6   )r<   ÚverstrÚvers_no_epochr   r   r   Ú_strip_epochÌ   s   
zMyCache._strip_epochFc                 C   s�  | | }|j j}d}|jj |j¡j}|j j}	|  |	¡}
| 	d¡}t
|ƒdkr+|d }|d }| d¡r:d|d  }|r?|}n
t|||||
|f }t|ƒ}|jdkrY|jrYtdƒ‚t|ƒ}d	}d
t |¡ }	 | ¡  dd¡}|d	krv	 |S t ||¡}|rÃt|jddƒ}|r“d|v r“| 	dd¡d }| d¡}|r¦d|v r¦| 	dd¡d }|r¶|rµt ||¡dk rµ	 |S n|rÃt ||¡dkrÃ	 |S || }qg)z* helper that fetches the file in question Úmainú/rr   r   Úlibé   ÚhttpszKhttps locations with username/password are notsupported to fetch changelogsrw   z^%s \((.*)\)(.*)$TzUTF-8Úreplacer`   Nrv   )rj   Úsource_nameÚ_pcacher,   Úget_candidate_verÚ_pkgÚsectionÚsource_versionr{   rx   r%   r   ÚCHANGELOGS_URIr   ÚschemeÚusernamer   r   r4   ÚescapeÚreadlineÚdecoderD   Úgetattrri   Úgroupr1   Úversion_compare)r<   r\   ÚfnameÚstrict_versioningÚchangelogs_urirn   ÚsrcpkgÚsrc_sectionr†   Úsrcver_epochÚsrcverÚsplit_sectionÚprefixÚuriÚresÚ	changelogÚalllinesÚregexpÚlinerD   ri   Úchangelogverr   r   r   Ú_get_changelog_or_newsÓ   st   



ÿÿä
ÿÿ€ûÿÿÿázMyCache._get_changelog_or_newsc           	      C   sð   t s	t d¡ dS | | j}|jD ]!}t|ƒjdkrqt d|¡}|dur2| 	d¡| 	d¡}} nqt 
d¡ dS | jdu rHt jdd	d
d�| _| jjjd||f d�}|du r_t 
d¡ dS |j|jd|jd�}|srt 
d¡ dS |d  ¡ S )zbReturn the changelog URI from the Launchpad API

        Return None in case of an error.
        z9Launchpadlib not available, cannot retrieve PPA changelogNzppa.launchpad.netzhttp.*/(.*)/(.*)/ubuntu/.*rr   é   z)Unable to find a valid PPA candidate URL.zupdate-managerÚ
productionÚdevel)r`   z~%s/ubuntu/%s)Ú	referencez6Unable to retrieve the archive from the Launchpad API.T)r‚   Úexact_matchr`   z;No published sources were retrieved from the Launchpad API.r   )r
   rl   Úwarningrj   Úurisr   Úhostnamer4   Úsearchr�   Úerrorr0   Úlogin_anonymouslyÚarchivesÚgetByReferenceÚgetPublishedSourcesr‚   r‡   ÚchangelogUrl)	r<   r\   Úcdtrš   rD   ÚuserÚppaÚarchiveÚspphsr   r   r   Ú_extract_ppa_changelog_uri*  sB   


þ

þ
ÿ
þ
z"MyCache._extract_ppa_changelog_uric                 C   sˆ   | | }|j j}|d u rd S |j j d¡}|sd S | d¡d  ¡ }d|v r1| d¡d  d¡}n|j j}| d¡d }|d||f  S )NÚSourcer   r   rr   r   r}   z/%s_%s.changelog)	rj   rš   ÚrecordÚgetrx   Ústripr:   r‡   Ú
rpartition)r<   r\   rn   Údeb_uriÚsrcrecr”   r—   Úbase_urir   r   r   Ú+_guess_third_party_changelogs_uri_by_sourceX  s   z3MyCache._guess_third_party_changelogs_uri_by_sourcec                 C   s,   | | }|j j}|rd| dd¡d  S dS )z` guess changelogs uri based on ArchiveURI by replacing .deb
            with .changelog
        z%s.changelogÚ.rr   r   N)rj   rš   Úrsplit)r<   r\   rn   r¼   r   r   r   Ú+_guess_third_party_changelogs_uri_by_binaryi  s
   z3MyCache._guess_third_party_changelogs_uri_by_binaryc                 C   s8   |   |¡ |  |¡ z| ¡  W d S  ty   Y d S w rG   )Úget_newsÚget_changelogÚreleaseÚ	Exception)r<   r\   Úlockr   r   r   Úget_news_and_changelogu  s   

ÿzMyCache.get_news_and_changelogc                 C   s>   z	|   |dd¡}W n
 ty   Y dS w |r|| j|< dS dS )z7 get the NEWS.Debian file from the changelogs location zNEWS.DebianTN)r¡   rÆ   r*   )r<   r\   Únewsr   r   r   rÃ   }  s   ÿÿzMyCache.get_newsc           
      C   sú   d }|D ]}|j  d¡r#z|  |¡}W  n ty"   t d¡ Y qw q|  |¡}|  |¡}d}|||fD ]<}|rqz|  |dd|¡}	| j	|  |	7  < W q5 t
tfy[   tdƒ}Y q5 tttjfyp   t d¡ tdƒ}Y q5w q5| j	|  |7  < d S )	NzLP-PPA-z'Unable to connect to the Launchpad API.rw   rœ   FzAThis update does not come from a source that supports changelogs.zerror on changelog fetchingúOFailed to download the list of changes. 
Please check your Internet connection.)Úoriginr   r¶   rÆ   rl   Ú	exceptionrÂ   r¿   r¡   r)   r   r   Ú_ÚIOErrorr   Úsocketr«   )
r<   r\   ÚoriginsÚchangelogs_uri_pparË   Úchangelogs_uri_binaryÚchangelogs_uri_sourceÚerror_messager“   rœ   r   r   r   Ú(_fetch_changelog_for_third_party_package†  sJ   
ÿü	ÿÿþÿÿ
ÿýöz0MyCache._fetch_changelog_for_third_party_packagec                 C   s    |D ]\}}}}|| j |< qd S rG   )r+   )r<   Úpro_pkgsr\   r`   Ú_ar   r   r   Úcreate_pro_cache°  s   ÿzMyCache.create_pro_cachec              
   C   s.  | | j j}| j || | j j¡}tdƒ|t| | jddƒ|f | j|< | j	dd„ |D ƒvr6|  
||¡ dS | | j j}| | j j dd¡}z|  |d¡}t|ƒd	kr[td
ƒ||f }W n/ tym   tdƒ||f }Y n tttjfy‹ } ztd|ƒ tdƒ}W Y d}~nd}~ww | j|  |7  < dS )z4 get the changelog file from the changelog location zFChanges for %s versions:
Installed version: %s
Available version: %s

r`   Nc                 S   s   g | ]}|j ‘qS r   )rË   )r   Úor   r   r   r   ¼  s    z)MyCache.get_changelog.<locals>.<listcomp>rv   z%3Arœ   r   z¬The 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.z¡The 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Ê   )rj   rÐ   r+   r¹   r`   rÍ   rŽ   ri   r)   ÚCHANGELOG_ORIGINrÕ   r‚   r‡   r�   r¡   r%   r   rÎ   r   rÏ   r«   Úprint)r<   r\   rÐ   r`   r”   r–   rœ   Úer   r   r   rÄ   ´  s6   
ýû€ü
€þzMyCache.get_changelogrG   )FN)r   r   r   rÚ   r#   r'   r(   rM   ÚpropertyrV   rY   r[   r/   rf   rp   r.   r{   r¡   r¶   r¿   rÂ   rÈ   rÃ   rÕ   rØ   rÄ   r   r   r   r   r   A   s8    
&	



ÿW.	*r   )&Ú
__future__r   r   ÚwarningsÚfilterwarningsÚFutureWarningr!   r1   rl   r?   Úurllib.errorr   Úurllib.requestr   Úurllib.parser   ÚImportErrorÚurllib2ÚurlparseÚhttp.clientr   ÚhttplibrÏ   r8   r4   ÚDistUpgrade.DistUpgradeCacheÚDistUpgrader	   rÍ   Úlaunchpadlib.launchpadr
   rJ   ÚCHANGELOGS_POOLrˆ   rÆ   r   ÚDistUpgradeCacher   r   r   r   r   Ú<module>   sF   þÿÿ