
S܋             m   @   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 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 y d d l m Z m Z Wn( e k
 r>d d l m Z m Z Yn Xy e Wn" e k
 rkd d l m Z Yn Xy$ e d d   Z d d l  m! Z" Wn: e k
 re# Z d d	 l$ m" Z" d d d
 d  Z% Yn Xd d l m& Z& y& d d l m' Z' m( Z( m) Z) d Z* Wn e k
 rd Z* Yn Xd d l m+ Z, d d l- m. Z. m/ Z/ e j0 d d f k ohe j1 j2 d k r}d d l3 j4 Z5 n d Z5 y d d l6 Z6 Wn e k
 rYn Xd d d  Z7 i  Z8 d d   Z9 d d   Z: d d   Z; d d   Z< d d    Z= d! d"   Z> d# d$   Z? d% d   Z@ ZA d& d'   ZB d( d) d* d+ d, d- d. d/ d0 d1 d2 d3 d4 d5 d6 d7 d8 d9 d: d; d< d= d> d? d@ dA dB dC dD dE dF dG dH dI dJ dK dL dM dN dO dP dQ dR dS dT dU dV dW dX dY dZ d[ d\ d] d^ d_ d` da db dc dd de df dg dh di dj dk dl gE ZC Gdm dC   dC eD  ZE Gdn dD   dD eE  ZF Gdo dE   dE eE  ZG Gdp dF   dF eE  ZH i  ZI e jJ d d  ZK d ZL dq ZM dr ZN d ZO dr ZP ds dh   ZQ dt d*   ZR g  du dv  ZS dw dx   ZT dy dz   ZU e jV d{  ZW e jV d|  ZX eU ZY d} dM   ZZ d~ d)   Z[ e[ Z\ d d+   Z] d d,   Z^ d d d-  Z_ d d.   Z` Gd d[   d[  Za Gd d\   d\ ea  Zb Gd d>   d> ec  Zd Gd d=   d= ec  Ze ee Zf Gd dG   dG eg  Zh Gd d?   d?  Zi d d<   Zj d dJ   Zk d dK   Zl d dP   Zm d dQ   Zn Gd d   d ec  Zo eo jp Zq eo jr Zr Gd db   db  Zs eQ ec es  Gd dc   dc es  Zt Gd dd   dd et  Zu eQ ev d  eu  e5 d k	 reQ e5 jw eu  n  Gd d`   d` es  Zx ex   Zy Gd d   d ez  Z{ Gd d   d e{  Z| Gd d   d e j}  Z~ Gd de   de et  Z eQ e j e  Gd d]   d] ex  Z Gd d^   d^ eu  Z Gd d_   d_ e  Z e9 d d i  d df   Z d d d9  Z d d d  Z e e j e  d d d  Z e ec e  d d d  Z e e j e  e5 d k	 re e5 j e  n  e9 d d i  e9 d d i  d dg   Z d d   Z d d6   Z d d di  Z d d   Z e e j e  e e j e  e5 d k	 re e5 j e  n  d d   Z e ec e  d dU   Z i  d d  Z d d   Z d dN   Z e jV d  j Z e jV d  j Z e jV d  j Z e jV d  j Z e jV d  j Z e jV d  j Z e jV d  j Z e jV d  j Z e jV d e j e j B j Z e jV d e j  Z i d d 6d d 6d d 6d d 6d d 6j Z d d   Z d dI   Z Gd dB   dB ec  Z d d   Z Gd d@   d@ ec  Z Gd d   d e  Z i e d 6e d 6e d 6Z d d   Z d dH   Z Gd dA   dA  Z i d d 6d d 6d d 6d d 6d d 6d d 6Z d d   Z d d   Z d dT   Z d dO   Z d d   Z ei   Z d d   Z e e    ed j   Z e9 d d7 e e j Z e j Z e j Z e j[ Z[ e[ Z\ e d d    g  e _ e e e j e j   d S)aZ  
Package resource API
--------------------

A resource is a logical file contained within a package, or a logical
subdirectory thereof.  The package resource API expects resource names
to have their path parts separated with ``/``, *not* whatever the local
path separator is.  Do not use os.path operations to manipulate resource
names being passed into the API.

The package resource API is designed to work with normal filesystem packages,
.egg files, and unpacked .egg files.  It can also work in a limited way with
.zip files and with custom PEP 302 loaders that support the ``get_data()``
method.
    N)get_importer)urlparse
urlunparse)ImmutableSetc             C   s
   |  j    S)N)next)o r   //usr/lib/python3/dist-packages/pkg_resources.py<lambda>1   s    r
   )StringIO)BytesIOc             C   sY   | d  k r t    } n  | d  k r- | } n  t t t |   j   |  d  | |  d  S)Nexec)globalsr   compileopenread)fnZglobsZlocsr   r   r	   execfile6   s
    	r   )utime)mkdirrenameunlinkTF)r   )isdirsplit   Zcpythoni  c             C   s^   t  s t d   n  t |   \ } } | rZ | rZ t |  rZ t |  t | |  n  d  S)Nz*"os.mkdir" not supported on this platform.)WRITE_SUPPORTIOErrorr   r   _bypass_ensure_directoryr   )namemodedirnamefilenamer   r   r	   r   T   s    
