B
    Uvg7                 @   s  d Z ddlZddlZddlmZmZ ddlmZ ddlm	Z	m
Z
mZmZmZ ddlmZmZmZmZmZ ddlmZ ddlmZ dd	lmZ dd
lmZmZ ddlmZ ee Z!dd Z"G dd de#Z$G dd de$Z%G dd de$Z&G dd de$Z'G dd de#Z(dS )z)Prepares a distribution for installation
    N)pkg_resourcesrequests)BuildEnvironment)
is_dir_urlis_file_url
is_vcs_url
unpack_urlurl_to_path)DirectoryUrlHashUnsupportedHashUnpinnedInstallationErrorPreviousBuildDirErrorVcsHashUnsupported)
expanduser)MissingHashes)
indent_log)display_pathnormalize_path)vcsc             C   s0   | j rt| S | jr$| jjr$t| S t| S dS )zFactory to make an abstract dist object.

    Preconditions: Either an editable req with a source_dir, or satisfied_by or
    a wheel link, or a non-editable req with a source_dir.

    :return: A concrete DistAbstraction.
    N)editableIsSDistlinkis_wheelIsWheel)req r   A/tmp/pip-install-r_9ig3yj/pip/pip/_internal/operations/prepare.pymake_abstract_dist   s
    r   c               @   s(   e Zd ZdZdd Zdd Zdd ZdS )	DistAbstractionay  Abstracts out the wheel vs non-wheel Resolver.resolve() logic.

    The requirements for anything installable are as follows:
     - we must be able to determine the requirement name
       (or we can't correctly handle the non-upgrade case).
     - we must be able to generate a list of run-time dependencies
       without installing any additional packages (or we would
       have to either burn time by doing temporary isolated installs
       or alternatively violate pips 'don't start installing unless
       all requirements are available' rule - neither of which are
       desirable).
     - for packages with setup requirements, we must also be able
       to determine their requirements without installing additional
       packages (for the same reason as run-time dependencies)
     - we must be able to create a Distribution object exposing the
       above metadata.
    c             C   s
   || _ d S )N)r   )selfr   r   r   r   __init__=   s    zDistAbstraction.__init__c             C   s   t | jdS )z Return a setuptools Dist object.N)NotImplementedErrordist)r   finderr   r   r   r"   @   s    zDistAbstraction.distc             C   s   t | jdS )z3Ensure that we can get a Dist for this requirement.N)r!   r"   )r   r#   build_isolationr   r   r   prep_for_distD   s    zDistAbstraction.prep_for_distN)__name__
__module____qualname____doc__r    r"   r%   r   r   r   r   r   *   s   r   c               @   s   e Zd Zdd Zdd ZdS )r   c             C   s   t t| jjd S )Nr   )listr   find_distributionsr   
source_dir)r   r#   r   r   r   r"   K   s    zIsWheel.distc             C   s   d S )Nr   )r   r#   r$   r   r   r   r%   O   s    zIsWheel.prep_for_distN)r&   r'   r(   r"   r%   r   r   r   r   r   I   s   r   c               @   s   e Zd Zdd Zdd ZdS )r   c             C   s,   | j  }|r(|dr(||d |S )Nzdependency_links.txt)r   get_disthas_metadataadd_dependency_linksget_metadata_lines)r   r#   r"   r   r   r   r"   V   s
    
