3
Ug                 @   s  d Z ddl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mZ ddlmZmZ ejeZdd	 Zd
d Zdd Zdd Zd"ddZej Zejddd ejddddd ejddddd ejdddd dd  Zed!kr eej  dS )#z%Build a project using PEP 517 hooks.
    N   )BuildEnvironment)Pep517HookCaller)tempdirmkdir_p)FileNotFoundError	toml_loadc             C   s4   ddh}|t | ks0dj|t |  d}t|dS )z7
    Ensure build system has the requisite fields.
    requireszbuild-backendz"Missing required fields: {missing})missingN)setformat
ValueError)systemrequiredmessage r   </tmp/pip-unpacked-wheel-0ht26j5g/pip/_vendor/pep517/build.pyvalidate_system   s
    r   c          	   C   s6   t jj| d}tj|d}t|}W dQ R X |d S )zC
    Load the build system from a source dir (pyproject.toml).
    zpyproject.tomlrbNzbuild-system)ospathjoinioopenr   )
source_dir	pyprojectfZpyproject_datar   r   r   load_system   s    r   c             C   sJ   yt | }W n ttfk
r(   i }Y nX |jdd |jdddg |S )z
    Given a source dir, attempt to get a build system backend
    and requirements from pyproject.toml. Fallback to
    setuptools but only if the file was not found or a build
    system was not indicated.
    zbuild-backendz setuptools.build_meta:__legacy__r	   
setuptoolswheel)r   r   KeyError
setdefault)r   r   r   r   r   compat_system'   s    
r"   c             C   s   dj f t }t| |}|i }tjd| |j| tjd t b}tjd|| dj f t }t| |}	|	|i }
tjj	||
}t
j|tjj	|tjj|
 W d Q R X d S )Nzget_requires_for_build_{dist}zGot build requires: %sz$Installed dynamic build dependencieszTrying to build %s in %szbuild_{dist})r   localsgetattrloginfopip_installr   r   r   r   shutilmovebasename)hooksenvdistdestZget_requires_nameget_requiresreqstdZ
build_namebuildfilenamesourcer   r   r   	_do_build:   s    




r5   c             C   sv   |p
t | }tjj| |pd}t| t| t| |d |jd}t "}|j	|d  t
|||| W d Q R X d S )Nr-   zbuild-backendzbackend-pathr	   )r   r   r   r   r   r   r   getr   r'   r5   )r   r-   r.   r   r+   r,   r   r   r   r2   L   s    r2   r   z%A directory containing pyproject.toml)helpz--binaryz-b
store_trueF)actiondefaultz--sourcez-sz	--out-dirz-oz>Destination in which to save the builds relative to source dirc             C   sb   t jd ttd | js| j r"dnd | js2| j r6dnd f}x|D ]}t| j|| j qFW d S )NzQpep517.build is deprecated. Consider switching to https://pypi.org/project/build/sdistr   )	r%   warninglistfilterr4   binaryr2   r   Zout_dir)argsdistsr-   r   r   r   mainp   s    

rB   __main__)NN)__doc__argparser   loggingr   r(   Zenvbuildr   wrappersr   Zdirtoolsr   r   compatr   r   	getLogger__name__r%   r   r   r"   r5   r2   ArgumentParserparseradd_argumentrB   
parse_argsr   r   r   r   <module>   sB   



