U
    La                     @   sJ   d dl mZ d dlmZ d dlZG dd deZG dd deZe ZdS )    )dirname)ObserverNc                   @   s0   e Zd ZdZdd Zdd Zdd Zdd	 Zd
S )CacheManagera|  
        A naive cache implementation for listdir and digest_file

        The listdir_cache is just a giant list of PkgFile objects, and
        for simplicity it is invalidated anytime a modification occurs
        within the directory it represents. If we were smarter about
        the way that the listdir data structure were created/stored,
        then we could do more granular invalidation. In practice, this
        is good enough for now.

        The digest_cache exists on a per-file basis, because computing
        hashes on large files can get expensive, and it's very easy to
        invalidate specific filenames.
    c                 C   sH   i | _ i | _t | _| j  t | _t | _	t | _
t | _d S N)listdir_cachedigest_cacher   observerstartsetwatched	threadingLock
watch_lockdigest_locklistdir_lock)self r   4/tmp/pip-unpacked-wheel-8ds7z0ev/pypiserver/cache.py__init__   s    


zCacheManager.__init__c                 C   s   | j  z| j| W W  5 Q R  S  tk
r   | j || jkrL| | W 5 Q R X t||}|| j|< | Y W  5 Q R  S X W 5 Q R X d S r   )r   r   KeyErrorr   r   _watchlist)r   rootimpl_fnvr   r   r   listdir0   s    

zCacheManager.listdirc                 C   s   | j  z| j| }W n" tk
r8   | j|i }Y nX z|| W W  5 Q R  S  tk
r   t|}| j || jkr| | W 5 Q R X Y nX |||}|||< |W  5 Q R  S Q R X d S r   )r   r   r   
setdefaultr   r   r   r   )r   ZfpathZ	hash_algor   cacher   r   r   r   r   digest_file>   s    

zCacheManager.digest_filec                 C   s(   | j | | jjt| ||dd d S )NT)	recursive)r   addr   schedule_EventHandler)r   r   r   r   r   r   T   s    zCacheManager._watchN)__name__
__module____qualname____doc__r   r   r   r   r   r   r   r   r      s
   r   c                   @   s   e Zd Zdd Zdd ZdS )r"   c                 C   s   || _ || _d S r   )r   r   )r   r   r   r   r   r   r   Z   s    z_EventHandler.__init__c              	   C   s   | j }|jrdS |j |j| jd W 5 Q R X g }|jdkrZ||j ||j	 n||j |j
0 |j D ]\}}|D ]}||d qqxW 5 Q R X dS )zCalled by watchdog observerNmoved)r   Zis_directoryr   r   popr   Z
event_typeappendZsrc_path	dest_pathr   r   items)r   eventr   paths_Zsubcachepathr   r   r   dispatch^   s    
z_EventHandler.dispatchN)r#   r$   r%   r   r0   r   r   r   r   r"   X   s   r"   )	os.pathr   Zwatchdog.observersr   r   objectr   r"   Zcache_managerr   r   r   r   <module>   s
   M 