U
    ee1                  	   @  s  d dl mZ d dlZd dlZd dlZd dlZd dlZd dl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mZ d dlmZ d dlmZmZ d dlZddlmZmZmZ ddlmZmZmZ dd	lmZ d
dddddddZ dd e D Z!ddddZ"e" Z#dWddddddZ$dXdddd d!d"dd#d$d%Z%ddd&d'Z&dYdd d(d)d*d+Z'G d,d- d-eZ(G d.d/ d/eZ)d0dd1d2d3Z*d4d4dd5dd6d7d8Z+dZd4d4dd5d:dd;d<d=Z,d:d4d4dd5d:dd>d?d@Z-ej.dAddBdCZ/d0ddDdEdFZ0d[d4d4d0d5d:d:d0dHdIdJZ1d\d4d4d0d5d:d:d0dHdKdLZ2dMddNdOZ3d]d0d"ddPdQdRZ4dddSdTZ5e6dUkrxe4ej7dd dV dRdOgZ8dS )^    )annotationsN)IteratorSequence)partial)NoReturnTextIO   )ConfigSettingsTypePathTypeProjectBuilder)BuildBackendExceptionBuildExceptionFailedProcessError)DefaultIsolatedEnvz[91mz[92mz[93mz[1mz[2mz[4mz[0m)ZredZgreenZyellowZboldZdimZ	underlineresetc                 C  s   i | ]
}|d qS )  ).0colorr   r   2/tmp/pip-unpacked-wheel-f07x2s22/build/__main__.py
<dictcomp>%   s      r   zdict[str, str])returnc                   C  sB   dt jkr&dt jkr"tjddd tS dt jks:tj r>tS tS )NZNO_COLORZFORCE_COLORzLBoth NO_COLOR and FORCE_COLOR environment variables are set, disabling color   )
stacklevel)	osenvironwarningswarn
_NO_COLORSsysstdoutisatty_COLORSr   r   r   r   _init_colors(   s    

r#   r   strNone)fmtmsgr   c                 C  s   t | j|ftdd d S )NT)flush)printformat_STYLES)r&   r'   r   r   r   _cprint5   s    r,   zWarning | strztype[Warning]intzTextIO | Nonez
str | None)messagecategoryfilenamelinenofileliner   c                 C  s   t dt|  d S )Nz{yellow}WARNING{reset} {})r,   r$   )r.   r/   r0   r1   r2   r3   r   r   r   _showwarning9   s    r4   c                  C  s@   t t_t dkr<zdd l} |   W n tk
r:   Y nX d S )NWindowsr   )r4   r   showwarningplatformsystemcoloramainitModuleNotFoundError)r9   r   r   r   
_setup_cliD   s    r<   r   )r'   coder   c                 C  s   t d|  t|dS )z
    Print an error message and exit. Will color the output when writing to a TTY.

    :param msg: Error message
    :param code: Error code
    z{red}ERROR{reset} {}N)r,   
