
R\J)                 @   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 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 m Z m Z m Z e j e  Z Gd d   d e  Z e   Z Gd d	   d	 e  Z Gd
 d   d e  Z Gd d   d e  Z Gd d   d e  Z Gd d   d e  Z i e e d  6e e
 j 6Z y* d  d l Z e e e j  <e e e j! <Wn e" e# f k
 rYn Xd d   Z$ i  Z% d d   Z& e	 j' e( d   Z) d d   Z* d S)    )unicode_literalsN   )DistlibException)cached_propertyget_cache_basepath_to_cache_dirc               @   sU   e  Z d  Z d Z d d d  Z d d   Z d d   Z d	 d
   Z d d   Z d S)Cachezt
    A class implementing a cache for resources that need to live in the file system
    e.g. shared libraries.
    Nc             C   st   | d k rO t  j j t   t d   } t  j j |  sO t  j |  qO n  t  j j t  j j |   |  _	 d S)a  
        Initialise an instance.

        :param base: The base directory where the cache should be located. If
                     not specified, this will be the ``resource-cache``
                     directory under whatever :func:`get_cache_base` returns.
        Nzresource-cache)
ospathjoinr   strisdirmakedirsabspathnormpathbase)selfr    r   @/var/www/dbchiro/venv/build/pip/pip/_vendor/distlib/resources.py__init__   s
    zCache.__init__c             C   s
   t  |  S)zN
        Converts a resource prefix to a directory name in the cache.
        )r   )r   prefixr   r   r   prefix_to_dir0   s    zCache.prefix_to_dirc             C   s   d S)z
        Is the cache stale for the given resource?

        :param resource: The :class:`Resource` being cached.
        :param path: The path of the resource in the cache.
        :return: True if the cache is stale.
        Tr   )r   resourcer
   r   r   r   is_stale6   s    	zCache.is_stalec          	   C   s   | j  j |  \ } } | d k r- | } n t j j |  j |  j |  |  } t j j |  } t j j |  s t j	 |  n  t j j
 |  s d } n |  j | |  } | r t | d   } | j | j  Wd QXn  | S)z
        Get a resource into the cache,

        :param resource: A :class:`Resource` instance.
        :return: The pathname of the resource in the cache.
        NTwb)finderget_cache_infor	   r
   r   r   r   dirnamer   r   existsr   openwritebytes)r   r   r   r
   resultr   stalefr   r   r   getA   s    	$	z	Cache.getc             C   s   g  } x t  j |  j  D] } t  j j |  j |  } yZ t  j j |  s^ t  j j |  rn t  j |  n" t  j j |  r t	 j
 |  n  Wq t k
 r | j |  Yq Xq W| S)z"
        Clear the cache.
        )r	   listdirr   r
   r   islinkisfileremover   shutilrmtree	Exceptionappend)r   not_removedfnr   r   r   clearZ   s    $zCache.clear)	__name__
__module____qualname____doc__r   r   r   r%   r0   r   r   r   r   r      s   r   c               @   s   e  Z d  Z d d   Z d S)ResourceBasec             C   s   | |  _  | |  _ d  S)N)r   name)r   r   r6   r   r   r   r   n   s    	zResourceBase.__init__N)r1   r2   r3   r   r   r   r   r   r5   m   s   r5   c               @   s^   e  Z d  Z d Z d Z d d   Z e d d    Z e d d    Z e d	 d
    Z	 d S)Resourcez
    A class representing an in-package resource, such as a data file. This is
    not normally instantiated by user code, but rather by a
    :class:`ResourceFinder` which manages the resource.
    Fc             C   s   |  j  j |   S)z
        Get the resource as a stream.

        This is not a property to make it obvious that it returns a new stream
        each time.
        )r   
