B
    ãUvg(  ã               @   sš   d Z ddlZddlZddlmZ ddlZddlmZ ddl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dd„Zddd„ZdS )zIBuild wheels/sdists by installing build deps to a temporary environment.
é    N)Úpytoml)Ú
check_call)Ú	get_paths)Úmkdtempé   )ÚPep517HookCallerc          	   C   s@   t tj | d¡ƒ�}t |¡}W d Q R X |d }|d |d fS )Nzpyproject.tomlzbuild-systemÚrequireszbuild-backend)ÚopenÚosÚpathÚjoinr   Úload)Ú
source_dirÚfZpyproject_dataZbuildsys© r   ú</tmp/pip-install-r_9ig3yj/pip/pip/_vendor/pep517/envbuild.pyÚ_load_pyproject   s    r   c               @   s6   e Zd ZdZdZddd„Zdd„ Zdd	„ Zd
d„ ZdS )ÚBuildEnvironmentz‡Context manager to install build deps in a simple temporary environment

    Based on code I wrote for pip, which is MIT licensed.
    NTc             C   s
   || _ d S )N)Ú_cleanup)ÚselfÚcleanupr   r   r   Ú__init__4   s    zBuildEnvironment.__init__c             C   sú   t dd�| _t d| j¡ tj dd ¡| _tj dd ¡| _tj	dkrHdnd}t
|| j| jdœd	�}|d
 }| jrˆ|tj | j tjd< n|tj tj tjd< |d |d kr¸|d }n|d tj |d  }| jrì|tj | j tjd< n
|tjd< | S )Nzpep517-build-env-)ÚprefixzTemporary build environment: %sÚPATHÚ
PYTHONPATHÚntÚposix_prefix)ÚbaseÚplatbase)ÚvarsÚscriptsÚpurelibÚplatlib)r   r   ÚlogÚinfor
   ÚenvironÚgetÚ	save_pathÚsave_pythonpathÚnamer   ÚpathsepÚdefpath)r   Úinstall_schemeÚinstall_dirsr    Úlib_dirsr   r   r   Ú	__enter__7   s*    


zBuildEnvironment.__enter__c             C   s:   |sdS t  d|¡ ttjddddd| jgt|ƒ ƒ dS )zAInstall dependencies into this env by calling pip in a subprocessNzCalling pip to install %sz-mÚpipÚinstallz--ignore-installedz--prefix)r#   r$   r   ÚsysÚ
executabler   Úlist)r   Úreqsr   r   r   Úpip_installW   s
    zBuildEnvironment.pip_installc             C   sz   | j r*| jd k	r*tj | j¡r*t | j¡ | jd krDtj dd ¡ n| jtjd< | j	d krjtj dd ¡ n| j	tjd< d S )Nr   r   )
r   r   r
   ÚisdirÚshutilÚrmtreer'   r%   Úpopr(   )r   Úexc_typeÚexc_valÚexc_tbr   r   r   Ú__exit___   s    

zBuildEnvironment.__exit__)T)	Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r/   r6   r>   r   r   r   r   r      s   
 r   c          	   C   s^   |dkri }t | ƒ\}}t| |ƒ}tƒ �,}| |¡ | |¡}| |¡ | ||¡S Q R X dS )am  Build a wheel from a source directory using PEP 517 hooks.

    :param str source_dir: Source directory containing pyproject.toml
    :param str wheel_dir: Target directory to create wheel in
    :param dict config_settings: Options to pass to build backend

    This is a blocking function which will run pip in a subprocess to install
    build requirements.
    N)r   r   r   r6   Úget_requires_for_build_wheelÚbuild_wheel)r   Ú	wheel_dirÚconfig_settingsr   ÚbackendÚhooksÚenvr5   r   r   r   rD   m   s    




rD   c          	   C   s^   |dkri }t | ƒ\}}t| |ƒ}tƒ �,}| |¡ | |¡}| |¡ | ||¡S Q R X dS )am  Build an sdist from a source directory using PEP 517 hooks.

    :param str source_dir: Source directory containing pyproject.toml
    :param str sdist_dir: Target directory to place sdist in
    :param dict config_settings: Options to pass to build backend

    This is a blocking function which will run pip in a subprocess to install
    build requirements.
    N)r   r   r   r6   Úget_requires_for_build_sdistÚbuild_sdist)r   Z	sdist_dirrF   r   rG   rH   rI   r5   r   r   r   rK   ƒ   s    




rK   )N)N)rB   r
   ÚloggingÚpip._vendorr   r8   Ú
subprocessr   r2   Ú	sysconfigr   Útempfiler   Úwrappersr   Ú	getLoggerr?   r#   r   Úobjectr   rD   rK   r   r   r   r   Ú<module>   s   
U
