
(qhX                 @   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 j d d e  d d l Z yH d d l Z d d l m Z m Z m Z e e e e f e Z e Z WnZ 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 Yn Xd d l Z d d
 l m  Z  Gd 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               @   sq  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! d"  Z d# d$   Z d% d&   Z d' d(   Z d) d*   Z d+ d,   Z d- d.   Z d  d/ d/ d/ d0 d1  Z e d2 d3    Z d4 d5   Z d6 d7   Z d/ d/ d  d8 d9  Z d: d;   Z  d< d=   Z! d  d/ d  d/ d/ d> d?  Z" d@ dA   Z# e dB dC    Z$ dD dE   Z% e dF dG    Z& dH dI   Z' dJ dK   Z( dL dM   Z) e dN dO    Z* e dP dQ    Z+ e dR dS    Z, dT dU   Z- d/ Z. dV dW   Z/ d/ Z0 dX dY   Z1 d/ S)Z__AptDpkgPackageInfozConcrete apport.PackageInfo class implementation for python-apt and
    dpkg, as found on Debian and derivatives such as Ubuntu.c             C   sZ   d  |  _  d  |  _ d  |  _ d  |  _ d  |  _ d |  _ |  j d |  _ |  j d |  _ d  S)Nzhttps://api.launchpad.net/develz/%s/main_archivez*/~%(user)s/+archive/%(distro)s/%(ppaname)s)
_apt_cache_sandbox_apt_cache_contents_dir_mirror_virtual_mapping_objZ_launchpad_base_archive_url_ppa_archive_url)self r   7/usr/lib/python3/dist-packages/apport/packaging_impl.py__init__-   s    						z__AptDpkgPackageInfo.__init__c             C   s6   y |  j  r t j |  j   Wn t k
 r1 Yn Xd  S)N)r   shutilrmtreeAttributeError)r   r   r   r   __del__7   s
    	z__AptDpkgPackageInfo.__del__c          	   C   s{   |  j  d  k	 r |  j  St j j | d  } t j j |  rk t | d   } t j |  |  _  Wd  QRXn	 i  |  _  |  j  S)Nz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   sT   t  j j | d  } |  j d  k	 rP t | d   } t j |  j |  Wd  QRXd  S)Nzvirtual_mapping.picklewb)r   r   r   r   r   r    dump)r   r"   r#   r$   r   r   r   _save_virtual_mappingK   s    z*__AptDpkgPackageInfo._save_virtual_mappingc             C   sq   d |  _  |  j sj y. t j j j   } t j | d d |  _ Wn' t k
 ri t j d d  |  _ Yn 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 s |  j | | | | |  t j j |  } t j d |  |  _ y |  j j |  Wn= t j	 j
 k
 r } z t t |    WYd d } ~ Xn X|  j j   n |  j j   |  j S)zBuild apt sandbox and return apt.Cache(rootdir=) (initialized lazily).

        Clear the package selection on subsequent calls.
        Nr)   )r   r   _build_apt_sandboxr   r   abspathr+   r.   updatecacheFetchFailedExceptionSystemErrorstrr   clear)	r   aptrootapt_sourcesfetchProgressdistro_namerelease_codenameoriginsr)   er   r   r   _sandbox_cache_   s    		
%z#__AptDpkgPackageInfo._sandbox_cachec             C   s;   y |  j    | SWn" t k
 r6 t d |   Yn Xd S)zuReturn apt.Cache()[package] (initialized lazily).

        Throw a ValueError if the package does not exist.
        zpackage %s does not existN)r/   KeyError
ValueError)r   packager   r   r   _apt_pkgt   s    z__AptDpkgPackageInfo._apt_pkgc             C   s5   |  j  |  } | j } | s. t d |   | j S)z*Return the installed version of a package.zpackage %s does not exist)rC   	installedrA   version)r   rB   pkgZinstr   r   r   get_version~   s
    	z __AptDpkgPackageInfo.get_versionc             C   s   |  j  |  j j S)z1Return the latest available version of a package.)rC   	candidaterE   )r   rB   r   r   r   get_available_version   s    z*__AptDpkgPackageInfo.get_available_versionc             C   se   |  j  |  j j } | s g  Sd d   | j j d g   | j j d g   | j 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)rC   Z_pkgZcurrent_verZdepends_listget)r   rB   Zcur_verr   r   r   get_dependencies   s    	%z%__AptDpkgPackageInfo.get_dependenciesc             C   s^   |  j  |  j r% |  j  |  j j S|  j  |  j rJ |  j  |  j j St d |   d S)z-Return the source package name for a package.zpackage %s does not existN)rC   rD   source_namerH   rA   )r   rB   r   r   r   
