3
ûâde_+  ã               @   sR  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mZm	Z	m
Z
mZmZmZmZmZ d dlmZ d dlmZmZ d dlmZ d dlmZmZ d dlmZmZmZ d dlmZ d d	l m!Z! d d
l"m#Z# erÖd dlm$Z$ ne%Z$eeef Z&ej'e(ƒZ)G dd„ de$ƒZ*G dd„ de$ƒZ+G dd„ dƒZ,G dd„ de$ƒZ-G dd„ de-ƒZ.G dd„ de-ƒZ/dS )é    N)	ÚIOÚTYPE_CHECKINGÚ
CollectionÚ	ContainerÚIterableÚIteratorÚListÚOptionalÚUnion)ÚRequirement)ÚInvalidSpecifierÚSpecifierSet)ÚNormalizedName)ÚLegacyVersionÚVersion)ÚDIRECT_URL_METADATA_NAMEÚ	DirectUrlÚDirectUrlValidationError)Ústdlib_pkgs)Úegg_link_path_from_sys_path)Úurl_to_path)ÚProtocolc               @   sB   e Zd Zeedœdd„ƒZeedœdd„ƒZeedœdd„ƒZdS )	ÚBaseEntryPoint)Úreturnc             C   s
   t ƒ ‚d S )N)ÚNotImplementedError)Úself© r   ú:/tmp/pip-build-red6nood/pip/pip/_internal/metadata/base.pyÚname+   s    zBaseEntryPoint.namec             C   s
   t ƒ ‚d S )N)r   )r   r   r   r   Úvalue/   s    zBaseEntryPoint.valuec             C   s
   t ƒ ‚d S )N)r   )r   r   r   r   Úgroup3   s    zBaseEntryPoint.groupN)Ú__name__Ú
__module__Ú__qualname__ÚpropertyÚstrr   r   r    r   r   r   r   r   *   s   r   c               @   sž  e Zd Zedœdd„Zedœdd„Zeee dœdd„ƒZeee dœdd	„ƒZ	eee dœd
d„ƒZ
eedœdd„ƒZeedœdd„ƒZeee dœdd„ƒZeedœdd„ƒZeedœdd„ƒZeedœdd„ƒZeedœdd„ƒZeedœdd„ƒZeedœdd„Zee dœdd „Zeejjdœd!d"„ƒZeee dœd#d$„ƒZeedœd%d&„ƒZ ee!dœd'd(„ƒZ"f fe#e ee$ d)œd*d+„Z%ee dœd,d-„Z&d.S )/ÚBaseDistribution)r   c             C   s   | j › d| j› d| j› d�S )Nú z (ú))Úraw_nameÚversionÚlocation)r   r   r   r   Ú__repr__9   s    zBaseDistribution.__repr__c             C   s   | j › d| j› �S )Nr'   )r)   r*   )r   r   r   r   Ú__str__<   s    zBaseDistribution.__str__c             C   s
   t ƒ ‚dS )aÐ  Where the distribution is loaded from.

        A string value is not necessarily a filesystem path, since distributions
        can be loaded from other sources, e.g. arbitrary zip archives. ``None``
        means the distribution is created in-memory.

        Do not canonicalize this value with e.g. ``pathlib.Path.resolve()``. If
        this is a symbolic link, we want to preserve the relative path between
        it and files in the distribution.
        N)r   )r   r   r   r   r+   ?   s    zBaseDistribution.locationc             C   s6   | j }|r|jƒ r2t|jƒS nt| jƒ}|r2| jS dS )zÉThe project location for editable distributions.

        This is the directory where pyproject.toml or setup.py is located.
        None if the distribution is not installed in editable mode.
        N)Ú
direct_urlZis_local_editabler   Úurlr   r)   r+   )r   r.   Úegg_link_pathr   r   r   Úeditable_project_locationM   s    
z*BaseDistribution.editable_project_locationc             C   s
   t ƒ ‚dS )a'  Location of the .[egg|dist]-info directory.

        Similarly to ``location``, a string value is not necessarily a
        filesystem path. ``None`` means the distribution is created in-memory.

        For a modern .dist-info installation on disk, this should be something
        like ``{location}/{raw_name}-{version}.dist-info``.

        Do not canonicalize this value with e.g. ``pathlib.Path.resolve()``. If
        this is a symbolic link, we want to preserve the relative path between
        it and other files in the distribution.
        N)r   )r   r   r   r   Úinfo_directoryc   s    zBaseDistribution.info_directoryc             C   s
   t ƒ ‚d S )N)r   )r   r   r   r   Úcanonical_names   s    zBaseDistribution.canonical_namec             C   s
   t ƒ ‚d S )N)r   )r   r   r   r   r*   w   s    zBaseDistribution.versionc             C   sn   y| j tƒ}W n tk