get_stream)r   r   r   r   	as_stream{   s    zResource.as_streamc             C   s   t  j |   S)N)cacher%   )r   r   r   r   	file_path   s    zResource.file_pathc             C   s   |  j  j |   S)N)r   	get_bytes)r   r   r   r   r!      s    zResource.bytesc             C   s   |  j  j |   S)N)r   get_size)r   r   r   r   size   s    zResource.sizeN)
r1   r2   r3   r4   is_containerr9   r   r;   r!   r>   r   r   r   r   r7   s   s   	r7   c               @   s(   e  Z d  Z d Z e d d    Z d S)ResourceContainerTc             C   s   |  j  j |   S)N)r   get_resources)r   r   r   r   	resources   s    zResourceContainer.resourcesN)r1   r2   r3   r?   r   rB   r   r   r   r   r@      s   r@   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 d   Z
 d d   Z d d   Z d d   Z d d   Z e e j j  Z d S)ResourceFinderz4
    Resource finder for file system resources.
    c             C   sC   | |  _  t | d d   |  _ t j j t | d d   |  _ d  S)N
__loader____file__ )modulegetattrloaderr	   r
   r   r   )r   rG   r   r   r   r      s    	zResourceFinder.__init__c             C   s   t  j j |  S)N)r	   r
   realpath)r   r
   r   r   r   _adjust_path   s    zResourceFinder._adjust_pathc             C   sA   | j  d  } | j d |  j  t j j |   } |  j |  S)N/r   )splitinsertr   r	   r
   r   rK   )r   resource_namepartsr"   r   r   r   
_make_path   s    zResourceFinder._make_pathc             C   s   t  j j |  S)N)r	   r
   r   )r   r
   r   r   r   _find   s    zResourceFinder._findc             C   s   d  | j  f S)N)r
   )r   r   r   r   r   r      s    zResourceFinder.get_cache_infoc             C   sd   |  j  |  } |  j |  s' d  } n9 |  j |  rH t |  |  } n t |  |  } | | _ | S)N)rQ   rR   _is_directoryr@   r7   r
   )r   rO   r
   r"   r   r   r   find   s    		zResourceFinder.findc             C   s   t  | j d  S)Nrb)r   r
   )r   r   r   r   r   r8      s    zResourceFinder.get_streamc             C   s)   t  | j d   } | j   SWd  QXd  S)NrU   )r   r
   read)r   r   r$   r   r   r   r<      s    zResourceFinder.get_bytesc             C   s   t  j j | j  S)N)r	   r
   getsize)r   r   r   r   r   r=      s    zResourceFinder.get_sizec                s5   d d     t    f d d   t j | j  D  S)Nc             S   s   |  d k o |  j  d  S)N__pycache__.pyc.pyo)rY   rZ   )endswith)r$   r   r   r   allowed   s    z-ResourceFinder.get_resources.<locals>.allowedc                s"   g  |  ] }   |  r |  q Sr   r   ).0r$   )r\   r   r   
<listcomp>   s   	 z0ResourceFinder.get_resources.<locals>.<listcomp>)setr	   r&   r
   )r   r   r   )r\   r   rA      s    zResourceFinder.get_resourcesc             C   s   |  j  | j  S)N)rS   r
   )r   r   r   r   r   r?      s    zResourceFinder.is_containerN)r1   r2   r3   r4   r   rK   rQ   rR   r   rT   r8   r<   r=   rA   r?   staticmethodr	   r
   r   rS   r   r   r   r   rC      s   rC   c                   s   e  Z d  Z d Z   f 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   S)ZipResourceFinderz6
    Resource finder for resources in .zip files.
    c                s   t  t |   j |  |  j j } d t |  |  _ t |  j d  rY |  j j |  _ n t	 j
 | |  _ t |  j  |  _ d  S)Nr   _files)superra   r   rI   archivelen