zIsSDist.distc          	   C   s   | j   | j jo|}|rt | j _| j j|| j jd g }| j jr\| j j}| j j|}|rt	
d| j  t	
ddttt| | j   | j   d S )NzInstalling build dependenciesz4Missing build requirements in pyproject.toml for %s.z`The project does not specify a build backend, and pip cannot fall back to setuptools without %s.z and )r   load_pyproject_toml
use_pep517r   	build_envinstall_requirementspyproject_requiresrequirements_to_checkmissing_requirementsloggerwarningjoinmapreprsortedrun_egg_infoassert_source_matches_version)r   r#   r$   should_isolatemissingcheckr   r   r   r%   _   s(    


zIsSDist.prep_for_distN)r&   r'   r(   r"   r%   r   r   r   r   r   T   s   	r   c               @   s   e Zd Zdd Zdd ZdS )	Installedc             C   s   | j jS )N)r   satisfied_by)r   r#   r   r   r   r"      s    zInstalled.distc             C   s   d S )Nr   )r   r#   r$   r   r   r   r%      s    zInstalled.prep_for_distN)r&   r'   r(   r"   r%   r   r   r   r   rC      s   rC   c                   sD   e Zd ZdZ fddZedd Zdd Zdd	 Zd
d Z	  Z
S )RequirementPreparerzPrepares a Requirement
    c                sH   t t|   || _|| _|| _|| _|r2t|}|| _|| _	|| _
d S )N)superrE   r    src_dir	build_dirreq_trackerdownload_dirr   wheel_download_dirprogress_barr$   )r   rH   rJ   rG   rK   rL   r$   rI   )	__class__r   r   r       s    zRequirementPreparer.__init__c             C   sD   | j r@t| j | _ tj| j r$dS td tdt| j  dS )NTz!Could not find download directoryz0Could not find or access download directory '%s'F)	rJ   r   ospathexistsr8   criticalr   r   )r   r   r   r   _download_should_save   s    
z)RequirementPreparer._download_should_savec             C   s  |j r0|j jdkr0t|j j}tdt| ntd| t  || j	 t
jt
j|jdr|td||jf |||| |j st|j }|rt|rt nt|rt|rt |js|jst |j| d}|r|st }y\| j}	d}
|j jr| jr| j}	|j jr0|	r,d}
nd}
t|j |j|	|
||| j d	 W nH t!j"k
r } z&t#d
|| t$d|||j f W dd}~X Y nX t%|}| j&'| |(|| j) W dQ R X | j*r|j jt+j,kr|-| j W dQ R X |S )zCPrepare a requirement that would be obtained from req.link
        filezProcessing %szCollecting %szsetup.pyzpip can't proceed with requirements '%s' due to a pre-existing build directory (%s). This is likely due to a previous installation that failed. pip is being responsible and not assuming it can delete this. Please delete it and try again.)trust_internetTF)sessionhashesrL   z4Could not install requirement %s because of error %szDCould not install requirement %s because of HTTP error %s for URL %sN).r   schemer	   urlr8   infor   r   ensure_has_source_dirrH   rN   rO   rP   r:   r,   r   populate_linkAssertionErrorr   r   r   r   r
   original_link	is_pinnedr   rV   r   rJ   r   rK   r   rL   r   	HTTPErrorrQ   r   r   rI   trackr%   r$   rR   r   all_schemesarchive)r   r   rU   r#   upgrade_allowedrequire_hashesrO   r   rV   rJ   autodelete_unpackedexcabstract_distr   r   r   prepare_linked_requirement   sh    
	

"z.RequirementPreparer.prepare_linked_requirementc          
   C   s   |j stdtd| t z |r2td| || j || j	  t
|}| j| ||| j W dQ R X | j	r|| j || W dQ R X |S )z(Prepare an editable requirement
        z-cannot prepare a non-editable req as editablezObtaining %szoThe editable requirement %s cannot be installed when requiring hashes, because there is no single file to hash.N)r   r\   r8   rY   r   r   rZ   rG   update_editablerR   r   rI   r`   r%   r$   rb   rJ   check_if_exists)r   r   rd   use_user_siter#   rg   r   r   r   prepare_editable_requirement1  s     z0RequirementPreparer.prepare_editable_requirementc          	   C   sf   |j std|dk	s&td|j f td|||j j t  |rPtd t|}W dQ R X |S )z1Prepare an already-installed requirement
        z(req should have been satisfied but isn'tNzAdid not get skip reason skipped but req.satisfied_by is set to %rzRequirement %s: %s (%s)zSince it is already installed, we are trusting this package without checking its hash. To ensure a completely repeatable environment, install into an empty virtualenv.)rD   r\   r8   rY   versionr   debugrC   )r   r   rd   skip_reasonrg   r   r   r   prepare_installed_requirementM  s    
z1RequirementPreparer.prepare_installed_requirement)r&   r'   r(   r)   r    propertyrR   rh   rl   rp   __classcell__r   r   )rM   r   rE      s   wrE   ))r)   loggingrN   pip._vendorr   r   pip._internal.build_envr   pip._internal.downloadr   r   r   r   r	   pip._internal.exceptionsr
   r   r   r   r   pip._internal.utils.compatr   pip._internal.utils.hashesr   pip._internal.utils.loggingr   pip._internal.utils.miscr   r   pip._internal.vcsr   	getLoggerr&   r8   r   objectr   r   r   rC   rE   r   r   r   r   <module>   s$   
.	