U
    2•@h‡ ã                   @   s*  d 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
Z
ddlmZ ddlZe dde¡ ddlZz4ddlZddlmZmZmZ eeeef eZeZW nH ek
rú   ddlmZmZ ddlmZ dd	lmZmZ ddlZY nX ddlZdd
lm Z  G dd„ de ƒZ!e!ƒ Z"dS )zyapport.PackageInfo class implementation for python-apt and dpkg.

This is used on Debian and derivatives such as Ubuntu.
é    N)ÚclosingÚignorezapt API not stable yet)ÚurlopenÚquoteÚunquote)ÚURLErrorÚ	HTTPError)r   )r   r   )ÚPackageInfoc                   @   s   e Zd Z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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_d%d&„Zd'd(„ Zd)d*„ Zd+d,„ Zd-d.„ Zd/d0„ Zd1d2„ Zd`d4d5„Zed6d7„ ƒZd8d9„ Zd:d;„ Z dad<d=„Z!d>d?„ Z"d@dA„ Z#dbdCdD„Z$dEdF„ Z%edGdH„ ƒZ&dIdJ„ Z'edKdL„ ƒZ(dMdN„ Z)dOdP„ Z*dQdR„ Z+edSdT„ ƒZ,edUdV„ ƒZ-edWdX„ ƒZ.dYdZ„ Z/d3Z0d[d\„ Z1d3Z2d]d^„ Z3d3S )cÚ__AptDpkgPackageInfoz€Concrete apport.PackageInfo class implementation for python-apt and
    dpkg, as found on Debian and derivatives such as Ubuntu.c                 C   sF   d | _ d | _d | _d | _d | _d | _d | _d| _| jd | _d| _	d S )Nzhttps://api.launchpad.net/develz*/~%(user)s/+archive/%(distro)s/%(ppaname)sF)
Ú
_apt_cacheÚ_sandbox_apt_cacheÚ_sandbox_apt_cache_archÚ_contents_dirÚ_mirrorÚ_virtual_mapping_objÚ_contents_mapping_objZ_launchpad_baseÚ_ppa_archive_urlÚ_contents_update©Úself© r   ú7/usr/lib/python3/dist-packages/apport/packaging_impl.pyÚ__init__-   s    z__AptDpkgPackageInfo.__init__c                 C   s0   z| j rt | j ¡ W n tk
r*   Y nX d S )N)r   ÚshutilÚrmtreeÚAttributeErrorr   r   r   r   Ú__del__9   s
    z__AptDpkgPackageInfo.__del__c              	   C   sZ   | j d k	r| j S tj |d¡}tj |¡rNt|dƒ}t |¡| _ W 5 Q R X ni | _ | j S )Núvirtual_mapping.pickleÚrb)r   ÚosÚpathÚjoinÚexistsÚopenÚpickleÚload©r   Ú	configdirÚmapping_fileÚfpr   r   r   Ú_virtual_mapping@   s    
z%__AptDpkgPackageInfo._virtual_mappingc              	   C   s@   t j |d¡}| jd k	r<t|dƒ}t | j|¡ W 5 Q R X d S )Nr   Úwb)r   r    r!   r   r#   r$   Údumpr&   r   r   r   Ú_save_virtual_mappingM   s    
z*__AptDpkgPackageInfo._save_virtual_mappingc              	   C   s¦   | j r(| j d |kr(| j d |kr(| j S tj |d||f ¡}tj |¡rdt |¡jdkrdt |¡ tj |¡r”t|dƒ}t	 
|¡| _ W 5 Q R X n||dœ| _ | j S )NÚreleaseÚarchúcontents_mapping-%s-%s.pickler   r   )r.   r/   )r   r   r    r!   r"   ÚstatÚst_sizeÚremover#   r$   r%   ©r   r'   r.   r/   r(   r)   r   r   r   Ú_contents_mappingS   s     ÿþ
ÿ
z&__AptDpkgPackageInfo._contents_mappingc              	   C   sb   t j |d||f ¡}| jd k	r^z(t|dƒ}t | j|¡ W 5 Q R X W n tk
r\   Y nX d S )Nr0   r+   )r   r    r!   r   r#   r$   r,   ÚMemoryErrorr4   r   r   r   Ú_save_contents_mappinge   s    
ÿ
z+__AptDpkgPackageInfo._save_contents_mappingc                 C   sV   d| _ | jsPz tjj ¡ }tj|dd| _W n" tk
rN   tjdd| _Y nX | jS )z(Return apt.Cache() (initialized lazily).Nú/©Úrootdir)r   r   ÚaptÚprogressÚbaseZ
OpProgressÚCacher   )r   r<   r   r   r   Ú_cacheq   s    z__AptDpkgPackageInfo._cachec           
   
   C   s¨   d| _ | jr|| jkr˜|  |||||¡ tj |¡}tj|d| _|| _z| j 	|¡ W n2 tj
jk
rŠ }	 ztt|	ƒƒ‚W 5 d}	~	X Y nX | j ¡  n
| j ¡  | jS )z‰Build apt sandbox and return apt.Cache(rootdir=) (initialized lazily).

        Clear the package selection on subsequent calls.
        Nr9   )r   r   r   Ú_build_apt_sandboxr   r    Úabspathr;   r>   ÚupdateÚcacheÚFetchFailedExceptionÚSystemErrorÚstrr#   Úclear)
r   ÚaptrootÚapt_sourcesÚfetchProgressÚdistro_nameÚrelease_codenameÚoriginsr/   r:   Úer   r   r   Ú_sandbox_cache   s     
 ÿ
z#__AptDpkgPackageInfo._sandbox_cachec                 C   s4   z|   ¡ | W S  tk
r.   td| ƒ‚Y nX dS )zuReturn apt.Cache()[package] (initialized lazily).

        Throw a ValueError if the package does not exist.
        úpackage %s does not existN)r?   ÚKeyErrorÚ
ValueError©r   Úpackager   r   r   Ú_apt_pkg–   s    z__AptDpkgPackageInfo._apt_pkgc                 C   s&   |   |¡}|j}|s td| ƒ‚|jS )z*Return the installed version of a package.rP   )rU   Ú	installedrR   Úversion)r   rT   ÚpkgÚinstr   r   r   Úget_version    s
    
