U
    ªÃT_}%  ã                   @   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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dd	d
ddgZG dd„ deƒZG dd„ dejjƒZdd„ Zdd„ Zdd„ Zdd„ ZG dd„ deƒZG dd„ deƒZeƒ ZejZejZejZejZej Z eƒ Z!dS )a-  A PEP 517 interface to setuptools

Previously, when a user or a command line tool (let's call it a "frontend")
needed to make a request of setuptools to take a certain action, for
example, generating a list of installation requirements, the frontend would
would call "setup.py egg_info" or "setup.py bdist_wheel" on the command line.

PEP 517 defines a different method of interfacing with setuptools. Rather
than calling "setup.py" directly, the frontend should:

  1. Set the current directory to the directory with a setup.py file
  2. Import this module into a safe python interpreter (one in which
     setuptools can potentially set global variables or crash hard).
  3. Call one of the functions defined in PEP 517.

What each function does is defined in PEP 517. However, here is a "casual"
definition of the functions (this definition should not be relied on for
bug reports or API stability):

  - `build_wheel`: build a wheel in the folder and return the basename
  - `get_requires_for_build_wheel`: get the `setup_requires` to build
  - `prepare_metadata_for_build_wheel`: get the `install_requires`
  - `build_sdist`: build an sdist in the folder and return the basename
  - `get_requires_for_build_sdist`: get the `setup_requires` to build

Again, this is not a formal definition! Just a "taste" of the module.
é    N)ÚTemporaryDirectory)Úparse_requirements)ÚmakedirsÚget_requires_for_build_sdistÚget_requires_for_build_wheelÚ prepare_metadata_for_build_wheelÚbuild_wheelÚbuild_sdistÚ
__legacy__ÚSetupRequirementsErrorc                   @   s   e Zd Zdd„ ZdS )r   c                 C   s
   || _ d S ©N)Ú
specifiers)Úselfr   © r   ú9/tmp/pip-unpacked-wheel-sr6cxcdi/setuptools/build_meta.pyÚ__init__4   s    zSetupRequirementsError.__init__N)Ú__name__Ú
__module__Ú__qualname__r   r   r   r   r   r   3   s   c                   @   s&   e Zd Zdd„ Zeejdd„ ƒƒZdS )ÚDistributionc                 C   s   t ttt|ƒƒƒ}t|ƒ‚d S r   )ÚlistÚmapÚstrr   r   )r   r   Zspecifier_listr   r   r   Úfetch_build_eggs9   s    zDistribution.fetch_build_eggsc                 c   s*   t jj}| t j_z
dV  W 5 |t j_X dS )zw
        Replace
        distutils.dist.Distribution with this class
        for the duration of this context.
        N)Ú	distutilsÚcorer   )ÚclsÚorigr   r   r   Úpatch>   s
    
zDistribution.patchN)r   r   r   r   ÚclassmethodÚ
contextlibÚcontextmanagerr   r   r   r   r   r   8   s   r   c                 C   s*   t jd dkr&t| tƒs&|  t  ¡ ¡S | S )z‘
    Convert a filename to a string (on Python 2, explicitly
    a byte string, not Unicode) as distutils checks for the
    exact type str.
    r   é   )ÚsysÚversion_infoÚ
