U
    ›À*e9  ã                   @   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mZ ddl	m
Z
 ddlmZ ddlmZ ddlmZ dd	lmZ dd
lmZmZ ddlmZ ddlmZmZ G dd„ deƒZdS )é    )ÚabstractmethodNé   )ÚAbstractBuilder)ÚBuildableFromSources)Úset_globals)Úwrite_metadata)ÚUserException)ÚInstallable)Ú
copy_sip_hÚcopy_sip_pyi)ÚOLDEST_SUPPORTED_MINOR)ÚSIP_VERSIONÚSIP_VERSION_STRc                   @   sv   e Zd ZdZdd„ Zeddœdd„ƒZedd	œd
d„ƒZdd„ Zdd„ Z	dd„ Z
edd	œdd„ƒZdd„ Zdd„ ZdS )ÚBuilderz7 The default base implementation of a project builder. c                 C   s*   |   ¡  |  ¡  | jjr&|  | jj¡ dS )z Build the project in-situ. N)Ú_generate_bindingsÚ_generate_scriptsÚprojectÚcompileÚbuild_projectÚ
target_dir)Úself© r   ú/lib/python3.8/site-packages/sipbuild/builder.pyÚbuild-   s    zBuilder.buildT)Úfatalc                C   s   dS )z{ Build an executable from a BuildableExecutable object and return
        the relative pathname of the executable.
        Nr   )r   Ú	buildabler   r   r   r   Úbuild_executable6   s    zBuilder.build_executableN©Ú	wheel_tagc                C   s   dS )z Build the project. Nr   ©r   r   r   r   r   r   r   <   s    zBuilder.build_projectc              	   C   s   | j }d |j dd¡|j¡}tj |j|¡}t 	|¡ g }|j
D ] }| t tj |j|¡¡¡ qBt |j¡D ]¢\}}}	||jkr|dd…= qpz| d¡ W n tk
r²   Y nX |	D ]X}
tj ||
¡}||krÔq¸tj |tj ||j¡¡}tjtj |¡dd t ||¡ q¸qpt|j| ¡ tj |d¡|jƒ |d	 }tj tj ||¡¡}t ¡ }t |j¡ d
dl}|j|d|jd}| |¡ |  ¡  t |¡ |S )zW Build an sdist for the project and return the name of the sdist
        file.
        ú{}-{}ú-Ú_NÚ__pycache__T)Úexist_okzPKG-INFOz.tar.gzr   zw:gz)Úformat)!r   r%   ÚnameÚreplaceÚversion_strÚosÚpathÚjoinÚ	build_dirÚmkdirZsdist_excludesÚextendÚglobÚroot_dirÚwalkÚremoveÚ
ValueErrorÚrelpathÚmakedirsÚdirnameÚshutilZcopy2r   ZmetadataZget_requires_distsÚabspathÚgetcwdÚchdirÚtarfileÚopenZ
PAX_FORMATÚaddÚclose)r   Zsdist_directoryr   Z
sdist_nameZ
sdist_rootZexcludedÚpattZdnameZdirnamesÚ	filenamesZs_fnZ	s_fn_pathZ	d_fn_pathZ
sdist_fileZ
sdist_pathÚ	saved_cwdr;   Ztfr   r   r   Úbuild_sdist@   sP    ÿ



ÿ ÿ

zBuilder.build_sdistc              	   C   sp  | j }tj |jd¡}t |¡ |  ¡  d}|jD ]}t|t	ƒr2|j
s2d} qPq2g }|rv| dttƒ ¡ | d¡ nhd tjd? d@ tjd	? d@ ¡}| d
 |¡¡ z| d| tj ¡ W n tk
rÜ   | d¡ Y nX | | ¡ ¡ d |¡}t|jƒD ]p\}\}	}
tj |	¡s*tj |j|	¡}	t |	¡}|r t|rLd |¡nd|
d}|j |¡ |j |¡ q | j||d | j||d d |j  dd¡|j!¡}|j"r¾|d |j"¡7 }|d |¡7 }tj #tj ||¡¡}t $¡ }t %|¡ ddl&m'}m(} ||d|dJ}t )d¡D ]8\}}}|D ]&}tj *tj ||¡¡}| +|¡ q,qW 5 Q R X t %|¡ |S )zV Build a wheel for the project and return the name of the wheel
        file.
        ZwheelTFZcp3Zabi3z{}{}é   éÿ   é   zcp{}ZcpZnoner!   zwheel_includes_{}Úwheel_includes©Útarget_subdirr   r    r"   z-{}z-{}.whlr   )ÚZipFileÚZIP_DEFLATEDÚw)ZcompressionÚ.),r   r)   r*   r+   r,   r-   r   Ú
buildablesÚ
isinstancer   Zuses_limited_apiÚappendÚstrr   r%   ÚsysÚ
hexversionÚabiflagsÚAttributeErrorZget_platform_tagÚ	enumeraterF   Úisabsr0   r/   r	   Úfilesr.   Úinstallablesr   Úinstall_projectr&   r'   r(   Z	build_tagr8   r9   r:   ZzipfilerI   rJ   r1   r4   Úwrite)r   Zwheel_directoryr   Zwheel_build_dirZall_use_limited_apir   r   Zmajor_minorZnrr?   rH   rF   ÚinstallableZ
wheel_fileZ
wheel_pathrA   rI   rJ   ZzfÚdirpathr"   r@   Úfilenamer&   r   r   r   Úbuild_wheel‚   sl    


