U
    „~$h.  ã                   @   sV  d 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
 ddlmZ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 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" ddl#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l0m1Z1 ddl2m3Z3 e 4e5¡Z6e 7dej8¡Z9e
ee$ ee$ f 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	e< dœd"d#„ZAe$ee;d$œd%d&„ZBe$e;dd'œd(d)„ZCe$e;e<ee; ee; e<e	e; d*œd+d,„ZDe$e;ee; ee; e<e	e; d-œd.d/„ZEe$ee; e<d0œd1d2„ZFee$ ee<ee; ee; e:d3œd4d5„ZGdS )6z;Orchestrator for building wheels from InstallRequirements.
é    N)ÚIterableÚListÚOptionalÚTuple)Úcanonicalize_nameÚcanonicalize_version)ÚInvalidVersionÚVersion)Ú
WheelCache)ÚInvalidWheelFilenameÚUnsupportedWheel)ÚFilesystemWheelÚget_wheel_distribution)ÚLink)ÚWheel)Úbuild_wheel_pep517)Úbuild_wheel_editable)Úbuild_wheel_legacy)ÚInstallRequirement)Ú
indent_log)Ú
ensure_dirÚ	hash_file)Úmake_setuptools_clean_args)Úcall_subprocess)ÚTempDirectory)Úpath_to_url)Úvcsz([a-z0-9_.]+)-([a-z0-9_.!+-]+))ÚsÚreturnc                 C   s   t t | ¡ƒS )zjDetermine whether the string looks like an egg_info.

    :param s: The string to parse. E.g. foo-2.1
    )ÚboolÚ_egg_info_reÚsearch)r   © r"   ú?/tmp/pip-unpacked-wheel-g27rp152/pip/_internal/wheel_builder.pyÚ_contains_egg_info%   s    r$   )ÚreqÚ
need_wheelr   c                 C   sH   | j r
dS | jr&|r"t d| j¡ dS |r.dS | js8dS | jrD| jS dS )zBReturn whether an InstallRequirement should be built into a wheel.Fz(Skipping %s, due to already being wheel.T)Ú
constraintÚis_wheelÚloggerÚinfoÚnameÚ
source_dirÚeditableZsupports_pyproject_editable)r%   r&   r"   r"   r#   Ú_should_build-   s     þr.   )r%   r   c                 C   s   t | dd�S )NT©r&   ©r.   ©r%   r"   r"   r#   Úshould_build_for_wheel_commandN   s    r2   c                 C   s   t | dd�S )NFr/   r0   r1   r"   r"   r#   Ú should_build_for_install_commandT   s    r3   c                 C   sŠ   | j s| jsdS | jrb| jjrb| j r(t‚| js2t‚t | jj¡}|sHt‚| | jj	| j¡r^dS dS | jslt‚| j 
¡ \}}t|ƒr†dS dS )zÍ
    Return whether a built InstallRequirement can be stored in the persistent
    wheel cache, assuming the wheel cache is available, and _should_build()
    has determined a wheel needs to be built.
    FT)r-   r,   ÚlinkÚis_vcsÚAssertionErrorr   Úget_backend_for_schemeÚschemeÚis_immutable_rev_checkoutÚurlÚsplitextr$   )r%   Úvcs_backendÚbaseÚextr"   r"   r#   Ú_should_cacheZ   s    


r?   )r%   Úwheel_cacher   c                 C   s>   t |jƒ}| jst‚|r.t| ƒr.| | j¡}n| | j¡}|S )zdReturn the persistent or temporary cache directory where the built
    wheel need to be stored.
    )r   Ú	cache_dirr4   r6   r?   Úget_path_for_linkÚget_ephem_path_for_link)r%   r@   Úcache_availablerA   r"   r"   r#   Ú_get_cache_dirz   s    

rE   )r%   Ú
wheel_pathr   c           	      C   sø   t | jp
dƒ}ttj |¡ƒ}t |jƒ|krBtd|›d|j›�ƒ‚tt|ƒ|ƒ}t	|j
ƒ}t|ƒt|j
ƒkr‚td|›d|j
›�ƒ‚|j}|d kr˜tdƒ‚zt|ƒ}W n& tk
rÊ   d|› �}t|ƒ‚Y nX |tdƒkrôt|j
tƒsôtd|›d�ƒ‚d S )	NÚ z)Wheel has unexpected file name: expected z, got zMissing Metadata-VersionzInvalid Metadata-Version: z1.2z+Metadata 1.2 mandates PEP 440 version, but z is not)r   r+   r   ÚosÚpathÚbasenamer   r   r   ÚstrÚversionr   Úmetadata_versionr   r	   r   Ú
isinstance)	r%   rF   Úcanonical_nameÚwÚdistZdist_verstrZmetadata_version_valuerM   Úmsgr"   r"   r#   Ú_verify_oneŠ   s0    ÿ
ÿ

ÿrS   )r%   Ú
output_dirÚverifyÚbuild_optionsÚglobal_optionsr-   r   c           	   
   C   sÐ   |rdnd}zt |ƒ W n: tk
rR } zt d|| j|¡ W Y ¢dS d}~X Y nX | j� t| ||||ƒ}W 5 Q R X |rÌ|rÌzt| |ƒ W n> tt	fk
rÊ } zt d|| j|¡ W Y ¢dS d}~X Y nX |S )zaBuild one wheel.

    :return: The filename of the built wheel, or None if the build failed.
    r-   ÚwheelzBuilding %s for %s failed: %sNzBuilt %s for %s is invalid: %s)
