o
    4f                     @   s  d dl mZ d dlZd dlZd dlZd dlZd dlmZmZm	Z	m
Z
mZmZmZmZmZmZmZ d dlZd dlmZmZ d dlZd dlmZmZmZ G dd deZG dd	 d	eZG d
d deZG dd deZ G dd de!Z"G dd de#Z$G dd de#Z%G dd de#Z&G dd de#Z'G dd de'Z(G dd de'Z)G dd de#Z*G dd de#Z+d d! Z,d"d# Z-d$d% Z.e/d&kre.  dS dS )'    )print_functionN)AnyCallableDictIteratorListOptionalSetTupleUnioncastKeysView)PackageVersion)AcquireProgressInstallProgress
OpProgressc                   @      e Zd ZdZdS )FetchCancelledExceptionzAException that is thrown when the user cancels a fetch operation.N__name__
__module____qualname____doc__ r   r   +/usr/lib/python3/dist-packages/apt/cache.pyr   '       r   c                   @   r   )FetchFailedExceptionz-Exception that is thrown when fetching fails.Nr   r   r   r   r   r   +   r   r   c                   @   r   )UntrustedExceptionz>Exception that is thrown when fetching fails for trust reasonsNr   r   r   r   r   r   /   r   r   c                   @   r   )LockFailedExceptionz,Exception that is thrown when locking fails.Nr   r   r   r   r   r   3   r   r   c                   @   r   )CacheClosedExceptionz>Exception that is thrown when the cache is used after close().Nr   r   r   r   r   r    7   r   r    c                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	_WrappedLockz`Wraps an apt_pkg.FileLock to raise LockFailedException.

    Initialized using a directory path.c                 C   s    || _ ttj|d| _d S )Nlock)_pathapt_pkgFileLockospathjoin_lock)selfr'   r   r   r   __init__@   s   z_WrappedLock.__init__c              
   C   s:   z| j  W S  tjy } z	td| j|f d }~ww )NzFailed to lock directory %s: %s)r)   	__enter__r$   Errorr   r#   )r*   er   r   r   r,   E   s   z_WrappedLock.__enter__c                 C   s   | j |||S N)r)   __exit__)r*   typvalue	tracebackr   r   r   r0   M   s   z_WrappedLock.__exit__N)r   r   r   r   r+   r,   r0   r   r   r   r   r!   ;   s
    r!   c                   @   s  e Zd ZdZd\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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ed.d/ Zed0d1 Zed2d3 Zd4d5 Z	d]d6d7Z			d_d8d9Zd:d; Z 	<	d`d=d>Z!	?	dad@dAZ"dBdC Z#			d_dDdEZ$dFdG Z%dHdI Z&dJdK Z'dLdM Z(dNdO Z)dPdQ Z*edRdS Z+edTdU Z,edVdW Z-edXdY Z.edZd[ Z/dS )bCachea  Dictionary-like package cache.

    The APT cache file contains a hash table mapping names of binary
    packages to their metadata. A Cache object is the in-core
    representation of the same. It provides access to APTs idea of the
    list of available packages.

    The cache can be used like a mapping from package names to Package
    objects (although only getting items is supported).

    Keyword arguments:
    progress -- a OpProgress object,
    rootdir  -- an alternative root directory. if that is given the system
    sources.list and system lists/files are not read, only file relative
    to the given rootdir,
    memonly  -- build the cache in memory only.


    .. versionchanged:: 1.0

        The cache now supports package names with special architecture
        qualifiers such as :all and :native. It does not export them
        in :meth:`keys()`, though, to keep :meth:`keys()` a unique set.
    NFc              	   C   sT  t tjd | _t tjd | _t tjd | _t tjd | _	i | _
i | _t | _t | _d| _d | _| dd | dd |rGtjdd |rtj|}tj|d r`ttj|d  tj|d rqttj|d  tjd	| tjd
|d  tjdtj|ddd | | t  tj d}t!|| _"| #| d S )Ncache_post_open_inc_changes_countcache_post_changezDir::Cache::pkgcache z/etc/apt/apt.confz/etc/apt/apt.conf.dDirDir::State::status/var/lib/dpkg/statuszDir::bin::dpkgusrbindpkgDir::Cache::Archives)$r   r$   r4   _cacheDepCache	_depcachePackageRecords_records
SourceList_list
_callbacks_callbacks2weakrefWeakValueDictionary_weakrefWeakSet_weakversions_changes_count_sorted_setconnectconfigsetr&   r'   abspathexistsread_config_fileisdirread_config_dirr(   _check_and_create_required_dirsinit_systemfind_dirr!   _archive_lockopen)r*   progressrootdirmemonlyarchive_dirr   r   r   r+   l   sH   



