????

Your IP : 18.188.32.156


Current Path : /lib/python3.6/site-packages/pip/_vendor/distlib/__pycache__/
Upload File :
Current File : //lib/python3.6/site-packages/pip/_vendor/distlib/__pycache__/database.cpython-36.pyc

3

�Pf��@s�dZddlmZddlZddlZddlZddlZddlZddlZddl	Z	ddl
Z
ddlZddlm
Z
mZddlmZddlmZmZddlmZmZmZdd	lmZmZmZmZmZmZmZd
ddd
dgZ ej!e"�Z#dZ$dZ%deddde$dfZ&dZ'Gdd�de(�Z)Gdd�de(�Z*Gdd
�d
e(�Z+Gdd�de+�Z,Gdd�de,�Z-Gdd
�d
e,�Z.e-Z/e.Z0Gdd�de(�Z1d)d!d"�Z2d#d$�Z3d%d&�Z4d'd(�Z5dS)*zPEP 376 implementation.�)�unicode_literalsN�)�DistlibException�	resources)�StringIO)�
get_scheme�UnsupportedVersionError)�Metadata�METADATA_FILENAME�WHEEL_METADATA_FILENAME)�parse_requirement�cached_property�parse_name_and_version�read_exports�
write_exports�	CSVReader�	CSVWriter�Distribution�BaseInstalledDistribution�InstalledDistribution�EggInfoDistribution�DistributionPathzpydist-exports.jsonzpydist-commands.jsonZ	INSTALLER�RECORD�	REQUESTED�	RESOURCES�SHAREDz
.dist-infoc@s(eZdZdZdd�Zdd�Zdd�ZdS)	�_CachezL
    A simple cache mapping names and .dist-info paths to distributions
    cCsi|_i|_d|_dS)zZ
        Initialise an instance. There is normally one for each DistributionPath.
        FN)�name�path�	generated)�self�r!�/usr/lib/python3.6/database.py�__init__0sz_Cache.__init__cCs|jj�|jj�d|_dS)zC
        Clear the cache, setting it to its initial state.
        FN)r�clearrr)r r!r!r"r$8s