isinstancer   ÚencodeÚgetfilesystemencoding)Úsr   r   r   Ú_to_strN   s    r)   c                    s   ‡ fdd„t  ˆ ¡D ƒS )Nc                    s&   g | ]}t j t j ˆ |¡¡r|‘qS r   )ÚosÚpathÚisdirÚjoin)Ú.0Úname©Za_dirr   r   Ú
<listcomp>\   s    ÿz1_get_immediate_subdirectories.<locals>.<listcomp>©r*   Úlistdirr0   r   r0   r   Ú_get_immediate_subdirectories[   s    r4   c                    s"   ‡ fdd„t  | ¡D ƒ}|\}|S )Nc                 3   s   | ]}|  ˆ ¡r|V  qd S r   ©Úendswith©r.   Úf©Ú	extensionr   r   Ú	<genexpr>a   s   
ÿz'_file_with_extension.<locals>.<genexpr>r2   )Ú	directoryr:   ZmatchingÚfiler   r9   r   Ú_file_with_extension`   s
    
ÿr>   c                 C   s&   t j | ¡st d¡S ttdtƒ| ƒS )Nz%from setuptools import setup; setup()Úopen)r*   r+   ÚexistsÚioÚStringIOÚgetattrÚtokenizer?   ©Úsetup_scriptr   r   r   Ú_open_setup_scripti   s    
rG   c                   @   s`   e Zd Zdd„ Zdd„ Zddd„Zdd	d
„Zddd„Zddd„Zdd„ Z	ddd„Z
ddd„ZdS )Ú_BuildMetaBackendc                 C   s   |pi }|  dg ¡ |S )Nú--global-option)Ú
setdefault©r   Úconfig_settingsr   r   r   Ú_fix_configs   s    z_BuildMetaBackend._fix_configc              
   C   sz   |   |¡}tjd d… dg |d  t_z t ¡ � |  ¡  W 5 Q R X W n, tk
rt } z||j7 }W 5 d }~X Y nX |S )Né   Úegg_inforI   )rM   r#   Úargvr   r   Ú	run_setupr   r   )r   rL   ÚrequirementsÚer   r   r   Ú_get_build_requiresx   s    
ÿ
z%_BuildMetaBackend._get_build_requiresúsetup.pyc              	   C   sD   |}d}t |ƒ�}| ¡  dd¡}W 5 Q R X tt||dƒtƒ ƒ d S )NÚ__main__z\r\nz\nÚexec)rG   ÚreadÚreplacerW   ÚcompileÚlocals)r   rF   Ú__file__r   r8   Úcoder   r   r   rQ   …   s
    
z_BuildMetaBackend.run_setupNc                 C   s   |   |¡}| j|dgd�S )NÚwheel©rR   ©rM   rT   rK   r   r   r   r   �   s    
z._BuildMetaBackend.get_requires_for_build_wheelc                 C   s   |   |¡}| j|g d�S )Nr_   r`   rK   r   r   r   r   ”   s    
z._BuildMetaBackend.get_requires_for_build_sdistc                 C   sÀ   t jd d… ddt|ƒg t _|  ¡  |}dd„ t |¡D ƒ}t|ƒdkrttt|ƒƒdkrttj 	|t |¡d ¡}q*t|ƒdksˆt
‚qˆq*||kr¸t tj 	||d ¡|¡ tj|dd� |d S )	NrN   Z	dist_infoz
--egg-basec                 S   s   g | ]}|  d ¡r|‘qS )z
.dist-infor5   r7   r   r   r   r1       s    
ÿzF_BuildMetaBackend.prepare_metadata_for_build_wheel.<locals>.<listcomp>r   T)Úignore_errors)r#   rP   r)   rQ   r*   r3   Úlenr4   r+   r-   ÚAssertionErrorÚshutilÚmoveÚrmtree)r   Úmetadata_directoryrL   Zdist_info_directoryZ
dist_infosr   r   r   r   ˜   s.    ÿÿ ÿþz2_BuildMetaBackend.prepare_metadata_for_build_wheelc              	   C   s¬   |   |¡}tj |¡}t|dd� t|d��v}tjd d… | d|g |d  t_|  ¡  t	||ƒ}tj 
||¡}tj |¡rˆt |¡ t tj 
||¡|¡ W 5 Q R X |S )NT)Úexist_ok)ÚdirrN   z
--dist-dirrI   )rM   r*   r+   Úabspathr   r   r#   rP   rQ   r>   r-   r@   ÚremoveÚrename)r   Zsetup_commandZresult_extensionZresult_directoryrL   Ztmp_dist_dirZresult_basenameÚresult_pathr   r   r   Ú_build_with_temp_dir·   s     
ÿþ

 z&_BuildMetaBackend._build_with_temp_dirc                 C   s   |   dgd||¡S )NÚbdist_wheelz.whl©rn   )r   Úwheel_directoryrL   rg   r   r   r   r   Î   s    
 ÿz_BuildMetaBackend.build_wheelc                 C   s   |   dddgd||¡S )NÚsdistz	--formatsÚgztarz.tar.gzrp   )r   Úsdist_directoryrL   r   r   r   r	   Ó   s
     þz_BuildMetaBackend.build_sdist)rU   )N)N)N)NN)N)r   r   r   rM   rT   rQ   r   r   r   rn   r   r	   r   r   r   r   rH   q   s   


 ÿ
  ÿ
rH   c                       s"   e Zd ZdZd‡ fdd„	Z‡  ZS )Ú_BuildMetaLegacyBackendaC  Compatibility backend for setuptools

    This is a version of setuptools.build_meta that endeavors to maintain backwards
    compatibility with pre-PEP 517 modes of invocation. It exists as a temporary
    bridge between the old packaging mechanism and the new packaging mechanism,
    and will eventually be removed.
    rU   c              
      sb   t tjƒ}tj tj |¡¡}|tjkr6tj d|¡ ztt| ƒj	|d� W 5 |tjd d …< X d S )Nr   rE   )
r   r#   r+   r*   Údirnamerj   ÚinsertÚsuperru   rQ   )r   rF   Úsys_pathZ
script_dir©Ú	__class__r   r   rQ   á   s    

ÿÿ
z!_BuildMetaLegacyBackend.run_setup)rU   )r   r   r   Ú__doc__rQ   Ú__classcell__r   r   rz   r   ru   Ù   s   ru   )"r|   rA   r*   r#   rD   rd   r    Ú
setuptoolsr   Zsetuptools.py31compatr   Úpkg_resourcesr   Zpkg_resources.py31compatr   Ú__all__ÚBaseExceptionr   Údistr   r)   r4   r>   rG   ÚobjectrH   ru   Z_BACKENDr   r   r   r   r	   r
   r   r   r   r   Ú<module>   sD   ú	h