U
    „~$hÖm  ã                	   @   sj  d Z ddlZddlZddlZddlmZ ddlmZ ddl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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# ddl$m%Z% ddl&m'Z'm(Z( ddl)m*Z*m+Z+ ddl,m-Z- ddl.m/Z/ ddl0m1Z1 ddl2m3Z3 ddl4m5Z5m6Z6 ddl7m8Z8m9Z9 ddl:m;Z; ddl<m=Z=m>Z>m?Z?m@Z@ ddlAmBZB ddlCmDZD ddlEmFZF e3eGƒZHe1e/eeIeIedœdd„ZJe#eKeLddœd d!„ZMeG d"d#„ d#ƒƒZNd3e#e(eeK ee8 eNd$œd%d&„ZOd4e#eeK ee8 eNd'œd(d)„ZPd5e#eKe(eLeeK ee8 eeN d*œd+d,„ZQd6e#eKee8 eIeeK d.œd/d0„ZRG d1d2„ d2ƒZSdS )7z)Prepares a distribution for installation
é    N)Ú	dataclass)ÚPath)ÚDictÚIterableÚListÚOptional)Úcanonicalize_name)Ú)make_distribution_for_install_requirement)ÚInstalledDistribution)ÚDirectoryUrlHashUnsupportedÚHashMismatchÚHashUnpinnedÚInstallationErrorÚMetadataInconsistentÚNetworkConnectionErrorÚVcsHashUnsupported)ÚPackageFinder)ÚBaseDistributionÚget_metadata_distribution)ÚArchiveInfo)ÚLink)ÚWheel)ÚBatchDownloaderÚ
Downloader)ÚHTTPRangeRequestUnsupportedÚdist_from_wheel_url)Ú
PipSession)ÚBuildTracker)ÚInstallRequirement)Ú	getLogger)Údirect_url_for_editableÚdirect_url_from_link)ÚHashesÚMissingHashes)Ú
indent_log)Údisplay_pathÚ	hash_fileÚhide_urlÚredact_auth_from_requirement)ÚTempDirectory)Úunpack_file)Úvcs)ÚreqÚbuild_trackerÚfinderÚbuild_isolationÚcheck_build_depsÚreturnc              	   C   sD   t | ƒ}|j}|dk	r<| | |¡� | |||¡ W 5 Q R X | ¡ S )z(Prepare a distribution for installation.N)r	   Zbuild_tracker_idÚtrackÚprepare_distribution_metadatar   )r,   r-   r.   r/   r0   Úabstract_distZ
tracker_id© r5   úD/tmp/pip-unpacked-wheel-g27rp152/pip/_internal/operations/prepare.pyÚ_get_prepared_distribution<   s      ÿr7   )ÚlinkÚlocationÚ	verbosityr1   c                 C   s2   t  | j¡}|d k	st‚|j|t| jƒ|d� d S )N)Úurlr:   )r+   Úget_backend_for_schemeÚschemeÚAssertionErrorÚunpackr'   r;   )r8   r9   r:   Úvcs_backendr5   r5   r6   Úunpack_vcs_linkN   s    rA   c                   @   s4   e Zd ZU eed< dZee ed< ddœdd„ZdS )ÚFileÚpathNÚcontent_type)r1   c                 C   s    | j d krt | j¡d | _ d S )Nr   )rD   Ú	mimetypesÚ
guess_typerC   )Úselfr5   r5   r6   Ú__post_init__Y   s    
zFile.__post_init__)Ú__name__Ú
__module__Ú__qualname__ÚstrÚ__annotations__rD   r   rH   r5   r5   r5   r6   rB   T   s   
rB   )r8   ÚdownloadÚdownload_dirÚhashesr1   c                 C   sV   t ddd�}d }|r t| ||ƒ}|r.|}d }n|| |jƒ\}}|rL| |¡ t||ƒS )Nr?   T©ÚkindÚglobally_managed)r)   Ú_check_download_dirrC   Úcheck_against_pathrB   )r8   rN   rO   rP   Útemp_dirÚalready_downloaded_pathÚ	from_pathrD   r5   r5   r6   Úget_http_url^   s    
rY   )r8   rO   rP   r1   c                 C   s<   d}|rt | ||ƒ}|r|}n| j}|r2| |¡ t|dƒS )z'Get file and optionally check its hash.N)rT   Ú	file_pathrU   rB   )r8   rO   rP   rW   rX   r5   r5   r6   Úget_file_urlv   s    
r[   )r8   r9   rN   r:   rO   rP   r1   c                 C   sd   | j rt| ||d� dS |  ¡ r$t‚| jr:t| ||d�}nt| |||d�}| js`t|j	||j
ƒ |S )a_  Unpack link into location, downloading if required.

    :param hashes: A Hashes object, one of whose embedded hashes must match,
        or HashMismatch will be raised. If the Hashes is empty, no matches are
        required, and unhashable types of requirements (like VCS ones, which
        would ordinarily raise HashUnsupported) are allowed.
    )r:   N©rP   )Úis_vcsrA   Úis_existing_dirr>   Úis_filer[   rY   Úis_wheelr*   rC   rD   )r8   r9   rN   r:   rO   rP   Úfiler5   r5   r6   Ú
unpack_urlŽ   s    ü	rb   T)r8   rO   rP   Úwarn_on_hash_mismatchr1   c                 C   st   t j || j¡}t j |¡s dS t d|¡ |rpz| |¡ W n0 tk
rn   |r^t 	d|¡ t  
|¡ Y dS X |S )z�Check download_dir for previously downloaded file with correct hash
    If a correct file is found return its path else None
    NzFile was already downloaded %sz;Previously-downloaded file %s has bad hash. Re-downloading.)ÚosrC   ÚjoinÚfilenameÚexistsÚloggerÚinforU   r   ÚwarningÚunlink)r8   rO   rP   rc   Údownload_pathr5   r5   r6   rT   ¹   s     	þ
rT   c                       s<  e Zd ZdZeee eeeeeee	eeee
eddœ‡ fdd„Zeddœdd„Zeedd	œd
d„Ze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d'ee eddœdd„Zd(eeed	œdd„Zd)ee eddœdd„Zeeed	œdd„Zeddœd d!„Zeedœd"d#„Zeeed$œd%d&„Z‡  ZS )*ÚRequirementPreparerzPrepares a RequirementN)Ú	build_dirrO   Úsrc_dirr/   r0   r-   ÚsessionÚprogress_barr.   Úrequire_hashesÚuse_user_siteÚ
lazy_wheelr:   Úlegacy_resolverr1   c                    s€   t ƒ  ¡  || _|| _|| _|| _t||ƒ| _t||ƒ| _	|	| _
|| _|| _|| _|
| _|| _|| _|| _|| _i | _d| _d S )N)Ú rv   )ÚsuperÚ__init__ro   rn   r-   Ú_sessionr   Ú	_downloadr   Ú_batch_downloadr.   rO   r/   r0   rr   rs   Úuse_lazy_wheelr:   ru   Ú_downloadedÚ_previous_requirement_header)rG   rn   rO   ro   r/   r0   r-   rp   rq   r.   rr   rs   rt   r:   ru   ©Ú	__class__r5   r6   rx   Ú   s$    
zRequirementPreparer.__init__)r,   r1   c              	   C   sÎ   |j jr$|js$d}tt|j jƒƒ}nd}|jr8t|jƒnt|ƒ}|jr~|jr~t	|jtƒr`|j}n
|j 
¡ }|r~|d|› d�7 }||f| jkr¢||f| _t ||¡ |jrÊtƒ � t d|j j¡ W 5 Q R X dS )z3Provide context for the requirement being prepared.zProcessing %szCollecting %sz (from ú)zUsing cached %sN)r8   r_   Úis_wheel_from_cacherL   r%   rZ   r,   r(   Ú
comes_fromÚ
isinstancerX   r~   rh   ri   r$   rf   )rG   r,   ÚmessageZinformationrƒ   r5   r5   r6   Ú_log_preparing_link  s"    