zCache.__init__c                 C      | j   dS )zFix broken packages.N)rC   
fix_brokenr*   r   r   r   rc         zCache.fix_brokenc                 C   s   |  j d7  _ dS )zIncrease the number of changes   N)rO   rd   r   r   r   r7         zCache._inc_changes_countc                 C   sh   ddg}g d}|D ]}t j|| st ||  q
|D ]}t j|| s1t|| d  qdS )zf
        check if the required apt directories/files are there and if
        not create them
        r<   z/etc/apt/sources.list)z/var/lib/dpkgz	/etc/apt/z/var/cache/apt/archives/partialz/var/lib/apt/lists/partialwN)r&   r'   rU   makedirsr]   close)r*   r_   filesdirsdfr   r   r   rY      s   z%Cache._check_and_create_required_dirsc                 C   sp   || j v r| j | D ]}|dkr|   q
|  q
|| jv r4| j| D ]\}}}|| g|R i | q#dS dS )z# internal helper to run a callback r7   N)rH   r7   rI   r*   namecallbackargskwdsr   r   r   _run_callbacks   s   


zCache._run_callbacksc                 C   s   |du r
t jj }|   || _| d t|| _	t
| j	| _t| j	| _t | _| j  d| _|   tt dk| _|  | d dS )zY Open the package cache, after that it can be used like
            a dictionary
        Ncache_pre_openrf   r6   )aptr^   baser   rj   op_progressrt   r$   r4   rA   rB   rC   rD   rE   rF   rG   read_main_listrP   _Cache__remaplenget_architectures_have_multi_archdone)r*   r^   r   r   r   r]      s   


z
Cache.openc              	   C   s   t | j D ].}z| j| }W n	 ty   Y qw z| j|jj|jjf |_W q ty5   | j|= Y qw t | j	D ]<}|j
jjD ].}|j|jjkrp|jdks]|jjdks]|j|jjkrp|j|jjkrp|j|jjkrp||_ nqB| j	| q;dS )zCalled after cache reopen() to relocate to new cache.

        Relocate objects like packages and versions from the old
        underlying cache to the new one.
        r   N)listrL   keysKeyErrorrA   _pkgrp   architectureLookupErrorrN   packageversion_listhash_candsize