r   ÚOSErrorr)   Úwarningr+   Ú	build_envÚ_build_one_inside_envrS   r   r   )	r%   rT   rU   rV   rW   r-   ZartifactÚerF   r"   r"   r#   Ú
_build_one§   s4    ü    ÿr^   )r%   rT   rV   rW   r-   r   c                 C   s|  t dd���f}| jst‚| jrŽ| js(t‚| js2t‚|rDt d| j¡ |rVt d| j¡ |rtt| j| j| j|j	d�}qªt
| j| j| j|j	d�}nt| j| j| j|||j	d�}|d k	�rRtj	 |¡}tj	 ||¡}zNt|ƒ\}	}
t ||¡ t d| j||
|	 ¡ ¡ t d|¡ |W W  5 Q R £ S  tk
�rP } zt d	| j|¡ W 5 d }~X Y nX | j�sdt| |ƒ W 5 Q R £ d S Q R X d S )
NrX   )Úkindz7Ignoring --global-option when building %s using PEP 517z6Ignoring --build-option when building %s using PEP 517)r+   ÚbackendÚmetadata_directoryÚtempd)r+   Úsetup_py_pathr,   rW   rV   rb   z3Created wheel for %s: filename=%s size=%d sha256=%szStored in directory: %sz Building wheel for %s failed: %s)r   r+   r6   Ú
use_pep517ra   Úpep517_backendr)   rZ   r   rI   r   r   rc   Úunpacked_source_directoryrH   rJ   Újoinr   ÚshutilÚmover*   Ú	hexdigestÚ	ExceptionÚ_clean_one_legacy)r%   rT   rV   rW   r-   Útemp_dirrF   Ú
wheel_nameÚ	dest_pathÚ
wheel_hashÚlengthr]   r"   r"   r#   r\   Í   sx    


 ÿ ÿüüú	
ûý
r\   )r%   rW   r   c                 C   s\   t | j|d�}t d| j¡ zt|d| jd� W dS  tk
rV   t d| j¡ Y dS X d S )N)rW   zRunning setup.py clean for %szpython setup.py clean)Úcommand_descÚcwdTz Failed cleaning build dir for %sF)	r   rc   r)   r*   r+   r   r,   rk   Úerror)r%   rW   Ú
clean_argsr"   r"   r#   rl     s    þ  ÿrl   )Úrequirementsr@   rU   rV   rW   r   c           
   	   C   s  | sg g fS t  dd dd„ | D ƒ¡¡ tƒ �ž g g  }}| D ]ˆ}|jsLt‚t||ƒ}t||||||jol|j	ƒ}	|	r¼|j
dk	rŒ| ||j
¡ tt|	ƒƒ|_|jj|_|jjs°t‚| |¡ q>| |¡ q>W 5 Q R X |ròt  dd dd	„ |D ƒ¡¡ |�rt  d
d dd	„ |D ƒ¡¡ ||fS )z˜Build wheels.

    :return: The list of InstallRequirement that succeeded to build and
        the list of InstallRequirement that failed to build.
    z*Building wheels for collected packages: %sz, c                 s   s   | ]}|j V  qd S )N©r+   ©Ú.0r%   r"   r"   r#   Ú	<genexpr>7  s     zbuild.<locals>.<genexpr>NzSuccessfully built %sú c                 S   s   g | ]
}|j ‘qS r"   rw   rx   r"   r"   r#   Ú
<listcomp>Z  s     zbuild.<locals>.<listcomp>zFailed to build %sc                 S   s   g | ]
}|j ‘qS r"   rw   rx   r"   r"   r#   r|   _  s     )r)   r*   rg   r   r+   r6   rE   r^   r-   Zpermit_editable_wheelsZdownload_infoZrecord_download_originr   r   r4   Ú	file_pathÚlocal_file_pathr(   Úappend)
rv   r@   rU   rV   rW   Úbuild_successesÚbuild_failuresr%   rA   Ú
wheel_filer"   r"   r#   Úbuild%  sL    þ



ú

þþrƒ   )HÚ__doc__ÚloggingÚos.pathrH   Úrerh   Útypingr   r   r   r   Úpip._vendor.packaging.utilsr   r   Zpip._vendor.packaging.versionr   r	   Úpip._internal.cacher
   Úpip._internal.exceptionsr   r   Zpip._internal.metadatar   r   Úpip._internal.models.linkr   Úpip._internal.models.wheelr   Ú$pip._internal.operations.build.wheelr   Z-pip._internal.operations.build.wheel_editabler   Ú+pip._internal.operations.build.wheel_legacyr   Úpip._internal.req.req_installr   Úpip._internal.utils.loggingr   Úpip._internal.utils.miscr   r   Ú$pip._internal.utils.setuptools_buildr   Úpip._internal.utils.subprocessr   Úpip._internal.utils.temp_dirr   Úpip._internal.utils.urlsr   Úpip._internal.vcsr   Ú	getLoggerÚ__name__r)   ÚcompileÚ
IGNORECASEr    ZBuildResultrK   r   r$   r.   r2   r3   r?   rE   rS   r^   r\   rl   rƒ   r"   r"   r"   r#   Ú<module>   sˆ   
	ý"þþþ!ýù'úGú