z_Cache.clearcCs2|j|jkr.||j|j<|jj|jg�j|�dS)z`
        Add a distribution to the cache.
        :param dist: The distribution to add.
        N)rr�
setdefault�key�append)r �distr!r!r"�add@sz
_Cache.addN)�__name__�
__module__�__qualname__�__doc__r#r$r)r!r!r!r"r,src@s�eZdZdZddd�Zdd�Zdd	�Zeee�Zd
d�Z	dd
�Z
dd�Zedd��Z
dd�Zdd�Zddd�Zdd�Zddd�ZdS)rzU
    Represents a set of distributions installed on a path (typically sys.path).
    NFcCsD|dkrtj}||_d|_||_t�|_t�|_d|_td�|_	dS)a�
        Create an instance from a path, optionally including legacy (distutils/
        setuptools/distribute) distributions.
        :param path: The path to use, as a list of directories. If not specified,
                     sys.path is used.
        :param include_egg: If True, this instance will look for and return legacy
                            distributions as well as those based on PEP 376.
        NT�default)
�sysr�
_include_dist�_include_eggr�_cache�
_cache_egg�_cache_enabledr�_scheme)r rZinclude_eggr!r!r"r#Ns	zDistributionPath.__init__cCs|jS)N)r4)r r!r!r"�_get_cache_enabledbsz#DistributionPath._get_cache_enabledcCs
||_dS)N)r4)r �valuer!r!r"�_set_cache_enabledesz#DistributionPath._set_cache_enabledcCs|jj�|jj�dS)z,
        Clears the internal cache.
        N)r2r$r3)r r!r!r"�clear_cachejs
zDistributionPath.clear_cachec
csTt�}�xF|jD�]:}tj|�}|dkr*q|jd�}|s|jrDqt|j�}�x�|D]�}|j|�}|sV|j|krvqV|jo�|jt	��rt
tg}x*|D] }tj
||�}	|j|	�}
|
r�Pq�WqVtj|
j���}t|dd�}WdQRXtjd|j�|j|j�t|j||d�VqV|jrV|jd	�rVtjd|j�|j|j�t|j|�VqVWqWdS)
zD
        Yield .dist-info and/or .egg(-info) distributions.
        N��legacy)�fileobj�schemezFound %s)�metadata�env�	.egg-info�.egg)r@rA)�setrr�finder_for_path�findZis_container�sortedr0�endswith�DISTINFO_EXTr
r�	posixpath�join�
contextlib�closing�	as_streamr	�logger�debugr)�new_dist_classr1�old_dist_class)
r �seenr�finder�rZrset�entryZpossible_filenamesZmetadata_filenameZ
metadata_pathZpydist�streamr>r!r!r"�_yield_distributionsrs@






z%DistributionPath._yield_distributionscCst|jj}|jo|jj}|s"|rpx4|j�D](}t|t�rH|jj|�q,|jj|�q,W|rdd|j_|rpd|j_dS)zk
        Scan the path for distributions and populate the cache with
        those that are found.
        TN)r2rr1r3rV�
isinstancerr))r Zgen_distZgen_eggr(r!r!r"�_generate_cache�s

z DistributionPath._generate_cachecCs|jdd�}dj||g�tS)ao
        The *name* and *version* parameters are converted into their
        filename-escaped form, i.e. any ``'-'`` characters are replaced
        with ``'_'`` other than the one in ``'dist-info'`` and the one
        separating the name from the version number.

        :parameter name: is converted to a standard distribution name by replacing
                         any runs of non- alphanumeric characters with a single
                         ``'-'``.
        :type name: string
        :parameter version: is converted to a standard version string. Spaces
                            become dots, and all other non-alphanumeric characters
                            (except dots) become dashes, with runs of multiple
                            dashes condensed to a single dash.
        :type version: string
        :returns: directory name
        :rtype: string�-�_)�replacerIrG)�clsr�versionr!r!r"�distinfo_dirname�sz!DistributionPath.distinfo_dirnameccsj|js x^|j�D]
}|VqWnF|j�x|jjj�D]
}|Vq6W|jrfx|jjj�D]
}|VqXWdS)a5
        Provides an iterator that looks for distributions and returns
        :class:`InstalledDistribution` or
        :class:`EggInfoDistribution` instances for each one of them.

        :rtype: iterator of :class:`InstalledDistribution` and
                :class:`EggInfoDistribution` instances
        N)r4rVrXr2r�valuesr1r3)r r(r!r!r"�get_distributions�s	
z"DistributionPath.get_distributionscCs�d}|j�}|js6xj|j�D]}|j|kr|}PqWnH|j�||jjkr\|jj|d}n"|jr~||jjkr~|jj|d}|S)a=
        Looks for a named distribution on the path.

        This function only returns the first result found, as no more than one
        value is expected. If nothing is found, ``None`` is returned.

        :rtype: :class:`InstalledDistribution`, :class:`EggInfoDistribution`
                or ``None``
        Nr)	�lowerr4rVr&rXr2rr1r3)r r�resultr(r!r!r"�get_distribution�s

z!DistributionPath.get_distributionc	cs�d}|dk	rJy|jjd||f�}Wn$tk
rHtd||f��YnXxd|j�D]X}|j}xL|D]D}t|�\}}|dkr�||kr�|VPqd||krd|j|�rd|VPqdWqTWdS)a
        Iterates over all distributions to find which distributions provide *name*.
        If a *version* is provided, it will be used to filter the results.

        This function only returns the first result found, since no more than
        one values are expected. If the directory is not found, returns ``None``.

        :parameter version: a version specifier that indicates the version
                            required, conforming to the format in ``PEP-345``

        :type name: string
        :type version: string
        Nz%s (%s)zinvalid name or version: %r, %r)r5�matcher�
ValueErrorrr`�providesr�match)	r rr]rdr(�provided�p�p_name�p_verr!r!r"�provides_distribution�s$
z&DistributionPath.provides_distributioncCs(|j|�}|dkrtd|��|j|�S)z5
        Return the path to a resource file.
        Nzno distribution named %r found)rc�LookupError�get_resource_path)r r�
relative_pathr(r!r!r"�
get_file_paths
zDistributionPath.get_file_pathccs`xZ|j�D]N}|j}||kr
||}|dk	r@||krX||Vq
x|j�D]
}|VqJWq
WdS)z�
        Return all of the exported entries in a particular category.

        :param category: The category to search for entries.
        :param name: If specified, only entries with that name are returned.
        N)r`�exportsr_)r �categoryrr(rS�d�vr!r!r"�get_exported_entries"sz%DistributionPath.get_exported_entries)NF)N)N)r*r+r,r-r#r6r8�propertyZ