r   c             K   s-   t    j |  t j t j | |    d  S)N)r   update_state_varsdictfromkeys)Zvartypekwr   r   r	   _declare_state`   s    r'   c              C   sL   i  }  t    } x6 t j   D]( \ } } | d | | |  |  | <q W|  S)NZ_sget_)r   r#   items)stategkvr   r   r	   __getstate__d   s
    	 r-   c             C   sJ   t    } x: |  j   D], \ } } | d t | | | | |  q W|  S)NZ_sset_)r   r(   r#   )r)   r*   r+   r,   r   r   r	   __setstate__k   s    	$r.   c             C   s
   |  j    S)N)copy)valr   r   r	   
_sget_dictq   s    r1   c             C   s   | j    | j |  d  S)N)clearr"   )keyobr)   r   r   r	   
_sset_dictt   s    
r5   c             C   s
   |  j    S)N)r-   )r0   r   r   r	   _sget_objectx   s    r6   c             C   s   | j  |  d  S)N)r.   )r3   r4   r)   r   r   r	   _sset_object{   s    r7   c              G   s   d  S)Nr   )argsr   r   r	   r
   ~   s    c              C   s   t    }  t j |   } | d k	 r~ t j d k r~ y3 d d j t   d d   | j d  f }  Wq~ t k
 rz Yq~ Xn  |  S)aZ  Return this platform's maximum compatible version.

    distutils.util.get_platform() normally reports the minimum version
    of Mac OS X that would be required to *use* extensions produced by
    distutils.  But what we want when checking compatibility is to know the
    version of Mac OS X that we are *running*.  To allow usage of packages that
    explicitly require a newer version of Mac OS X, we must also know the
    current version of the OS.

    If this condition occurs for any other platform with a version in its
    platform strings, this function should be extended accordingly.
    Ndarwinzmacosx-%s-%s.   r   )	get_build_platformmacosVersionStringmatchsysplatformjoin_macosx_versgroup
ValueError)platmr   r   r	   get_supported_platform   s    	3rG   require
run_scriptget_providerget_distributionload_entry_pointget_entry_mapget_entry_infoiter_entry_pointsresource_stringresource_streamresource_filenameresource_listdirresource_existsresource_isdirdeclare_namespaceworking_setadd_activation_listenerfind_distributionsset_extraction_pathcleanup_resourcesget_default_cacheEnvironment
WorkingSetResourceManagerDistributionRequirement
EntryPointResolutionErrorVersionConflictDistributionNotFoundUnknownExtraExtractionErrorparse_requirementsparse_version	safe_namesafe_versionget_platformcompatible_platformsyield_linessplit_sections
safe_extrato_filenameinvalid_markerevaluate_markerensure_directorynormalize_pathEGG_DISTBINARY_DISTSOURCE_DISTCHECKOUT_DISTDEVELOP_DISTIMetadataProviderIResourceProviderFileMetadataPathMetadataEggMetadataEmptyProviderempty_providerNullProviderEggProviderDefaultProviderZipProviderregister_finderregister_namespace_handlerregister_loader_typefixup_namespace_packagesr   run_mainAvailableDistributionsc               @   s"   e  Z d  Z d Z d d   Z d S)rc   z.Abstract base for dependency resolution errorsc             C   s   |  j  j t |  j  S)N)	__class____name__reprr8   )selfr   r   r	   __repr__   s    zResolutionError.__repr__N)r   
__module____qualname____doc__r   r   r   r   r	   rc      s   c               @   s   e  Z d  Z d Z d S)rd   zAAn already-installed version conflicts with the requested versionN)r   r   r   r   r   r   r   r	   rd      s   c               @   s   e  Z d  Z d Z d S)re   z&A requested distribution was not foundN)r   r   r   r   r   r   r   r	   re      s   c               @   s   e  Z d  Z d Z d S)rf   z>Distribution doesn't have an "extra feature" of the given nameN)r   r   r   r   r   r   r   r	   rf      s   r;      c             C   s   | t  |  <d S)a  Register `provider_factory` to make providers for `loader_type`

    `loader_type` is the type or class of a PEP 302 ``module.__loader__``,
    and `provider_factory` is a function that, passed a *module* object,
    returns an ``IResourceProvider`` for that module.
    N)_provider_factories)Zloader_typeZprovider_factoryr   r   r	   r      s    c             C   s   t  |  t  r2 t j |   p1 t t |    d Sy t j |  } Wn) t k
 rn t	 |   t j |  } Yn Xt
 | d d  } t t |  |  S)z?Return an IResourceProvider for the named module or requirementr   
__loader__N)
isinstancera   rW   findrH   strr?   modulesKeyError
__import__getattr_find_adapterr   )ZmoduleOrReqmoduleloaderr   r   r	   rJ      s    #
c             C   s   |  s t  j   d } | d k rz d } t j j |  rz t t d  rw t j |  } d | k rt | d } qt qw qz n  |  j | j	 d   n  |  d S)Nr    z0/System/Library/CoreServices/SystemVersion.plist	readPlistZProductVersionr:   )
r@   Zmac_verospathexistshasattrplistlibr   appendr   )_cacheversionZplistZplist_contentr   r   r	   rB      s    rB   c             C   s   i d d 6d d 6j  |  |   S)NZppcZPowerPCZPower_Macintosh)get)machiner   r   r	   _macosx_arch   s    r   c              C   s   y d d l  m }  Wn" t k
 r8 d d l m }  Yn X|    } t j d k r | j d  r yT t   } t j	   d j
 d d  } d t | d  t | d	  t |  f SWq t k
 r Yq Xn  | S)
zReturn this platform's string for platform-specific distributions

    XXX Currently this is the same as ``distutils.util.get_platform()``, but it
    needs some hacks for Linux and Mac OS X.
    r   )rl   r9   zmacosx-    _zmacosx-%d.%d-%sr   )	sysconfigrl   ImportErrorZdistutils.utilr?   r@   
startswithrB   r   unamereplaceintr   rD   )rl   rE   r   r   r   r   r	   r<      s    		r<   zmacosx-(\d+)\.(\d+)-(.*)zdarwin-(\d+)\.(\d+)\.(\d+)-(.*)c             C   sO  |  d k s$ | d k s$ |  | k r( d St  j |  } | rKt  j |   } | s t j |   } | r t | j d   } d | j d  | j d  f } | d k r | d k s | d k r | d	 k r d Sn  d
 S| j d  | j d  k s| j d  | j d  k rd
 St | j d   t | j d   k rGd
 Sd Sd
 S)zCan code for the `provided` platform run on the `required` platform?

    Returns true if either platform is ``None``, or the platforms are equal.

    XXX Needs compatibility checks for Linux and other unixy OSes.
    NTr   z%s.%sr;      z10.3   z10.4Fr   )r=   r>   darwinVersionStringr   rC   )ZprovidedZrequiredZreqMacZprovMacZ
provDarwinZdversionZmacosversionr   r   r	   rm     s*    $"*c             C   sN   t  j d  j } | d } | j   | | d <t |   d j | |  d S)z@Locate distribution `dist_spec` and run its `script_name` scriptr   r   r   N)r?   	_getframe	f_globalsr2   rH   rI   )Z	dist_specscript_namensr   r   r   r	   rI   M  s
    


c             C   sd   t  |  t  r! t j |   }  n  t  |  t  r? t |   }  n  t  |  t  s` t d |    n  |  S)z@Return a current distribution object for a Requirement or stringz-Expected string, Requirement, or Distribution)r   
basestringra   parserJ   r`   	TypeError)distr   r   r	   rK   X  s    c             C   s   t  |   j | |  S)zDReturn `name` entry point of `group` for `dist` or raise ImportError)rK   rL   )r   rC   r   r   r   r	   rL   b  s    c             C   s   t  |   j |  S)z=Return the entry point map for `group`, or the full entry map)rK   rM   )r   rC   r   r   r	   rM   f  s    c             C   s   t  |   j | |  S)z<Return the EntryPoint object for `group`+`name`, or ``None``)rK   rN   )r   rC   r   r   r   r	   rN   j  s    c               @   sX   e  Z d  Z d d   Z d d   Z d d   Z d d   Z d	 d
   Z d d   Z d S)r{   c             C   s   d S)z;Does the package's distribution contain the named metadata?Nr   )r   r   r   r	   has_metadataq  s    zIMetadataProvider.has_metadatac             C   s   d S)z'The named metadata resource as a stringNr   )r   r   r   r	   get_metadatat  s    zIMetadataProvider.get_metadatac             C   s   d S)zYield named metadata resource as list of non-blank non-comment lines

       Leading and trailing whitespace is stripped from each line, and lines
       with ``#`` as the first non-blank character are omitted.Nr   )r   r   r   r	   get_metadata_linesw  s    z$IMetadataProvider.get_metadata_linesc             C   s   d S)z>Is the named metadata a directory?  (like ``os.path.isdir()``)Nr   )r   r   r   r	   metadata_isdir}  s    z IMetadataProvider.metadata_isdirc             C   s   d S)z?List of metadata names in the directory (like ``os.listdir()``)Nr   )r   r   r   r	   metadata_listdir  s    z"IMetadataProvider.metadata_listdirc             C   s   d S)z=Execute the named script in the supplied namespace dictionaryNr   )r   	namespacer   r   r	   rI     s    zIMetadataProvider.run_scriptN)	r   r   r   r   r   r   r   r   rI   r   r   r   r	   r{   o  s   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)r|   z3An object that provides access to package resourcesc             C   s   d S)zdReturn a true filesystem path for `resource_name`

        `manager` must be an ``IResourceManager``Nr   )managerresource_namer   r   r	   get_resource_filename  s    z'IResourceProvider.get_resource_filenamec             C   s   d S)ziReturn a readable file-like object for `resource_name`

        `manager` must be an ``IResourceManager``Nr   )r   r   r   r   r	   get_resource_stream  s    z%IResourceProvider.get_resource_streamc             C   s   d S)zmReturn a string containing the contents of `resource_name`

        `manager` must be an ``IResourceManager``Nr   )r   r   r   r   r	   get_resource_string  s    z%IResourceProvider.get_resource_stringc             C   s   d S)z,Does the package contain the named resource?Nr   )r   r   r   r	   has_resource  s    zIResourceProvider.has_resourcec             C   s   d S)z>Is the named resource a directory?  (like ``os.path.isdir()``)Nr   )r   r   r   r	   rU     s    z IResourceProvider.resource_isdirc             C   s   d S)z?List of resource names in the directory (like ``os.listdir()``)Nr   )r   r   r   r	   rS     s    z"IResourceProvider.resource_listdirN)
r   r   r   r   r   r   r   r   rU   rS   r   r   r   r	   r|     s   c               @   s  e  Z d  Z d Z d d d  Z e d d    Z e d d    Z d	 d
   Z d d   Z	 d d   Z
 d d d  Z d d   Z d d   Z d d d d d  Z d d d d d  Z d d d d d  Z d d   Z d d    Z d! d"   Z d# d$   Z d% d&   Z d S)'r^   zDA collection of active distributions on sys.path (or a similar list)Nc             C   s^   g  |  _  i  |  _ i  |  _ g  |  _ | d k r< t j } n  x | D] } |  j |  qC Wd S)z?Create working set from list of path entries (default=sys.path)N)entries
entry_keysby_key	callbacksr?   r   	add_entry)r   r   entryr   r   r	   __init__  s    				zWorkingSet.__init__c             C   sm   |    } y d d l  m } Wn t k
 r5 | SYn Xy | j |  Wn t k
 rh |  j |  SYn X| S)z1
        Prepare the master working set.
        r   )__requires__)__main__r   r   rH   rd   _build_from_requirements)clswsr   r   r   r	   _build_master  s    		zWorkingSet._build_masterc             C   s   |  g   } t  |  } | j | t    } x | D] } | j |  q4 Wx0 t j D]% } | | j k rU | j |  qU qU W| j t j d d  <| S)zQ
        Build a working set from a requirement spec. Rewrites sys.path.
        N)rh   resolver]   addr?   r   r   r   )r   Zreq_specr   reqsdistsr   r   r   r   r	   r     s    z#WorkingSet._build_from_requirementsc             C   sT   |  j  j | g   |  j j |  x* t | d  D] } |  j | | d  q3 Wd S)a  Add a path item to ``.entries``, finding any distributions on it

        ``find_distributions(entry, True)`` is used to find distributions
        corresponding to the path entry, and they are added.  `entry` is
        always appended to ``.entries``, even if it is already present.
        (This is because ``sys.path`` can contain the same value more than
        once, and the ``.entries`` of the ``sys.path`` WorkingSet should always
        equal ``sys.path``.)
        TFN)r   
setdefaultr   r   rY   r   )r   r   r   r   r   r	   r     s    
zWorkingSet.add_entryc             C   s   |  j  j | j  | k S)z9True if `dist` is the active distribution for its project)r   r   r3   )r   r   r   r   r	   __contains__  s    zWorkingSet.__contains__c             C   sG   |  j  j | j  } | d k	 r? | | k r? t | |   n | Sd S)a  Find a distribution matching requirement `req`

        If there is an active distribution for the requested project, this
        returns it as long as it meets the version requirement specified by
        `req`.  But, if there is an active distribution for the project and it
        does *not* meet the `req` requirement, ``VersionConflict`` is raised.
        If there is no active distribution for the requested project, ``None``
        is returned.
        N)r   r   r3   rd   )r   reqr   r   r   r	   r     s    
zWorkingSet.findc             c   sg   x` |  D]X } | j  |  } | d k rG x4 | j   D] } | Vq5 Wq | | k r | | Vq q Wd S)a  Yield entry point objects from `group` matching `name`

        If `name` is None, yields all entry points in `group` from all
        distributions in the working set, otherwise only ones matching
        both `group` and `name` are yielded (in distribution order).
        N)rM   values)r   rC   r   r   r   epr   r   r	   rO     s    zWorkingSet.iter_entry_pointsc             C   sQ   t  j d  j } | d } | j   | | d <|  j |  d j | |  d S)z?Locate distribution for `requires` and run `script_name` scriptr   r   r   N)r?   r   r   r2   rH   rI   )r   requiresr   r   r   r   r   r	   rI     s
    


zWorkingSet.run_scriptc             c   sp   i  } xc |  j  D]X } | |  j k r+ q n  x: |  j | D]+ } | | k r9 d | | <|  j | Vq9 q9 Wq Wd S)zYield distributions for non-duplicate projects in the working set

        The yield order is the order in which the items' path entries were
        added to the working set.
        r   N)r   r   r   )r   seenitemr3   r   r   r	   __iter__  s    
zWorkingSet.__iter__TFc             C   s   | r | j  |  j |  n  | d k r4 | j } n  |  j j | g   } |  j j | j g   } | r~ | j |  j k r~ d S| |  j | j <| j | k r | j | j  n  | j | k r | j | j  n  |  j |  d S)a  Add `dist` to working set, associated with `entry`

        If `entry` is unspecified, it defaults to the ``.location`` of `dist`.
        On exit from this routine, `entry` is added to the end of the working
        set's ``.entries`` (if it wasn't already present).

        `dist` is only added to the working set if it's for a project that
        doesn't already have a distribution in the set, unless `replace=True`.
        If it's added, any callbacks registered with the ``subscribe()`` method
        will be called.
        N)		insert_onr   locationr   r   r3   r   r   
_added_new)r   r   r   insertr   keysZkeys2r   r   r	   r   )  s    zWorkingSet.addc             C   s  t  |  d d d  } i  } i  } g  } xc| r| j d  } | | k rU q. n  | j | j  }	 |	 d k r?|  j j | j  }	 |	 d k s |	 | k r/| r/|  }
 | d k r |	 d k r t |  j  } q t g   } t g   }
 n  | j | |
 |  }	 | | j <|	 d k r/t	 |   q/n  | j
 |	  n  |	 | k r]t |	 |   n  | j |	 j | j  d d d   d | | <q. W| S)ae  List all distributions needed to (recursively) meet `requirements`

        `requirements` must be a sequence of ``Requirement`` objects.  `env`,
        if supplied, should be an ``Environment`` instance.  If
        not supplied, it defaults to all distributions available within any
        entry or distribution in the working set.  `installer`, if supplied,
        will be invoked with each requirement that cannot be met by an
        already-installed distribution; it should return a ``Distribution`` or
        ``None``.

        Unless `replace_conflicting=True`, raises a VersionConflict exception if
        any requirements are found on the path that have the correct name but
        the wrong version.  Otherwise, if an `installer` is supplied it will be
        invoked to obtain the correct version of the requirement and activate
        it.
        Nr   r   Tr   )listpopr   r3   r   r]   r   r^   
best_matchre   r   rd   extendr   extras)r   requirementsenv	installerZreplace_conflictingZ	processedZbestZto_activater   r   r   r   r   r	   r   G  s6    	 &zWorkingSet.resolvec             C   sP  t  |  } | j   i  } i  } | d k rJ t |  j  } | | 7} n
 | | } |  j g   }	 t  t |	 j |    x | D] }
 x | |
 D] } | j   g } y |	 j | | |  } Wn9 t	 k
 r t
 j   d } | | | <| r w n PYq Xt  t |	 j |   | j t j |   Pq Wq Wt  |  } | j   | | f S)as  Find all activatable distributions in `plugin_env`

        Example usage::

            distributions, errors = working_set.find_plugins(
                Environment(plugin_dirlist)
            )
            # add plugins+libs to sys.path
            map(working_set.add, distributions)
            # display errors
            print('Could not load', errors)

        The `plugin_env` should be an ``Environment`` instance that contains
        only distributions that are in the project's "plugin directory" or
        directories. The `full_env`, if supplied, should be an ``Environment``
        contains all currently-available distributions.  If `full_env` is not
        supplied, one is created automatically from the ``WorkingSet`` this
        method is called on, which will typically mean that every directory on
        ``sys.path`` will be scanned for distributions.

        `installer` is a standard installer callback as used by the
        ``resolve()`` method. The `fallback` flag indicates whether we should
        attempt to resolve older versions of a plugin if the newest version
        cannot be resolved.

        This method returns a 2-tuple: (`distributions`, `error_info`), where
        `distributions` is a list of the distributions found in `plugin_env`
        that were loadable, along with any other distributions that are needed
        to resolve their dependencies.  `error_info` is a dictionary mapping
        unloadable plugin distributions to an exception instance describing the
        error that occurred. Usually this will be a ``DistributionNotFound`` or
        ``VersionConflict`` instance.
        Nr   )r   sortr]   r   r   mapr   as_requirementr   rc   r?   exc_infor"   r$   r%   )r   Z