r"   dS X y
tj|ƒS  ttjtfk
rh } zt	j
dt| j|ƒ dS d}~X nX dS )z¬Obtain a DirectUrl from this distribution.

        Returns None if the distribution has no `direct_url.json` metadata,
        or if `direct_url.json` is invalid.
        NzError parsing %s for %s: %s)Ú	read_textr   ÚFileNotFoundErrorr   Z	from_jsonÚUnicodeDecodeErrorÚjsonÚJSONDecodeErrorr   ÚloggerÚwarningr3   )r   ÚcontentÚer   r   r   r.   {   s     
zBaseDistribution.direct_urlc             C   s
   t ƒ ‚d S )N)r   )r   r   r   r   Ú	installer•   s    zBaseDistribution.installerc             C   s
   t | jƒS )N)Úboolr1   )r   r   r   r   Úeditable™   s    zBaseDistribution.editablec             C   s
   t ƒ ‚d S )N)r   )r   r   r   r   Úlocal�   s    zBaseDistribution.localc             C   s
   t ƒ ‚d S )N)r   )r   r   r   r   Úin_usersite¡   s    zBaseDistribution.in_usersitec             C   s
   t ƒ ‚d S )N)r   )r   r   r   r   Úin_site_packages¥   s    z!BaseDistribution.in_site_packages)r   r   c             C   s
   t ƒ ‚dS )zªRead a file in the .dist-info (or .egg-info) directory.

        Should raise ``FileNotFoundError`` if ``name`` does not exist in the
        metadata directory.
        N)r   )r   r   r   r   r   r4   ©   s    zBaseDistribution.read_textc             C   s
   t ƒ ‚d S )N)r   )r   r   r   r   Úiter_entry_points±   s    z"BaseDistribution.iter_entry_pointsc             C   s
   t ƒ ‚dS )z?Metadata of distribution parsed from e.g. METADATA or PKG-INFO.N)r   )r   r   r   r   Úmetadata´   s    zBaseDistribution.metadatac             C   s   | j jdƒS )zDValue of "Metadata-Version:" in distribution metadata, if available.zMetadata-Version)rD   Úget)r   r   r   r   Úmetadata_version¹   s    z!BaseDistribution.metadata_versionc             C   s   | j jd| jƒS )z*Value of "Name:" in distribution metadata.ÚName)rD   rE   r3   )r   r   r   r   r)   ¾   s    zBaseDistribution.raw_namec             C   sf   | j jdƒ}|dkrtƒ S ytt|ƒƒ}W n6 tk
