U
    «ÃT_á$  ã                   @   sˆ  d Z ddl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 ddl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 ddlmZ ddlmZ ddlmZ e�r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) e e)ge*f Z+e%e"e) e"e) f Z,e -e.¡Z/e 0dej1¡fdd„Z2dd„ Z3dd„ Z4dd„ Z5dd„ Z6dd„ Z7dd„ Z8d d!„ Z9d"d#„ Z:d$d%„ Z;d&d'„ Z<dS )(z;Orchestrator for building wheels from InstallRequirements.
é    N)ÚLink)Úbuild_wheel_pep517)Úbuild_wheel_legacy)Ú
indent_log)Ú
ensure_dirÚ	hash_fileÚis_wheel_installed)Úmake_setuptools_clean_args)Úcall_subprocess)ÚTempDirectory)ÚMYPY_CHECK_RUNNING)Úpath_to_url)Úvcs)ÚAnyÚCallableÚIterableÚListÚOptionalÚPatternÚTuple)Ú
WheelCache)ÚInstallRequirementz([a-z0-9_.]+)-([a-z0-9_.!+-]+)c                 C   s   t | | ¡ƒS )zjDetermine whether the string looks like an egg_info.

    :param s: The string to parse. E.g. foo-2.1
    )ÚboolÚsearch)ÚsÚ_egg_info_re© r   ú?/tmp/pip-unpacked-wheel-8u7dkzi1/pip/_internal/wheel_builder.pyÚ_contains_egg_info&   s    r   c                 C   sl   | j r
dS | jr&|r"t d| j¡ dS |r.dS | js>tƒ s>dS | jsJ| jsNdS || ƒsht d| j¡ dS dS )zBReturn whether an InstallRequirement should be built into a wheel.Fz(Skipping %s, due to already being wheel.TzCSkipping wheel build for %s, due to binaries being disabled for it.)	Ú
constraintÚis_wheelÚloggerÚinfoÚnameÚ
use_pep517r   ÚeditableÚ
source_dir)ÚreqÚ
need_wheelÚcheck_binary_allowedr   r   r   Ú_should_build0   s,     ÿþr*   c                 C   s   t | dtd�S )NT©r(   r)   )r*   Ú_always_true)r'   r   r   r   Úshould_build_for_wheel_commandY   s
      ÿr-   c                 C   s   t | d|d�S )NFr+   )r*   )r'   r)   r   r   r   Ú should_build_for_install_commandb   s
      ÿr.   c                 C   s€   t | td�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 ¡ \}}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.
    )r)   FT)r.   r,   ÚlinkÚis_vcsr%   ÚAssertionErrorr&   r   Úget_backend_for_schemeÚschemeÚis_immutable_rev_checkoutÚurlÚsplitextr   )r'   Úvcs_backendÚbaseÚextr   r   r   Ú_should_cachel   s"    	 ÿ

r:   c                 C   s4   t |jƒ}|r$t| ƒr$| | j¡}n| | j¡}|S )zdReturn the persistent or temporary cache directory where the built
    wheel need to be stored.
    )r   Ú	cache_dirr:   Úget_path_for_linkr/   Úget_ephem_path_for_link)r'   Úwheel_cacheÚcache_availabler;   r   r   r   Ú_get_cache_dir�   s
    
r@   c                 C   s   dS )NTr   )Ú_r   r   r   r,   Ÿ   s    r,   c              
   C   sr   zt |ƒ W n8 tk
rD } zt d| j|¡ W Y ¢dS d}~X Y nX | j� t| |||ƒW  5 Q R £ S Q R X dS )zaBuild one wheel.

    :return: The filename of the built wheel, or None if the build failed.
    ú Building wheel for %s failed: %sN)r   ÚOSErrorr!   Úwarningr#   Ú	build_envÚ_build_one_inside_env)r'   Ú
output_dirÚbuild_optionsÚglobal_optionsÚer   r   r   Ú
_build_one¤   s      þ   ÿrK   c                 C   s  t dd���}| jr0t| j| j| j||jd�}nt| j| j| j	|||jd�}|d k	rðt
j |¡}t
j ||¡}zNt|ƒ\}}	t ||¡ t d| j||	| ¡ ¡ t d|¡ |W W  5 Q R £ S  tk
rî }
 zt d| j|
¡ W 5 d }
~
X Y nX | j�st| |ƒ W 5 Q R £ d S Q R X d S )NÚwheel)Úkind)r#   ÚbackendÚmetadata_directoryrH   Útempd)r#   Úsetup_py_pathr&   rI   rH   rP   z3Created wheel for %s: filename=%s size=%d sha256=%szStored in directory: %srB   )r   r$   r   r#   Úpep517_backendrO   Úpathr   rQ   Úunpacked_source_directoryÚosÚbasenameÚjoinr   ÚshutilÚmover!   r"   Ú	hexdigestÚ	ExceptionrD   Ú_clean_one_legacy)r'   rG   rH   rI   Útemp_dirÚ
wheel_pathÚ
wheel_nameÚ	dest_pathÚ
wheel_hashÚlengthrJ   r   r   r   rF   ¿   sP    ûú	  ý þ
rF   c                 C   sZ   t | j|d�}t d| j¡ zt|| jd� W dS  tk
rT   t d| j¡ Y dS X d S )N)rI   zRunning setup.py clean for %s)ÚcwdTz Failed cleaning build dir for %sF)	r	   rQ   r!   r"   r#   r
   r&   r[   Úerror)r'   rI   Ú
clean_argsr   r   r   r\   ð   s    þr\   c           	   	   C   sì   | sg g fS t  dd dd„ | D ƒ¡¡ tƒ �p g g  }}| D ]Z}t||ƒ}t||||ƒ}|rŽ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>  s     zbuild.<locals>.<genexpr>zSuccessfully built %sú c                 S   s   g | ]
}|j ‘qS r   rf   rg   r   r   r   Ú
<listcomp>)  s     zbuild.<locals>.<listcomp>zFailed to build %sc                 S   s   g | ]
}|j ‘qS r   rf   rg   r   r   r   rk   .  s     )r!   r"   rW   r   r@   rK   r   r   r/   Ú	file_pathÚlocal_file_pathr    r1   Úappend)	Úrequirementsr>   rH   rI   Úbuild_successesÚbuild_failuresr'   r;   Ú
wheel_filer   r   r   Úbuild   sB    þ

   ÿ
þþrs   )=Ú__doc__ÚloggingÚos.pathrU   ÚrerX   Úpip._internal.models.linkr   Ú$pip._internal.operations.build.wheelr   Ú+pip._internal.operations.build.wheel_legacyr   Úpip._internal.utils.loggingr   Úpip._internal.utils.miscr   r   r   Ú$pip._internal.utils.setuptools_buildr	   Úpip._internal.utils.subprocessr
   Úpip._internal.utils.temp_dirr   Úpip._internal.utils.typingr   Úpip._internal.utils.urlsr   Úpip._internal.vcsr   Útypingr   r   r   r   r   r   r   Úpip._internal.cacher   Úpip._internal.req.req_installr   r   ZBinaryAllowedPredicateZBuildResultÚ	getLoggerÚ__name__r!   ÚcompileÚIr   r*   r-   r.   r:   r@   r,   rK   rF   r\   rs   r   r   r   r   Ú<module>   sD   $
ÿ

)	
#1