cache_enabledr9rVrX�classmethodr^r`rcrlrprur!r!r!r"rJs

*
$	c@s�eZdZdZdZdZdd�Zedd��ZeZ	edd��Z
ed	d
��Zdd�Zed
d��Z
edd��Zedd��Zedd��Zedd��Zdd�Zdd�Zdd�Zdd�ZdS) rz�
    A base class for distributions, whether installed or from indexes.
    Either way, it must have some metadata, so that's all that's needed
    for construction.
    FcCsL||_|j|_|jj�|_|j|_d|_d|_d|_d|_t	�|_
i|_dS)z�
        Initialise an instance.
        :param metadata: The instance of :class:`Metadata` describing this
        distribution.
        N)r>rrar&r]Zlocator�digest�extras�contextrBZ
download_urlsZdigests)r r>r!r!r"r#GszDistribution.__init__cCs|jjS)zH
        The source archive download URL for this distribution.
        )r>�
source_url)r r!r!r"r{XszDistribution.source_urlcCsd|j|jfS)zX
        A utility property which displays the name and version in parentheses.
        z%s (%s))rr])r r!r!r"�name_and_versionaszDistribution.name_and_versioncCs.|jj}d|j|jf}||kr*|j|�|S)z�
        A set of distribution names and versions provided by this distribution.
        :return: A set of "name (version)" strings.
        z%s (%s))r>rfrr]r')r Zplist�sr!r!r"rfhs

zDistribution.providescCs8|j}tjd|j��t||�}t|j||j|jd��S)Nz%Getting requirements from metadata %r)ryr?)	r>rMrNZtodict�getattrrBZget_requirementsryrz)r Zreq_attr�mdZreqtsr!r!r"�_get_requirementsts

zDistribution._get_requirementscCs
|jd�S)N�run_requires)r�)r r!r!r"r�{szDistribution.run_requirescCs
|jd�S)N�
meta_requires)r�)r r!r!r"r�szDistribution.meta_requirescCs
|jd�S)N�build_requires)r�)r r!r!r"r��szDistribution.build_requirescCs
|jd�S)N�
test_requires)r�)r r!r!r"r��szDistribution.test_requirescCs
|jd�S)N�dev_requires)r�)r r!r!r"r��szDistribution.dev_requiresc
Cs�t|�}t|jj�}y|j|j�}Wn6tk
rZtjd|�|j	�d}|j|�}YnX|j
}d}xJ|jD]@}t|�\}}	||kr�qny|j
|	�}PWqntk
r�YqnXqnW|S)z�
        Say if this instance matches (fulfills) a requirement.
        :param req: The requirement to match.
        :rtype req: str
        :return: True if it matches, else False.
        z+could not read version %r - using name onlyrF)rrr>r=rd�requirementrrM�warning�splitr&rfrrg)
r �reqrSr=rdrrbrirjrkr!r!r"�matches_requirement�s*	

z Distribution.matches_requirementcCs(|jrd|j}nd}d|j|j|fS)zC
        Return a textual representation of this instance,
        z [%s]r:z<Distribution %s (%s)%s>)r{rr])r �suffixr!r!r"�__repr__�szDistribution.__repr__cCs>t|�t|�k	rd}n$|j|jko8|j|jko8|j|jk}|S)a<
        See if this distribution is the same as another.
        :param other: The distribution to compare with. To be equal to one
                      another. distributions must have the same type, name,
                      version and source_url.
        :return: True if it is the same, else False.
        F)�typerr]r{)r �otherrbr!r!r"�__eq__�szDistribution.__eq__cCst|j�t|j�t|j�S)zH
        Compute hash in a way which matches the equality test.
        )�hashrr]r{)r r!r!r"�__hash__�szDistribution.__hash__N)r*r+r,r-Zbuild_time_dependency�	requestedr#rvr{Zdownload_urlr|rfr�r�r�r�r�r�r�r�r�r�r!r!r!r"r5s$"
cs0eZdZdZdZd�fdd�	Zddd�Z�ZS)	rz]
    This is the base class for installed distributions (whether PEP 376 or
    legacy).
    Ncs tt|�j|�||_||_dS)a
        Initialise an instance.
        :param metadata: An instance of :class:`Metadata` which describes the
                         distribution. This will normally have been initialised
                         from a metadata file in the ``path``.
        :param path:     The path of the ``.dist-info`` or ``.egg-info``
                         directory for the distribution.
        :param env:      This is normally the :class:`DistributionPath`
                         instance where this distribution was found.
        N)�superrr#r�	dist_path)r r>rr?)�	__class__r!r"r#�sz"BaseInstalledDistribution.__init__cCsd|dkr|j}|dkr"tj}d}ntt|�}d|j}||�j�}tj|�jd�jd�}d||fS)a�
        Get the hash of some data, using a particular hash algorithm, if
        specified.

        :param data: The data to be hashed.
        :type data: bytes
        :param hasher: The name of a hash implementation, supported by hashlib,
                       or ``None``. Examples of valid values are ``'sha1'``,
                       ``'sha224'``, ``'sha384'``, '``sha256'``, ``'md5'`` and
                       ``'sha512'``. If no hasher is specified, the ``hasher``
                       attribute of the :class:`InstalledDistribution` instance
                       is used. If the hasher is determined to be ``None``, MD5
                       is used as the hashing algorithm.
        :returns: The hash of the data. If a hasher was explicitly specified,
                  the returned hash will be prefixed with the specified hasher
                  followed by '='.
        :rtype: str
        Nr:z%s=�=�asciiz%s%s)	�hasher�hashlib�md5r~rx�base64Zurlsafe_b64encode�rstrip�decode)r �datar��prefixrxr!r!r"�get_hash�s

z"BaseInstalledDistribution.get_hash)N)N)r*r+r,r-r�r#r��
__classcell__r!r!)r�r"r�scs�eZdZdZdZd'�fdd�	Zdd�Zdd	�Zd
d�Ze	dd
��Z
dd�Zdd�Zdd�Z
dd�Zd(dd�Zdd�Ze	dd��Zd)dd�Zdd �Zd!d"�Zd#d$�Zd%d&�ZejZ�ZS)*ra
    Created with the *path* of the ``.dist-info`` directory provided to the
    constructor. It reads the metadata contained in ``pydist.json`` when it is
    instantiated., or uses a passed in Metadata instance (useful for when
    dry-run mode is being used).
    Zsha256Ncs0tj|�|_}|dkr(ddl}|j�|rN|jrN||jjkrN|jj|j}nt|dkr�|j	t
�}|dkrr|j	t�}|dkr�|j	d�}|dkr�tdt
|f��t
j|j���}t|dd�}WdQRXtt|�j|||�|r�|jr�|jj|�y|j	d�}Wn&tk
�r ddl}|j�YnX|dk	|_dS)NrZMETADATAzno %s found in %sr;)r<r=r)rrCrR�pdbZ	set_tracer4r2rr>rDr
rrerJrKrLr	r�rr#r)�AttributeErrorr�)r rr>r?rRr�rSrU)r�r!r"r#s4




zInstalledDistribution.__init__cCsd|j|j|jfS)Nz#<InstalledDistribution %r %s at %r>)rr]r)r r!r!r"r�2szInstalledDistribution.__repr__cCsd|j|jfS)Nz%s %s)rr])r r!r!r"�__str__6szInstalledDistribution.__str__c
Cs�g}|jd�}tj|j���`}t|d��J}xB|D]:}dd�tt|�d�D�}||\}}}	|j|||	f�q0WWdQRXWdQRX|S)a"
        Get the list of installed files for the distribution
        :return: A list of tuples of path, hash and size. Note that hash and
                 size might be ``None`` for some entries. The path is exactly
                 as stored in the file (which is as in PEP 376).
        r)rUcSsg|]}d�qS)Nr!)�.0�ir!r!r"�
<listcomp>Hsz6InstalledDistribution._get_records.<locals>.<listcomp>�N)�get_distinfo_resourcerJrKrLr�range�lenr')
r �resultsrSrUZ
record_reader�row�missingr�checksum�sizer!r!r"�_get_records9s

(z"InstalledDistribution._get_recordscCsi}|jt�}|r|j�}|S)a
        Return the information exported by this distribution.
        :return: A dictionary of exports, mapping an export category to a dict
                 of :class:`ExportEntry` instances describing the individual
                 export entries, and keyed by name.
        )r��EXPORTS_FILENAMEr)r rbrSr!r!r"rqPs

zInstalledDistribution.exportsc	Cs8i}|jt�}|r4tj|j���}t|�}WdQRX|S)z�
        Read exports data from a file in .ini format.

        :return: A dictionary of exports, mapping an export category to a list
                 of :class:`ExportEntry` instances describing the individual
                 export entries.
        N)r�r�rJrKrLr)r rbrSrUr!r!r"r^s
z"InstalledDistribution.read_exportsc
Cs.|jt�}t|d��}t||�WdQRXdS)a
        Write a dictionary of exports to a file in .ini format.
        :param exports: A dictionary of exports, mapping an export category to
                        a list of :class:`ExportEntry` instances describing the
                        individual export entries.
        �wN)�get_distinfo_filer��openr)r rqZrf�fr!r!r"rms
z#InstalledDistribution.write_exportscCsh|jd�}tj|j���:}t|d��$}x|D]\}}||kr,|Sq,WWdQRXWdQRXtd|��dS)aW
        NOTE: This API may change in the future.

        Return the absolute path to a resource file with the given relative
        path.

        :param relative_path: The path, relative to .dist-info, of the resource
                              of interest.
        :return: The absolute path where the resource is to be found.
        r)rUNz3no resource file with relative path %r is installed)r�rJrKrLr�KeyError)r rorSrUZresources_readerZrelativeZdestinationr!r!r"rnxs
z'InstalledDistribution.get_resource_pathccsx|j�D]
}|Vq
WdS)z�
        Iterates over the ``RECORD`` entries and returns a tuple
        ``(path, hash, size)`` for each line.

        :returns: iterator of (path, hash, size)
        N)r�)r rbr!r!r"�list_installed_files�sz*InstalledDistribution.list_installed_filesFcCs,tjj|d�}tjj|j�}|j|�}tjj|d�}|jd�}tjd|�|rRdSt|���}x�|D]�}tjj	|�s||j