plugin_envZfull_envr   ZfallbackZplugin_projectsZ
error_infoZdistributionsr   Z
shadow_setproject_namer   r   Z	resolveesr,   r   r   r	   find_plugins  s6    $


	
zWorkingSet.find_pluginsc             G   s7   |  j  t |   } x | D] } |  j |  q W| S)a  Ensure that distributions matching `requirements` are activated

        `requirements` must be a string or a (possibly-nested) sequence
        thereof, specifying the distributions and versions required.  The
        return value is a sequence of the distributions that needed to be
        activated to fulfill the requirements; all relevant distributions are
        included, even if they were already activated in this working set.
        )r   rh   r   )r   r   Zneededr   r   r   r	   rH     s    	zWorkingSet.requirec             C   sB   | |  j  k r d S|  j  j |  x |  D] } | |  q* Wd S)zAInvoke `callback` for all distributions (including existing ones)N)r   r   )r   callbackr   r   r   r	   	subscribe  s
    zWorkingSet.subscribec             C   s"   x |  j  D] } | |  q
 Wd  S)N)r   )r   r   r   r   r   r	   r     s    zWorkingSet._added_newc             C   s<   |  j  d  d   |  j j   |  j j   |  j d  d   f S)N)r   r   r/   r   r   )r   r   r   r	   r-     s    (zWorkingSet.__getstate__c             C   sZ   | \ } } } } | d  d   |  _  | j   |  _ | j   |  _ | d  d   |  _ d  S)N)r   r/   r   r   r   )r   Ze_k_b_cr   r   r   r   r   r   r	   r.     s
    zWorkingSet.__setstate__)r   r   r   r   r   classmethodr   r   r   r   r   rO   rI   r   r   r   r   rH   r   r   r-   r.   r   r   r   r	   r^     s(   CTc               @   s   e  Z d  Z d Z d e   e d d  Z d d   Z d d   Z d d	 d
  Z	 d d   Z
 d d   Z d d d  Z d d d  Z d d   Z d d   Z d d   Z d S)r]   z5Searchable snapshot of distributions on a search pathNc             C   s,   i  |  _  | |  _ | |  _ |  j |  d S)a!  Snapshot distributions available on a search path

        Any distributions found on `search_path` are added to the environment.
        `search_path` should be a sequence of ``sys.path`` items.  If not
        supplied, ``sys.path`` is used.

        `platform` is an optional string specifying the name of the platform
        that platform-specific distributions must be compatible with.  If
        unspecified, it defaults to the current platform.  `python` is an
        optional string naming the desired version of Python (e.g. ``'3.3'``);
        it defaults to the current version.

        You may explicitly set `platform` (and/or `python`) to ``None`` if you
        wish to map *all* distributions, not just those compatible with the
        running platform or Python version.
        N)_distmapr@   pythonscan)r   search_pathr@   r   r   r   r	   r     s    			zEnvironment.__init__c             C   sC   |  j  d k s0 | j d k s0 | j |  j  k oB t | j |  j  S)zIs distribution `dist` acceptable for this environment?

        The distribution must match the platform and python version
        requirements specified when this environment was created, or False
        is returned.
        N)r   
py_versionrm   r@   )r   r   r   r   r	   can_add$  s    zEnvironment.can_addc             C   s   |  j  | j j |  d S)z"Remove `dist` from the environmentN)r   r3   remove)r   r   r   r   r	   r  /  s    zEnvironment.removec             C   sQ   | d k r t  j } n  x2 | D]* } x! t |  D] } |  j |  q2 Wq Wd S)ad  Scan `search_path` for distributions usable in this environment

        Any distributions found are added to the environment.
        `search_path` should be a sequence of ``sys.path`` items.  If not
        supplied, ``sys.path`` is used.  Only distributions conforming to
        the platform/python version defined at initialization are added.
        N)r?   r   rY   r   )r   r  r   r   r   r   r	   r  3  s
    zEnvironment.scanc             C   s   | j    } |  j j | g   S)a  Return a newest-to-oldest list of distributions for `project_name`

        Uses case-insensitive `project_name` comparison, assuming all the
        project's distributions use their project's name converted to all
        lowercase as their key.

        )lowerr   r   )r   r   Zdistribution_keyr   r   r	   __getitem__B  s    zEnvironment.__getitem__c             C   su   |  j  |  rq | j   rq |  j j | j g   } | | k rq | j |  | j d t j d  d d  qq n  d S)zLAdd `dist` if we ``can_add()`` it and it has not already been added
        r3   hashcmpreverseTN)	r  has_versionr   r   r3   r   r   operator
attrgetter)r   r   r   r   r   r	   r   M  s
    zEnvironment.addc             C   sW   | j  |  } | d k	 r | Sx% |  | j D] } | | k r- | Sq- W|  j | |  S)a  Find distribution best matching `req` and usable on `working_set`

        This calls the ``find(req)`` method of the `working_set` to see if a
        suitable distribution is already active.  (This may raise
        ``VersionConflict`` if an unsuitable version of the project is already
        active in the specified `working_set`.)  If a suitable distribution
        isn't active, this method returns the newest distribution in the
        environment that meets the ``Requirement`` in `req`.  If no suitable
        distribution is found, and `installer` is supplied, then the result of
        calling the environment's ``obtain(req, installer)`` method will be
        returned.
        N)r   r3   obtain)r   r   rW   r   r   r   r   r	   r   V  s    zEnvironment.best_matchc             C   s   | d k	 r | |  Sd S)a  Obtain a distribution matching `requirement` (e.g. via download)

        Obtain a distro that matches requirement (e.g. via download).  In the
        base ``Environment`` class, this routine just returns
        ``installer(requirement)``, unless `installer` is None, in which case
        None is returned instead.  This method is a hook that allows subclasses
        to attempt other ways of obtaining a distribution before falling back
        to the `installer` argument.Nr   )r   Zrequirementr   r   r   r	   r  l  s    	zEnvironment.obtainc             c   s0   x) |  j  j   D] } |  | r | Vq q Wd S)z=Yield the unique project names of the available distributionsN)r   r   )r   r3   r   r   r	   r   x  s    
zEnvironment.__iter__c             C   s{   t  | t  r |  j |  nX t  | t  rd xF | D]( } x | | D] } |  j |  qF Wq5 Wn t d | f   |  S)z2In-place addition of a distribution or environmentzCan't add %r to environment)r   r`   r   r]   r   )r   otherZprojectr   r   r   r	   __iadd__~  s    zEnvironment.__iadd__c             C   s@   |  j  g  d d d d } x |  | f D] } | | 7} q( W| S)z4Add an environment or distribution to an environmentr@   Nr   )r   )r   r  newr   r   r   r	   __add__  s    zEnvironment.__add__)r   r   r   r   rG   PY_MAJORr   r  r  r  r  r   r   r  r   r  r  r   r   r   r	   r]   
  s   		c               @   s   e  Z d  Z d Z d S)rg   aT  An error occurred extracting a resource

    The following attributes are available from instances of this exception:

    manager
        The resource manager that raised this exception

    cache_path
        The base directory for resource extraction

    original_error
        The exception instance that caused extraction to fail
    N)r   r   r   r   r   r   r   r	   rg     s   c               @   s   e  Z d  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 f  d d  Z e d d    Z d d   Z d d   Z d d d  Z d S)r_   z'Manage resource extraction and packagesNc             C   s   i  |  _  d  S)N)cached_files)r   r   r   r	   r     s    zResourceManager.__init__c             C   s   t  |  j |  S)zDoes the named resource exist?)rJ   r   )r   package_or_requirementr   r   r   r	   rT     s    zResourceManager.resource_existsc             C   s   t  |  j |  S)z,Is the named resource an existing directory?)rJ   rU   )r   r  r   r   r   r	   rU     s    zResourceManager.resource_isdirc             C   s   t  |  j |  |  S)z4Return a true filesystem path for specified resource)rJ   r   )r   r  r   r   r   r	   rR     s    z!ResourceManager.resource_filenamec             C   s   t  |  j |  |  S)z9Return a readable file-like object for specified resource)rJ   r   )r   r  r   r   r   r	   rQ     s    zResourceManager.resource_streamc             C   s   t  |  j |  |  S)z%Return specified resource as a string)rJ   r   )r   r  r   r   r   r	   rP     s    zResourceManager.resource_stringc             C   s   t  |  j |  S)z1List the contents of the named resource directory)rJ   rS   )r   r  r   r   r   r	   rS     s    z ResourceManager.resource_listdirc             C   s]   t  j   d } |  j p t   } t d | | f  } |  | _ | | _ | | _ |  d S)z5Give an error message for problems extracting file(s)r   a}  Can't extract file(s) to egg cache

The following error occurred while trying to extract file(s) to the Python egg
cache:

  %s

The Python egg cache directory is currently set to:

  %s

Perhaps your account does not have write access to this directory?  You can
change the cache directory by setting the PYTHON_EGG_CACHE environment
variable to point to an accessible directory.
N)r?   r   extraction_pathr\   rg   r   
cache_pathZoriginal_error)r   old_excr  errr   r   r	   extraction_error  s    			z ResourceManager.extraction_errorc          	   C   so   |  j  p t   } t j j | | d |  } y t |  Wn |  j   Yn X|  j |  d |  j | <| S)a  Return absolute location in cache for `archive_name` and `names`

        The parent directory of the resulting path will be created if it does
        not already exist.  `archive_name` should be the base filename of the
        enclosing egg (which may not be the name of the enclosing zipfile!),
        including its ".egg" extension.  `names`, if provided, should be a
        sequence of path name parts "under" the egg's extraction location.

        This method should only be called by resource providers that need to
        obtain an extraction location, and only for names they intend to
        extract, as it tracks the generated names for possible cleanup later.
        z-tmpr   )	r  r\   r   r   rA   r   r  _warn_unsafe_extraction_pathr  )r   Zarchive_namenamesZextract_pathZtarget_pathr   r   r	   get_cache_path  s    zResourceManager.get_cache_pathc             C   sw   t  j d k r* |  j t  j d  r* d St  j |   j } | t j @sV | t j @rs d |  } t j	 | t
  n  d S)aN  
        If the default extraction path is overridden and set to an insecure
        location, such as /tmp, it opens up an opportunity for an attacker to
        replace an extracted file with an unauthorized payload. Warn the user
        if a known insecure location is used.

        See Distribute #375 for more details.
        ntZwindirNz%s is writable by group/others and vulnerable to attack when used with get_resource_filename. Consider a more secure location (set with .set_extraction_path or the PYTHON_EGG_CACHE environment variable).)r   r   r   environstatst_modeS_IWOTHS_IWGRPwarningswarnUserWarning)r   r   msgr   r   r	   r    s    