get_source   s
    z__AptDpkgPackageInfo.get_sourcec             C   sL   |  j  |  j } | s$ t d   x! | j D] } | j r. | j Sq. Wd S)zReturn 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)rC   rD   rA   r=   origin)r   rB   rF   rR   r   r   r   get_package_origin   s    	z'__AptDpkgPackageInfo.get_package_originc             C   s   |  j  |  } | j r. | j j d k r. d S|  j   d } | j r} | j j r} x' | j j D] } | j | k r` d Sq` Wt d d   | j j D  t d g  k r t j	 j
 d  r d Sd S)	zCheck 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 Sr   )rR   )rK   or   r   r   rM      s   	 z:__AptDpkgPackageInfo.is_distro_package.<locals>.<listcomp> z/etc/system-image/channel.ini)rC   rD   rE   get_os_versionrH   r=   rR   setr   r   r   )r   rB   rF   r;   rT   r   r   r   is_distro_package   s    .z&__AptDpkgPackageInfo.is_distro_packagec             C   s   |  j  |  } | j r. | j j d k r. d Sg  } xv t j d  D]e } yJ t |  7 } x- | D]% } | j   } | rc | j |  qc WWd QRXWqD t k
 r YqD XqD W| j r | j j	 r x' | j j	 D] } | j
 | k r d Sq Wd S)zCheck 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)rC   rD   rE   globr   stripappendIOErrorrH   r=   rR   )r   rB   rF   Znative_originsffdlinerT   r   r   r   is_native_origin_package   s$    	z-__AptDpkgPackageInfo.is_native_origin_packagec             C   s<  t  |  } t  |  } |  j |  j |  } | s8 d S| d } | d | | f } d } yp |  j | d d }	 |	 s~ d SxJ |	 D]B }
 |
 d d k r |
 d } Pq |
 d j |  r |
 d } Pq WWn t k
 r d SYn X| s d S| d	 } |  j |  } | sd Sx# | D] } t | d
  | d f SWd  S)N	self_linkzG/?ws.op=getPublishedBinaries&binary_name=%s&version=%s&exact_match=truerU   entriesTZarchitecture_specificfalseZdistro_arch_series_linkz'?ws.op=binaryFileUrls&include_meta=trueurlZsha1)NN)NN)NN)NN)NN)r   json_requestr   endswith
IndexErrorr   )r   	distro_idrB   rE   archmama_linkZpb_urlZbpub_urlZpbsZpbZbf_urlsZbfsZbfr   r   r   get_lp_binary_package   s<    


		
z*__AptDpkgPackageInfo.get_lp_binary_packageFc             C   s   y8 t  |  } | j   d k r7 t d | j     Wn3 t t f k
 rm t j d t |   d SYn Xy | j   } Wn- t k
 r t j d t |   d SYn Xt	 | t
  r | j d  } | r t j |  d St j |  Sd S)zOpen, 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.
        i  z%uzcannot connect to: %sNzfailure reading data at: %szutf-8rb   )r   getcoder   r   apportwarningr   readr\   
isinstancebytesdecodejsonloads)r   rd   rb   responseZcontentr   r   r   re   
  s"    		z!__AptDpkgPackageInfo.json_requestc             C   s!  t  |  } t  |  } |  j |  j |  } | s8 d  S| d } | d | | f } y! |  j | d d d d } Wn t k
 r d  SYn X| s d  S| d } |  j |  }	 |	 s d  Sg  }
 xW |	 D]O } t j j d k r t | t  r | j	 d  } t
 |  } |
 j |  q W|
 S)	Nra   zF/?ws.op=getPublishedSources&exact_match=true&source_name=%s&version=%srb   Tr   z?ws.op=sourceFileUrls   zutf-8)r   re   r   rg   sysversion_infomajorrq   Zunicodeencoder   r[   )r   rh   rB   rE   rj   rk   Zps_urlZpssf_urlsZsfusZsource_filesZsfur   r   r   get_lp_source_package$  s2    