d	�r�d}	}
n4dtjj|�}
t|d��}|j
|j��}	WdQRX|j|�s�|r�|j|�r�tjj||�}|j||	|
f�qbW|j|��rtjj||�}|j|ddf�WdQRX|S)
z�
        Writes the ``RECORD`` file, using the ``paths`` iterable passed in. Any
        existing ``RECORD`` file is silently overwritten.

        prefix is used to determine when to write absolute paths.
        r:rzcreating %sN�.pyc�.pyoz%d�rb)r�r�)�osrrI�dirname�
startswithr�rM�infor�isdirrF�getsizer�r��read�relpathZwriterow)r �pathsr��dry_run�baseZbase_under_prefix�record_path�writerr�
hash_valuer��fpr!r!r"�write_installed_files�s.





z+InstalledDistribution.write_installed_filesc
Csg}tjj|j�}|jd�}�x�|j�D]�\}}}tjj|�sLtjj||�}||krVq(tjj|�sv|j|dddf�q(tjj	|�r(t
tjj|��}|r�||kr�|j|d||f�q(|r(d|kr�|jdd�d}nd	}t
|d
��2}	|j|	j�|�}
|
|k�r|j|d||
f�Wd	QRXq(W|S)a�
        Checks that the hashes and sizes of the files in ``RECORD`` are
        matched by the files themselves. Returns a (possibly empty) list of
        mismatches. Each entry in the mismatch list will be a tuple consisting
        of the path, 'exists', 'size' or 'hash' according to what didn't match
        (existence is checked first, then size, then hash), the expected
        value and the actual value.
        r�existsTFr��=rrNr�r�)r�rr�r�r��isabsrIr�r'�isfile�strr�r�r�r�r�)r �
mismatchesr�r�rr�r�Zactual_sizer�r�Zactual_hashr!r!r"�check_installed_files�s.	

 z+InstalledDistribution.check_installed_filescCs�i}tjj|jd�}tjj|�r�tj|ddd��}|j�j�}WdQRXx@|D]8}|jdd�\}}|dkr~|j	|g�j
|�qN|||<qNW|S)	a�
        A dictionary of shared locations whose keys are in the set 'prefix',
        'purelib', 'platlib', 'scripts', 'headers', 'data' and 'namespace'.
        The corresponding value is the absolute path of that category for
        this distribution, and takes into account any paths selected by the
        user at installation time (e.g. via command-line arguments). In the
        case of the 'namespace' key, this would be a list of absolute paths
        for the roots of namespace packages in this distribution.

        The first time this property is accessed, the relevant information is
        read from the SHARED file in the .dist-info directory.
        rrSzutf-8)�encodingNr�r�	namespace)r�rrIr��codecsr�r��
splitlinesr�r%r')r rb�shared_pathr��lines�liner&r7r!r!r"�shared_locations�s
z&InstalledDistribution.shared_locationsc	
Cs�tjj|jd�}tjd|�|r$dSg}x6dD].}||}tjj||�r.|jd	||f�q.Wx"|jd
f�D]}|jd|�qnWtj	|dd
d��}|j
dj|��WdQRX|S)aa
        Write shared location information to the SHARED file in .dist-info.
        :param paths: A dictionary as described in the documentation for
        :meth:`shared_locations`.
        :param dry_run: If True, the action is logged but no file is actually
                        written.
        :return: The path of the file written to.
        rzcreating %sNr��lib�headers�scriptsr�z%s=%sr�znamespace=%sr�zutf-8)r��
)r�r�r�r�r�)r�rrIrMr�r�r'�getr�r��write)	r r�r�r�r�r&r�nsr�r!r!r"�write_shared_locations�s	
z,InstalledDistribution.write_shared_locationscCsF|tkrtd||jf��tj|j�}|dkr<td|j��|j|�S)Nz+invalid path for a dist-info file: %r at %rzUnable to get a finder for %s)�
DIST_FILESrrrrCrD)r rrRr!r!r"r�sz+InstalledDistribution.get_distinfo_resourcecCs~|jtj�dkrT|jtj�dd�\}}||jjtj�dkrTtd||j|jf��|tkrntd||jf��tjj	|j|�S)	a�
        Returns a path located under the ``.dist-info`` directory. Returns a
        string representing the path.

        :parameter path: a ``'/'``-separated path relative to the
                         ``.dist-info`` directory or an absolute path;
                         If *path* is an absolute path and doesn't start
                         with the ``.dist-info`` directory path,
                         a :class:`DistlibException` is raised
        :type path: str
        :rtype: str
        r�Nrz;dist-info file %r does not belong to the %r %s distributionz+invalid path for a dist-info file: %r at %r������)
rDr��sepr�rrrr]r�rI)r rr^r!r!r"r�sz'InstalledDistribution.get_distinfo_fileccsVtjj|j�}xB|j�D]6\}}}tjj|�s<tjj||�}|j|j�r|VqWdS)z�
        Iterates over the ``RECORD`` entries and returns paths for each line if
        the path is pointing to a file located in the ``.dist-info`` directory
        or one of its subdirectories.

        :returns: iterator of paths
        N)r�rr�r�r�rIr�)r r�rr�r�r!r!r"�list_distinfo_files6sz)InstalledDistribution.list_distinfo_filescCst|t�o|j|jkS)N)rWrr)r r�r!r!r"r�Fs