SystemExit)r'   r=   r   r   r   _errorP   s    
r?   c                   @  s    e Zd ZedddddZdS )_ProjectBuilderr$   r%   r.   r   c                 C  s   t d|  d S Nz{bold}* {}{reset}r,   r.   r   r   r   log\   s    z_ProjectBuilder.logN__name__
__module____qualname__staticmethodrE   r   r   r   r   r@   [   s   r@   c                   @  s    e Zd ZedddddZdS )_DefaultIsolatedEnvr$   r%   rA   c                 C  s   t d|  d S rB   rC   rD   r   r   r   rE   b   s    z_DefaultIsolatedEnv.logNrF   r   r   r   r   rK   a   s   rK   zSequence[str])	dep_chainr   c                 C  s   d dd | D S )Nz -> c                 s  s    | ]}| d d  V  qdS );r   N)	partitionstrip)r   depr   r   r   	<genexpr>h   s     z$_format_dep_chain.<locals>.<genexpr>)join)rL   r   r   r   _format_dep_chaing   s    rS   r
   zConfigSettingsType | None)srcdiroutdirdistributionconfig_settingsr   c              
   C  s^   t  N}t|| }||j ||||p0i  ||||pDi W  5 Q R  S Q R X d S N)rK   r@   Zfrom_isolated_envinstallZbuild_system_requiresZget_requires_for_buildbuild)rT   rU   rV   rW   envbuilderr   r   r   _build_in_isolated_envk   s
    r]   Fbool)rT   rU   rV   rW   skip_dependency_checkr   c                 C  sZ   t | }|sH|||pi }|rHddd |D }t  td|  ||||pVi S )Nr   c                 s  s8   | ]0}|d  t |dd fD ]}|rd| V  qqdS )r   r   Nz
	)rS   )r   depsrP   r   r   r   rQ      s
        z(_build_in_current_env.<locals>.<genexpr>zMissing dependencies:)r@   Zcheck_dependenciesrR   r,   r?   rZ   )rT   rU   rV   rW   r_   r\   missingZdependenciesr   r   r   _build_in_current_envw   s    rb   )	isolationrT   rU   rV   rW   r_   r   c                 C  s&   | rt ||||S t|||||S d S rX   )r]   rb   )rc   rT   rU   rV   rW   r_   r   r   r   _build   s    rd   zIterator[None]c               
   c  s   z
d V  W n t tfk
r< }  ztt|  W 5 d } ~ X Y n tk
r }  zt| jtjrlt	  tt|  | j
rtj| j
d | j
d | j
d dd}d|}n
td}t	d|d tt|  W 5 d } ~ X Y nX d S )	Nr   r   r   )limitr   
{dim}{}{reset}

)r   r   r?   r$   r   
isinstance	exception
subprocessCalledProcessErrorr,   exc_info	tracebackformat_exceptionrR   
format_excrO   )eZtb_linestbr   r   r   _handle_build_error   s&    

rs   )elementsr   c                 C  sP   t | dkrd}t|n2t | dkr.| d S dd| d d | d S d S )Nr   zno elementsr   z	{} and {}z, re   )len
IndexErrorr*   rR   )rt   r'   r   r   r   _natural_language_list   s    
rw   T)rT   rU   distributionsrW   rc   r_   r   c           	      C  s6   g }|D ](}t || ||||}|tj| q|S )a  
    Run the build process.

    :param srcdir: Source directory
    :param outdir: Output directory
    :param distribution: Distribution to build (sdist or wheel)
    :param config_settings: Configuration settings to be passed to the backend
    :param isolation: Isolate the build in a separate environment
    :param skip_dependency_check: Do not perform the dependency check
    )rd   appendr   pathbasename)	rT   rU   rx   rW   rc   r_   builtrV   outr   r   r   build_package   s
    r~   c                 C  s   ddl m} d|kr d}t|t|| |d||}tj|}	tjdd}
g }|r|	|}|
|
 zdtd	t| d
 tj|
|	dtd  } |D ](}t|| ||||}|tj| qW 5 tj|
dd X W 5 Q R X |	f|S )a  
    Build a sdist and then the specified distributions from it.

    :param srcdir: Source directory
    :param outdir: Output directory
    :param distribution: Distribution to build (only wheel)
    :param config_settings: Configuration settings to be passed to the backend
    :param isolation: Isolate the build in a separate environment
    :param skip_dependency_check: Do not perform the dependency check
    r   )TarFilesdistz=Only binary distributions are allowed but sdist was specifiedzbuild-via-sdist-)prefixT)ignore_errorsz	Building z from sdistNz.tar.gz)Z_utilr   
