o
    {fI                     @   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||}|rt|jdd}|rd|v r|	ddd }|d}|rd|v r|	ddd }|r|rt||dk r	 |S n|rt||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 fetchingOFailed 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   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   )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   