zInstalledDistribution.__eq__)NN)F)F)r*r+r,r-r�r#r�r�r�r
rqrrrnr�r�r�r�r�r�r�r�r��objectr�r�r!r!)r�r"r	s(

##
	csjeZdZdZdZiZd�fdd�	Zdd�Zdd	�Zd
d�Z	dd
�Z
dd�Zddd�Zdd�Z
ejZ�ZS)raCreated with the *path* of the ``.egg-info`` directory or file provided
    to the constructor. It reads the metadata contained in the file itself, or
    if the given path happens to be a directory, the metadata is read from the
    file ``PKG-INFO`` under that directory.TNcs�dd�}||_||_|rJ|jrJ||jjkrJ|jj|j}|||j|j�n0|j|�}|||j|j�|rz|jrz|jj|�t	t
|�j|||�dS)NcSs||_|j�|_||_dS)N)rrar&r])r}�nrtr!r!r"�set_name_and_versionXs
z:EggInfoDistribution.__init__.<locals>.set_name_and_version)rr�r4r3r>rr]�
_get_metadatar)r�rr#)r rr?r�r>)r�r!r"r#Ws

zEggInfoDistribution.__init__c
s2d}dd���fdd�}|jd�r�tjj|�rdtjj|dd�}t|dd	�}tjj|dd
�}||�}n`tj|�}t|j	d�j
d��}t|dd
�}y|j	d�}	�|	j
d��}Wntk
r�d}YnXnX|jd��rtjj|��rtjj|d
�}||�}tjj|d�}t|dd	�}ntd|��|�r.|j
|�|S)NcSs�g}|j�}x�|D]�}|j�}|jd�r6tjd|�Pt|�}|sPtjd|�q|jr`tjd�|jst|j|j	�qdj
dd�|jD��}|jd|j	|f�qW|S)	z�Create a list of dependencies from a requires.txt file.

            *data*: the contents of a setuptools-produced requires.txt file.
            �[z.Unexpected line: quitting requirement scan: %rz#Not recognised as a requirement: %rz4extra requirements in requires.txt are not supportedz, css|]}d|VqdS)z%s%sNr!)r��cr!r!r"�	<genexpr>�szQEggInfoDistribution._get_metadata.<locals>.parse_requires_data.<locals>.<genexpr>z%s (%s))r��stripr�rMr�rryZconstraintsr'rrI)r��reqsr�r�rSZconsr!r!r"�parse_requires_dataos&


z>EggInfoDistribution._get_metadata.<locals>.parse_requires_datacsHg}y*tj|dd��}�|j��}WdQRXWntk
rBYnX|S)z�Create a list of dependencies from a requires.txt file.

            *req_path*: the path to a setuptools-produced requires.txt file.
            rSzutf-8N)r�r�r��IOError)�req_pathr�r�)r�r!r"�parse_requires_path�sz>EggInfoDistribution._get_metadata.<locals>.parse_requires_pathz.eggzEGG-INFOzPKG-INFOr;)rr=zrequires.txtzEGG-INFO/PKG-INFO�utf8)r<r=zEGG-INFO/requires.txtzutf-8z	.egg-infoz,path must end with .egg-info or .egg, got %r)rFr�rr�rIr	�	zipimport�zipimporterr�get_datar�r�rZadd_requirements)
r r�requiresr��	meta_pathr>r�Zzipfr<r�r!)r�r"r�ls:




z!EggInfoDistribution._get_metadatacCsd|j|j|jfS)Nz!<EggInfoDistribution %r %s at %r>)rr]r)r r!r!r"r��szEggInfoDistribution.__repr__cCsd|j|jfS)Nz%s %s)rr])r r!r!r"r��szEggInfoDistribution.__str__cCsdg}tjj|jd�}tjj|�r`x>|j�D]2\}}}||kr>q*tjj|�s*|j|dddf�q*W|S)a�
        Checks that the hashes and sizes of the files in ``RECORD`` are
        matched by the files themselves. Returns a (possibly empty) list of
        mismatches. Each entry in the mismatch list will be a tuple consisting
        of the path, 'exists', 'size' or 'hash' according to what didn't match
        (existence is checked first, then size, then hash), the expected
        value and the actual value.
        zinstalled-files.txtr�TF)r�rrIr�r�r')r r�r�rrZr!r!r"r��s	z)EggInfoDistribution.check_installed_filesc
Cs�dd�}dd�}tjj|jd�}g}tjj|�r�tj|ddd��|}xt|D]l}|j�}tjjtjj|j|��}tjj|�s�tj	d	|�|j
d
�r�qHtjj|�sH|j|||�||�f�qHWWdQRX|j|ddf�|S)z�
        Iterates over the ``installed-files.txt`` entries and returns a tuple
        ``(path, hash, size)`` for each line.

        :returns: a list of (path, hash, size)
        c
Ss0t|d�}z|j�}Wd|j�Xtj|�j�S)Nr�)r�r��closer�r�Z	hexdigest)rr�Zcontentr!r!r"�_md5�s


z6EggInfoDistribution.list_installed_files.<locals>._md5cSstj|�jS)N)r��stat�st_size)rr!r!r"�_size�sz7EggInfoDistribution.list_installed_files.<locals>._sizezinstalled-files.txtrSzutf-8)r�zNon-existent file: %s�.pyc�.pyoN)rr)
r�rrIr�r�r�r��normpathrMr�rFr�r')r r�rr�rbr�r�rir!r!r"r��s"

&z(EggInfoDistribution.list_installed_filesFccs�tjj|jd�}d}tj|ddd��d}x\|D]T}|j�}|dkrFd}q,|s,tjjtjj|j|��}|j|j�r,|rz|Vq,|Vq,WWdQRXdS)	a
        Iterates over the ``installed-files.txt`` entries and returns paths for
        each line if the path is pointing to a file located in the
        ``.egg-info`` directory or one of its subdirectories.

        :parameter absolute: If *absolute* is ``True``, each returned path is
                          transformed into a local absolute path. Otherwise the
                          raw value from ``installed-files.txt`` is returned.
        :type absolute: boolean
        :returns: iterator of paths
        zinstalled-files.txtTrSzutf-8)r�z./FN)r�rrIr�r�r�rr�)r Zabsoluter��skipr�r�rir!r!r"r��s
z'EggInfoDistribution.list_distinfo_filescCst|t�o|j|jkS)N)rWrr)r r�r!r!r"r�s
zEggInfoDistribution.__eq__)N)F)r*r+r,r-r�r�r#r�r�r�r�r�r�r�r�r�r�r!r!)r�r"rNsK&
c@s^eZdZdZdd�Zdd�Zddd�Zd	d
�Zdd�Zddd�Z	ddd�Z
dd�Zdd�ZdS)�DependencyGrapha�
    Represents a dependency graph between distributions.

    The dependency relationships are stored in an ``adjacency_list`` that maps
    distributions to a list of ``(other, label)`` tuples where  ``other``
    is a distribution and the edge is labeled with ``label`` (i.e. the version
    specifier, if such was provided). Also, for more efficient traversal, for
    every distribution ``x``, a list of predecessors is kept in
    ``reverse_list[x]``. An edge from distribution ``a`` to
    distribution ``b`` means that ``a`` depends on ``b``. If any missing
    dependencies are found, they are stored in ``missing``, which is a
    dictionary that maps distributions to a list of requirements that were not
    provided by any other distributions.
    cCsi|_i|_i|_dS)N)�adjacency_list�reverse_listr�)r r!r!r"r#.szDependencyGraph.__init__cCsg|j|<g|j|<dS)z�Add the *distribution* to the graph.

        :type distribution: :class:`distutils2.database.InstalledDistribution`
                            or :class:`distutils2.database.EggInfoDistribution`
        N)rr)r �distributionr!r!r"�add_distribution3s
z DependencyGraph.add_distributionNcCs6|j|j||f�||j|kr2|j|j|�dS)a�Add an edge from distribution *x* to distribution *y* with the given
        *label*.

        :type x: :class:`distutils2.database.InstalledDistribution` or
                 :class:`distutils2.database.EggInfoDistribution`
        :type y: :class:`distutils2.database.InstalledDistribution` or
                 :class:`distutils2.database.EggInfoDistribution`
        :type label: ``str`` or ``None``
        N)rr'r)r �x�y�labelr!r!r"�add_edge=s
zDependencyGraph.add_edgecCs&tjd||�|jj|g�j|�dS)a
        Add a missing *requirement* for the given *distribution*.

        :type distribution: :class:`distutils2.database.InstalledDistribution`
                            or :class:`distutils2.database.EggInfoDistribution`
        :type requirement: ``str``
        z
%s missing %rN)rMrNr�r%r')r rr�r!r!r"�add_missingLszDependencyGraph.add_missingcCsd|j|jfS)Nz%s %s)rr])r r(r!r!r"�
_repr_distWszDependencyGraph._repr_distrcCs�|j|�g}xv|j|D]h\}}|j|�}|dk	r>d||f}|jd|t|��|j||d�}|jd�}|j|dd��qWdj|�S)zPrints only a subgraphNz%s [%s]z    rr�)rrr'r��	repr_noder��extendrI)r r(�level�outputr�rZ	suboutputZsubsr!r!r"rZs

zDependencyGraph.repr_nodeTcCs�g}|jd�x||jj�D]n\}}t|�dkr>|r>|j|�xH|D]@\}}|dk	rn|jd|j|j|f�qD|jd|j|jf�qDWqW|r�t|�dkr�|jd�|jd�|jd�x&|D]}|jd	|j�|jd
�q�W|jd�|jd�dS)a9Writes a DOT output for the graph to the provided file *f*.

        If *skip_disconnected* is set to ``True``, then all distributions
        that are not dependent on any other distribution are skipped.

        :type f: has to support ``file``-like operations
        :type skip_disconnected: ``bool``
        zdigraph dependencies {
rNz"%s" -> "%s" [label="%s"]
z
"%s" -> "%s"
zsubgraph disconnected {
zlabel = "Disconnected"
zbgcolor = red
z"%s"r�z}
)r�r�itemsr�r'r)r r�Zskip_disconnectedZdisconnectedr(�adjsr�rr!r!r"�to_dotgs&	






zDependencyGraph.to_dotcs�g}i}x&|jj�D]\}}|dd�||<qWx�g�x4t|j��dd�D]\}}|sL�j|�||=qLW�srPx*|j�D]\}}�fdd�|D�||<q|Wtjddd��D��|j��q2W|t|j��fS)aa
        Perform a topological sort of the graph.
        :return: A tuple, the first element of which is a topologically sorted
                 list of distributions, and the second element of which is a
                 list of distributions that cannot be sorted because they have
                 circular dependencies and so form a cycle.
        Ncs g|]\}}|�kr||f�qSr!r!)r�rsrS)�	to_remover!r"r��sz4DependencyGraph.topological_sort.<locals>.<listcomp>zMoving to result: %scSsg|]}d|j|jf�qS)z%s (%s))rr])r�rsr!r!r"r��s)rr�listr'rMrNr�keys)r rbZalist�krtr!)rr"�topological_sort�s$

z DependencyGraph.topological_sortcCs6g}x&|jj�D]\}}|j|j|��qWdj|�S)zRepresentation of the graphr�)rrr'rrI)r rr(rr!r!r"r��szDependencyGraph.__repr__)N)r)T)
r*r+r,r-r#r	r
rrrrrr�r!r!r!r"rs



 rr.cCsft|�}t�}i}xX|D]P}|j|�x@|jD]6}t|�\}}tjd|||�|j|g�j||f�q.WqWx�|D]�}|j	|j
B|jB|jB}x�|D]�}	y|j
|	�}
Wn6tk
r�tjd|	�|	j�d}|j
|�}
YnX|
j}d}||k�rJxV||D]J\}}y|
j|�}
Wntk
�r,d}
YnX|
r�|j|||	�d}Pq�W|s�|j||	�q�WqrW|S)a6Makes a dependency graph from the given distributions.

    :parameter dists: a list of distributions
    :type dists: list of :class:`distutils2.database.InstalledDistribution` and
                 :class:`distutils2.database.EggInfoDistribution` instances
    :rtype: a :class:`DependencyGraph` instance
    zAdd to provided: %s, %s, %sz+could not read version %r - using name onlyrFT)rrr	rfrrMrNr%r'r�r�r�r�rdrr�r�r&rgr
r)�distsr=�graphrhr(rirr]r�r�rdZmatchedZproviderrgr!r!r"�
make_graph�sD





rcCs~||krtd|j��t|�}|g}|j|}x@|rn|j�}|j|�x$|j|D]}||krR|j|�qRWq0W|jd�|S)z�Recursively generate a list of distributions from *dists* that are
    dependent on *dist*.

    :param dists: a list of distributions
    :param dist: a distribution, member of *dists* for which we are interested
    z1given distribution %r is not a member of the listr)rrrr�popr')rr(rZdep�todorsZsuccr!r!r"�get_dependent_dists�s



r!cCsv||krtd|j��t|�}g}|j|}xD|rp|j�d}|j|�x$|j|D]}||krT|j|�qTWq.W|S)z�Recursively generate a list of distributions from *dists* that are
    required by *dist*.

    :param dists: a list of distributions
    :param dist: a distribution, member of *dists* for which we are interested
    z1given distribution %r is not a member of the listr)rrrrrr')rr(rr�r rsZpredr!r!r"�get_required_dists�s


r"cKs4|jdd�}tf|�}||_||_|p(d|_t|�S)zO
    A convenience method for making a dist given just a name and version.
    �summaryzPlaceholder for summary)rr	rr]r#r)rr]�kwargsr#rr!r!r"�	make_dists

r%)r.)6r-Z
__future__rr�r�rJr�Zloggingr�rHr/r�r:rr�compatrr]rrr>r	r
r�utilrr
rrrrr�__all__Z	getLoggerr*rMr�ZCOMMANDS_FILENAMEr�rGr�rrrrrrrOrPrrr!r"r%r!r!r!r"�<module>sV$

l7GM
6