&z,ResourceManager._warn_unsafe_extraction_pathc             C   s@   t  j d k r< t  j |  j d Bd @} t  j | |  n  d S)a4  Perform any platform-specific postprocessing of `tempname`

        This is where Mac header rewrites should be done; other platforms don't
        have anything special they should do.

        Resource providers should call this method ONLY after successfully
        extracting a compressed resource.  They must NOT call it on resources
        that are already in the filesystem.

        `tempname` is the current (temporary) name of the file, and `filename`
        is the name it will be renamed to by the caller after this routine
        returns.
        posixim  i  N)r   r   r  r   chmod)r   Ztempnamer!   r   r   r   r	   postprocess  s    zResourceManager.postprocessc             C   s%   |  j  r t d   n  | |  _ d S)a  Set the base path where resources will be extracted to, if needed.

        If you do not call this routine before any extractions take place, the
        path defaults to the return value of ``get_default_cache()``.  (Which
        is based on the ``PYTHON_EGG_CACHE`` environment variable, with various
        platform-specific fallbacks.  See that routine's documentation for more
        details.)

        Resources are extracted to subdirectories of this path based upon
        information given by the ``IResourceProvider``.  You may set this to a
        temporary directory, but then you must call ``cleanup_resources()`` to
        delete the extracted files when done.  There is no guarantee that
        ``cleanup_resources()`` will be able to remove all extracted files.

        (Note: you may not change the extraction path for a given resource
        manager once resources have been extracted, unless you first call
        ``cleanup_resources()``.)
        z5Can't change extraction path, files already extractedN)r  rD   r  )r   r   r   r   r	   rZ   /  s    	z#ResourceManager.set_extraction_pathFc             C   s   d S)aB  
        Delete all extracted resource files and directories, returning a list
        of the file and directory names that could not be successfully removed.
        This function does not have any concurrency protection, so it should
        generally only be called when the extraction path is a temporary
        directory exclusive to a single process.  This method is not
        automatically called; you must call it explicitly or register it as an
        ``atexit`` function if you wish to ensure cleanup of a temporary
        directory used for extractions.
        Nr   )r   Zforcer   r   r	   r[   I  s    z!ResourceManager.cleanup_resources)r   r   r   r   r  r   rT   rU   rR   rQ   rP   rS   r  r  staticmethodr  r)  rZ   r[   r   r   r   r	   r_     s   c              C   s  y t  j d SWn t k
 r# Yn Xt  j d k rC t  j j d  Sd }  d d |  f d |  f d |  f d d |  f g } x | D] \ } } d } xn | D]5 } | t  j k r t  j j | t  j |  } q Pq W| r t  j j | |  } n  t  j j | d  Sq Wt d   d S)a  Determine the default cache location

    This returns the ``PYTHON_EGG_CACHE`` environment variable, if set.
    Otherwise, on Windows, it returns a "Python-Eggs" subdirectory of the
    "Application Data" directory.  On all other systems, it's "~/.python-eggs".
    ZPYTHON_EGG_CACHEr  z~/.python-eggszApplication DataAPPDATANUSERPROFILE	HOMEDRIVEHOMEPATHHOMEWINDIRr   zPython-Eggsz3Please set the PYTHON_EGG_CACHE enviroment variablezAPPDATA)r1  N)r,  )r-  r.  )r.  zHOME)r2  N)r0  )r   r  r   r   r   
expanduserrA   RuntimeError)Zapp_dataZ	app_homesr   Zsubdirr    r3   r   r   r	   r\   V  s0    			c             C   s   t  j d d |   S)zConvert an arbitrary string to a standard distribution name

    Any runs of non-alphanumeric/. characters are replaced with a single '-'.
    z[^A-Za-z0-9.]+-)resub)r   r   r   r	   rj     s    c             C   s%   |  j  d d  }  t j d d |   S)zConvert an arbitrary string to a standard version string

    Spaces become dots, and all other non-alphanumeric characters become
    dashes, with runs of multiple dashes condensed to a single dash.
    r   r:   z[^A-Za-z0-9.]+r5  )r   r6  r7  )r   r   r   r	   rk     s    c             C   s   t  j d d |   j   S)zConvert an arbitrary string to a standard 'extra' name

    Any runs of non-alphanumeric characters are replaced with a single '_',
    and the result is always lowercased.
    z[^A-Za-z0-9.]+r   )r6  r7  r  )extrar   r   r	   rp     s    c             C   s   |  j  d d  S)z|Convert a project or version name to its filename-escaped form

    Any '-' characters are currently replaced with '_'.
    r5  r   )r   )r   r   r   r	   rq     s    c               @   sX  e  Z d  Z i d d   d 6d d   d 6e j d 6d d   d 6e j d	 6e j d
 6e j d 6Z e	 d d    Z
 e d d    Z e	 d d    Z e	 d d    Z e	 d d    Z e	 d d    Z e	 d d    Z e	 d d d   Z e	 d d    Z d e   k re Z n  e	 d  d!    Z e	 d" d#    Z e d$ d%    Z d S)&MarkerEvaluationc               C   s   t  j S)N)r   r   r   r   r   r	   r
     s    zMarkerEvaluation.<lambda>Zos_namec               C   s   t  j S)N)r?   r@   r   r   r   r	   r
     s    Zsys_platformZpython_full_versionc               C   s   t  j   d  d  S)Nr   )r@   python_versionr   r   r   r	   r
     s    r:  Zplatform_versionZplatform_machinepython_implementationc             C   sA   y |  j  |  Wn) t k
 r< |  j t j   d  SYn Xd S)z{
        Validate text as a PEP 426 environment marker; return an exception
        if invalid or False otherwise.
        r   F)rs   SyntaxErrornormalize_exceptionr?   r   )r   textr   r   r	   is_invalid_marker  s
    z"MarkerEvaluation.is_invalid_markerc             C   sE   i d d 6d d 6} d |  _  d |  _ | j |  j |  j  |  _ |  S)z
        Given a SyntaxError from a marker evaluation, normalize the error
        message:
         - Remove indications of filename and line number.
         - Replace platform-specific error messages with standard error
           messages.
        zinvalid syntaxzunexpected EOF while parsingzparenthesis is never closedN)r!   linenor   r&  )excZsubsr   r   r	   r=    s    	
		z$MarkerEvaluation.normalize_exceptionc                sA      f d d   t  d t   d  D } t j t j |  S)Nc                s#   g  |  ] }   j   |   q Sr   )	interpret).0i)r   nodelistr   r	   
<listcomp>  s   	z-MarkerEvaluation.and_test.<locals>.<listcomp>r   r;   )rangelen	functoolsreducer  and_)r   rE  r(   r   )r   rE  r	   and_test  s    zMarkerEvaluation.and_testc                sA      f d d   t  d t   d  D } t j t j |  S)Nc                s#   g  |  ] }   j   |   q Sr   )rB  )rC  rD  )r   rE  r   r	   rF    s   	z)MarkerEvaluation.test.<locals>.<listcomp>r   r;   )rG  rH  rI  rJ  r  or_)r   rE  r(   r   )r   rE  r	   test  s    zMarkerEvaluation.testc             C   sj   | d d } | t  j k rT | d d t  j k rC t d   n  |  j | d  Sd } t |   d  S)Nr   r   r;   zEmpty parenthesesz5Language feature not supported in environment markers)tokenLPARRPARr<  rB  )r   rE  tr&  r   r   r	   atom  s    zMarkerEvaluation.atomc             C   s   t  |  d k r' d } t |   n  | d d } | d } | d t j k r t  | d  d k r | d k r} d } q d	 } q n  y |  j |  } Wn. t k
 r t |  d
 } t |   Yn X| |  j | d  |  j | d   S)Nr   z5Chained comparison not allowed in environment markersr;   r   r   r   notznot inzis notz, operator not allowed in environment markers)rH  r<  rO  NAMEget_opr   r   evaluate)r   rE  r&  compZcopr   r   r	   
comparison  s     
	zMarkerEvaluation.comparisonc             C   s   i |  j  t j  6|  j t j 6|  j t j 6|  j t j 6d d   d 6d d   d 6t j d 6t j d 6} t t d  r |  j  | t j	 <n  | | S)	Nc             S   s
   |  | k S)Nr   )xyr   r   r	   r
     s    z)MarkerEvaluation.get_op.<locals>.<lambda>znot inc             S   s
   |  | k S)Nr   )rZ  r[  r   r   r	   r
     s    inz==z!=or_test)
rN  symbolrL  rS  rY  r  eqner   r]  )r   opZopsr   r   r	   rV    s    
zMarkerEvaluation.get_opNc             C   s#   |  j  t j |  j d  d  S)az  
        Evaluate a PEP 426 environment marker on CPython 2.4+.
        Return a boolean indicating the marker result in this environment.
        Raise SyntaxError if marker is invalid.

        This implementation uses the 'parser' module, which is not implemented
        on
        Jython and has been superseded by the 'ast' module in Python 2.6 and
        later.
        r   )rB  parserexprZtotuple)r   r>  r8  r   r   r	   rs     s    z MarkerEvaluation.evaluate_markerc             C   s   d d l  } | j   } x9 | j   D]+ } | j d d  } | j |  | | <q% Wy | j | |  } Wn5 t k
 r t j   d } t	 | j
 d   Yn X| S)z
        Evaluate a PEP 426 environment marker using markerlib.
        Return a boolean indicating the marker result in this environment.
        Raise SyntaxError if marker is invalid.
        r   Nr:   r   r   )
_markerlibZdefault_environmentr   r   r   rB  	NameErrorr?   r   r<  r8   )r   r>  rd  r   r3   Znew_keyresulter   r   r	   _markerlib_evaluate  s    z$MarkerEvaluation._markerlib_evaluaterb  c             C   se   x  t  |  d k r" | d } q Wy |  j | d  } Wn t k
 rZ t d   Yn X| |  S)Nr;   r   r   z)Comparison or logical expression expected)rH  rV  r   r<  )r   rE  ra  r   r   r	   rB  6  s     zMarkerEvaluation.interpretc             C   s   x  t  |  d k r" | d } q W| d } | d } | t j k r y |  j | } Wn" t k
 r{ t d |   Yn X|   S| t j k r | d } |  j |  s t d   n  | d d  Sd } t |   d  S)Nr;   r   r   zUnknown name %rz1Only plain strings allowed in environment markersz5Language feature not supported in environment markersr   )rH  rO  rU  r   r   r<  STRING_safe_string)r   rE  Zkindr   ra  sr&  r   r   r	   rW  ?  s$     


zMarkerEvaluation.evaluatec             C   s@   |  d  d  d k o? |  j  d  o? |  j  d  o? d |  k S)Nr   z'"z"""z'''\)r   )Zcandr   r   r	   rj  S  s    zMarkerEvaluation._safe_string)r   r   r   r@   r:  r   r   r;  r   r   r?  r*  r=  rL  rN  rS  rY  rV  rs   rh  r   rB  rW  rj  r   r   r   r	   r9    s.   


		
		r9  c               @   s  e  Z d  Z d Z d Z d Z d Z d d   Z d d   Z d d   Z	 d	 d
   Z
 d d   Z d d   Z e j d) k r d d   Z n 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 S)*r   zETry to implement resources and metadata for arbitrary PEP 302 loadersNc             C   s:   t  | d d   |  _ t j j t  | d d   |  _ d  S)Nr   __file__r   )r   r   r   r   r    module_path)r   r   r   r   r	   r   f  s    zNullProvider.__init__c             C   s   |  j  |  j |  S)N)_fnrn  )r   r   r   r   r   r	   r   j  s    z"NullProvider.get_resource_filenamec             C   s   t  |  j | |   S)N)r   r   )r   r   r   r   r   r	   r   m  s    z NullProvider.get_resource_streamc             C   s   |  j  |  j |  j |   S)N)_getro  rn  )r   r   r   r   r   r	   r   p  s    z NullProvider.get_resource_stringc             C   s   |  j  |  j |  j |   S)N)_hasro  rn  )r   r   r   r   r	   r   s  s    zNullProvider.has_resourcec             C   s%   |  j  o$ |  j |  j |  j  |   S)N)egg_inforq  ro  )r   r   r   r   r	   r   v  s    zNullProvider.has_metadatar   c             C   s)   |  j  s d S|  j |  j |  j  |   S)Nr   )rr  rp  ro  )r   r   r   r   r	   r   z  s    	zNullProvider.get_metadatac             C   s2   |  j  s d S|  j |  j |  j  |   j d  S)Nr   zutf-8)rr  rp  ro  decode)r   r   r   r   r	   r     s    	c             C   s   t  |  j |   S)N)rn   r   )r   r   r   r   r	   r     s    zNullProvider.get_metadata_linesc             C   s   |  j  |  j |  j |   S)N)_isdirro  rn  )r   r   r   r   r	   rU     s    zNullProvider.resource_isdirc             C   s%   |  j  o$ |  j |  j |  j  |   S)N)rr  rt  ro  )r   r   r   r   r	   r     s    zNullProvider.metadata_isdirc             C   s   |  j  |  j |  j |   S)N)_listdirro  rn  )r   r   r   r   r	   rS     s    zNullProvider.resource_listdirc             C   s)   |  j  r% |  j |  j |  j  |   Sg  S)N)rr  ru  ro  )r   r   r   r   r	   r     s    	zNullProvider.metadata_listdirc             C   s   d | } |  j  |  s, t d |   n  |  j |  j d d  } | j d d  } |  j |  j |  } | | d <t j j |  r t	 | | |  nW d d l
 m } t |  d | j d  | f | | <t | | d	  } t | | |  d  S)