z __AptDpkgPackageInfo.get_versionc                 C   s   |   |¡jjS )z1Return the latest available version of a package.)rU   Ú	candidaterW   rS   r   r   r   Úget_available_version©   s    z*__AptDpkgPackageInfo.get_available_versionc                 C   sJ   |   |¡jj}|sg S dd„ |j dg ¡|j dg ¡ |j dg ¡ D ƒS )z/Return a list of packages a package depends on.c                 S   s   g | ]}|d  j j‘qS ©r   )Z
target_pkgÚname)Ú.0Údr   r   r   Ú
<listcomp>µ   s     z9__AptDpkgPackageInfo.get_dependencies.<locals>.<listcomp>ZDependsZ
PreDependsZ
Recommends)rU   Z_pkgZcurrent_verZdepends_listÚget)r   rT   Zcur_verr   r   r   Úget_dependencies®   s    ÿþÿz%__AptDpkgPackageInfo.get_dependenciesc                 C   sD   |   |¡jr|   |¡jjS |   |¡jr4|   |¡jjS td| ƒ‚dS )z-Return the source package name for a package.rP   N)rU   rV   Úsource_namer[   rR   rS   r   r   r   Ú
get_sourceº   s
    z__AptDpkgPackageInfo.get_sourcec                 C   s8   |   |¡j}|stdƒ‚|jD ]}|jr|j  S qdS )zÊReturn package origin.

        Return the repository name from which a package was installed, or None
        if it cannot be determined.

        Throw ValueError if package is not installed.
        zpackage is not installedN)rU   rV   rR   rM   Úorigin)r   rT   rX   rf   r   r   r   Úget_package_originÄ   s    
z'__AptDpkgPackageInfo.get_package_originc                 C   sŒ   |   |¡}|jr |jjdkr dS |  ¡ d }|jrX|jjrX|jjD ]}|j|krB dS qBtdd„ |jjD ƒƒtdgƒkrˆtj	 
d¡rˆdS dS )	zŸCheck if a package is a genuine distro package.

        Return True for a native distro package, False if it comes from a
        third-party source.
        NFr   Tc                 S   s   g | ]
}|j ‘qS r   )rf   )r_   Úor   r   r   ra   ç   s     z:__AptDpkgPackageInfo.is_distro_package.<locals>.<listcomp>Ú z/etc/system-image/channel.ini)rU   rV   rW   Úget_os_versionr[   rM   rf   Úsetr   r    r"   )r   rT   rX   rK   rh   r   r   r   Úis_distro_packageÔ   s    

 
ÿz&__AptDpkgPackageInfo.is_distro_packagec              
   C   s²   |   |¡}|jr |jjdkr dS g }t d¡D ]R}z8t|ƒ&}|D ]}| ¡ }|rB| |¡ qBW 5 Q R X W q. tk
