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 )BuildEnvironmentzContext 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