Nzscripts/zNo script named %rz

rm  r   )cacher   )r   rc   r   r   ro  rr  r   r   r   r   	linecacherx  rH  r   r   r   )r   r   r   ZscriptZscript_textZscript_filenamerx  Zscript_coder   r   r	   rI     s    

%zNullProvider.run_scriptc             C   s   t  d   d  S)Nz9Can't perform this operation for unregistered loader type)NotImplementedError)r   r   r   r   r	   rq    s    zNullProvider._hasc             C   s   t  d   d  S)Nz9Can't perform this operation for unregistered loader type)rz  )r   r   r   r   r	   rt    s    zNullProvider._isdirc             C   s   t  d   d  S)Nz9Can't perform this operation for unregistered loader type)rz  )r   r   r   r   r	   ru    s    zNullProvider._listdirc             C   s&   | r" t  j j | | j d   S| S)N/)r   r   rA   r   )r   baser   r   r   r	   ro    s    zNullProvider._fnc             C   s2   t  |  j d  r" |  j j |  St d   d  S)Nget_dataz=Can't perform this operation for loaders without 'get_data()')r   r   r}  rz  )r   r   r   r   r	   rp    s    zNullProvider._get)r   )r   r   r   r   egg_namerr  r   r   r   r   r   r   r   r?   version_infor   r   rU   r   rS   r   rI   rq  rt  ru  ro  rp  r   r   r   r	   r   _  s0   c               @   s.   e  Z d  Z d Z d d   Z d d   Z d S)r   z&Provider based on a virtual filesystemc             C   s   t  j |  |  |  j   d  S)N)r   r   _setup_prefix)r   r   r   r   r	   r     s    zEggProvider.__init__c             C   s   |  j  } d  } x} | | k r | j   j d  rm t j j |  |  _ t j j | d  |  _ | |  _	 Pn  | } t j j
 |  \ } } q Wd  S)Nz.eggzEGG-INFO)rn  r  endswithr   r   basenamer~  rA   rr  egg_rootr   )r   r   oldr|  r   r   r	   r    s    		zEggProvider._setup_prefixN)r   r   r   r   r   r  r   r   r   r	   r     s   c               @   sR   e  Z d  Z d Z d d   Z d d   Z d d   Z d d	   Z d
 d   Z d S)r   z6Provides access to package resources in the filesystemc             C   s   t  j j |  S)N)r   r   r   )r   r   r   r   r	   rq    s    zDefaultProvider._hasc             C   s   t  j j |  S)N)r   r   r   )r   r   r   r   r	   rt    s    zDefaultProvider._isdirc             C   s   t  j |  S)N)r   listdir)r   r   r   r   r	   ru    s    zDefaultProvider._listdirc             C   s   t  |  j |  j |  d  S)Nrb)r   ro  rn  )r   r   r   r   r   r	   r     s    z#DefaultProvider.get_resource_streamc             C   s&   t  | d   } | j   SWd  QXd  S)Nr  )r   r   )r   r   streamr   r   r	   rp    s    zDefaultProvider._getN)	r   r   r   r   rq  rt  ru  r   rp  r   r   r   r	   r     s   c               @   sP   e  Z d  Z d Z d d   Z Z d d   Z d d   Z d Z d d   Z	 d S)	r   z.Provider that returns nothing for all requestsc             C   s   d S)NFr   )r   r   r   r   r	   r
     s    zEmptyProvider.<lambda>c             C   s   d S)Nr   r   )r   r   r   r   r	   r
     s    c             C   s   g  S)Nr   )r   r   r   r   r	   r
     s    Nc             C   s   d  S)Nr   )r   r   r   r	   r     s    zEmptyProvider.__init__)
r   r   r   r   rt  rq  rp  ru  rn  r   r   r   r   r	   r     s   c               @   s.   e  Z d  Z d Z e d d    Z e Z d S)ZipManifestsz
    zip manifest builder
    c          
      sB   t  |  0     f d d     j   D } t |  SWd QXd S)a  
        Build a dictionary similar to the zipimport directory
        caches, except instead of tuples, store ZipInfo objects.

        Use a platform-specific path separator (os.sep) for the path keys
        for compatibility with pypy on Windows.
        c             3   s3   |  ]) } | j  d  t j    j |  f Vq d S)r{  N)r   r   sepZgetinfo)rC  r   )zfiler   r	   	<genexpr>  s   z%ZipManifests.build.<locals>.<genexpr>N)ContextualZipFileZnamelistr$   )r   r   r(   r   )r  r	   build  s    	zZipManifests.buildN)r   r   r   r   r   r  loadr   r   r   r	   r    s   r  c               @   s4   e  Z d  Z d Z e j d d  Z d d   Z d S)MemoizedZipManifestsz%
    Memoized zipfile manifests.
    manifest_modzmanifest mtimec             C   sv   t  j j |  } t  j |  j } | |  k sC |  | j | k rk |  j |  } |  j | |  |  | <n  |  | j S)zW
        Load a manifest at path or return a suitable manifest already loaded.
        )	r   r   normpathr  st_mtimemtimer  r  manifest)r   r   r  r  r   r   r	   r  "  s    zMemoizedZipManifests.loadN)r   r   r   r   collections
namedtupler  r  r   r   r   r	   r    s   r  c                   s@   e  Z d  Z d Z d d   Z d d   Z   f d d   Z   S)r  zL
    Supplement ZipFile class to support context manager for Python 2.6
    c             C   s   |  S)Nr   )r   r   r   r	   	__enter__5  s    zContextualZipFile.__enter__c             C   s   |  j    d  S)N)close)r   typevalue	tracebackr   r   r	   __exit__8  s    zContextualZipFile.__exit__c                s8   t  t j d  r" t j | |   St t |   j |   S)zI
        Construct a ZipFile or ContextualZipFile as appropriate
        r  )r   zipfileZipFilesuperr  __new__)r   r8   kwargs)r   r   r	   r  ;  s    zContextualZipFile.__new__)r   r   r   r   r  r  r  r   r   )r   r	   r  0  s   r  c               @   s   e  Z d  Z d Z d Z e j j d  r3 e   n e	   Z
 d d   Z d d   Z d d	   Z e d
 d    Z d d   Z e 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)"r   z"Resource support for zips and eggsNZ!PKG_RESOURCES_CACHE_ZIP_MANIFESTSc             C   s*   t  j |  |  |  j j t j |  _ d  S)N)r   r   r   archiver   r  zip_pre)r   r   r   r   r	   r   N  s    zZipProvider.__init__c             C   sF   | j  |  j  r) | t |  j  d   St d | |  j f   d  S)Nz%s is not a subpath of %s)r   r  rH  AssertionError)r   fspathr   r   r	   _zipinfo_nameR  s    zZipProvider._zipinfo_namec             C   sj   |  j  | } | j |  j t j  rM | t |  j  d d   j t j  St d | |  j f   d  S)Nr   z%s is not a subpath of %s)r  r   r  r   r  rH  r   r  )r   zip_pathr  r   r   r	   _parts[  s
    'zZipProvider._partsc             C   s   |  j  j |  j j  S)N)_zip_manifestsr  r   r  )r   r   r   r	   zipinfoe  s    zZipProvider.zipinfoc             C   s   |  j  s t d   n  |  j |  } |  j   } d j |  j |   | k r~ x* | D] } |  j | |  j |   qX Wn  |  j | |  S)Nz5resource_filename() only supported for .egg, not .zipr{  )r~  rz  _resource_to_zip_get_eager_resourcesrA   r  _extract_resource_eager_to_zip)r   r   r   r  eagersr   r   r   r	   r   i  s    	 z!ZipProvider.get_resource_filenamec             C   s/   |  j  } |  j d } t j |  } | | f S)Nr   r   r   )r   r   r   )Z	file_size	date_timetimeZmktime)Zzip_statsizer  	timestampr   r   r	   _get_date_and_sizev  s    	zZipProvider._get_date_and_sizec       
      C   s  | |  j    k r^ x9 |  j    | D]' } |  j | t j j | |   } q# Wt j j |  S|  j |  j |  \ } } t s t	 d   n  y)| j
 |  j |  j |   } |  j | |  r | St d d t j j |  \ } }	 t j | |  j j |   t j |  t |	 | | f  | j |	 |  y t |	 |  Wnm t j k
 rt j j |  r|  j | |  r| St j d k rt |  t |	 |  | Sn    Yn XWn t j k
 r| j   Yn X| S)Nz>"os.rename" and "os.unlink" are not supported on this platformz	.$extractdirr  )_indexr  r   r   rA   r    r  r  r   r   r  r~  r  _is_current_mkstempwriter   r}  r  r   r)  r   errorisfiler   r   r  )
r   r   r  r   lastr  r  Z	real_pathZoutfZtmpnamr   r   r	   r    s@    $
zZipProvider._extract_resourcec       	      C   s   |  j  |  j |  \ } } t j j |  s2 d St j |  } | j | k s_ | j | k rc d S|  j j	 |  } t
 | d   } | j   } Wd QX| | k S)zK
        Return True if the file_path is current for this zip_path
        Fr  N)r  r  r   r   r  r  st_sizer  r   r}  r   r   )	r   Z	file_pathr  r  r  r  Zzip_contentsfZfile_contentsr   r   r	   r    s    zZipProvider._is_currentc             C   sa   |  j  d  k rZ g  } x6 d D]. } |  j |  r | j |  j |   q q W| |  _  n  |  j  S)Nnative_libs.txteager_resources.txt)r  r  )r  r   r   r   )r   r  r   r   r   r	   r    s    z ZipProvider._get_eager_resourcesc             C   s   y |  j  SWn t k
 r i  } x |  j D]y } | j t j  } x^ | r t j j | d  d   } | | k r | | j | d  PqF | j   g | | <qF Wq+ W| |  _  | SYn Xd  S)Nr   r   r   )	Z	_dirindexAttributeErrorr  r   r   r  rA   r   r   )r   Zindr   partsparentr   r   r	   r    s    		zZipProvider._indexc             C   s.   |  j  |  } | |  j k p- | |  j   k S)N)r  r  r  )r   r  r  r   r   r	   rq    s    zZipProvider._hasc             C   s   |  j  |  |  j   k S)N)r  r  )r   r  r   r   r	   rt    s    zZipProvider._isdirc             C   s%   t  |  j   j |  j |  f    S)N)r   r  r   r  )r   r  r   r   r	   ru    s    zZipProvider._listdirc             C   s   |  j  |  j |  j |   S)N)r  ro  r  )r   r   r   r   r	   r    s    zZipProvider._eager_to_zipc             C   s   |  j  |  j |  j |   S)N)r  ro  rn  )r   r   r   r   r	   r    s    zZipProvider._resource_to_zip)r   r   r   r   r  r   r  r   r  r  r  r   r  r  propertyr  r   r*  r  r  r  r  r  rq  rt  ru  r  r  r   r   r   r	   r   D  s&   		
	4	c               @   sF   e  Z d  Z d Z d d   Z d d   Z d d   Z d d	   Z d
 S)r}   a*  Metadata handler for standalone PKG-INFO files

    Usage::

        metadata = FileMetadata("/path/to/PKG-INFO")

    This provider rejects all data and metadata requests except for PKG-INFO,
    which is treated as existing, and will be the contents of the file at
    the provided location.
    c             C   s   | |  _  d  S)N)r   )r   r   r   r   r	   r     s    zFileMetadata.__init__c             C   s
   | d k S)NzPKG-INFOr   )r   r   r   r   r	   r     s    zFileMetadata.has_metadatac             C   sG   | d k r7 t  |  j d   } | j   } Wd  QX| St d   d  S)NzPKG-INFOZrUz(No metadata except PKG-INFO is available)r   r   r   r   )r   r   r  metadatar   r   r	   r     s
    zFileMetadata.get_metadatac             C   s   t  |  j |   S)N)rn   r   )r   r   r   r   r	   r   	  s    zFileMetadata.get_metadata_linesN)r   r   r   r   r   r   r   r   r   r   r   r	   r}     s
   