multi_archver_strremove)r*   keypkgvervr   r   r   __remap   s0   zCache.__remapc                 C   s   | ` ttjd| _ dS )z Close the package cache N)rE   r   r$   rD   rd   r   r   r   rj     s   zCache.closec                 C   s   | S )z Enter the with statement r   rd   r   r   r   r,   
  s   zCache.__enter__c                 C   s   |    dS )z Exit the with statement N)rj   )r*   exc_type	exc_valuer3   r   r   r   r0        zCache.__exit__c                 C   sV   zt |}| j| }W n ty   td| w | |s$td| | |}|S )z" look like a dictionary (get key) z!The cache has no package named %r)strrA   r   _Cache__is_real_pkg_rawpkg_to_pkg)r*   r   rawpkgr   r   r   r   __getitem__  s   

zCache.__getitem__c                 C   s"   z| | W S  t y   | Y S w )zaReturn *self*[*key*] or *default* if *key* not in *self*.

        .. versionadded:: 1.1
        )r   )r*   r   defaultr   r   r   get%  s
   
z	Cache.getc                 C   s    |j dd}| j|t| |S )zgReturns the apt.Package object for an apt_pkg.Package object.

        .. versionadded:: 1.0.0
        Tpretty)get_fullnamerL   
setdefaultr   )r*   r   fullnamer   r   r   r   0  s   zCache._rawpkg_to_pkgc                 c   s4    |   D ]}t| | j| }| j||V  qd S r/   )r   r   rA   rL   r   r*   pkgnamer   r   r   r   __iter__:  s
   zCache.__iter__c                 C      |j S )z8Check if the apt_pkg.Package provided is a real package.)has_versions)r*   r   r   r   r   __is_real_pkgE  s   zCache.__is_real_pkgc                 C      || v S r/   r   r*   r   r   r   r   has_keyJ     zCache.has_keyc                 C   s,   z|  | jt| W S  ty   Y dS w NF)r   rA   r   r   r   r   r   r   __contains__N  s
   zCache.__contains__c                 C   s   t |  S r/   )r{   r   rd   r   r   r   __len__U     zCache.__len__c                    s0    j d u rt fdd jjD  _ t j S )Nc                 3   s&    | ]}  |r|jd dV  qdS )Tr   N)r   r   ).0prd   r   r   	<genexpr>\  s
    zCache.keys.<locals>.<genexpr>)rP   sortedrA   packagesr   rd   r   rd   r   r   Y  s
   


z
Cache.keysc                 C   s6   g }| j j}| jjD ]}||s|| | q
|S )z Get the marked changes )rC   marked_keeprA   r   appendr   )r*   changesr   r   r   r   r   get_changesa  s   zCache.get_changesc                 C   s    |    | j| |   dS )zUpgrade all packages.

        If the parameter *dist_upgrade* is True, new dependencies will be
        installed as well (and conflicting packages may be removed). The
        default value is False.
        N)cache_pre_changerC   upgrader8   )r*   dist_upgrader   r   r   r   k  s   zCache.upgradec                 C   s>   | j du r	tdt| j}t }||| j| j  |jS )z;Get the size of the packages that are required to download.N&Cache object used after close() called)	rE   r    r$   PackageManagerrC   Acquireget_archivesrG   fetch_needed)r*   pmfetcherr   r   r   required_downloadw  s   
zCache.required_downloadc                 C      | j jS )z8Get the size of the additional required space on the fs.)rC   usr_sizerd   r   r   r   required_space     zCache.required_spacec                 C   s^   t  }| jj}ttjtjf}| jjD ]}||}|r,|j	s,|j
|v r,||jdd q|S )zAReturn the packages not downloadable packages in reqreinst state.Tr   )rS   rC   get_candidate_ver	frozensetr$   INSTSTATE_REINSTREQINSTSTATE_HOLD_REINSTREQrA   r   downloadable
inst_stateaddr   )r*   	reqreinstr   statesr   candr   r   r   req_reinstall_pkgs  s   zCache.req_reinstall_pkgsc                 C   s   |d u rt jdd}dd |jD }|r%|s%tdddd |D  | }d}d	}|jD ]}|j|jkr9q0|j	r=q0|d
|j
|jf 7 }d}q0||jkrSt||rYt||S )NzAPT::Get::AllowUnauthenticatedFc                 S   s   g | ]}|j s|qS r   )
is_trusted)r   itemr   r   r   
<listcomp>  s    z&Cache._run_fetcher.<locals>.<listcomp>zUntrusted packages:
%s
c                 s   s    | ]}|j V  qd S r/   )desc_uri)r   ir   r   r   r     s    z%Cache._run_fetcher.<locals>.<genexpr>r9   zFailed to fetch %s %s
T)r$   rR   find_bitemsr   r(   runstatus	STAT_DONE	STAT_IDLEr   
error_textRESULT_CANCELLEDr   r   )r*   r   allow_unauthenticated	untrustedresfailederr_msgr   r   r   r   _run_fetcher  s4   

zCache._run_fetcherc                 C   s4   | j du r	td||| j| j sdS | ||S )z fetch the needed archives Nr   F)rE   r    r   rG   r   )r*   r   r   r   r   r   r   _fetch_archives  s   
zCache._fetch_archivesc                 C   s~   |dur|durt d|du rtjj }|du rt|}| j | |t	| j
|W  d   S 1 s8w   Y  dS )a  Fetch the archives for all packages marked for install/upgrade.

        You can specify either an :class:`apt.progress.base.AcquireProgress()`
        object for the parameter *progress*, or specify an already
        existing :class:`apt_pkg.Acquire` object for the parameter *fetcher*.

        The return value of the function is undefined. If an error occurred,
        an exception of type :class:`FetchFailedException` or
        :class:`FetchCancelledException` is raised.

        The keyword-only parameter *allow_unauthenticated* specifies whether
        to allow unauthenticated downloads. If not specified, it defaults to
        the configuration option `APT::Get::AllowUnauthenticated`.

        .. versionadded:: 0.8.0
        Nz'Takes a progress or a an Acquire object)
ValueErrorrv   r^   textr   r$   r   r\   r   r   rC   )r*   r^   r   r   r   r   r   fetch_archives  s   

$zCache.fetch_archivesc                 C   s6   z| j | }W n
 ty   Y dS w t|jo|j S )z0Return whether the package is a virtual package.F)rA   r   boolhas_providesr   r   r   r   r   is_virtual_package  s   zCache.is_virtual_packageTc                 C   s   t  }| jj}z| j| }|jr|st|W S W n ty%   t| Y S w |jD ]\}}}	|	j}
|r9|	||
krA|	| 
|
 q)t|S )a)  Return a list of all packages providing a package.

        Return a list of packages which provide the virtual package of the
        specified name.

        If 'candidate_only' is False, return all packages with at
        least one version providing the virtual package. Otherwise,
        return only those packages where the candidate version
        provides the virtual package.

        If 'include_nonvirtual' is True then it will search for all
        packages providing pkgname, even if pkgname is not itself
        a virtual pkg.
        )rS   rC   r   rA   r   r   r   provides_list
parent_pkgr   r   )r*   r   candidate_onlyinclude_nonvirtual	providersr   vpprovidesprovidesverversionr   r   r   r   get_providing_packages  s    


zCache.get_providing_packagesr   c                 C   sf  t tjd |r?tjd}tjd}tjd}tjdtj| tjdd tjdd t	 }|
  n| j}zM|du rMtjj }z
| j|||}	W n tyg }
 zt|
d}
~
ww |	so|rot |	W |rtjd| tjd| tjd| W  d   S |rtjd| tjd| tjd| w w 1 sw   Y  dS )a  Run the equivalent of apt-get update.

        You probably want to call open() afterwards, in order to utilise the
        new cache. Otherwise, the old cache will be used which can lead to
        strange bugs.

        The first parameter *fetch_progress* may be set to an instance of
        apt.progress.FetchProgress, the default is apt.progress.FetchProgress()
        .
        sources_list -- Update a alternative sources.list than the default.
        Note that the sources.list.d directory is ignored in this case
        zDir::State::ListszDir::Etc::sourcelistzDir::Etc::sourcepartszAPT::List-Cleanupxxx0N)r!   r$   rR   r[   findrS   r&   r'   rT   rF   ry   rG   rv   r^   rw   r   rA   updateSystemErrorr   )r*   fetch_progresspulse_intervalraise_on_errorsources_listold_sources_listold_sources_list_dold_cleanupslistr   r.   r   r   r   r     sf   



zCache.updatec                 C   s   z|   W n ty   |  Y nw t }|rt  z||}W |r+t  n|r3t  w w z|  W |S  tyI   |	  Y |S w )aY  
        The first parameter *pm* refers to an object returned by
        apt_pkg.PackageManager().

        The second parameter *install_progress* refers to an InstallProgress()
        object of the module apt.progress.

        This releases a system lock in newer versions, if there is any,
        and reestablishes it afterwards.
        )
startUpdateAttributeErrorstart_updater$   pkgsystem_is_lockedpkgsystem_unlock_innerr   pkgsystem_lock_innerfinishUpdatefinish_update)r*   r   install_progress
did_unlockr   r   r   r   install_archivesK  s.   


zCache.install_archivesc              	   C   s   |du r
t jj }|du rt jj }|dusJ t T t| j}t	|}| j
6 	 | |||}| ||}||jkrBn||jkrKtd||jkrQntd| |  q/W d   n1 sfw   Y  W d   n1 suw   Y  ||jkS )a!  Apply the marked changes to the cache.

        The first parameter, *fetch_progress*, refers to a FetchProgress()
        object as found in apt.progress, the default being
        apt.progress.FetchProgress().

        The second parameter, *install_progress*, is a
        apt.progress.InstallProgress() object.

        The keyword-only parameter *allow_unauthenticated* specifies whether
        to allow unauthenticated downloads. If not specified, it defaults to
        the configuration option `APT::Get::AllowUnauthenticated`.
        NTzinstallArchives() failedz<internal-error: unknown result code from InstallArchives: %s)rv   r^   rw   r   r   r$   
SystemLockr   rC   r   r\   r   r  RESULT_COMPLETEDRESULT_FAILEDr   RESULT_INCOMPLETEshutdown)r*   r   r  r   r   r   r   r   r   r   commitm  s<   





zCache.commitc                 C   rb   )z Unmark all changes N)rC   initrd   r   r   r   clear  re   zCache.clearc                 C      |  d dS )z@ called internally if the cache has changed, emit a signal then r8   Nrt   rd   r   r   r   r8     re   zCache.cache_post_changec                 C   r  )zS called internally if the cache is about to change, emit
            a signal then r   Nr  rd   r   r   r   r        zCache.cache_pre_changec                 C   s>   |dkrt dtd || jvrg | j|< | j| | dS )zConnect to a signal.

        .. deprecated:: 1.0

            Please use connect2() instead, as this function is very
            likely to cause a memory leak.
        r7   zAconnect() likely causes a reference cycle, use connect2() instead   N)warningswarnRuntimeWarningrH   r   )r*   rp   rq   r   r   r   rQ     s   	

zCache.connectc                 O   s.   || j vr
g | j |< | j | |||f dS )a;  Connect to a signal.

        The callback will be passed the cache as an argument, and
        any arguments passed to this function. Make sure that, if you
        pass a method of a class as your callback, your class does not
        contain a reference to the cache.

        Cyclic references to the cache can cause issues if the Cache object
        is replaced by a new one, because the cache keeps a lot of objects and
        tens of open file descriptors.

        currently only used for cache_{post,pre}_{changed,open}.

        .. versionadded:: 1.0
        N)rI   r   ro   r   r   r   connect2  s   

zCache.connect2c                 C   s   t | jS )a  Return an `ActionGroup` object for the current cache.

        Action groups can be used to speedup actions. The action group is
        active as soon as it is created, and disabled when the object is
        deleted or when release() is called.

        You can use the action group as a context manager, this is the
        recommended way::

            with cache.actiongroup():
                for package in my_selected_packages:
                    package.mark_install()

        This way, the action group is automatically released as soon as the
        with statement block is left. It also has the benefit of making it
        clear which parts of the code run with a action group and which
        don't.
        )r$   ActionGrouprC   rd   r   r   r   actiongroup  s   zCache.actiongroupc                 C   sD   t jtjd}t t j|dD ]}t|dr dS qdS )zReturn True if the dpkg was interrupted

        All dpkg operations will fail until this is fixed, the action to
        fix the system if dpkg got interrupted is to run
        'dpkg --configure -a' as root.
        r;   updatesz[0-9]*TF)	r&   r'   dirnamer$   rR   	find_filelistdirr(   fnmatch)r*   dpkg_status_dirrn   r   r   r   dpkg_journal_dirty  s   	
zCache.dpkg_journal_dirtyc                 C   r   )z7Return the number of packages with broken dependencies.)rC   broken_countrd   r   r   r   r(    r   zCache.broken_countc                 C   r   )z2Return the number of packages marked for deletion.)rC   	del_countrd   r   r   r   delete_count
  r   zCache.delete_countc                 C   r   )z6Return the number of packages marked for installation.)rC   
inst_countrd   r   r   r   install_count  r   zCache.install_countc                 C   r   )z-Return the number of packages marked as keep.)rC   
keep_countrd   r   r   r   r-    r   zCache.keep_count)NNFr/   )F)NNN)TF)Nr   TN)0r   r   r   r   r+   rc   r7   rY   rt   r]   rz   rj   r,   r0   r   r   r   r   r   r   r   r   r   r   r   propertyr   r   r   r   r   r   r   r   r   r  r  r  r8   r   rQ   r  r   r'  r(  r*  r,  r-  r   r   r   r   r4   R   s    
.
 






$

"

!
3#
:



r4   c                   @   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S )ProblemResolverzuResolve problems due to dependencies and conflicts.

    The first argument 'cache' is an instance of apt.Cache.
    c                 C   s   t |j| _|| _d S r/   )r$   r/  rC   	_resolverrA   r*   cacher   r   r   r+   #  s   
zProblemResolver.__init__c                 C      | j |j dS )z'Reset the package to the default state.N)r0  r  r   r*   r   r   r   r   r  (  rg   zProblemResolver.clearc                 C   r3  )z)Protect a package so it won't be removed.N)r0  protectr   r4  r   r   r   r5  -  rg   zProblemResolver.protectc                 C   r3  )zMark a package for removal.N)r0  r   r   r4  r   r   r   r   2  rg   zProblemResolver.removec                 C   "   | j   | j  | j   dS )z:Resolve dependencies, try to remove packages where needed.N)rA   r   r0  resolver8   rd   r   r   r   r7  7     

zProblemResolver.resolvec                 C   r6  )z4Resolve dependencies, do not try to remove packages.N)rA   r   r0  resolve_by_keepr8   rd   r   r   r   r9  >  r8  zProblemResolver.resolve_by_keepN)
r   r   r   r   r+   r  r5  r   r7  r9  r   r   r   r   r/    s    r/  c                   @      e Zd ZdZdd ZdS )Filterz Filter base class c                 C   s   dS )zn Filter function, return True if the package matchs a
            filter criteria and False otherwise
        Tr   r*   r   r   r   r   applyL  s   zFilter.applyNr   r   r   r   r=  r   r   r   r   r;  I      r;  c                   @   r:  )MarkedChangesFilterz( Filter that returns all marked changes c                 C   s   |j s	|js	|jrdS dS )NTF)marked_installmarked_deletemarked_upgrader<  r   r   r   r=  W  s   zMarkedChangesFilter.applyNr>  r   r   r   r   r@  T  r?  r@  c                   @   r:  )InstalledFilterzMFilter that returns all installed packages.

    .. versionadded:: 1.0.0
    c                 C   r   r/   )is_installedr<  r   r   r   r=  e  s   zInstalledFilter.applyNr>  r   r   r   r   rD  _  s    rD  c                   @   s0   e Zd ZdZdd Zdd Zdd Zdd	 Zd
S )_FilteredCacheHelperz:Helper class for FilteredCache to break a reference cycle.c                 C   s,   i | _ g | _|d| j |d| j d S )Nr8   r6   )	_filtered_filtersr  filter_cache_post_changer1  r   r   r   r+   m  s   z_FilteredCacheHelper.__init__c                 C   s:   i | _ |D ]}| jD ]}||rd| j |j<  nq
qdS )z internal helper to refilter TN)rG  rH  r=  rp   )r*   r2  r   rn   r   r   r   _reapply_filterv  s   

z$_FilteredCacheHelper._reapply_filterc                 C   s   g | _ | j | dS zSet the current active filter.N)rH  r   r*   filterr   r   r   
set_filter  s   z_FilteredCacheHelper.set_filterc                 C   s   |  | dS z;Called internally if the cache changes, emit a signal then.N)rJ  r1  r   r   r   rI    r  z-_FilteredCacheHelper.filter_cache_post_changeN)r   r   r   r   r+   rJ  rN  rI  r   r   r   r   rF  j  s    	rF  c                   @   sb   e Zd Z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d ZdS )FilteredCacheza A package cache that is filtered.

        Can work on a existing cache or create a new one
    Nc                 C   s*   |d u r
t || _n|| _t| j| _d S r/   )r4   r2  rF  _helper)r*   r2  r^   r   r   r   r+     s   zFilteredCache.__init__c                 C   s   t | jjS r/   )r{   rQ  rG  rd   r   r   r   r     r   zFilteredCache.__len__c                 C   s
   | j | S r/   r2  r   r   r   r   r     s   
zFilteredCache.__getitem__c                 c   s     | j jD ]}| j| V  qd S r/   )rQ  rG  r2  )r*   r   r   r   r   r     s   zFilteredCache.__iter__c                 C   s   | j j S r/   )rQ  rG  r   rd   r   r   r   r     r   zFilteredCache.keysc                 C   r   r/   r   r   r   r   r   r     r   zFilteredCache.has_keyc                 C   s,   z| j | j| jjv W S  ty   Y dS w r   )r2  rp   rQ  rG  r   r   r   r   r   r     s
   zFilteredCache.__contains__c                 C   s   | j | | j  dS rK  )rQ  rN  r2  r8   rL  r   r   r   rN    s   zFilteredCache.set_filterc                 C   s   | j | j dS rO  )rQ  rI  r2  rd   r   r   r   rI    rg   z&FilteredCache.filter_cache_post_changec                 C   s   t | j|S )z)we try to look exactly like a real cache.)getattrr2  r   r   r   r   __getattr__  r   zFilteredCache.__getattr__)NN)r   r   r   r   r+   r   r   r   r   r   r   rN  rI  rT  r   r   r   r   rP    s    
rP  c                 C      t d d S )Nzcache pre changedprintrR  r   r   r   cache_pre_changed  r   rX  c                 C   rU  )Nzcache post changedrV  rR  r   r   r   cache_post_changed  r   rY  c                  C   s  t d t  ttjj } | dt	 | dt
 t d| v  | d }t |j t t|  |  D ]}| | j|ks@J q5|   |  }t t| |D ]}|jsXJ qQdD ]}tj|sht| q[tjdd t| j}ttjj }| ||d t d	 t| }|jdt	 |jdt
 |j  |t  t t| | D ]}||| jksJ qt t| t d
 ttjj d}|jdt	 |jdt
 |j  |t  t t| | D ]}||| jksJ qt t| dS )zInternal test code.zCache self testr   r8   aptitude)/tmp/pytestz/tmp/pytest/partialr@   r[  Nz.Testing filtered cache (argument is old cache)z$Testing filtered cache (no argument))r^   ) rW  r$   r  r4   rv   r^   r   r   r  rX  rY  rp   r{   r   r   r   r&   r'   rU   mkdirrR   rS   r   rC   r   r   r   rP  r2  rN  r@  rw   )r2  r   r   r   r"  r   r   filteredr   r   r   _test  sX   



r^  __main__)0
__future__r   r%  r&   r  rJ   typingr   r   r   r   r   r   r	   r
   r   r   r   r$   apt.packager   r   apt.progress.textrv   apt.progress.baser   r   r   IOErrorr   r   r   r   	Exceptionr    objectr!   r4   r/  r;  r@  rD  rF  rP  rX  rY  r^  r   r   r   r   r   <module>   sD   4     P,$<9