z'RequirementPreparer._log_preparing_link)r,   Úparallel_buildsr1   c                 C   sP   |j jrdS |jdkst‚|j  ¡ r2|j j|_dS |j| jd|d� | ¡  dS )z1Ensure source_dir of a linked InstallRequirement.NT)Z
autodeleter‡   )	r8   r`   Ú
source_dirr>   r^   rZ   Úensure_has_source_dirrn   Zensure_pristine_source_checkout)rG   r,   r‡   r5   r5   r6   Ú_ensure_link_req_src_dir/  s    

ýz,RequirementPreparer._ensure_link_req_src_dirc                 C   sT   | j s|jdd�S |jjr tƒ ‚|j ¡ r0tƒ ‚|jsB|jsBt	ƒ ‚|jdd�pRt
ƒ S )NT)Útrust_internetF)rr   rP   r8   r]   r   r^   r   Ú	is_directÚ	is_pinnedr   r#   ©rG   r,   r5   r5   r6   Ú_get_linked_req_hashesF  s    
z*RequirementPreparer._get_linked_req_hashesc                 C   s>   | j rt d¡ d S | jr(t d¡ d S |  |¡p<|  |j¡S )Nz9Metadata-only fetching is not used in the legacy resolverz?Metadata-only fetching is not used as hash checking is required)ru   rh   Údebugrr   Ú$_fetch_metadata_using_link_data_attrÚ _fetch_metadata_using_lazy_wheelr8   rŽ   r5   r5   r6   Ú_fetch_metadata_onlyf  s    ÿÿÿ
þz(RequirementPreparer._fetch_metadata_onlyc              	   C   sª   |j  ¡ }|dkrdS |jdk	s$t‚t d|j|¡ t|| j| ¡ d�}t	|j
dƒ�}| ¡ }W 5 Q R X t||j j|jjƒ}t|jƒt|jjƒkr¦t|d|jj|jƒ‚|S )zGFetch metadata from the data-dist-info-metadata attribute, if possible.Nz/Obtaining dependency information for %s from %sr\   ÚrbÚName)r8   Úmetadata_linkr,   r>   rh   ÚverboserY   rz   Z	as_hashesÚopenrC   Úreadr   rf   Únamer   Zraw_namer   )rG   r,   r–   Zmetadata_fileÚfZmetadata_contentsÚmetadata_distr5   r5   r6   r‘   y  s:    
ýýý   ÿz8RequirementPreparer._fetch_metadata_using_link_data_attr)r8   r1   c                 C   s”   | j s
dS |js|js&t d|¡ dS t|jƒ}t|jƒ}t 	d||j
¡ |j dd¡d }zt||| jƒW S  tk
rŽ   t d|¡ Y dS X dS )z-Fetch metadata using lazy wheel, if possible.Nz=Lazy wheel is not used as %r does not point to a remote wheelz+Obtaining dependency information from %s %sú#é   r   z"%s does not support range requests)r|   r_   r`   rh   r�   r   rf   r   rš   ri   Úversionr;   Úsplitr   ry   r   )rG   r8   Úwheelrš   r;   r5   r5   r6   r’   ¢  s*    þ