c               @   s"   e  Z d  Z d Z d d   Z d S)r~   as  Metadata provider for egg directories

    Usage::

        # Development eggs:

        egg_info = "/path/to/PackageName.egg-info"
        base_dir = os.path.dirname(egg_info)
        metadata = PathMetadata(base_dir, egg_info)
        dist_name = os.path.splitext(os.path.basename(egg_info))[0]
        dist = Distribution(basedir, project_name=dist_name, metadata=metadata)

        # Unpacked egg directories:

        egg_path = "/path/to/PackageName-ver-pyver-etc.egg"
        metadata = PathMetadata(egg_path, os.path.join(egg_path,'EGG-INFO'))
        dist = Distribution.from_filename(egg_path, metadata=metadata)
    c             C   s   | |  _  | |  _ d  S)N)rn  rr  )r   r   rr  r   r   r	   r   !  s    	zPathMetadata.__init__N)r   r   r   r   r   r   r   r   r	   r~     s   c               @   s"   e  Z d  Z d Z d d   Z d S)r   z Metadata provider for .egg filesc             C   s`   | j  t j |  _ | |  _ | j rF t j j | j  | j  |  _ n | j  |  _ |  j	   d S)z-Create a metadata provider from a zipimporterN)
r  r   r  r  r   prefixr   rA   rn  r  )r   importerr   r   r	   r   )  s    		!zEggMetadata.__init__N)r   r   r   r   r   r   r   r   r	   r   &  s   r$   _distribution_findersc             C   s   | t  |  <d S)ax  Register `distribution_finder` to find distributions in sys.path items

    `importer_type` is the type or class of a PEP 302 "Importer" (sys.path item
    handler), and `distribution_finder` is a callable that, passed a path
    item and the importer instance, yields ``Distribution`` instances found on
    that path item.  See ``pkg_resources.find_on_path`` for an example.N)r  )importer_typeZdistribution_finderr   r   r	   r   6  s    c             C   s+   t  |   } t t |  } | | |  |  S)z.Yield distributions accessible via `path_item`)r   r   r  )	path_itemonlyr  finderr   r   r	   rY   @  s    c             c   s   |  j  j d  r d St |   } | j d  rH t j | d | Vn  | rR d Sxf | j d  D]U } | j d  rb t j j	 | |  } x( t
 t j |  |  D] } | Vq Wqb qb Wd S)z@
    Find eggs in zip files; possibly multiple nested eggs.
    z.whlNzPKG-INFOr  r{  z.egg)r  r  r   r   r`   from_filenamerS   r   r   rA   find_eggs_in_zip	zipimportzipimporter)r  r  r  r  Zsubitemsubpathr   r   r   r	   r  F  s    r  c             C   s   f  S)Nr   )r  r  r  r   r   r	   find_nothing\  s    r  c             c   s  t  |  } t j j |  rt j | t j  r| j   j d  rw t j	 | d t
 | t j j | d   Vqxt j |  D]} | j   } | j d  s | j d  rt j j | |  } t j j |  r t
 | |  } n t |  } t j | | | d t Vq | rc| j d  rct t j j | |   } x | D] } | VqQWq | r | j d  r t t j j | |    }	 |	 j   }
 Wd QXxa |
 D]V } | j   sqn  t j j | | j    } t |  } x | D] } | VqWPqWq q Wn  d S)	z6Yield distributions accessible on a sys.path directoryz.eggr  zEGG-INFOz	.egg-infoz
.dist-info
precedencez	.egg-linkN)_normalize_cachedr   r   r   accessR_OKr  r  r`   r  r~   rA   r  r}   from_locationrz   rY   r   	readlinesstriprstrip)r  r  r  r   r  Zfullpathr  r   r   Z
entry_fileZentry_linesliner   r   r   r   r	   find_on_path`  s<    '	 	r  _namespace_handlers_namespace_packagesc             C   s   | t  |  <d S)a  Register `namespace_handler` to declare namespace packages

    `importer_type` is the type or class of a PEP 302 "Importer" (sys.path item
    handler), and `namespace_handler` is a callable like this::

        def namespace_handler(importer, path_entry, moduleName, module):
            # return a path_entry to use for child packages

    Namespace handlers are only called if the importer object has already
    agreed that it can handle the relevant path item, and they should only
    return a subpath if the module __path__ does not already contain an
    equivalent subpath.  For an example namespace handler, see
    ``pkg_resources.file_ns_handler``.
    N)r  )r  Znamespace_handlerr   r   r	   r     s    c             C   s7  t  |  } | d k r d S| j |   } | d k r; d St j j |   } | d k r t j |   } t j |  <g  | _ t |   n! t	 | d  s t
 d |    n  t t |  } | | | |  |  } | d k	 r3| j } | j |  | j |   x3 | D]( } | | j k r| j j |  qqWn  | S)zEEnsure that named package includes a subpath of path_item (if needed)N__path__zNot a package:)r   find_moduler?   r   r   impZ
new_moduler  _set_parent_nsr   r   r   r  r   load_module)packageNamer  r  r   r   Zhandlerr  r   r   r   r	   
_handle_ns  s.    		r  c             C   s  t  j   z |  t k r d St j d } } d |  k r d j |  j d  d d   } t |  | t k r~ t |  n  y t j	 | j
 } Wq t k
 r t d |   Yq Xn  t j | g   j |   t j |  g   x | D] } t |  |  q WWd t  j   Xd S)z9Declare that package 'packageName' is a namespace packageNr:   r   zNot a package:r   )r  acquire_lockr  r?   r   rA   r   rV   r   r   r  r  r   r   r   r  release_lock)r  r   r  r  r   r   r	   rV     s&    
"
c          
   C   sb   t  j   zF x? t j | f   D]+ } t | |   } | r  t | |  q  q  WWd t  j   Xd S)zDEnsure that previously-declared namespace packages include path_itemN)r  r  r  r   r  r   r  )r  r  packager  r   r   r	   r     s    
c             C   s`   t  j j | | j d  d  } t |  } x+ | j D] } t |  | k r8 Pq8 q8 W| Sd S)zBCompute an ns-package subpath for a filesystem or zipfile importerr:   r   Nr   )r   r   rA   r   r  r  )r  r  r  r   r  Z
normalizedr   r   r   r	   file_ns_handler  s    "r  c             C   s   d  S)Nr   )r  r  r  r   r   r   r	   null_ns_handler  s    r  c             C   s   t  j j t  j j |    S)z1Normalize a file/dir name for comparison purposes)r   r   normcaserealpath)r!   r   r   r	   ru     s    c             C   s=   y | |  SWn* t  k
 r8 t |   | |  <} | SYn Xd  S)N)r   ru   )r!   r   rf  r   r   r	   r    s
    r  c             C   sU   |  j  d  } | j   } | rQ d j |  } t t j | | t j |   n  d  S)Nr:   )r   r   rA   setattrr?   r   )r  r  r   r  r   r   r	   r    s
    r  c             c   s   t  |  t  rS xn |  j   D]0 } | j   } | r | j d  r | Vq q Wn- x* |  D]" } x t |  D] } | Vqm WqZ Wd S)zAYield non-empty/non-comment lines of a ``basestring`` or sequence#N)r   r   