r` } zd}tj|| j|ƒ tƒ S d}~X nX |S )z±Value of "Requires-Python:" in distribution metadata.

        If the key does not exist or contains an invalid value, an empty
        SpecifierSet should be returned.
        zRequires-PythonNz-Package %r has an invalid Requires-Python: %s)rD   rE   r   r%   r   r9   r:   r)   )r   r   Úspecr<   Úmessager   r   r   Úrequires_pythonÅ   s    z BaseDistribution.requires_python)Úextrasr   c             C   s
   t ƒ ‚dS )z®Dependencies of this distribution.

        For modern .dist-info distributions, this is the collection of
        "Requires-Dist:" entries in distribution metadata.
        N)r   )r   rK   r   r   r   Úiter_dependenciesØ   s    z"BaseDistribution.iter_dependenciesc             C   s
   t ƒ ‚dS )z²Extras provided by this distribution.

        For modern .dist-info distributions, this is the collection of
        "Provides-Extra:" entries in distribution metadata.
        N)r   )r   r   r   r   Úiter_provided_extrasà   s    z%BaseDistribution.iter_provided_extrasN)'r!   r"   r#   r%   r,   r-   r$   r	   r+   r1   r2   r   r3   ÚDistributionVersionr*   r   r.   r=   r>   r?   r@   rA   rB   r4   r   r   rC   ÚemailrI   ÚMessagerD   rF   r)   r   rJ   r   r   rL   rM   r   r   r   r   r&   8   sH   r&   c               @   s¢   e Zd ZdZed dœdd„ƒZeeee  d dœdd„ƒZ	eed d	œd
d„Z
ed dœdd„Zed dœdd„Zdedddfeee eeeee dœdd„ZdS )ÚBaseEnvironmentz6An environment containing distributions to introspect.)r   c             C   s
   t ƒ ‚d S )N)r   )Úclsr   r   r   Údefaultì   s    zBaseEnvironment.default)Úpathsr   c             C   s
   t ƒ ‚d S )N)r   )rR   rT   r   r   r   Ú
from_pathsð   s    zBaseEnvironment.from_pathsr&   )r   r   c             C   s
   t ƒ ‚dS )z=Given a requirement name, return the installed distributions.N)r   )r   r   r   r   r   Úget_distributionô   s    z BaseEnvironment.get_distributionc             C   s
   t ƒ ‚dS )a  Iterate through installed distributions.

        This function should be implemented by subclass, but never called
        directly. Use the public ``iter_distribution()`` instead, which
        implements additional logic to make sure the distributions are valid.
        N)r   )r   r   r   r   Ú_iter_distributionsø   s    z#BaseEnvironment._iter_distributionsc             c   sH   xB| j ƒ D ]6}tjd|jtjd�}|s:tjd|j|jƒ q
|V  q
W dS )z(Iterate through installed distributions.z)^([A-Z0-9]|[A-Z0-9][A-Z0-9._-]*[A-Z0-9])$)Úflagsz%Ignoring invalid distribution %s (%s)N)rW   ÚreÚmatchr3   Ú
IGNORECASEr9   r:   r+   )r   ÚdistZproject_name_validr   r   r   Úiter_distributions  s    
z"BaseEnvironment.iter_distributionsTF)Ú
local_onlyÚskipÚinclude_editablesÚeditables_onlyÚ	user_onlyr   c                sb   | j ƒ }|rdd„ |D ƒ}|s,dd„ |D ƒ}|r>dd„ |D ƒ}|rPdd„ |D ƒ}‡ fdd„|D ƒS )a  Return a list of installed distributions.

        :param local_only: If True (default), only return installations
        local to the current virtualenv, if in a virtualenv.
        :param skip: An iterable of canonicalized project names to ignore;
            defaults to ``stdlib_pkgs``.
        :param include_editables: If False, don't report editables.
        :param editables_only: If True, only report editables.
        :param user_only: If True, only report installations in the user
        site directory.
        c             s   s   | ]}|j r|V  qd S )N)r@   )Ú.0Údr   r   r   ú	<genexpr>+  s    z?BaseEnvironment.iter_installed_distributions.<locals>.<genexpr>c             s   s   | ]}|j s|V  qd S )N)r?   )rc   rd   r   r   r   re   -  s    c             s   s   | ]}|j r|V  qd S )N)r?   )rc   rd   r   r   r   re   /  s    c             s   s   | ]}|j r|V  qd S )N)rA   )rc   rd   r   r   r   re   1  s    c             3   s   | ]}|j ˆ kr|V  qd S )N)r3   )rc   rd   )r_   r   r   re   2  s    )r]   )r   r^   r_   r`   ra   rb   Úitr   )r_   r   Úiter_installed_distributions  s    z,BaseEnvironment.iter_installed_distributionsN)r!   r"   r#   Ú__doc__ÚclassmethodrS   r	   r   r%   rU   rV   r   rW   r]   r   r>   r   r&   rg   r   r   r   r   rQ   é   s   	rQ   c               @   s"   e Zd ZU eejdœdd„ZdS )ÚWheel)r   c             C   s
   t ƒ ‚d S )N)r   )r   r   r   r   Ú
as_zipfile8  s    zWheel.as_zipfileN)r!   r"   r#   r%   r+   ÚzipfileÚZipFilerk   r   r   r   r   rj   5  s   
rj   c               @   s,   e Zd Zeddœdd„Zejdœdd„ZdS )ÚFilesystemWheelN)r+   r   c             C   s
   || _ d S )N)r+   )r   r+   r   r   r   Ú__init__=  s    zFilesystemWheel.__init__)r   c             C   s   t j| jdd�S )NT)Ú
allowZip64)rl   rm   r+   )r   r   r   r   rk   @  s    zFilesystemWheel.as_zipfile)r!   r"   r#   r%   ro   rl   rm   rk   r   r   r   r   rn   <  s   rn   c               @   s2   e Zd Zeee ddœdd„Zejdœdd„Z	dS )ÚMemoryWheelN)r+   Ústreamr   c             C   s   || _ || _d S )N)r+   rr   )r   r+   rr   r   r   r   ro   E  s    zMemoryWheel.__init__)r   c             C   s   t j| jdd�S )NT)rp   )rl   rm   rr   )r   r   r   r   rk   I  s    zMemoryWheel.as_zipfile)
r!   r"   r#   r%   r   Úbytesro   rl   rm   rk   r   r   r   r   rq   D  s   rq   )0Úemail.messagerO   r7   ÚloggingrY   rl   Útypingr   r   r   r   r   r   r   r	   r
   Ú"pip._vendor.packaging.requirementsr   Z pip._vendor.packaging.specifiersr   r   Úpip._vendor.packaging.utilsr   Zpip._vendor.packaging.versionr   r   Zpip._internal.models.direct_urlr   r   r   Zpip._internal.utils.compatr   Zpip._internal.utils.egg_linkr   Zpip._internal.utils.urlsr   r   ÚobjectrN   Ú	getLoggerr!   r9   r   r&   rQ   rj   rn   rq   r   r   r   r   Ú<module>   s2   ,
 2L