r~   Y q.X q.|jr®|jj	r®|jj	D ]}|j
|kr˜ dS q˜dS )zÔCheck if a package originated from a native location

        Return True for a package which came from an origin which is listed in
        native-origins.d, False if it comes from a third-party source.
        NFz/etc/apport/native-origins.d/*T)rU   rV   rW   Úglobr#   ÚstripÚappendÚIOErrorr[   rM   rf   )r   rT   rX   Znative_originsÚfÚfdÚlinerh   r   r   r   Úis_native_origin_packageí   s$    


z-__AptDpkgPackageInfo.is_native_origin_packagec                 C   sô   t jj dd¡ ddlm} |jdddd}|jd	 }|j| 	¡ d
 d}	|	j
|d}
|jdd}|j|||
ddd}|s~dS d}|D ]D}|jdkr–q†|js®|jdd} qÌq†|j |¡r†|jdd} qÌq†|sÔdS |D ]}|d |d f  S d S )NzAPT::Get::AllowUnauthenticatedÚTruer   ©Ú	Launchpadúapport-retraceÚ
productionÚdevel©rW   Úubuntuéÿÿÿÿ)Zname_or_version)ZarchtagÚprimary©r^   FT)Zbinary_namerW   Zdistro_arch_seriesZorderedÚexact_match)NNri   ÚDeleted)Zinclude_metaÚurlZsha1)r;   Úapt_pkgÚconfigrk   Úlaunchpadlib.launchpadrw   Úlogin_anonymouslyÚdistributionsZ	getSeriesÚsplitZgetDistroArchSeriesÚ
getArchiveZgetPublishedBinariesÚstatusZarchitecture_specificZbinaryFileUrlsZdistro_arch_series_linkÚendswith)r   Ú	distro_idr.   rT   rW   r/   rw   Ú	launchpadr|   ZseriesZdasr~   ZbpphZbf_urlsZbpZbfr   r   r   Úget_lp_binary_package	  s<    ÿ
ý
z*__AptDpkgPackageInfo.get_lp_binary_packageFc              	   C   sÀ   z(t |ƒ}| ¡ dkr&td| ¡  ƒ‚W n, ttfk
rT   t dt|ƒ ¡ Y dS X z| ¡ }W n( tk
rŠ   t dt|ƒ ¡ Y dS X t	|t
ƒr | d¡}|r²t |¡d S t |¡S dS )zËOpen, read and parse the json of a url

        Set entries to True when the json data returned by Launchpad
        has a dictionary with an entries key which contains the data
        desired.
        é  ú%uzcannot connect to: %sNzfailure reading data at: %szutf-8Úentries)r   Úgetcoder   r   ÚapportÚwarningr   Úreadrp   Ú
isinstanceÚbytesÚdecodeÚjsonÚloads)r   r‚   r‘   ÚresponseZcontentr   r   r   Újson_request+  s"    

z!__AptDpkgPackageInfo.json_requestc                 C   s”   ddl m} |jdddd}|jd }|jdd	}|j||d
d}|sJd S d}	|D ]}
|
jdkrbqR|
 ¡ }	 qpqR|	sxd S g }|	D ]}| |¡ q€|S )Nr   rv   rx   ry   rz   r{   r|   r~   r   T)rd   rW   r€   ri   r   )	r…   rw   r†   r‡   r‰   ZgetPublishedSourcesrŠ   ZsourceFileUrlsro   )r   rŒ   rT   rW   rw   r   r|   r~   ZpssZsfusZpsZsource_filesZsfur   r   r   Úget_lp_source_packageE  s.    ÿ
ÿ
z*__AptDpkgPackageInfo.get_lp_source_packagec                 C   sL   |   |¡jr|   |¡jjpdS |   |¡jr<|   |¡jjp:dS td| ƒ‚dS )z–Return the architecture of a package.

        This might differ on multiarch architectures (e. g. an i386 Firefox
        package on a x86_64 system)ÚunknownrP   N)rU   rV   Úarchitecturer[   rR   rS   r   r   r   Úget_architecture]  s
    z%__AptDpkgPackageInfo.get_architecturec                 C   s,   |   d|g¡}|dkrdS dd„ | ¡ D ƒS )z*Return list of files shipped by a package.z-LNc                 S   s   g | ]}|  d ¡s|‘qS )Zdiverted)Ú
startswith)r_   rq   r   r   r   ra   p  s     
 z2__AptDpkgPackageInfo.get_files.<locals>.<listcomp>)Ú
_call_dpkgÚ
splitlines)r   rT   Úlistr   r   r   Ú	get_filesj  s    z__AptDpkgPackageInfo.get_filesc           
   
   C   sh  d||   ¡ f }tj |¡s$d| }z,t |¡}t |j¡s@t‚t|j	|j
ƒ}W n tk
rh   g  Y S X d}d||   ¡ f }tj |¡s¢d| }tj |¡s¢g S t|dƒž}|D ]’}znd|krÐt d|¡ W q²| ¡ }	|	sìt d	|¡ W q²t d
|	d  d¡  d¡¡}t|j	|j
ƒ|kr"W q²W n tk
r:   Y nX ||7 }q²W 5 Q R X |r`|  |¡S g S dS )z/Return list of all modified files of a package.z/var/lib/dpkg/info/%s:%s.listz/var/lib/dpkg/info/%s.listó    z /var/lib/dpkg/info/%s:%s.md5sumsz/var/lib/dpkg/info/%s.md5sumsr   ó    z(%s contains NUL character, ignoring linez%%s contains empty line, ignoring liner8   r}   úUTF-8N)Úget_system_architecturer   r    r"   r1   ÚS_ISREGÚst_modeÚOSErrorÚmaxÚst_mtimeÚst_ctimer#   r“   r”   rˆ   r˜   ÚencodeÚ_check_files_md5)
r   rT   ZlistfileÚsZmax_timeZsumsÚsumfilerr   rs   Zwordsr   r   r   Úget_modified_filesr  sF    


z'__AptDpkgPackageInfo.get_modified_filesc                 C   s  t jdddd|gt jd}| ¡ d  ¡ }|jdkr8i S i }| ¡ D ]¸}|sNqD| ¡  ¡ dd… \}}t	j
 |¡rôzLt|d	ƒ}| ¡ }	W 5 Q R X t ¡ }
|
 |	¡ |
 ¡ }||kr¼|	||< W qü tk
rð } zd
t|ƒ ||< W 5 d}~X Y qüX qDd||< qD|S )a?  Return modified configuration files of a package.

        Return a file name -> file contents map of all configuration files of
        package. Please note that apport.hookutils.attach_conffiles() is the
        official user-facing API for this, which will ask for confirmation and
        allows filtering.
        z
dpkg-queryz-Wz--showformat=${Conffiles}ú--©Ústdoutr   Né   r   z[inaccessible: %s]z	[deleted])Ú
subprocessÚPopenÚPIPEÚcommunicater˜   Ú
returncoder£   rn   rˆ   r   r    r"   r#   r•   ÚhashlibZmd5rB   Z	hexdigestrp   rF   )r   rT   ÚdpkgÚoutÚmodifiedrs   r    Zdefault_md5sumrr   ÚcontentsÚmZcalculated_md5sumrN   r   r   r   Úget_modified_conffiles¢  s6    
 ÿÿ

$
z+__AptDpkgPackageInfo.get_modified_conffilesc                 C   s|   d}d}d}|sx|t |ƒk rxtjdddd|g|||| …  tjtjtjd}| ¡ d  d	¡}|jdkrn|}||7 }q|S )
zsCall fgrep for a pattern on given file list and return the first
        matching file, or None if no file matches.Néd   r   Zfgrepz-lxmÚ1rµ   )Ústdinr·   Ústderrr¨   )Úlenr¹   rº   r»   r¼   r˜   r½   )r   ÚpatternZ	file_listÚmatchZ
slice_sizeÚiÚprÀ   r   r   r   Z__fgrep_filesÊ  s     ÿ þ

z"__AptDpkgPackageInfo.__fgrep_filesNc                 C   st  |r|   ||||¡S tjdd|gtjtjd}| ¡ d  d¡}|jdkrf|rf| ¡ d }|dkrf|S tj	 
tj	 |¡¡d  ¡ }	g }
g }t d¡D ]P}tj	 
tj	 |¡¡d  ¡  d	¡d }||	ksÎ|	|krÚ| |¡ q”|
 |¡ q”|  ||¡}|s|  ||
¡}|sH| d
¡rH|dd… }|  d| |¡}|sH|  d| |
¡}|rptj	 
tj	 |¡¡d  d	¡d S dS )ap  Return the package a file belongs to.

        Return None if the file is not shipped by any package.

        If uninstalled is True, this will also find files of uninstalled
        packages; this is very expensive, though, and needs network access and
        lots of CPU and I/O resources. In this case, map_cachedir can be set to
        an existing directory which will be used to permanently store the
        downloaded maps. If it is not set, a temporary directory will be used.
        Also, release and arch can be set to a foreign release/architecture
        instead of the one from the current system.
        zdpkg-divertz--list©r·   rÈ   r   r¨   r}   zhardening-wrapperz/var/lib/dpkg/info/*.listú:z/usré   Nú%s)Ú_search_contentsr¹   rº   r»   r¼   r˜   r½   rˆ   r   r    ÚsplitextÚbasenameÚlowerrm   ro   Ú _AptDpkgPackageInfo__fgrep_filesr¡   )r   ÚfileZuninstalledÚmap_cachedirr.   r/   r¿   rÀ   rX   ÚfnameZ	all_listsZlikely_listsrq   rÍ   rË   r   r   r   Úget_file_packageÝ  s<     ÿ&"z%__AptDpkgPackageInfo.get_file_packagec                 C   sB   t jddgt jd}| ¡ d  ¡  ¡ }|jdks6t‚|s>t‚|S )zcReturn the architecture of the system, in the notation used by the
        particular distribution.r¿   z--print-architecturer¶   r   ©r¹   rº   r»   r¼   r˜   rn   r½   ÚAssertionError)Úklassr¿   r/   r   r   r   r©     s    
ÿz,__AptDpkgPackageInfo.get_system_architecturec                 C   s>   t jddgt jd}| ¡ d  ¡  ¡ }|jdks6t‚d| S )zØReturn a list of default library search paths.

        The entries should be separated with a colon ':', like for
        $LD_LIBRARY_PATH. This needs to take any multiarch directories into
        account.
        zdpkg-architecturez-qDEB_HOST_MULTIARCHr¶   r   z/lib/%s:/librÛ   )r   r¿   Zmultiarch_tripler   r   r   Úget_library_paths  s    
ÿz&__AptDpkgPackageInfo.get_library_pathsc                 C   s<   || _ z| jr t | j¡ d| _W n tk
r6   Y nX dS )zãExplicitly set a distribution mirror URL for operations that need to
        fetch distribution files/packages from the network.

        By default, the mirror will be read from the system configuration
        files.
        N)r   r   r   r   r   )r   r‚   r   r   r   Ú
set_mirror-  s    
z__AptDpkgPackageInfo.set_mirrorc                 C   s&  t j ¡ }|r:t ¡ }| d|  ¡ ¡ | ¡  |j|d< |rRt	j
dddg|d dddd|g}|rx|d	  d
| 7  < zt	j
|||ddkr–|sœW dS |  |  ¡ ||¡}	|	rd}
tjj d¡dkrètjj d¡}
tjj dd¡ tjj ¡ }tj |¡}g }|	D ]}| tjj|||d¡ q| ¡ }||jkrBW dS |
rXtjj d|
¡ t t j |d¡¡D ] }t	j
ddd|gt	j|d qlnW dS W n tk
r°   Y dS X d}t t j ||d ¡¡D ]}t j |¡rÎ|}qÎ|söt dƒ‚zt	j
dd|d W n tk
r    Y nX |S )a:  Download source package and unpack it into dir.

        This also has to care about applying patches etc., so that dir will
        eventually contain the actually compiled source. dir needs to exist and
        should be empty.

        If version is given, this particular version will be retrieved.
        Otherwise this will fetch the latest available version.

        If sandbox is given, it calls apt-get source in that sandbox, otherwise
        it uses the system apt configuration.

        If apt_update is True, it will call apt-get update before apt-get
        source. This is mostly necessary for freshly created sandboxes.

        Return the directory that contains the actual source root directory
        (which might be a subdirectory of dir). Return None if the source is
        not available.
        zODir "%s";
Dir::State::Status "/var/lib/dpkg/status";
Debug::NoLocking "true";
 Z
APT_CONFIGzapt-getz-qqrB   )Úenvz--assume-yesÚsourcer}   ú=)Úcwdrà   r   Nri   zAcquire::http::Proxy)Údestdirz*.dsczdpkg-sourcez-snú-x)r·   rã   z-*z.could not determine source tree root directoryzª(debian/rules patch || debian/rules apply-patches || debian/rules apply-dpatches || debian/rules unpack || debian/rules patch-stamp || debian/rules setup) >/dev/null 2>&1T)Úshellrã   )!r   ÚenvironÚcopyÚtempfileZNamedTemporaryFileÚwriter°   Úflushr^   r¹   Úcallr   Úget_distro_namer;   rƒ   r„   Úfindrk   r<   r=   ÚAcquireProgressÚAcquirero   ÚAcquireFileÚrunZRESULT_CONTINUErm   r    r!   r»   r¬   ÚisdirrÜ   )r   Z
srcpackageÚdirrW   ZsandboxZ
apt_updaterà   rq   ÚargvZsf_urlsÚproxyrJ   ÚfetcherZaf_queueZsfÚresultZdscÚrootr`   r   r   r   Úget_source_tree>  s~    
ý


 ÿ ÿ ÿþ
 ý
z$__AptDpkgPackageInfo.get_source_treec                 C   s   dt  ¡ d  S )z„Return the actual Linux kernel package name.

        This is used when the user reports a bug against the "linux" package.
        zlinux-image-r¸   )r   Úunamer   r   r   r   Úget_kernel_package–  s    z'__AptDpkgPackageInfo.get_kernel_packagec                 C   s  g }g }|d   ¡ d }|d }|d   ¡ d }d| }|  ¡ }||krZ|| jrZ||fS tjj d¡d }	d|||f }
d	|
 }ttj	 
|	|
¡d
ƒ}t|ƒ}| ¡ dkr®dS | d¡}|s¾qÊ| |¡ q®| ¡  | ¡  t ddtj	 
|	|
¡g¡}|dkr| |
  d¡d ¡ ||fS )z¿Install kernel debug package

        Ideally this would be just another package but the kernel is
        special in various ways currently so we can not use the apt
        method.
        ZUnameé   ZArchitectureZPackagezlinux-image-debug-%súDir::Cache::archivesz/partialz%s_%s_%s.ddebz,http://ddebs.ubuntu.com/pool/main/l/linux/%sÚwr   )ri   Zlinuxi    r¿   z-ir   Ú_)rˆ   r?   ZisInstalledr;   rƒ   r„   Úfind_dirr#   r   r    r!   r   r’   r•   rê   rë   Úcloser¹   rì   ro   )r   ÚreportrV   ZoutdatedZkverr/   ÚverZdebug_pkgnameÚcZ
target_dirÚdebr‚   rÀ   ÚuÚblockZretr   r   r   Ú_install_debug_kernelž  s4    

z*__AptDpkgPackageInfo._install_debug_kernelTc           =         sÔ  |s|   ¡ }|s d}|  ¡ | _n¶tj ||d¡}||   ¡ kr^tj |||d¡}tj |¡r^|}z|  |  |¡¡ W n4 t	k
r¦ } zt
 dt|ƒ ¡ W 5 d}~X Y nX ttj ||d¡ƒ}| ¡  ¡ | _W 5 Q R X tj |¡sît	d| ƒ‚|rVd}||   ¡ kr|}nd}|r*tj |||d	¡}ntj |d
|d	¡}tj |¡sbt |¡ nd}t ¡ }tjj d|¡ tjj dd¡ tjj dd¡ tjj dd¡ |r¶tjj ¡ }ntjj ¡ }|sæ|  ||||  ¡ | j|	|¡‰ nx|  |||  ¡ | j|	¡ tjtj  |¡d‰ zˆ  !|¡ W n4 tj"j#k
rT } zt	t|ƒƒ‚W 5 d}~X Y nX ˆ  ¡  tjj $d¡}d}tj %¡ }tj |d¡}i }tj |¡ràt|ƒ8}|D ],}| ¡ }|s¾q¨| &¡ \}}|||< q¨W 5 Q R X tƒ }i }tj '|¡}g }|rNg } |D ]8\}!}"zˆ |! }#W nB t(k
r`   d|! )dd¡ }$||$d 7 }t
 |$¡ Y qY nX |#j*j+D ]Ô}%ˆ |%d j, j*j-}&|%d j,dd„ |D ƒkržqj|%d j,|krú||%d j, }'|  .|'|&¡dkrä|  /|%d j,|'f¡ n|  /|%d j,|&f¡ n|  /|%d j,|&f¡ |%d j,dd„ |D ƒkrj| /|%d j,df¡ qjq| 0| ¡ |D ]²\}!}"zˆ |! }#W nB t(k
rª   d|! )dd¡ }$||$d 7 }t
 |$¡ Y qRY nX z|"rÀ|#j1|" |#_*W nt t(k
r6   |  2|  ¡ ||!|"|¡\}(})|(r| /tjj3||(d|) |d¡ |"||!< n|d |!|"|#j*j-f 7 }Y nX |#j*}*| 4|!¡ |rB|  5|¡}+|*j6D ]}|+ 7|tƒ ¡ 4|!¡ q^g },d!|*j8kr |,tj 9|*j8d! ¡7 },d"|*j8krÂ|,tj 9|*j8d" ¡7 },|,D ]x}-|-d d |*j:j,kræqÆ|-d }-ˆ  ;|-d ¡rÂz|+|-d  }.W n t(k
r*   Y qÆY nX |.D ]Œ}||*j:j,krFq0tj |d#| ¡}/t< <|/¡D ]4}0|  =|0¡}"tj >|"|-d$ |-d% ¡rbt ?|0¡ qbz
||= W n t(k
r¸   Y nX q0~.n|tj |d#|-d  ¡}/t< <|/¡D ]Z}0|  =|0¡}"tj >|"|-d$ |-d% ¡rât ?|0¡ z||-d = W n t(k
r8   Y nX qâqÆ|*j@d&krR|
rRzì|!d' }1ˆ |1 }2d}3|"rìz|2j1|" |2_*d}3W nf t(k
rê   |  2|  ¡ ||1|"|¡\}(})|(râ| /tjj3||(d|) |d¡ |"||1< d}3nt(‚Y nX |3s2z|2j1|*j- |2_*W n, t(k
r0   |d(|!|"|2j*j-f 7 }Y nX | 4|1¡ W qR t(k
r   | A¡  | B|*jC¡r¬‡ fd)d„|jDD ƒ}4|"r°|4D ]"}2ˆ |2 j*j-|"kr†|4 E|2¡ q†ng }4|4	rž|4D ]Þ}||krÌqºd}3|"	rTzˆ | j1|" ˆ | _*d}3W n` t(k
	rR   |  2|  ¡ |||"|¡\}(})|(	rN| /tjj3||(d|) |d¡ |"||< d}3Y nX |3	sŒzˆ | j1|*j- ˆ | _*W n t(k
	rŠ   Y nX | 4|¡ qºn`d}3|!d* }5zÚˆ |5 }6| 4|5¡ |"
r:z|6j1|" |6_*d}3W n` t(k

r8   |  2|  ¡ ||5|"|¡\}(})|(
r4| /tjj3||(d|) |d¡ |"||5< d}3Y nX |3
s‚z|6j1|*j- |6_*W n. t(k

r€   |d+|!|*j-|6j*j-f 7 }Y nX W nx t(k

rü   |"
ræ|  2|  ¡ ||5|"|¡\}(})|(
ræ| /tjj3||(d|) |d¡ |"||5< d}3|3
sø|d,|! 7 }Y nX Y nX qRtF|ƒ}7| G¡ D ]ä}||7krÆ|7| dkrj| H|¡ˆ | j*j-krX| E|¡ nˆ |  Idd¡ nZ| H|¡|7| krˆ |  Idd¡ n4| H|¡ˆ | j*j-krºˆ |  Idd¡ n
| E|¡ n4| H|¡ˆ | j*j-krðˆ |  Idd¡ n
| E|¡ qtJ J¡ }8zˆ jK|d- W nB tj"j#k
rX } zt
 Ld.t|ƒ¡ tM Nd%¡ W 5 d}~X Y nX |rhtOd/ƒ |jPD ]Æ}9tQ Rd0d1|9jSg¡ T¡ }:|: ¡  &¡ \}}|r¼||ks¼tj U|9jS¡|8kr| H|¡|krÎn6||krè|| |krèntQ Vd2d3|9jS|g¡ |||< tj W|9jS¡ &d4d%¡d };|;|krn| E|;¡ qntX| Y¡ ƒ}<|< Z¡  t|d5ƒ>}|<D ]2}| [|¡ | [d6¡ | [|| ¡ | [d¡ qZW 5 Q R X |r¨t\ ]|¡ |rÀt^d7d6 |¡ ƒ‚|rÐ|  _|¡ |S )8aŸ  Install packages into a sandbox (for apport-retrace).

        In order to work without any special permissions and without touching
        the running system, this should only download and unpack packages into
        the given root directory, not install them into the system.

        configdir points to a directory with by-release configuration files for
        the packaging system; this is completely dependent on the backend
        implementation, the only assumption is that this looks into
        configdir/release/, so that you can use retracing for multiple
        DistroReleases. As a special case, if configdir is None, it uses the
        current system configuration, and "release" is ignored.

        release is the value of the report's 'DistroRelease' field.

        packages is a list of ('packagename', 'version') tuples. If the version
        is None, it should install the most current available version.

        If cache_dir is given, then the downloaded packages will be stored
        there, to speed up subsequent retraces.

        If permanent_rootdir is True, then the sandbox created from the
        downloaded packages will be reused, to speed up subsequent retraces.

        If architecture is given, the sandbox will be created with packages of
        the given architecture (as specified in a report's "Architecture"
        field). If not given it defaults to the host system's architecture.

        If origins is given, the sandbox will be created with apt data sources
        for foreign origins.

        If install_deps is True, then the dependencies of packages will also
        be installed.

        Return a string with outdated packages, or an empty string if all
        packages were installed.

        If something is wrong with the environment (invalid configuration,
        package servers down, etc.), this should raise a SystemError with a
        meaningful error message.
        ú/etc/apt/sources.listúsources.listzcannot determine mirror: %sNZcodenamez%s does not existFri   r;   ÚsystemTzAPT::ArchitecturezAcquire::LanguagesZnonez'Acquire::http::Proxy::api.launchpad.netZDIRECTz#Acquire::http::Proxy::launchpad.netr9   rþ   zpackages.txtz#package %s does not exist, ignoringú%z%%Ú
r   c                 S   s   g | ]}|d  ‘qS r]   r   ©r_   rX   r   r   r   ra   `  s     z9__AptDpkgPackageInfo.install_packages.<locals>.<listcomp>r}   c                 S   s   g | ]}|d  ‘qS r]   r   r  r   r   r   ra   n  s     zsha1:%s)Úhashrä   z,%s version %s required, but %s is available
Z	ConflictsZReplacesz%s_*.debr¸   rý   Úallú-dbgzAoutdated -dbg package for %s: package version %s -dbg version %s
c                    s2   g | ]*}|  d ¡r|ˆ krdˆ | jjkr|‘qS )r  Ztransitional)r‹   r[   Zdescription)r_   rÍ   ©rC   r   r   ra   ñ  s
    
 þz-dbgsymzKoutdated debug symbol package for %s: package version %s dbgsym version %s
z%no debug symbol package found for %s
)r÷   z+Package download error, try again later: %szExtracting downloaded debs...údpkg-debz--showr¿   rå   r   rÿ   ú z*apt fetcher did not fetch these packages: )`r©   Úget_distro_codenameÚcurrent_release_codenamer   r    r!   r"   rß   Ú$_get_primary_mirror_from_apt_sourcesrE   r“   r”   rF   r#   r•   rn   ró   Úmakedirsré   Úmkdtempr;   rƒ   r„   rk   r<   Útextrï   r=   rO   rí   r@   r>   rA   rB   rC   rD   r  ZSourceRecordsrˆ   rð   rQ   Úreplacer[   Zdependenciesr^   rW   Úcompare_versionsro   ÚextendZversionsrŽ   rñ   Úaddr*   ZprovidesÚ
setdefaultÚrecordZparse_dependsrT   Zis_virtual_packagerm   Ú_deb_versionZ	check_depÚunlinkrŸ   ZrestartÚlookuprd   Zbinariesr3   Údictrè   rb   Zmark_installÚtimeZfetch_archivesÚerrorÚsysÚexitÚprintÚitemsr¹   Zcheck_outputZdestfiler˜   ÚgetctimeZ
check_callrÔ   r¤   ÚkeysÚsortrê   r   r   rÜ   r-   )=r   r:   r'   r.   ZpackagesÚverboseZ	cache_dirZpermanent_rootdirrŸ   rM   Zinstall_dbgZinstall_depsrI   Zarch_apt_sourcesrN   rq   Ztmp_aptrootZaptroot_archrH   rJ   Z
archivedirZobsoleteZsrc_recordsZpkg_listZpkg_versionsrs   rÍ   ÚvZ	real_pkgsZlp_cacher÷   Zacquire_queueZdepsrX   r  Z	cache_pkgrÃ   ZdepZdep_pkg_versZinst_versionZlp_urlZsha1sumr[   Zvirtual_mappingZ	conflictsZconflictZ	providersZdebsr    Zdbg_pkgZdbgZ	pkg_foundZdbgsZ
dbgsym_pkgZdbgsymZrequested_pkgsZlast_writtenrÌ   rÀ   Úpkg_nameZpkgsr   r  r   Úinstall_packagesÄ  s®   -
 ÿ$
 ý þ





  þý










  þý
  ÿ

  þý


  þý
  ÿ
  þý

$




ÿ
z%__AptDpkgPackageInfo.install_packagesc                 C   s   t j |  ¡  ¡ |¡S )z2Return known package names which match given glob.)rm   ZfnmatchÚfilterr?   r-  )r   Znameglobr   r   r   Úpackage_name_glob™  s    z&__AptDpkgPackageInfo.package_name_globc                 C   sH   t jdg| t jt jd}| t¡d  d¡}|jdkr<|S tdƒ‚dS )zRCall dpkg with given arguments and return output, or return None on
        error.r¿   rÎ   r   r¨   zpackage does not existN)r¹   rº   r»   r¼   Úinputr˜   r½   rR   )rÝ   Úargsr¿   rÀ   r   r   r   r¢   ¢  s    ÿ
z__AptDpkgPackageInfo._call_dpkgc                 C   sÒ   t j |¡rBtjdd|gtjtjdi d}| ¡ d jddd}nLt|ƒt	ksVt
d	ƒ‚tjddgtjtjtjdi d
}| |¡d jddd}|jdkrœg S g }| ¡ D ]$}| d¡r¨| | dd¡d ¡ q¨|S )zŽInternal function for calling md5sum.

        This is separate from get_modified_files so that it is automatically
        testable.
        z/usr/bin/md5sumz-cr8   )r·   rÈ   rã   rà   r   r¨   r  )Úerrorsz&md5sum list value must be a byte array)rÇ   r·   rÈ   rã   rà   ZFAILEDrÏ   rý   )r   r    r"   r¹   rº   r»   r¼   r˜   Útyper—   rÜ   r½   r£   r‹   ro   Úrsplit)r   r³   rÃ   rÀ   Z
mismatchesrs   r   r   r   r±   ¯  s0      þ
   þ

z%__AptDpkgPackageInfo._check_files_md5c              
   C   sš   t |ƒˆ}|D ]p}| ¡ }t|ƒdkr|d dkr|d  d¡rFd}nd}||  d¡sf||  d¡r||   W  5 Q R £ S qtd	| ƒ‚W 5 Q R X d
S )z?Heuristically determine primary mirror from an apt sources.listé   r   r  rý   ú[r¸   zhttp://zhttps://zEcannot determine default mirror: %s does not contain a valid deb lineN)r#   rˆ   rÉ   r¡   rE   )rÝ   rI   rq   rs   ZfieldsZ
mirror_idxr   r   r   r  Ì  s    
ÿÿz9__AptDpkgPackageInfo._get_primary_mirror_from_apt_sourcesc                 C   s   | j s|  d¡| _ | j S )zReturn the distribution mirror URL.

        If it has not been set yet, it will be read from the system
        configuration.r
  )r   r  r   r   r   r   Ú_get_mirrorà  s    z __AptDpkgPackageInfo._get_mirrorc                 C   s&   t | dƒr| jdk	r| jS tdƒ‚dS )z7Map a DistroRelease: field value to a release code namer  NzBCannot map DistroRelease to a code name without install_packages())Úhasattrr  ÚNotImplementedError)r   r.   r   r   r   Ú_distro_release_to_codenameê  s    z0__AptDpkgPackageInfo._distro_release_to_codenamec               
   C   s  |r
|}n| j st ¡ | _ | j }|dkr0|  ¡ }|dkrB|  ¡ }n
|  |¡}dD ]\}tj |d|||f ¡}d}z t 	|¡}	t
t ¡ |	j ƒ}
W n tk
rª   d}
Y nX |
dks¾|
dkr.d|  ¡ |||f }|
r–zddlm} dd	lm} W n. tk
r"   ddlm} dd	lm} Y nX dd
lm} ||ƒd }||ƒ}| d||ƒd ¡ | ¡ }| dd¡}|r| |d¡}|| |	j¡k}nd}nd}|r.d| _zt|ƒ}W n( tk
rÚ   |dkrÒ‚ nY qPY nX t|dƒ(}| d¡}|süq
|  |¡ qèW 5 Q R X | !¡  tj "|¡s.t#‚|  $|||¡}|rPt%|ƒdkrVd| _| jrPddl&}| d| d¡2}d}|D ] }|d7 }|dkr°|dkr°|dk r°q€| '¡ d }| 'd¡d dkr6| 'd¡d dkrèq€| 'd¡d dkr| 'd¡d dkrq€| '¡ d  'd¡d  'd¡d }n:| 'd¡d dkr€| '¡ d  'd¡d  'd¡d }nq€||kr˜||| krŽq€n|||< n|||< q€W 5 Q R X qP| jrÌ|  (|||¡ d| _t)|t*ƒrà| +¡ }|d d krú|dd… }| ,¡ g}| -d!¡r(| .|d"d…  ,¡ ¡ n²| -d#¡rL| .|d"d…  ,¡ ¡ nŽ| -d$¡rp| .|d"d…  ,¡ ¡ nj| -d%¡r”| .|d"d…  ,¡ ¡ nF| -d&¡r¸| .|d"d…  ,¡ ¡ n"| -d'¡rÚ| .|d"d…  ,¡ ¡ |D ]4}z||  +¡ }|W   S  t/k
r   Y nX qÞdS )(z4Internal function for searching file in Contents.gz.N)z	-proposedri   z	-securityz-updatesz%s%s-Contents-%s.gzFi€Q z%s/dists/%s%s/Contents-%s.gzr   )ÚHTTPConnection)Úurlparse)Údatetimerý   ZHEADr¸   zlast-modifiedz%a, %d %b %Y %H:%M:%S %ZTri   r+   i@B rÑ   r   )ZtrustyZxenialé!   ó   /s   usr)ó   libs   libexecs   libx32ó   binó   sbinó   shares   gamess   BrotherrH  )s   docs   iconss   mans   texlives   gocodes   locales   helpr}   ó   ,)rE  rF  rG  r8   zusr/lib/x86_64-linux-gnu/rÐ   zusr/lib/i386-linux-gnu/zusr/lib/systemd/zusr/lib/udev/zusr/bin/z	usr/sbin/)0r   ré   r  r©   r  r?  r   r    r!   r1   Úintr&  r®   r¬   r<  Zhttplibr@  rA  ÚImportErrorZhttp.clientÚurllib.parserB  ZrequestZgetresponseZ	getheaderÚstrptimeZfromtimestampr   r   rp   r#   r•   rê   r  r"   rÜ   r5   rÉ   Úgziprˆ   r7   r–   r—   r˜   r°   r¡   ro   rQ   ) r   r×   rØ   r.   r/   rô   ZpocketÚmaprB   ÚstZager‚   r@  rA  rB  ZserverZconnÚresZmodified_strrÁ   Úsrcrq   ÚdataZcontents_mappingrN  rÂ   Zline_numrs   r    rT   ÚfilesrX   r   r   r   rÒ   ô  sä    




ÿ

ÿ



ÿÿ""




z%__AptDpkgPackageInfo._search_contentsc              	   C   sÒ  |  d¡rÎ| d¡dd… }d}t|ƒdkr<| d¡ d}d}|t|ƒk r t d|d|… ¡}t d||d… ¡}zPtttj	j
|||d	œ ƒƒ*}	|	 ¡  |	 ¡ d
krºtd|	 ¡  ƒ‚W 5 Q R X W nR ttfk
r   |d7 }|t|ƒkr|r| d¡ d}d}nd}Y q@Y nX q q@|rÎ|rÎd||||f }
d||||f }zFtt|ƒƒ,}	|	 ¡  |	 ¡ d
kr‚td|	 ¡  ƒ‚W 5 Q R X d}W n ttfk
r°   d}Y nX |
| d |
dd…  d S dS )a¤  For an origin from a Launchpad PPA create sources.list content.

        distro is the distribution for which content is being created e.g.
        ubuntu.

        release_codename is the codename of the release for which content is
        being created e.g. trusty.

        Return a string containing content suitable for writing to a sources.list
        file, or None if the origin is not a Launchpad PPA.
        úLP-PPA-ú-r¸   NTrý   ÚppaF©ÚuserÚdistroZppanamer   r   z-deb http://ppa.launchpad.net/%s/%s/%s %s mainz5http://ppa.launchpad.net/%s/%s/%s/dists/%s/main/debugz main/debugri   z
deb-srcr:  r  )r¡   rˆ   rÉ   ro   rF   r!   r   r   r“   Ú	packagingr   r•   r’   r   r   )rÝ   rf   rZ  rL   Z
componentsZtry_ppaÚindexrY  Úppa_namer›   Zppa_lineZ	debug_urlZ	add_debugr   r   r   Úcreate_ppa_source_from_origin†  s^    

ÿÿ


ÿ
ÿ
z2__AptDpkgPackageInfo.create_ppa_source_from_originc              
   C   sr  t j t j |ddd¡¡sšt  t j |ddddd¡¡ t  t j |ddddd¡¡ t  t j |ddd¡¡ t  t j |d	dd
¡¡ t  t j |d	dd¡¡ t j |d	dd¡}t j |¡rÂt |¡ t j |d ¡rät |d |¡ n
t  |¡ t	|ƒ8}t	t j |d	dd¡dƒ}| 
| ¡ ¡ W 5 Q R X W 5 Q R X |rÆd}	i }
|D ]|}|dkrZqFd }t j |d ¡ràt j |d |d ¡}t j |¡sÎd|krÎt j |d | d¡d ¡}t j |¡sàd }nt j |¡sàd }|rt	|ƒ}| ¡ }	W 5 Q R X n|  |||¡}	|	r´t	t j |d	dd|d ¡dƒ}| 
|	¡ W 5 Q R X |	 ¡ D ]\}| d¡rhqTd|krvqT| ¡ d  d¡d }| ¡ d  d¡d }||f|
|< qTnt d| ¡ qFt j t j |¡d¡}t j |¡rt |t j |d	d¡¡ n&t j d¡r*t dt j |d	d¡¡ t j |d	dd¡}t j |¡rTt |¡ t j |d ¡rxt |d |¡ n&t j d ¡r”t d |¡ n
t  |¡ |rn|	rn|
 ¡ D ]º\}\}}tjjt|ƒ|t|ƒd!œ }tj |¡}|sðq²z|d" }W n* tk
r&   t d#| ¡ Y q²Y nX d$d%t j |d&| ¡d'd(d)d*d+|g	}t |¡d,kr²t d-| ¡ q²d S ).NÚvarÚlibr;   ZlistsÚpartialrC   Zarchivesr¿   Úetcz
apt.conf.dzpreferences.dzsources.list.dz.dr  rÿ   ri   rž   z.listzLP-PPArU  Úaú#zppa.launchpad.netrý   r8   r:  rÐ   z-Could not find or create source config for %sztrusted.gpgz/etc/apt/trusted.gpgztrusted.gpg.dz/etc/apt/trusted.gpg.drX  Úsigning_key_fingerprintz/Error: can't find signing_key_fingerprint at %szapt-keyz	--keyringz%s.gpgZadvz--quietz--keyserverzkeyserver.ubuntu.comz
--recv-keyr   zUnable to import key for %s)r   r    r"   r!   r  r   r   ró   Zcopytreer#   rê   r•   rn   r^  r£   r¡   rˆ   r“   r”   Údirnamerè   r+  r[  r   r   rœ   Ú
IndexErrorr¹   rì   )rÝ   Zapt_rootrI   rK   rL   rM   Zlist_drR  ÚdestZsource_list_contentZorigin_datarf   Zorigin_pathZsrc_extrs   rY  rW  Ztrusted_gpgZ	trusted_dZppa_userr]  Zppa_archive_urlZppa_infore  rõ   r   r   r   r@   È  sÄ    


"

ÿ
 ÿÿ


ÿÿÿ   üÿz'__AptDpkgPackageInfo._build_apt_sandboxc                 C   sJ   t jdd|dgt jd}| t¡d  d¡ ¡ }|jdks>t‚|sFt‚|S )z!Return the version of a .deb filer  z-fZVersionr¶   r   r¨   )	r¹   rº   r»   r¼   r5  r˜   rn   r½   rÜ   )rÝ   rX   r¿   rÀ   r   r   r   r"  4  s
    z!__AptDpkgPackageInfo._deb_versionc                 C   s   t j ||¡S )zkCompare two package versions.

        Return -1 for ver < ver2, 0 for ver1 == ver2, and 1 for ver1 > ver2.)r;   rƒ   Zversion_compare)r   Zver1Zver2r   r   r   r  >  s    z%__AptDpkgPackageInfo.compare_versionsc                 C   sJ   | j dkrDtjddgtjd}| ¡ d  d¡ ¡ | _ |jdksDt‚| j S )z(Get "lsb_release -sc", cache the result.NÚlsb_releasez-scr¶   r   r¨   )	Ú_distro_codenamer¹   rº   r»   r¼   r˜   rn   r½   rÜ   )r   ri  r   r   r   r  G  s    

ÿz(__AptDpkgPackageInfo.get_distro_codenamec                 C   s<   | j dkr6|  ¡ d  ¡ | _ d| j kr6| j  dd¡| _ | j S )zuGet osname from /etc/os-release, or if that doesn't exist,
           'lsb_release -sir' output and cache the result.Nr   r  rV  )Ú_distro_namerj   rÕ   r  r   r   r   r   rí   T  s
    

z$__AptDpkgPackageInfo.get_distro_name)F)FNNN)NNF)FNFNNTF)4Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r*   r-   r5   r7   r?   rO   rU   rZ   r\   rc   re   rg   rl   rt   rŽ   rœ   r   r    r¥   r´   rÄ   rÖ   rÚ   Úclassmethodr©   rÞ   rß   rú   rü   r	  r2  r4  r¢   r±   r  r<  r?  rÒ   r^  r@   r"  r  rj  r  rk  rí   r   r   r   r   r
   )   sŒ   
	
"
0(    ÿ
6
  ÿ
X'           ý
   X	



 
A
k
	r
   )#ro  r¹   r   rm   r1   r(  ré   r   r&  r¾   r™   Ú
contextlibr   ÚwarningsÚfilterwarningsÚFutureWarningr;   ZcPickler$   Zurllibr   r   r   rp   r   r   rK  Zurllib.errorZurllib.requestrL  r“   Zapport.packagingr	   r
   Úimplr   r   r   r   Ú<module>   sD   @            D