U
    P^,                     @   s  d Z ddlZddlZddlmZ ddlmZ ddl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 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rddlmZ ddlm Z  ddl!m"Z" ddl#m$Z$ ddl%m&Z& ddl'm(Z( e)e*Z+dd Z,G dd de-Z.dS )z)Prepares a distribution for installation
    N)requests))make_distribution_for_install_requirement)InstalledDistribution)
unpack_url)DirectoryUrlHashUnsupportedHashUnpinnedInstallationErrorPreviousBuildDirErrorVcsHashUnsupported)
expanduser)MissingHashes)
indent_log)write_delete_marker_file)display_pathnormalize_path)MYPY_CHECK_RUNNING)Optional)AbstractDistribution)PackageFinder)
PipSession)InstallRequirement)RequirementTrackerc              	   C   s.   t | }||  ||| W 5 Q R X |S )z-Prepare a distribution for installation.
    )r   trackZprepare_distribution_metadata)reqreq_trackerfinderbuild_isolationabstract_dist r   A/tmp/pip-install-220asx0h/pip/pip/_internal/operations/prepare.py_get_prepared_distribution,   s    r    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                    sT   t t|   || _|| _|| _|r,t|}|| _|r>t|}|| _	|| _
|| _d S )N)superr!   __init__src_dir	build_dirr   r   download_dirr   wheel_download_dirprogress_barr   )selfr%   r&   r$   r'   r(   r   r   	__class__r   r   r#   9   s    zRequirementPreparer.__init__c                 C   s<   | j s
dS tj| j rdS td tdt| j  d S )NFTz!Could not find download directoryz0Could not find or access download directory '%s')r&   ospathexistsloggercriticalr   r   )r)   r   r   r   _download_should_savea   s    
z)RequirementPreparer._download_should_savec                 C   s  |j s
t|j }|jdkr2|j}tdt| ntd|jp@| t b |	| j
 tjtj|jdrtd||jf |r|jrt n| rt |js|jst |j| d}|r|st }| j}|jr| jr| j}zt||j|||| jd W nF tj k
rL }	 z$t!d||	 t"d	||	|f W 5 d
}	~	X Y nX |jrh|rbd}
nd}
nd}
|
r|t#|j t$|| j%|| j&}| j'r|jr|(| j W 5 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_internet)sessionhashesr(   z4Could not install requirement %s because of error %szDCould not install requirement %s because of HTTP error %s for URL %sNTF))linkAssertionErrorscheme	file_pathr/   infor   r   r   ensure_has_source_dirr%   r,   r-   r.   join
source_dirr	   Zis_vcsr
   Zis_existing_dirr   original_link	is_pinnedr   r5   r   r&   is_wheelr'   r   r(   r   	HTTPErrorr0   r   r   r    r   r   r1   archive)r)   r   r4   r   require_hashesr6   r-   r5   r&   excautodelete_unpackedr   r   r   r   prepare_linked_requiremento   s    



   

   z.RequirementPreparer.prepare_linked_requirementc              	   C   s   |j stdtd| t ^ |r2td| || j || j	  t
|| j|| j}| j	rp|| j || W 5 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.)editabler7   r/   r:   r   r   r;   r$   update_editabler1   r    r   r   rB   r&   check_if_exists)r)   r   rC   use_user_siter   r   r   r   r   prepare_editable_requirement   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 5 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.)satisfied_byr7   r/   r:   versionr   debugr   )r)   r   rC   skip_reasonr   r   r   r   prepare_installed_requirement  s&    	
  z1RequirementPreparer.prepare_installed_requirement)__name__
__module____qualname____doc__r#   propertyr1   rF   rK   rP   __classcell__r   r   r*   r   r!   5   s   (
z"r!   )/rT   loggingr,   pip._vendorr   Zpip._internal.distributionsr   Z%pip._internal.distributions.installedr   pip._internal.downloadr   pip._internal.exceptionsr   r   r   r	   r
   pip._internal.utils.compatr   pip._internal.utils.hashesr   pip._internal.utils.loggingr   Z pip._internal.utils.marker_filesr   pip._internal.utils.miscr   r   pip._internal.utils.typingr   typingr   r   pip._internal.indexr   Zpip._internal.network.sessionr   Zpip._internal.req.req_installr   pip._internal.req.req_trackerr   	getLoggerrQ   r/   r    objectr!   r   r   r   r   <module>   s.   
	