!	
!z*__AptDpkgPackageInfo.get_lp_source_packagec             C   sj   |  j  |  j r+ |  j  |  j j p* d S|  j  |  j rV |  j  |  j j pU d St d |   d S)zReturn the architecture of a package.

        This might differ on multiarch architectures (e. g. an i386 Firefox
        package on a x86_64 system)unknownzpackage %s does not existN)rC   rD   architecturerH   rA   )r   rB   r   r   r   get_architectureB  s
    z%__AptDpkgPackageInfo.get_architecturec             C   s<   |  j  d | g  } | d k r% d Sd d   | j   D S)z*Return list of files shipped by a package.z-LNc             S   s%   g  |  ] } | j  d   s |  q S)Zdiverted)
startswith)rK   r]   r   r   r   rM   U  s   	 z2__AptDpkgPackageInfo.get_files.<locals>.<listcomp>)
_call_dpkg
splitlines)r   rB   listr   r   r   	get_filesO  s    z__AptDpkgPackageInfo.get_filesc       
      C   s  d | |  j    f } t j j |  s2 d | } y@ t j |  } t j | j  s\ t  t | j	 | j
  } Wn t k
 r g  SYn Xd } d | |  j    f } t j j |  s d | } t j j |  s g  St | d   } x | D] } y d | k rt j d |  w | j   }	 |	 s?t j d	 |  w t j d
 |	 d j d  j d   } t | j	 | j
  | k rw Wn t k
 rYn X| | 7} q WWd QRX| r|  j |  Sg  Sd 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   s    z(%s contains NUL character, ignoring linez%%s contains empty line, ignoring liner*      zUTF-8N)get_system_architecturer   r   r   statS_ISREGst_modeOSErrormaxst_mtimest_ctimer   rn   ro   splitrs   r{   _check_files_md5)
r   rB   ZlistfilesZmax_timeZsumssumfiler^   r_   Zwordsr   r   r   get_modified_filesW  sF    
	
)z'__AptDpkgPackageInfo.get_modified_filesc             C   s[  t  j d d d d | g d t  j } | j   d j   } | j d k rP i  Si  } x | j   D] } | sr qc | j   j   d d  \ } } t	 j
 j |  rIyd t | d	   } | j   }	 Wd QRXt j   }
 |
 j |	  |
 j   } | | k r	|	 | | <WqSt k
 rE} z d
 t |  | | <WYd d } ~ XqSXqc d | | <qc W| 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}z--stdoutr   Nrw   r   z[inaccessible: %s]z	[deleted])
subprocessPopenPIPEcommunicaters   
returncoder   rZ   r   r   r   r   r   rp   hashlibZmd5r2   Z	hexdigestr\   r6   )r   rB   dpkgoutmodifiedr_   r   Zdefault_md5sumr^   contentsmZcalculated_md5sumr>   r   r   r   get_modified_conffiles  s.    "*z+__AptDpkgPackageInfo.get_modified_conffilesc             C   s   d } d } d } x | r | t  |  k  r t j d d d d | g | | | |  d t j d	 t j d
 t j } | j   d j d  } | j d k r | } | | 7} q W| S)zsCall fgrep for a pattern on given file list and return the first
        matching file, or None if no file matches.Nd   r   Zfgrepz-lxm1z--stdinr   stderrzUTF-8)lenr   r   r   r   rs   r   )r   patternZ	file_listmatchZ