ValueErrorrd   r   rz   r{   tempfilemkdtempopen
extractallshutilrmtreer@   rE   rw   rR   ru   ry   )rT   rU   rx   rW   rc   r_   r   r'   r   Z
sdist_nameZ	sdist_outr|   trV   r}   r   r   r   build_package_via_sdist   s&    
r   zargparse.ArgumentParserc               
   C  s   t jttd dtt jtt	
 jd ddd} | jdtdt d	d
 | jddddtj ddtj dd | jddddd | jddddd | jddtdtj ddd  | jd!d"dd#d | jd$d%dd&d | jd'd(d)d*d+d, | S )-z$
    Construct the main parser.
    a  
                A simple, correct Python build frontend.

                By default, a source distribution (sdist) is built from {srcdir}
                and a binary distribution (wheel) is built from the sdist.
                This is recommended as it will ensure the sdist can be used
                to build wheels.

                Pass -s/--sdist and/or -w/--wheel to build a specific distribution.
                If you do this, the default behavior will be disabled, and all
                artifacts will be built from {srcdir} (even if you combine
                -w/--wheel with -s/--sdist, the wheel will be built from {srcdir}).
                z    r      )width)descriptionformatter_classrT   ?z0source directory (defaults to current directory))typenargsdefaulthelpz	--versionz-Vversionzbuild z (,))actionr   z--sdistz-s
store_truez;build a source distribution (disables the default behavior))r   r   z--wheelz-wz-build a wheel (disables the default behavior)z--outdirz-oz&output directory (defaults to {srcdir}zdist)PATH)r   r   metavarz--skip-dependency-checkz-xz2do not check that build dependencies are installedz--no-isolationz-nzdisable building the project in an isolated virtual environment. Build dependencies must be installed separately when this option is usedz--config-settingz-Cry   zsettings to pass to the backend.  Multiple settings can be provided. Settings beginning with a hyphen will erroneously be interpreted as options to build if separated by a space character; use ``--config-setting=--my-setting -C--my-other-setting``zKEY[=VALUE])r   r   r   )argparseArgumentParsertextwrapindentdedentrO   r   RawDescriptionHelpFormatterminr   get_terminal_sizecolumnsadd_argumentr$   r   getcwdrZ   __version__rR   __path__sep)parserr   r   r   main_parser  s    	r   )cli_argsprogr   c              
   C  st  t   t }|r||_|| }g }i }|jr|jD ]P}|d\}}}	||krZ|	||< q4t|| tsv|| g||< || |	 q4|j	r|d |j
r|d |jdkrtj|jdn|j}
|rt}n
t}dg}zLt < ||j|
|||j |j}tdd |D }td| W 5 Q R X W nH tk
rn } z(t d	}td
| tt| W 5 d}~X Y nX dS )z
    Parse the CLI arguments and invoke the build process.

    :param cli_args: CLI arguments
    :param prog: Program name to show in help text
    =r   wheelNdistc                 S  s   g | ]}d j |ftqS )z!{underline}{}{reset}{bold}{green})r*   r+   )r   Zartifactr   r   r   
<listcomp>  s     zmain.<locals>.<listcomp>z){bold}{green}Successfully built {}{reset}rh   rg   )r<   r   r   
parse_argsZconfig_settingrN   ri   listry   r   r   rU   r   rz   rR   rT   r~   r   rs   Zno_isolationr_   rw   r,   	Exceptionrn   rp   rO   r?   r$   )r   r   r   argsrx   rW   argZsetting_valuerU   Z
build_callr|   Zartifact_listrq   rr   r   r   r   mainZ  sT    




      
r   c                   C  s   t tjdd   d S )Nr   )r   r   argvr   r   r   r   
entrypoint  s    r   __main__zpython -m build)r   r   )NN)r   )F)NTF)NTF)N)9
__future__r   r   
contextlibr   r7   r   rk   r   r   r   rn   r   collections.abcr   r   	functoolsr   typingr   r   rZ   r   r	   r
   r   _exceptionsr   r   r   r[   r   r"   r   r#   r+   r,   r4   r<   r?   r@   rK   rS   r]   rb   rd   contextmanagerrs   rw   r~   r   r   r   r   rG   r   __all__r   r   r   r   <module>   sz   	
	         ,W7
