U
    ٲg}%                     @   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-aqb5epr1/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st
qq*||krt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|rt| 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