slice_sizeipr   r   r   r   Z__fgrep_files  s    z"__AptDpkgPackageInfo.__fgrep_filesNc             C   s  | r |  j  | | | |  St j d d | g d t j d t j } | j   d j d  } | j d k r | r | j   d } | d k r | St j	 j
 t j	 j |   d j   }	 g  }
 g  } x t j d	  D]p } t j	 j
 t j	 j |   d j   j d
  d } | |	 k s+|	 | k r;| j |  q |
 j |  q W|  j | |  } | sv|  j | |
  } | rt j	 j
 t j	 j |   d j d
  d Sd 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--listr   r   r   zUTF-8r   zhardening-wrapperz/var/lib/dpkg/info/*.list:Nr   )_search_contentsr   r   r   r   rs   r   r   r   r   splitextbasenamelowerrY   r[    _AptDpkgPackageInfo__fgrep_files)r   fileZuninstalledmap_cachedirreleaseri   r   r   rF   fnameZ	all_listsZlikely_listsr]   r   r   r   r   r   get_file_package  s.    (5-z%__AptDpkgPackageInfo.get_file_packagec             C   s_   t  j d d g d t  j } | j   d j   j   } | j d k sO t  | 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   rs   rZ   r   AssertionError)klassr   ri   r   r   r   r     s    z,__AptDpkgPackageInfo.get_system_architecturec             C   sW   t  j d d g d t  j } | j   d j   j   } | j d k sO t  d | S)zReturn 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:/lib)r   r   r   r   rs   rZ   r   r   )r   r   Zmultiarch_tripler   r   r   get_library_paths  s
    z&__AptDpkgPackageInfo.get_library_pathsc             C   sH   | |  _  y& |  j r. t j |  j  d |  _ Wn t k
 rC Yn Xd S)zExplicitly 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   rd   r   r   r   
set_mirror  s    		z__AptDpkgPackageInfo.set_mirrorc             C   s  t  j j   } | rO t j   } | j d | j    | j   | j | d <| rq t	 j
 d d d g d | d d d d | g } | r | d d
 | 7<yt	 j
 | d | d | d k r)| s d S|  j |  j   | |  }	 |	 r%d }
 t j j j d  d k r9t j j j d  }
 t j j j d d  t j j j   } t j j |  } g  } x0 |	 D]( } | j t j j | | d |  qjW| j   } | | j k rd S|
 rt j j j d |
  xU t j t  j j | d   D]. } t	 j
 d d d | g d t	 j d | qWn d SWn t k
 rBd SYn Xd } xB t j t  j j | | d   D] } t  j j |  rl| } qlW| st  d   y t	 j
 d d d d | Wn t k
 rYn 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.
        z$Dir "%s";
Debug::NoLocking "true";
 Z
APT_CONFIGzapt-getz-qqr2   envz--assume-yessourcer   =cwdr   NrU   zAcquire::http::Proxydestdirz*.dsczdpkg-sourcez-snz-xr   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>&1shellTr   )!r   environcopytempfileZNamedTemporaryFilewriter{   flushrJ   r   callr}   get_distro_namer+   apt_pkgconfigfindrW   r,   r-   AcquireProgressAcquirer[   AcquireFileZrunZRESULT_CONTINUErY   r   r   r   r   isdirr   )r   Z
srcpackagedirrE   ZsandboxZ
apt_updater   r]   argvr|   proxyr:   fetcherZaf_queueZsfresultZdscrootrL   r   r   r   get_source_tree  sh    
!%	)
z$__AptDpkgPackageInfo.get_source_treec             C   s   d t  j   d S)zReturn the actual Linux kernel package name.

        This is used when the user reports a bug against the "linux" package.
        zlinux-image-rw   )r   uname)r   r   r   r   get_kernel_packages  s    z'__AptDpkgPackageInfo.get_kernel_packagec             C   s  g  } g  } | d j    d } | d } | d j    d } d | } |  j   } | | k rw | | j rw | | f St j j j d  d }	 d | | | f }
 d	 |
 } t t j	 j
 |	 |
  d
  } t |  } | j   d k r d Sx' | j d  } | sP| j |  q W| j   | j   t j d d t j	 j
 |	 |
  g  } | d k rx| j |
 j  d  d  | | f S)zInstall 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Unamer   ZArchitectureZPackagezlinux-image-debug-%szDir::Cache::archivesz/partialz%s_%s_%s.ddebz,http://ddebs.ubuntu.com/pool/main/l/linux/%swi  rU   linux   i   r   z-ir   _)rU   zlinuxi    )r   r/   ZisInstalledr+   r   r   find_dirr   r   r   r   r   rm   rp   r   r   closer   r   r[   )r   reportrD   ZoutdatedZkverri   verZdebug_pkgnamecZ
target_dirdebrd   r   ublockZretr   r   r   _install_debug_kernel{  s6    





'z*__AptDpkgPackageInfo._install_debug_kernelc
       5   Z      s  | s |  j    } | s0 d }
 |  j   |  _ n t j j | | d  }
 | |  j    k r t j j | | | d  } t j j |  r | }
 y |  j |  j |
   Wn< t	 k
 r } z t
 j d t |   WYd d } ~ Xn Xt t j j | | d    } | j   j   |  _ Wd QRXt j j |
  sEt	 d |
   | rd } | rrt j j | | d  } n t j j | d	 d  } t j j |  st j |  n d
 } t j   } t j j j d |  t j j j d d  t j j j d d  t j j j d d  | r1t j j j   } n t j j j   } | ss|  j | |
 | |  j   |  j |	    n |  j | |
 |  j   |  j |	  t j d t j j  |     y   j! |  Wn= t j" j# k
 r} z t	 t |    WYd d } ~ Xn X  j   t j j j$ d  } d } t j j%   } t j j | d  } i  } t j j |  rt |  I } x? | D]7 } | j   } | sq{| j&   \ } } | | | <q{WWd QRXt   } i  } t j j' |  } g  } x| D]\ } } y   | } WnF t( k
 rMd | j) d d  }  | |  d 7} t
 j |   wYn Xy | rg| j* | | _+ Wn t( k
 r|  j, |  j   | | |  \ }! }" |! r| j- t j j. | |! d d |" d |  | | | <n | d | | | j+ j/ f 7} Yn X| j+ }# | j0 |  | rK|  j1 |  }$ x- |# j2 D]" } |$ j3 | t    j0 |  q2Wg  }% d |# j4 k r|% t j j5 |# j4 d  7}% d |# j4 k r|% t j j5 |# j4 d  7}% x|% D]}& |& d }&   j6 |& d  ry |$ |& d }' Wn t( k
 r	wYn Xx |' D] } t j j | d  |  }( xS t7 j7 |(  D]B }) |  j8 |)  } t j j9 | |& d! |& d"  r@t j: |)  q@Wy | | =Wqt( k
 rYqXqW~' qt j j | d  |& d  }( xS t7 j7 |(  D]B }) |  j8 |)  } t j j9 | |& d! |& d"  rt j: |)  qWy | |& d =Wqt( k
 rFYqXqW|# j; d# k ry| d$ }*   |* }+ d }, | ry |+ j* | |+ _+ d
 }, Wnx t( k
 r|  j, |  j   |* | |  \ }! }" |! r| j- t j j. | |! d d |" d |  | | |* <d
 }, Yn X|, say |+ j* |# j/ |+ _+ Wn/ t( k
 r`| d% | | |+ j+ j/ f 7} Yn X| j0 |*  Wqt( k
 r| j<   | j= |# j>  r  f d& d'   | j? D }- n g  }- |- r	x|- D]} | | k rqd }, | r	y"   | j* |   | _+ d
 }, Wnx t( k
 r	|  j, |  j   | | |  \ }! }" |! r	| j- t j j. | |! d d |" d |  | | | <d
 }, Yn X|, s	y   | j* |# j/   | _+ Wn t( k
 r	Yn X| j0 |  qWnd }, | d( }. y  |. }/ | j0 |.  | r
y |/ j* | |/ _+ d
 }, Wnx t( k
 r
|  j, |  j   |. | |  \ }! }" |! r
| j- t j j. | |! d d |" d |  | | |. <d
 }, Yn X|, s
y |/ j* |# j/ |/ _+ Wn2 t( k
 r
| d) | |# j/ |/ j+ j/ f 7} Yn XWn t( k
 r| rm|  j, |  j   |. | |  \ }! }" |! rm| j- t j j. | |! d d |" d |  | | |. <d
 }, |, s| d* | 7} Yn XYqXqWx | j@   D] } | r| jA |  | k r  | jB d d  q]| jA |    | j+ j/ k r
  | jB d d  q]| jC |  q| jA |    | j+ j/ k rP  | jB d d  q| jC |  qWtD jD   }0 y   jE d+ |  WnN t j" j# k
 r} z( t
 jF d, t |   tG jH d"  WYd d } ~ Xn X| rtI d-  x| jJ D] }1 | st j jK |1 jL  |0 k rtM jN d. d/ |1 jL g  jO   }2 |2 j   j&   \ } } | jA |  | k renE | | k r| | | k rn& tM jP d0 d1 |1 jL | g  | | | <t j jQ |1 jL  j& d2 d"  d }3 |3 | k r| jC |3  qWtR | jS    }4 |4 jT   t | d3  P } xF |4 D]> } | jU |  | jU d4  | jU | |  | jU d  q!WWd QRX| r}tV jW |  | stX d5 d4 j |    | r|  jY |  | S)6a0  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.

        Return a string with outdated packages, or None 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.
        z/etc/apt/sources.listzsources.listzcannot determine mirror: %sNZcodenamez%s does not existFr+   systemTzAPT::ArchitecturezAcquire::LanguagesZnonez'Acquire::http::Proxy::api.launchpad.netZdirectz#Acquire::http::Proxy::launchpad.netr)   zDir::Cache::archivesrU   zpackages.txtz#package %s does not exist, ignoring%z%%
hashzsha1:%sr   z,%s version %s required, but %s is available
Z	ConflictsZReplacesr   z%s_*.debrw   r   allz-dbgzAoutdated -dbg package for %s: package version %s -dbg version %s
c                sG   g  |  ]= } | j  d   r |   k r d   | j j k r |  q S)z-dbgZtransitional)rf   rH   Zdescription)rK   r   )r3   r   r   rM     s   	 z9__AptDpkgPackageInfo.install_packages.<locals>.<listcomp>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...zdpkg-debz--showr   z-xr   r    z*apt fetcher did not fetch these packages: )Zr   get_distro_codenamecurrent_release_codenamer   r   r   r   r   $_get_primary_mirror_from_apt_sourcesr5   rn   ro   r6   r   rp   rZ   r   makedirsr   mkdtempr+   r   r   rW   r,   textr   r-   r?   r   r0   r.   r1   r2   r3   r4   r   ZSourceRecordsr   r   r@   replaceZversionsrH   rl   r[   r   rE   addr%   Zprovides
setdefaultrecordZparse_dependsZis_virtual_packagerY   _deb_versionZ	check_depunlinkr   ZrestartlookuprP   Zbinariesr   rN   Zmark_installremovetimeZfetch_archiveserrorrx   exitprintitemsgetctimedestfiler   Zcheck_outputrs   Z
check_callr   r   keyssortr   r   r   r   r(   )5r   r)   r"   r   ZpackagesverboseZ	cache_dirZpermanent_rootdirr   r=   r9   Zarch_apt_sourcesr>   r]   Ztmp_aptrootr8   r:   Z
archivedirZobsoleteZsrc_recordsZpkg_listZpkg_versionsr_   r   vZ	real_pkgsZlp_cacher   Zacquire_queuerF   r   Z	cache_pkgr   Zlp_urlZsha1sumrH   Zvirtual_mappingZ	conflictsZconflictZ	providersZdebsr   Zdbg_pkgZdbgZ	pkg_foundZdbgsZ
dbgsym_pkgZdbgsymZlast_writtenr   r   pkg_nameZpkgsr   )r3   r   install_packages  s   **!		%
	

"	 
 	 	
















#


 
"!
%
z%__AptDpkgPackageInfo.install_packagesc             C   s   t  j j |  j   j   |  S)z2Return known package names which match given glob.)rY   Zfnmatchfilterr/   r   )r   Znameglobr   r   r   package_name_glob%  s    z&__AptDpkgPackageInfo.package_name_globc             C   sg   t  j d g | d t  j d t  j } | j t  d j d  } | j d k rW | St d   d S)zRCall dpkg with given arguments and return output, or return None on
        error.r   r   r   r   zUTF-8zpackage does not existN)r   r   r   r   inputrs   r   rA   )r   argsr   r   r   r   r   r   .  s    z__AptDpkgPackageInfo._call_dpkgc             C   sF  t  j j |  rj t j d d | g d t j d t j d d d i  } | j   d j d	 d
 d } n| t |  t	 k s t
 d   t j d d g d t j d t j d t j d d d i  } | j |  d j d	 d
 d } | j d k r g  Sg  } x@ | j   D]2 } | j d  r| j | j d d  d  qW| S)zInternal function for calling md5sum.

        This is separate from get_modified_files so that it is automatically
        testable.
        z/usr/bin/md5sumz-cr   r   r   r*   r   r   zUTF-8errorsr   z&md5sum list value must be a byte arrayr   ZFAILEDr   r   )r   r   r   r   r   r   r   rs   typerr   r   r   r   rf   r[   rsplit)r   r   r   r   Z
mismatcheslr   r   r   r   ;  s"    ""!z%__AptDpkgPackageInfo._check_files_md5c          
   C   s   t  |   } x | D]q } | j   } t |  d k r | d d k r | d j d  rf d } n d } | | j d  r | | Sq Wt d |   Wd	 QRXd	 S)
z?Heuristically determine primary mirror from an apt sources.list   r   r   r   [rw   zhttp://zEcannot determine default mirror: %s does not contain a valid deb lineN)r   r   r   r   r5   )r   r9   r]   r  ZfieldsZ
mirror_idxr   r   r   r   X  s    "	z9__AptDpkgPackageInfo._get_primary_mirror_from_apt_sourcesc             C   s"   |  j  s |  j d  |  _  |  j  S)zReturn the distribution mirror URL.

        If it has not been set yet, it will be read from the system
        configuration.z/etc/apt/sources.list)r   r   )r   r   r   r   _get_mirrork  s    	z __AptDpkgPackageInfo._get_mirrorc             C   s5   t  |  d  r% |  j d k	 r% |  j St 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_codenameu  s    z0__AptDpkgPackageInfo._distro_release_to_codenamec          &   C   s  | r | } n! |  j  s' t j   |  _  |  j  } | d k rH |  j   } | d k rc |  j   } n |  j |  } x
d d d d g D]} t j j | d | | | f  } y, t j	 |  } t
 t j   | j  }	 Wn t k
 r d }	 Yn X|	 d k s|	 d k rd |  j   | | | f }
 |	 r'y$ d	 d
 l m } d	 d l m } Wn2 t k
 rd	 d
 l m } d	 d l m } Yn Xd	 d l m } | |
  d } | |  } | j d | |
  d  | j   } | j d d  } | r| j | d  } | | j | j  k } q-d } n d } | ry t |
  } Wn' t k
 rl| d k re  n w Yn Xt | d  1 } x' | j d  } | sP| j |  qWWd QRX| j    t j j! |  st"  | j# d  r| d d  } d } t$ j% d d d | | g d t$ j& d t$ j& } | j'   d	 j( d  } | rq| j)   d j) d  d	 j) d  d } | r | Sq Wd S)z4Internal function for searching file in Contents.gz.Nz-updatesz	-securityz	-proposedrU   z%s%s-Contents-%s.gziQ z%s/dists/%s%s/Contents-%s.gzr   )HTTPConnection)urlparse)datetimer   ZHEADrw   zlast-modifiedz%a, %d %b %Y %H:%M:%S %ZTr&   i@B r*   zgrepz-m1z^%s[[:space:]]r   r   zUTF-8,r   )*r   r   r   r   r   r  r   r   r   r   intr   r   r   r  Zhttplibr  r  ImportErrorZhttp.clienturllib.parser  ZrequestZgetresponseZ	getheaderZstrptimeZfromtimestampr   r\   r   rp   r   r   r   r   r   r   r   r   r   rs   r   )r   r   r   r   ri   r   ZpocketmapstZagerd   r  r  r  ZserverZconnresZmodified_strr   r2   srcr]   datarB   r  r   r   r   r   r     s    			"			
*z%__AptDpkgPackageInfo._search_contentsc             C   sN  | j  d  rJ| j d  d d  } d } t |  d k rS | j d  d } d } x| t |  k  rzt j d | d |   } t j d | | d   } yn t t t j	 j
 d	 | d
 | d | i   9 }	 |	 j   |	 j   d k rt d |	 j     Wd QRXWn_ t t f k
 ru| d 7} | t |  k rn| rh| j d  d } d } n d } w\ Yn XPq\ W| rJ| rJd | | | | f }
 d | | | | f } yX t t |   9 }	 |	 j   |	 j   d k rt d |	 j     Wd QRXd } Wn t t f k
 r+d } Yn X|
 | d |
 d d  d Sd 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.
        zLP-PPA--rw   NTr   ppaFuserdistroppanamei  z%uz-deb http://ppa.launchpad.net/%s/%s/%s %s mainz5http://ppa.launchpad.net/%s/%s/%s/dists/%s/main/debugz main/debugrU   z
deb-srcr  r   )r   r   r   r[   r6   r   r   r   rn   	packagingr   rp   rm   r   r   )r   rR   r$  r<   Z
componentsZtry_ppaindexr#  ppa_namerv   Zppa_lineZ	debug_urlZ	add_debugr   r   r   create_ppa_source_from_origin  sT    
!
	

z2__AptDpkgPackageInfo.create_ppa_source_from_originc          !   C   s  t  j j t  j j | d d d   s t  j t  j j | d d d d d   t  j t  j j | d d d d d   t  j t  j j | d d d   t  j t  j j | d	 d d
   t  j t  j j | d	 d d   t  j j | d	 d d  } t  j j |  rt j |  t  j j | d  rDt j | d |  n t  j |  t	 |  H } t	 t  j j | d	 d d  d   } | j
 | j    Wd  QRXWd  QRX| rd }	 i  }
 x| D]} | d k rqd  } t  j j | d  rt  j j | d | d  } t  j j |  rod | k rot  j j | d | j d  d  } t  j j |  sd  } n t  j j |  sd  } | rt	 |   } | j   }	 Wd  QRXn |  j | | |  }	 |	 rt	 t  j j | d	 d d | d  d   } | j
 |	  Wd  QRXx |	 j   D]q } | j d  r4qd | k rCq| j   d j d  d } | j   d j d  d } | | f |
 | <qWqt j d |  qWt  j j t  j j |  d  } t  j j |  rt j | t  j j | d	 d   n4 t  j j d  r5t j d t  j j | d	 d   t  j j | d	 d d  } t  j j |  rot j |  t  j j | d  rt j | d |  n2 t  j j d   rt j d  |  n t  j |  | r|	 rx|
 j   D] \ } \ } } t j j d! t |  d" | d# t |  i } t j j |  } | sBqy | d$ } Wn& t k
 rxt j d% |  wYn Xd& d' d( d) d* t  j j | d+ |  g } | d, d- d. d/ d0 | g 7} t j |  d1 k rt j d2 |  qWd  S)3Nvarlibr+   Zlistspartialr3   Zarchivesr   etcz
apt.conf.dzpreferences.dzsources.list.dz.dzsources.listr   rU   r~   z.listzLP-PPAzLP-PPA-a#zppa.launchpad.netr   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.dr#  r$  r%  signing_key_fingerprintz/Error: can't find signing_key_fingerprint at %sZgpgz--no-optionsz--no-default-keyringz--no-auto-check-trustdbz	--keyringz%s.gpgz--quietz--batchz--keyserverzhkp://keyserver.ubuntu.com:80/z--recvr   zUnable to import key for %s)r   r   r   r   r   r   r   r   Zcopytreer   r   rp   rZ   r)  r   r   r   rn   ro   dirnamer   r   r&  r   r   re   rg   r   r   )r   Zapt_rootr9   r;   r<   r=   Zlist_dr  destZsource_list_contentZorigin_datarR   Zorigin_pathZsrc_extr_   r#  r"  Ztrusted_gpgZ	trusted_dZppa_userr(  Zppa_archive_urlZppa_infor1  r   r   r   r   r0     s    '(("""'!	!%"				z'__AptDpkgPackageInfo._build_apt_sandboxc             C   sk   t  j d d | d g d t  j } | j t  d j d  j   } | j d k s[ t  | sg t  | S)z!Return the version of a .deb filezdpkg-debz-fZVersionr   r   zUTF-8)	r   r   r   r   r  rs   rZ   r   r   )r   rF   r   r   r   r   r   r     s
    $"z!__AptDpkgPackageInfo._deb_versionc             C   s   t  j 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   compare_versions  s    z%__AptDpkgPackageInfo.compare_versionsc             C   sk   |  j  d k rd t j d d g d t j } | j   d j d  j   |  _  | j d k sd t  |  j  S)z(Get "lsb_release -sc", cache the result.Nlsb_releasez-scr   r   zUTF-8)	_distro_codenamer   r   r   r   rs   rZ   r   r   )r   r5  r   r   r   r     s    "z(__AptDpkgPackageInfo.get_distro_codenamec             C   sV   |  j  d k rO |  j   d j   |  _  d |  j  k rO |  j  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   r!  )_distro_namerV   r   r   )r   r   r   r   r     s
    z$__AptDpkgPackageInfo.get_distro_name)2__name__
__module____qualname____doc__r   r   r%   r(   r/   r?   rC   rG   rI   rO   rQ   rS   rX   r`   rl   re   r}   r   r   r   r   r   r   classmethodr   r   r   r   r   r   r  r  r   r   r   r  r  r   r)  r0   r   r4  r6  r   r7  r   r   r   r   r   r
   )   sf   

	
#0(.V' 	

YBp
r
   )#r;  r   r   rY   r   rx   r   r   r   r   rt   
contextlibr   warningsfilterwarningsFutureWarningr+   ZcPickler    Zurllibr   r   r   r\   r   r   r  Zurllib.errorZurllib.requestr  rn   Zapport.packagingr	   r
   implr   r   r   r   <module>   s6   `
     