ýz4RequirementPreparer._fetch_metadata_using_lazy_wheelF)Úpartially_downloaded_reqsr‡   r1   c           
      C   sª   t ddd�j}i }|D ]}|js$t‚|||j< q|  | ¡ |¡}|D ]J\}\}}	t d||¡ || }||_|| j	|jj
< |jsD| t|ƒ¡ qD|D ]}|  ||¡ q”dS )z>Download any requirements which were only fetched by metadata.r?   TrQ   zDownloading link %s to %sN)r)   rC   r8   r>   r{   Úkeysrh   r�   Úlocal_file_pathr}   r;   r`   Zneeds_unpacked_archiver   Ú_prepare_linked_requirement)
rG   r¢   r‡   rV   Zlinks_to_fully_downloadr,   Zbatch_downloadr8   ÚfilepathÚ_r5   r5   r6   Ú_complete_partial_requirements¿  s$    
þz2RequirementPreparer._complete_partial_requirementsc              
   C   s¶   |j s
t‚|  |¡ tƒ �’ d}| jdk	rT|j jrT|  |¡}t|j | j||j d�}|dk	rl|| j	|j j
< n(|  |¡}|dk	r”d|_|W  5 Q R £ S |  ||¡W  5 Q R £ S Q R X dS )z3Prepare a requirement to be obtained from req.link.N)rc   T)r8   r>   r†   r$   rO   r`   r�   rT   r‚   r}   r;   r“   Úneeds_more_preparationr¥   )rG   r,   r‡   rZ   rP   rœ   r5   r5   r6   Úprepare_linked_requirementë  s&    