ÿ

þÿ

zBuilder.build_wheelc                 C   s0   | j j}|  ¡  |  ¡  |  |¡ |  |¡ dS )z Install the project. N)r   r   r   r   r   rY   )r   r   r   r   r   ÚinstallÞ   s
    
zBuilder.installc                C   s   dS )z. Install the project into a target directory. Nr   r   r   r   r   rY   è   s    zBuilder.install_projectc              
   C   sÂ  | j }|j d¡\}}t|jƒ}|jr|| |j¡ tj	 
|jd¡}| |¡ | tj	 
|j| ¡ ¡¡ t||j|j|jd tt|jrŠtndt|ƒt|ƒ|jtƒ g }|j ¡ D ]j}| d |j¡¡ ||_| ¡ }|`|js| tj	 
|j|jd ¡¡ |jr| |¡ |j |¡ q°|j r&| !¡ }	tj	 
|jd¡}
| "|
¡}| #| $¡ ¡ | %¡  t&d|	d	}|j' |
¡ |j( |¡ |j ¡ D ]š}|j)rŠt*||jƒ t&d
|	d	}|j' tj	 
|jd¡¡ |j( |¡ tj	 
|jd¡}t+|dƒ}W 5 Q R X t&d|	d	}|j' |¡ |j( |¡  q&qŠ|j,r¾|jd }| d |¡¡ tj	 
|j|¡}| "|¡}|D ](}t+|ƒ}| #| -¡ ¡ W 5 Q R X qf| %¡  t&d|j,d	}|j' |¡ |j( |¡ dS )z0 Generate the bindings for all enabled modules. rL   Úbindings)Úversion_infoNzGenerating the {0} bindingsz.apiz__init__.pyZinitrG   Zsip_pyizsip.pyizpy.typedrK   Zpy_typedzGenerating the {0} fileZapi).r   Zabi_versionÚsplitÚlistÚsip_include_dirsZ
sip_modulerO   Zsip_files_dirr)   r*   r+   r,   r   Zget_bindings_dirr
   ra   r   r   r   Úintr   r`   ÚvaluesÚprogressr%   r&   Z_sip_include_dirsZgenerateZinternalÚtargetZwrite_configurationrM   Zdunder_initZget_package_dirÚopen_for_writingrZ   Zget_dunder_initr>   r	   rW   rX   Z
pep484_pyir   r<   Zapi_dirÚread)r   r   Zabi_major_versionZabi_minor_versionrd   Zlocal_bindings_dirZ	api_filesr`   r   Zpackage_dirZ	init_pathZinit_fr[   Zpy_typed_pathr"   Zapi_fnZapi_pathZapi_fZpart_fnZpart_fr   r   r   r   ì   s     


ÿÿ ÿ  ý
ÿ
ÿÿ

ÿÿ
ÿ



zBuilder._generate_bindingsc           
      C   sF  | j }|j|j }|sdS td|jd}|D ]}| dd¡ d¡}t|ƒdkr^td 	|¡ƒ‚|\}}| d	¡d
 }|j
dkr†|d7 }| d 	|¡¡ tj |j|¡}|j |¡ | |¡}	|j
dkrÜ|	 d 	tj|¡¡ n|	 d¡ |	 dtj|f ¡ |	 ¡  t |tjtjB tjB tjB tjB tjB tjB ¡ q,|j  |¡ dS )z, Generate the scripts for any entry points. NÚscriptsrG   ú Ú ú=é   z('{0}' is an invalid script specificationú:r   Zwin32z.batzGenerating the {} scriptz%@{} -m {} %1 %2 %3 %4 %5 %6 %7 %8 %9
z
#!/bin/sh
zexec %s -m %s ${1+"$@"}
)!r   Zconsole_scriptsZgui_scriptsr	   Zscripts_dirr'   rb   Úlenr   r%   Zpy_platformrg   r)   r*   r+   r,   rW   rO   ri   rZ   rQ   Ú
executabler>   ÚchmodÚstatÚS_IRUSRÚS_IWUSRÚS_IXUSRÚS_IRGRPÚS_IXGRPÚS_IROTHÚS_IXOTHrX   )
r   r   rk   r[   ZepZep_partsZscriptÚmoduleZscript_pathZscript_fr   r   r   r   `  sZ    
ÿ


 ÿÿ
ÿÿÿÿþþÿzBuilder._generate_scripts)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   rB   r^   r_   rY   r   r   r   r   r   r   r   *   s   	B\
tr   )Úabcr   r/   r)   r7   rt   rQ   Zabstract_builderr   r   r   Zcode_generatorr   Zdistinfor   Ú
exceptionsr   r[   r	   r|   r
   r   Zpy_versionsr   Úversionr   r   r   r   r   r   r   Ú<module>   s   