splitlinesr  r   rn   )strsrk  Zssr   r   r	   rn     s    z
\s*(#.*)?$z\s*\\\s*(#.*)?$z\s*((\w|[-.])+)z!\s*(<=?|>=?|==|!=)\s*((\w|[-.])+)z\s*,z\s*\[z\s*\]z\w+(\.\w+)*$zL(?P<name>[^-]+)( -(?P<ver>[^-]+) (-py(?P<pyver>[^-]+) (-(?P<plat>.+))? )? )?z(\d+ | [a-z]+ | \.| -)cZpreZpreviewzfinal-r5  rc@Zdevc             c   s{   xo t  j |   D]^ } t | |  } | s | d k r> q n  | d  d  d k re | j d  Vq d | Vq Wd Vd  S)Nr:   r   
0123456789r   *z*final)component_rer   r   zfill)rk  partr   r   r	   _parse_version_parts8  s    r  c             C   s   g  } x t  |  j    D] } | j d  r | d k  rd x' | r` | d d k r` | j   q= Wn  x' | r | d d k r | j   qg Wn  | j |  q Wt |  S)a  Convert a version string to a chronologically-sortable key

    This is a rough cross between distutils' StrictVersion and LooseVersion;
    if you give it versions that would work with StrictVersion, then it behaves
    the same; otherwise it acts like a slightly-smarter LooseVersion. It is
    *possible* to create pathological version coding schemes that will fool
    this parser, but they should be very rare in practice.

    The returned value will be a tuple of strings.  Numeric portions of the
    version are padded to 8 digits so they will compare numerically, but
    without relying on how numbers compare relative to strings.  Dots are
    dropped, but dashes are retained.  Trailing zeros between alpha segments
    or dashes are suppressed, so that e.g. "2.4.0" is considered the same as
    "2.4". Alphanumeric parts are lower-cased.

    The algorithm assumes that strings like "-" and any alpha string that
    alphabetically follows "final"  represents a "patch level".  So, "2.4-1"
    is assumed to be a branch or patch of "2.4", and therefore "2.4.1" is
    considered newer than "2.4-1", which in turn is newer than "2.4".

    Strings like "a", "b", "c", "alpha", "beta", "candidate" and so on (that
    come before "final" alphabetically) are assumed to be pre-release versions,
    so that the version "2.4" is considered newer than "2.4a1".

    Finally, to handle miscellaneous cases, the strings "pre", "preview", and
    "rc" are treated as if they were "c", i.e. as though they were release
    candidates, and therefore are not as new as a version string that does not
    contain them, and "dev" is replaced with an '@' so that it sorts lower than
    than any other pre-release tag.
    r  z*finalr   z*final-Z00000000r   r   )r  r  r   r   r   tuple)rk  r  r  r   r   r	   ri   F  s    c               @   s   e  Z d  Z d Z f  f  d d d  Z d d   Z d d   Z d	 d d d
 d  Z d d d d  Z e	 d d d   Z
 e	 d d d   Z e	 d d d   Z d S)rb   z3Object representing an advertised importable objectNc             C   sn   t  |  s t d |   n  | |  _ | |  _ t |  |  _ t j d d j |   j	 |  _	 | |  _
 d  S)NzInvalid module namezx[%s],)MODULErD   r   module_namer  attrsra   r   rA   r   r   )r   r   r  r  r   r   r   r   r	   r   v  s    		"zEntryPoint.__init__c             C   sf   d |  j  |  j f } |  j r< | d d j |  j  7} n  |  j rb | d d j |  j  7} n  | S)Nz%s = %s:r:   z [%s]r  )r   r  r  rA   r   )r   rk  r   r   r	   __str__  s    		zEntryPoint.__str__c             C   s   d t  |   S)NzEntryPoint.parse(%r))r   )r   r   r   r	   r     s    zEntryPoint.__repr__Tc             C   s   | r |  j  | |  n  t |  j t   t   d g  } xO |  j D]D } y t | |  } WqD t k
 r t d | | f   YqD XqD W| S)Nr   z%r has no %r attribute)rH   r   r  r   r  r   r  r   )r   rH   r   r   r   attrr   r   r	   r    s    zEntryPoint.loadc             C   si   |  j  r% |  j r% t d |    n  |  j j |  j   } t j | | |  } t t t j |   d  S)Nz&Can't require() without a distribution)	r   r   rf   r   rW   r   r   r   r   )r   r   r   r   r(   r   r   r	   rH     s
    zEntryPoint.requirec       	      C   s"  y f  } } | j  d d  \ } } d | k rz | j  d d  \ } } t j d |  } | j rn t  n  | j } n  d | k r | j  d d  \ } } t | j    s t  n  | j   j  d  } n  Wn' t k
 r d } t | |   Yn# X|  | j   | j   | | |  Sd S)	a  Parse a single entry point from string `src`

        Entry point syntax follows the form::

            name = some.module:some.attr [extra1, extra2]

        The entry name and module name are required, but the ``:attrs`` and
        ``[extras]`` parts are optional
        =r   [zx[r  r:   z9EntryPoint must be in 'name=module:attrs [extras]' formatN)	r   ra   r   specsrD   r   r  r  r  )	r   srcr   r  r   r   r  r   r&  r   r   r	   r     s$    
			zEntryPoint.parsec             C   s   t  |  s t d |   n  i  } xZ t |  D]L } |  j | |  } | j | k rp t d | | j   n  | | | j <q1 W| S)zParse an entry point groupzInvalid group namezDuplicate entry point)r  rD   rn   r   r   )r   rC   linesr   thisr  r   r   r   r	   parse_group  s    zEntryPoint.parse_groupc             C   s   t  | t  r | j   } n t |  } i  } x~ | D]v \ } } | d k rj | s[ q7 n  t d   n  | j   } | | k r t d |   n  |  j | | |  | | <q7 W| S)z!Parse a map of entry point groupsNz%Entry points must be listed in groupszDuplicate group name)r   r$   r(   ro   rD   r  r  )r   datar   mapsrC   r  r   r   r	   	parse_map  s    zEntryPoint.parse_map)r   r   r   r   r   r   r   r  rH   r   r   r  r  r   r   r   r	   rb   s  s   	c             C   sE   |  s
 d St  |   } | d j d  rA t | d  d  d  S|  S)Nr   r   zmd5=r   r   )r   )r   r   r   )r   parsedr   r   r	   _remove_md5_fragment  s    r  c            	   @   s  e  Z d  Z d Z d Z d d d d e d e d d  Z e d d d   Z	 e
 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
 d d    Z e
 d d    Z e
 d d    Z e
 d d    Z f  d  d!  Z d" d#   Z d d$ d%  Z d& d'   Z d( d)   Z d* d+   Z d, d-   Z e d d. d/   Z d0 d1   Z d2 d3   Z  d d4 d5  Z! d6 d7   Z" d d8 d9  Z# d: d;   Z$ d< d=   Z% d> d?   Z& e
 d@ dA    Z' d S)Br`   z5Wrap an actual or potential sys.path entry w/metadatazPKG-INFONc             C   sj   t  | p d  |  _ | d  k	 r3 t |  |  _ n  | |  _ | |  _ | |  _ | |  _ | p` t |  _	 d  S)NZUnknown)
rj   r   rk   _versionr  r@   r   r  r   	_provider)r   r   r  r   r   r  r@   r  r   r   r	   r     s    				zDistribution.__init__c             K   s   d  g d \ } } } } t  j j |  \ } }	 |	 j   t k r t |  }
 |
 r| |
 j d d d d  \ } } } } n  t |	 j   }  n  |  | | d | d | d | d	 | | S)
Nr   r   verZpyverrE   r   r   r  r@   )r   r   splitextr  _distributionImplEGG_NAMErC   )r   r   r  r  r&   r   r   r  r@   extr>   r   r   r	   r    s    !zDistribution.from_locationc             C   s7   t  |  d f   |  j |  j t |  j  |  j |  j f S)Nparsed_version)r   r  r3   r  r   r  r@   )r   r   r   r	   r  	  s    zDistribution.hashcmpc             C   s   t  |  j  S)N)hashr  )r   r   r   r	   __hash__	  s    zDistribution.__hash__c             C   s   |  j  | j  k  S)N)r  )r   r  r   r   r	   __lt__	  s    zDistribution.__lt__c             C   s   |  j  | j  k S)N)r  )r   r  r   r   r	   __le__	  s    zDistribution.__le__c             C   s   |  j  | j  k S)N)r  )r   r  r   r   r	   __gt__	  s    zDistribution.__gt__c             C   s   |  j  | j  k S)N)r  )r   r  r   r   r	   __ge__	  s    zDistribution.__ge__c             C   s&   t  | |  j  s d S|  j | j k S)NF)r   r   r  )r   r  r   r   r	   __eq__!	  s    zDistribution.__eq__c             C   s   |  | k S)Nr   )r   r  r   r   r	   __ne__'	  s    zDistribution.__ne__c             C   s>   y |  j  SWn, t k
 r9 |  j j   |  _  } | SYn Xd  S)N)_keyr  r   r  )r   r3   r   r   r	   r3   .	  s
    zDistribution.keyc             C   s>   y |  j  SWn, t k
 r9 t |  j  |  _  } | SYn Xd  S)N)Z_parsed_versionr  ri   r   )r   Zpvr   r   r	   r  6	  s
    zDistribution.parsed_versionc             C   s   y |  j  SWn t k
 r xw |  j |  j  D]G } | j   j d  r. t | j d d  d j    |  _  |  j  Sq. Wd } t	 | |  j |    Yn Xd  S)Nzversion:r  r   z(Missing 'Version:' header and/or %s file)
r  r  _get_metadataPKG_INFOr  r   rk   r   r  rD   )r   r  Ztmplr   r   r	   r   >	  s    %zDistribution.versionc             C   s   y |  j  SWn t k
 r i g  d  6} |  _  x d D] } x t |  j |   D] \ } } | r d | k r | j d d  \ } } t |  r g  } q t |  s g  } q n  t |  p d  } n  | j | g   j	 t
 |   qR Wq6 W| SYn Xd  S)Nrequires.txtdepends.txtr  r   )r!  r"  )Z_Distribution__dep_mapr  ro   r  r   rr   rs   rp   r   r   rh   )r   dmr   r8  r   Zmarkerr   r   r	   _dep_mapK	  s     "	'zDistribution._dep_mapc             C   s   |  j  } g  } | j | j d f    xT | D]L } y | j | t |   Wq/ t k
 rz t d |  | f   Yq/ Xq/ W| S)z@List of Requirements needed for this distro if `extras` are usedNz%s has no such extra feature %r)r$  r   r   rp   r   rf   )r   r   r#  Zdepsr  r   r   r	   r   _	  s    	zDistribution.requiresc             c   s5   |  j  |  r1 x |  j |  D] } | Vq Wn  d  S)N)r   r   )r   r   r  r   r   r	   r  m	  s    zDistribution._get_metadatac             C   s~   | d k r t  j } n  |  j |  | t  j k rz t |  j  x6 |  j d  D]" } | t  j k rQ t |  qQ qQ Wn  d S)z>Ensure distribution is importable on `path` (default=sys.path)Nznamespace_packages.txt)r?   r   r   r   r   r  r   rV   )r   r   pkgr   r   r	   activater	  s    zDistribution.activatec             C   sO   d t  |  j  t  |  j  |  j p' t f } |  j rK | d |  j 7} n  | S)z@Return what this distribution's standard .egg filename should bez
%s-%s-py%sr5  )rq   r   r   r  r  r@   )r   r!   r   r   r	   r~  }	  s    	zDistribution.egg_namec             C   s(   |  j  r d |  |  j  f St |   Sd  S)Nz%s (%s))r   r   )r   r   r   r	   r   	  s    	zDistribution.__repr__c             C   sN   y t  |  d d   } Wn t k
 r0 d  } Yn X| p: d } d |  j | f S)Nr   z[unknown version]z%s %s)r   rD   r   )r   r   r   r   r	   r   	  s    zDistribution.__str__c             C   s.   | j  d  r t |   n  t |  j |  S)zADelegate all unrecognized public attributes to .metadata providerr   )r   r  r   r  )r   r  r   r   r	   __getattr__	  s    zDistribution.__getattr__c             K   s(   |  j  t |  t j j |  | |  S)N)r  r  r   r   r  )r   r!   r  r&   r   r   r	   r  	  s    zDistribution.from_filenamec             C   s   t  j d |  j |  j f  S)z?Return a ``Requirement`` that matches this distribution exactlyz%s==%s)ra   r   r   r   )r   r   r   r	   r   	  s    zDistribution.as_requirementc             C   sD   |  j  | |  } | d k r: t d | | f f   n  | j   S)z=Return the `name` entry point of `group` or raise ImportErrorNzEntry point %r not found)rN   r   r  )r   rC   r   r   r   r   r	   rL   	  s    zDistribution.load_entry_pointc             C   sd   y |  j  } Wn4 t k
 rC t j |  j d  |   } |  _  Yn X| d k	 r` | j | i   S| S)z=Return the entry point map for `group`, or the full entry mapzentry_points.txtN)Z_ep_mapr  rb   r  r  r   )r   rC   Zep_mapr   r   r	   rM   	  s    !zDistribution.get_entry_mapc             C   s   |  j  |  j |  S)z<Return the EntryPoint object for `group`+`name`, or ``None``)rM   r   )r   rC   r   r   r   r	   rN   	  s    zDistribution.get_entry_infoc       	      C   sM  | p |  j  } | s d St |  } t j j |  } d d   | D } x t |  D]w \ } } | | k rs PqW | | k rW |  j t k rW | t j k r |  j	   n  | j
 | |  | j
 | |  PqW qW W| t j k r |  j	   n  | j |  d SxG y | j | | d  } Wn t k
 r1PYqX| | =| | =| } qd S)z@Insert self.location in path before its nearest parent directoryNc             S   s(   g  |  ] } | r t  |  p! |  q Sr   )r  )rC  pr   r   r	   rF  	  s   	 z*Distribution.insert_on.<locals>.<listcomp>r   )r   r  r   r   r    	enumerater  rv   r?   check_version_conflictr   r   indexrD   )	r   r   locZnlocZbdirZnpathr(  r   Znpr   r   r	   r   	  s6    	zDistribution.insert_onc             C   s   |  j  d k r d  St j |  j d   } t |  j  } x |  j d  D] } | t j k sJ | | k sJ | t k r} qJ n  | d k r qJ n  t	 t j | d d   } | r t |  j
 |  sJ | j
 |  j  r qJ n  t d | | |  j f  qJ Wd  S)	N
setuptoolsznamespace_packages.txtztop_level.txtpkg_resourcessiterm  zIModule %s was already imported from %s, but %s is being added to sys.path)r.  r-  r/  )r3   r$   r%   r  ru   r   r?   r   r  r   r   issue_warning)r   Znspr,  modnamer   r   r   r	   r*  	  s"    z#Distribution.check_version_conflictc             C   s<   y |  j  Wn* t k
 r7 t d t |    d SYn Xd S)NzUnbuilt egg for FT)r   rD   r0  r   )r   r   r   r	   r
  	  s    	zDistribution.has_versionc             K   sY   d } x0 | j    D]" } | j | t |  | d   q W| j d |  j  |  j |   S)z@Copy this distribution, substituting in any changed keyword argsz<project_name version py_version platform location precedenceNr  )r   r   r   r  r   )r   r&   r  r  r   r   r	   clone
  s
     zDistribution.clonec             C   s   d d   |  j  D S)Nc             S   s   g  |  ] } | r |  q Sr   r   )rC  Zdepr   r   r	   rF  
  s   	 z'Distribution.extras.<locals>.<listcomp>)r$  )r   r   r   r	   r   
  s    zDistribution.extras)(r   r   r   r   r   r  rv   r   r   r  r  r  r  r  r  r  r  r  r  r3   r  r   r$  r   r  r&  r~  r   r   r'  r  r   rL   rM   rN   r   r*  r
  r2  r   r   r   r   r	   r`     sJ   		