ö
z.RequirementPreparer.prepare_linked_requirement)Úreqsr‡   r1   c                 C   sž   dd„ |D ƒ}|D ]L}| j dk	r|jjr|  |¡}t|j| j |ƒ}|dk	r|| j|jj< d|_qg }|D ]"}|jr~| |¡ qh|  	||¡ qh| j
||d� dS )z,Prepare linked requirements more, if needed.c                 S   s   g | ]}|j r|‘qS r5   )r©   )Ú.0r,   r5   r5   r6   Ú
<listcomp>  s      zHRequirementPreparer.prepare_linked_requirements_more.<locals>.<listcomp>NF)r‡   )rO   r8   r`   r�   rT   r}   r;   r©   Úappendr¥   r¨   )rG   r«   r‡   r,   rP   rZ   r¢   r5   r5   r6   Ú prepare_linked_requirements_more  s"    
þz4RequirementPreparer.prepare_linked_requirements_morec           
   
   C   sÐ  |j s
t‚|j }|  |¡}|rŒ|jrŒ|jd k	s2t‚|js<t‚|jsFt‚t|jjt	ƒrt|jjj
rt| |jjj
¡rtd }nt d¡ |j|_ |j }|  ||¡ | ¡ r¦d }n–|j| jk�rzt||j| j| j| j|ƒ}W n> tk
�r } ztd|› d|› d|› �ƒ‚W 5 d }~X Y nX n(| j|j }|�r0| |¡ t|d d�}|jd k�r¦|j�rTt‚t||jƒ|_t|jjt	ƒ�r¦|jjj
�s¦|�r¦t|jƒd   ¡ }d|› �|jj_!|�r´|j|_"t#|| j$| j%| j&| j'ƒ}	|	S )NzyThe hashes of the source archive found in cache entry don't match, ignoring cached built wheel and re-downloading source.zCould not install requirement z because of HTTP error z	 for URL )rD   r   zsha256=)(r8   r>   r�   r‚   Údownload_infor`   r_   r„   ri   r   rP   Z
has_one_ofrh   rj   Zcached_wheel_source_linkrŠ   r^   r;   r}   rb   rˆ   rz   r:   rO   r   r   rU   rB   Úeditabler!   r&   rC   Ú	hexdigestÚhashr¤   r7   r-   r.   r/   r0   )
rG   r,   r‡   r8   rP   Ú
local_fileÚexcrZ   r³   Údistr5   r5   r6   r¥   /  sz    