prefix_lenhasattrrb   	zipimport_zip_directory_cachesortedindex)r   rG   rd   )	__class__r   r   r      s    zZipResourceFinder.__init__c             C   s   | S)Nr   )r   r
   r   r   r   rK      s    zZipResourceFinder._adjust_pathc             C   s   | |  j  d   } | |  j k r+ d } ns | rT | d t j k rT | t j } n  t j |  j |  } y |  j | j |  } Wn t k
 r d } Yn X| s t j	 d | |  j
 j  n t j	 d | |  j
 j  | S)NTr   Fz_find failed: %r %rz_find worked: %r %r)rf   rb   r	   sepbisectrk   
startswith
IndexErrorloggerdebugrI   r   )r   r
   r"   ir   r   r   rR      s    	zZipResourceFinder._findc             C   s3   |  j  j } | j d t |  d   } | | f S)Nr   )rI   rd   r
   re   )r   r   r   r
   r   r   r   r      s    z ZipResourceFinder.get_cache_infoc             C   s   |  j  j | j  S)N)rI   get_datar
   )r   r   r   r   r   r<      s    zZipResourceFinder.get_bytesc             C   s   t  j |  j |   S)N)ioBytesIOr<   )r   r   r   r   r   r8      s    zZipResourceFinder.get_streamc             C   s%   | j  |  j d   } |  j | d S)N   )r
   rf   rb   )r   r   r
   r   r   r   r=     s    zZipResourceFinder.get_sizec             C   s   | j  |  j d   } | r? | d t j k r? | t j 7} n  t |  } t   } t j |  j |  } xt | t |  j  k  r |  j | j |  s Pn  |  j | | d   } | j	 | j
 t j d  d  | d 7} ql W| S)Nr   r   rm   )r
   rf   r	   rn   re   r_   ro   rk   rp   addrM   )r   r   r
   plenr"   rt   sr   r   r   rA     s    	 zZipResourceFinder.get_resourcesc             C   s   | |  j  d   } | r< | d t j k r< | t j 7} n  t j |  j |  } y |  j | j |  } Wn t k
 r d } Yn X| S)Nr   Frm   )rf   r	   rn   ro   rk   rp   rq   )r   r
   rt   r"   r   r   r   rS     s    zZipResourceFinder._is_directory)r1   r2   r3   r4   r   rK   rR   r   r<   r8   r=   rA   rS   r   r   )rl   r   ra      s   ra   c             C   s   | t  t |   <d  S)N)_finder_registrytype)rI   finder_makerr   r   r   register_finder,  s    r   c             C   s   |  t  k r t  |  } n |  t j k r5 t |   n  t j |  } t | d d  } | d k ro t d   n  t | d d  } t j t |   } | d k r t d |    n  | |  } | t  |  <| S)z
    Return a resource finder for a package.
    :param package: The name of the package.
    :return: A :class:`ResourceFinder` instance for the package.
    __path__Nz8You cannot get a finder for a module, only for a packagerD   zUnable to locate finder for %r)	_finder_cachesysmodules
__import__rH   r   r|   r%   r}   )packager"   rG   r
   rI   r~   r   r   r   r   2  s    
r   	__dummy__c             C   sz   d } t  j |   t j j |   } t j t |   } | rv t } t j	 j
 |  d  | _ | | _ | |  } n  | S)z
    Return a resource finder for a path, which should represent a container.

    :param path: The path.
    :return: A :class:`ResourceFinder` instance for the path.
    NrF   )pkgutilget_importerr   path_importer_cacher%   r|   r}   _dummy_moduler	   r
   r   rE   rD   )r
   r"   rI   r   rG   r   r   r   finder_for_pathN  s    	r   )+
__future__r   ro   rv   loggingr	   r   r*   r   typesrh   rF   r   utilr   r   r   	getLoggerr1   rr   objectr   r:   r5   r7   r@   rC   ra   r}   zipimporterr|   _frozen_importlibSourceFileLoader
FileFinderImportErrorAttributeErrorr   r   r   
ModuleTyper   r   r   r   r   r   r   <module>   sB   Q	9M