(c               @   sg   e  Z d  Z d Z d Z e j d  Z e d d    Z	 e d d    Z
 d d	   Z d
 d   Z d S)DistInfoDistributionzGWrap an actual or potential sys.path entry w/metadata, .dist-info styleZMETADATAz([\(,])\s*(\d.*?)\s*([,\)])c             C   sX   y |  j  SWnF t k
 rS |  j |  j  } t j j   j |  |  _  |  j  SYn Xd S)zParse and cache metadataN)Z	_pkg_infor  r   r   emailrb  ZParserZparsestr)r   r  r   r   r	   _parsed_pkg_info
  s    z%DistInfoDistribution._parsed_pkg_infoc             C   s:   y |  j  SWn( t k
 r5 |  j   |  _  |  j  SYn Xd  S)N)_DistInfoDistribution__dep_mapr  _compute_dependencies)r   r   r   r	   r$   
  s
    zDistInfoDistribution._dep_mapc             C   sy   | j  d d  d g } | d j   } | d j   } t j |  j d |  } | j d d  j d d  } | | f S)zConvert 'Foobar (1); baz' to ('Foobar ==1', 'baz')
        Split environment marker, add == prefix to version specifiers as
        necessary, and remove parenthesis.
        ;r   r   r   z\1==\2\3())r   r  r6  r7  EQEQr   )r   Zrequires_distr  distversmarkr   r   r	   _preparse_requirement(
  s    z*DistInfoDistribution._preparse_requirementc       
         s  d d l  m } i g  d 6} |  _ g    xc |  j j d  pB g  D]I } |  j |  \ } } t t |   } | |  | _   j	 |  qC W  f d d   } t
 | d   } | d j |  xR |  j j d  p g  D]8 }	 t |	 j    }	 t t
 | |	   |  | |	 <q W| S)z+Recompute this distribution's dependencies.r   )r   NzRequires-Distc             3   s6   x/   D]' } | j  d i |  d 6 r | Vq q Wd  S)NZoverrider8  )	marker_fn)r8  r   )r   r   r	   reqs_for_extraA
  s    zBDistInfoDistribution._compute_dependencies.<locals>.reqs_for_extrazProvides-Extra)rd  r   r6  r5  Zget_allr>  r   rh   r?  r   	frozensetr   rp   r  r   )
r   Zcompile_markerr#  r   r<  r=  r  r@  Zcommonr8  r   )r   r	   r7  4
  s    $z*DistInfoDistribution._compute_dependenciesN)r   r   r   r   r   r6  r   r;  r  r5  r$  r>  r7  r   r   r   r	   r3  
  s   
r3  z.eggz	.egg-infoz
.dist-infoc              O   so   d } t    } y- x& t j |  j | k r: | d 7} q WWn t k
 rP Yn Xt j d | d |  |  d  S)Nr   
stacklevel)r   r?   r   r   rD   r#  r$  )r8   r&   levelr*   r   r   r	   r0  W
  s    	r0  c             #   s  t  t |        f d d   } x   D] } t |  } | sU t d |   n  | j d  } | j   } g  } t | |  } | r | j   } | t t | | d d  \ } } } n  | t t	 | | d d  \ } } } d d	   | D } t
 | | |  Vq+ Wd
 S)zYield ``Requirement`` objects for each specification in `strs`

    `strs` must be an instance of ``basestring``, or a (possibly-nested)
    iterable thereof.
    c       	         sX  g  } x| | |  s&t  | |  ra y t    } d } Wqa t k
 r] t d   Yqa Xn  |  | |  } | s d | d } t | | d | | d     n  | j | j |    | j   } t | |  } | r | j   } q	 | | |  s	 d } t | | d | | d     q	 q	 W| | |  } | rK| j   } n  | | | f S)Nr   z+\ must not appear on the last nonblank linez	Expected z inatzExpected ',' or end-of-list in)CONTINUEr   StopIterationrD   r   rC   endCOMMA)	ZITEMZ
TERMINATORr  r(  groupsZ	item_namer(   r>   r&  )r  r   r	   	scan_listm
  s2    
"&z%parse_requirements.<locals>.scan_listzMissing distribution specr   z'extra' namer;   zversion specc             S   s(   g  |  ] \ } } | t  |  f  q Sr   )rk   )rC  ra  r0   r   r   r	   rF  
  s   	 z&parse_requirements.<locals>.<listcomp>N)r   )r   r;   )iterrn   DISTROrD   rC   rG  OBRACKETCBRACKETVERSIONLINE_ENDra   )r  rJ  r  r>   r   r(  r   r  r   )r  r	   rh   d
  s$    $$c               @   sj   e  Z d  Z d d   Z d d   Z d d   Z d d   Z d	 d
   Z d d   Z e	 d d    Z
 d S)ra   c             C   s   | t  |  |  _ } | | j   |  _ |  _ d d   | D } | j   d d   | D |  _ | t t t	 |   |  _
 |  _ |  j t d d   | D  t |  j  f |  _ t |  j  |  _ d S)z>DO NOT CALL THIS UNDOCUMENTED METHOD; use Requirement.parse()!c             S   s2   g  |  ]( \ } } t  |  t | | | f  q Sr   )ri   state_machine)rC  ra  r,   r   r   r	   rF  
  s   	z(Requirement.__init__.<locals>.<listcomp>c             S   s(   g  |  ] \ } } } } | | f  q Sr   r   )rC  r  transra  r  r   r   r	   rF  
  s   	 c             s   s'   |  ] \ } } } } | | f Vq d  S)Nr   )rC  r  rR  ra  r  r   r   r	   r  
  s    z'Requirement.__init__.<locals>.<genexpr>N)rj   Zunsafe_namer  r   r3   r   r  r  r   rp   r+  r   rA  hashCmpr  _Requirement__hash)r   r   r  r   r+  r   r   r	   r   
  s    	

"zRequirement.__init__c             C   sX   d j  d d   |  j D  } d j  |  j  } | rD d | } n  d |  j | | f S)Nr  c             S   s   g  |  ] } d  j  |   q S)r   )rA   )rC  rk  r   r   r	   rF  
  s   	 z'Requirement.__str__.<locals>.<listcomp>z[%s]z%s%s%s)rA   r  r   r   )r   r  r   r   r   r	   r   
  s
    zRequirement.__str__c             C   s   t  | t  o |  j | j k S)N)r   ra   rS  )r   r  r   r   r	   r  
  s    zRequirement.__eq__c       	      C   s  t  | t  r= | j |  j k r% d S|  j r[ | j } q[ n t  | t  r[ t |  } n  d  } d d   } x |  j D]{ \ } } } } | | | |  } | d k r d S| d k r d S| d k r d } qw | d k s | d  k rw d } qw qw W| d  k rd } n  | S)	NFc             S   s   |  | k |  | k  S)Nr   )abr   r   r	   r
   
  s    z*Requirement.__contains__.<locals>.<lambda>FTT+r5  )r   r`   r3   r+  r  r   ri   )	r   r   r  Zcomparer  rR  ra  r  actionr   r   r	   r   
  s,    			zRequirement.__contains__c             C   s   |  j  S)N)rT  )r   r   r   r	   r  
  s    zRequirement.__hash__c             C   s   d t  |   S)NzRequirement.parse(%r))r   )r   r   r   r	   r   
  s    zRequirement.__repr__c             C   sW   t  t |    } | rD t |  d k r2 | d St d |    n  t d |    d  S)Nr   r   zExpected only one requirementzNo requirements found)r   rh   rH  rD   )rk  r   r   r   r	   r   
  s    zRequirement.parseN)r   r   r   r   r   r  r   r  r   r*  r   r   r   r   r	   ra   
  s   z--T<zT-Tz<=zF+F>zT+Fz>=zT..z==zF++z!=c             C   s@   t  |  t  s9 Gd d   d |  t  }  |  j d d  S|  j S)z&Get an mro for a type or classic classc               @   s   e  Z d  Z d S)z_get_mro.<locals>.clsN)r   r   r   r   r   r   r	   r   
  s    r   r   N)r   r  object__mro__)r   r   r   r	   _get_mro
  s    r_  c             C   sA   x: t  t | d t |    D] } | |  k r |  | Sq Wd S)z2Return an adapter factory for `ob` from `registry`r   N)r_  r   r  )registryr4   rR  r   r   r	   r     s    %r   c             C   s8   t  j j |   } t  j j |  s4 t  j |  n  d S)z1Ensure that the parent directory of `path` existsN)r   r   r    r   makedirs)r   r    r   r   r	   rt   	  s    c             c   s   d } g  } x t  |   D]| } | j d  r | j d  rv | sI | rW | | f Vn  | d d  j   } g  } q t d |   q | j |  q W| | f Vd S)as  Split a string or iterable thereof into (section, content) pairs

    Each ``section`` is a stripped version of the section header ("[section]")
    and each ``content`` is a list of stripped lines excluding blank lines and
    comment-only lines.  If there are any such lines before the first section
    header, they're returned in a first ``section`` of ``None``.
    Nr  ]r   zInvalid section headingr   )rn   r   r  r  rD   r   )rk  ZsectionZcontentr  r   r   r	   ro     s    	c              O   s7   t  j } z t t  _ t j |  |   SWd  | t  _ Xd  S)N)r   r   os_opentempfileZmkstemp)r8   r&   Zold_openr   r   r	   r  (  s
    		r  c             C   s@   x9 t  t  D]+ } | j d  s t t |  |  | <q q Wd  S)Nr   )r  _managerr   r   )r*   r   r   r   r	   _initialize5  s    rf  r]  c             C   s
   |  j    S)N)r&  )r   r   r   r	   r
   H  s    )r   r?   r   r  r6  r  r  r  r#  r  rI  pkgutilrO  r^  r  r@   r  r   Zemail.parserr4  rd  r   r   r   r   Zurllib.parserA  re  Zsetsr   r   r   	cStringIOr   r   r   ior   r   r   r   r   r   r   rc  os.pathr   r   r  implementationr   importlib._bootstrap
_bootstrapZimportlib_bootstraprb  r   r#   r'   r-   r.   r1   r5   r6   r7   Z
_sget_noneZ
_sset_nonerG   __all__	Exceptionrc   rd   re   rf   r   r   r  rv   rw   rx   ry   rz   r   rJ   rB   r   r<   r   r=   r   rl   rm   rI   r   rK   rL   rM   rN   r{   r|   r]  r^   r]   r   r4  rg   r_   r\   rj   rk   rp   rq   r9  r?  rr   rs   r   r   r   r  SourceFileLoaderr   r   r$   r  r  r  r  r   r  r}   r~   r   r   rY   r  r  r  ImpImporter
FileFinderr   r  rV   r   r  r  ru   r  r  rn   r>   rP  rE  rL  rO  rH  rM  rN  r  VERBOSE
IGNORECASEr  r  r   r   r  ri   rb   r  r`   r3  r  r0  rh   ra   rQ  r_  r   rt   ro   r  re  rf  r   r   rW   rH   rO   r   rX   r   r   r   r   r   r   r   r   r	   <module>   s  
'
											.	
 h,
			c	
) ,-j	 ,?
BJ
						