ÿþý	ÿúÿ
ÿþýûz/RequirementPreparer._prepare_linked_requirementc                 C   s°   | j d k	st‚|jd k	st‚|j}|js6| ¡ rF|jrF| | j ¡ d S | ¡ r^t d|¡ d S |j	d krld S t
j | j |j¡}t
j |¡s¬t |j	|¡ t|ƒ}t d|¡ d S )NzENot copying link to destination directory since it is a directory: %szSaved %s)rO   r>   r8   r]   r^   r±   Úarchiverh   r�   r¤   rd   rC   re   rf   rg   ÚshutilÚcopyr%   ri   )rG   r,   r8   Údownload_locationrl   r5   r5   r6   Úsave_linked_requirement‹  s&    ý
z+RequirementPreparer.save_linked_requirementc              	   C   s”   |j stdƒ‚t d|¡ tƒ �j | jr8td|› d�ƒ‚| | j¡ | 	¡  |j
sVt‚t|jƒ|_t|| j| j| j| jƒ}| | j¡ W 5 Q R X |S )z Prepare an editable requirement.z-cannot prepare a non-editable req as editablezObtaining %szThe editable requirement zT cannot be installed when requiring hashes, because there is no single file to hash.)r±   r>   rh   ri   r$   rr   r   r‰   ro   Úupdate_editablerˆ   r    Úunpacked_source_directoryr°   r7   r-   r.   r/   r0   Úcheck_if_existsrs   )rG   r,   r¶   r5   r5   r6   Úprepare_editable_requirement¥  s(    
ÿ
ûz0RequirementPreparer.prepare_editable_requirement)r,   Úskip_reasonr1   c              
   C   st   |j stdƒ‚|dk	s&td|j › �ƒ‚t d|||j j¡ tƒ �* | jrRt d¡ t|ƒ 	¡ W  5 Q R £ S Q R X dS )z)Prepare an already-installed requirement.z(req should have been satisfied but isn'tNz?did not get skip reason skipped but req.satisfied_by is set to zRequirement %s: %s (%s)z§Since 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_byr>   rh   ri   rŸ   r$   rr   r�   r
   r   )rG   r,   rÀ   r5   r5   r6   Úprepare_installed_requirementÆ  s     

ÿ   ÿÿz1RequirementPreparer.prepare_installed_requirement)F)F)F)rI   rJ   rK   Ú__doc__rL   r   Úboolr   r   r   Úintrx   r   r†   rŠ   r"   r�   r   r“   r‘   r   r’   r   r¨   rª   r¯   r¥   r»   r¿   rÂ   Ú__classcell__r5   r5   r   r6   rm   ×   s|   ð: þ"ýý+ý  ýü- ÿ þ' ÿ þ þ\ý#ürm   )NN)NN)NN)T)TrÃ   rE   rd   r¸   Zdataclassesr   Úpathlibr   Útypingr   r   r   r   Úpip._vendor.packaging.utilsr   Úpip._internal.distributionsr	   Ú%pip._internal.distributions.installedr
   Úpip._internal.exceptionsr   r   r   r   r   r   r   Ú"pip._internal.index.package_finderr   Zpip._internal.metadatar   r   Zpip._internal.models.direct_urlr   Úpip._internal.models.linkr   Úpip._internal.models.wheelr   Úpip._internal.network.downloadr   r   Z pip._internal.network.lazy_wheelr   r   Úpip._internal.network.sessionr   Z,pip._internal.operations.build.build_trackerr   Úpip._internal.req.req_installr   Zpip._internal.utils._logr   Z&pip._internal.utils.direct_url_helpersr    r!   Úpip._internal.utils.hashesr"   r#   Úpip._internal.utils.loggingr$   Úpip._internal.utils.miscr%   r&   r'   r(   Úpip._internal.utils.temp_dirr)   Úpip._internal.utils.unpackingr*   Úpip._internal.vcsr+   rI   rh   rÄ   r7   rL   rÅ   rA   rB   rY   r[   rb   rT   rm   r5   r5   r5   r6   Ú<module>   s˜   $	ú  üû   ÿ  þ  úù/ üû