o
    Š�bc&C  ã                   @   sz   d 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T ddl	m
Z
mZ ddlmZ G dd„ deƒZG d	d
„ d
eeƒZdS )zHdistutils.command.build_py

Implements the Distutils 'build_py' command.é    N)ÚCommand)Ú*)Úconvert_pathÚ	Mixin2to3)Úlogc                   @   sÂ   e Zd ZdZg d¢ZddgZddiZdd„ Zdd	„ Zd
d„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zd d!„ Zd.d#d$„Zd%d&„ Zd'd(„ Zd)d*„ Zd+d,„ Zd-S )/Úbuild_pyz5"build" pure Python modules (copy to build directory)))z
build-lib=Údzdirectory to "build" (copy) to)ÚcompileÚczcompile .py to .pyc)ú
no-compileNz!don't compile .py files [default])z	optimize=ÚOzlalso compile with optimization: -O1 for "python -O", -O2 for "python -OO", and -O0 to disable [default: -O0])ÚforceÚfz2forcibly build everything (ignore file timestamps)r	   r   r   c                 C   s4   d | _ d | _d | _d | _d | _d| _d| _d | _d S )Nr   )Ú	build_libÚ
py_modulesÚpackageÚpackage_dataÚpackage_dirr	   Úoptimizer   ©Úself© r   ú1/usr/lib/python3.10/distutils/command/build_py.pyÚinitialize_options    s   
zbuild_py.initialize_optionsc              	   C   sÄ   |   ddd¡ | jj| _| jj| _| jj| _i | _| jjr/| jj ¡ D ]\}}t|ƒ| j|< q#|  ¡ | _	t
| jtƒs`zt| jƒ| _d| j  krMdksPJ ‚ J ‚W d S  ttfy_   tdƒ‚w d S )NÚbuild)r   r   )r   r   r   é   zoptimize must be 0, 1, or 2)Úset_undefined_optionsÚdistributionÚpackagesr   r   r   Úitemsr   Úget_data_filesÚ
data_filesÚ
isinstancer   ÚintÚ
ValueErrorÚAssertionErrorÚDistutilsOptionError)r   ÚnameÚpathr   r   r   Úfinalize_options*   s(   þ



$ÿüzbuild_py.finalize_optionsc                 C   s:   | j r|  ¡  | jr|  ¡  |  ¡  |  | jdd�¡ d S ©Nr   )Úinclude_bytecode)r   Úbuild_modulesr   Úbuild_packagesÚbuild_package_dataÚbyte_compileÚget_outputsr   r   r   r   ÚrunC   s   zbuild_py.runc                    s‚   g }| j s|S | j D ]4}|  |¡}tjj| jg| d¡ Ž }d‰ |r(t|ƒd ‰ ‡ fdd„|  ||¡D ƒ}| 	||||f¡ q
|S )z?Generate list of '(package,src_dir,build_dir,filenames)' tuplesÚ.r   é   c                    s   g | ]}|ˆ d … ‘qS ©Nr   )Ú.0Úfile©Úplenr   r   Ú
<listcomp>s   s    ÿz+build_py.get_data_files.<locals>.<listcomp>)
r   Úget_package_dirÚosr(   Újoinr   ÚsplitÚlenÚfind_data_filesÚappend)r   Údatar   Úsrc_dirÚ	build_dirÚ	filenamesr   r7   r   r    a   s   



ÿzbuild_py.get_data_filesc                    sd   | j  dg ¡| j  |g ¡ }g ‰ |D ]}t tj t |¡t|ƒ¡¡}ˆ  ‡ fdd„|D ƒ¡ qˆ S )z6Return filenames for package's data files in 'src_dir'Ú c                    s$   g | ]}|ˆ vrt j |¡r|‘qS r   )r;   r(   Úisfile)r5   Úfn©Úfilesr   r   r9   ‚   s    

ÿz,build_py.find_data_files.<locals>.<listcomp>)	r   ÚgetÚglobr;   r(   r<   Úescaper   Úextend)r   r   rB   ÚglobsÚpatternÚfilelistr   rH   r   r?   y   s   ÿzbuild_py.find_data_filesc                 C   s`   d}| j D ](\}}}}|D ]}tj ||¡}|  tj |¡¡ | jtj ||¡|dd� qqdS )z$Copy data files into build directoryNF©Úpreserve_mode)r!   r;   r(   r<   ÚmkpathÚdirnameÚ	copy_file)r   Úlastdirr   rB   rC   rD   ÚfilenameÚtargetr   r   r   r.   †   s   ÿýÿzbuild_py.build_package_datac                 C   sº   |  d¡}| js|rtjj|Ž S dS g }|rCz
| jd |¡ }W n ty4   | d|d ¡ |d= Y nw | d|¡ tjj|Ž S |s| j d¡}|durS| d|¡ |r[tjj|Ž S dS )z½Return the directory, relative to the top of the source
           distribution, where package 'package' should be found
           (at least according to the 'package_dir' option, if any).r2   rE   r   éÿÿÿÿN)r=   r   r;   r(   r<   ÚKeyErrorÚinsertrJ   )r   r   r(   ÚtailÚpdirr   r   r   r:   �   s,   

þøzbuild_py.get_package_dirc                 C   sj   |dkrt j |¡std| ƒ‚t j |¡std| ƒ‚|r3t j |d¡}t j |¡r-|S t d|¡ d S )NrE   z%package directory '%s' does not existz>supposed package directory '%s' exists, but is not a directoryz__init__.pyz8package init file '%s' not found (or not a regular file))	r;   r(   ÚexistsÚDistutilsFileErrorÚisdirr<   rF   r   Úwarn)r   r   r   Úinit_pyr   r   r   Úcheck_package·   s&   ÿÿÿÿzbuild_py.check_packagec                 C   s"   t j |¡st d||¡ dS dS )Nz!file %s (for module %s) not foundFT)r;   r(   rF   r   ra   )r   ÚmoduleÚmodule_filer   r   r   Úcheck_moduleÒ   s   zbuild_py.check_modulec           	      C   s”   |   ||¡ t tj t |¡d¡¡}g }tj | jj¡}|D ](}tj |¡}||kr@tj 	tj 
|¡¡d }| |||f¡ q|  d| ¡ q|S )Nz*.pyr   zexcluding %s)rc   rK   r;   r(   r<   rL   Úabspathr   Úscript_nameÚsplitextÚbasenamer@   Údebug_print)	r   r   r   Úmodule_filesÚmodulesÚsetup_scriptr   Úabs_frd   r   r   r   Úfind_package_modulesÙ   s   zbuild_py.find_package_modulesc              	   C   sÎ   i }g }| j D ]]}| d¡}d |dd… ¡}|d }z|| \}}W n ty3   |  |¡}d}Y nw |sL|  ||¡}	|df||< |	rL| |d|	f¡ tj ||d ¡}
|  	||
¡s\q| |||
f¡ q|S )aÈ  Finds individually-specified Python modules, ie. those listed by
        module name in 'self.py_modules'.  Returns a list of tuples (package,
        module_base, filename): 'package' is a tuple of the path through
        package-space to the module; 'module_base' is the bare (no
        packages, no dots) module name, and 'filename' is the path to the
        ".py" file (relative to the distribution root) that implements the
        module.
        r2   r   rY   r3   Ú__init__ú.py)
r   r=   r<   rZ   r:   rc   r@   r;   r(   rf   )r   r   rm   rd   r(   r   Úmodule_baser   Úcheckedrb   re   r   r   r   Úfind_modulesè   s,   


þzbuild_py.find_modulesc                 C   sN   g }| j r| |  ¡ ¡ | jr%| jD ]}|  |¡}|  ||¡}| |¡ q|S )a4  Compute the list of all modules that will be built, whether
        they are specified one-module-at-a-time ('self.py_modules') or
        by whole packages ('self.packages').  Return a list of tuples
        (package, module, module_file), just like 'find_modules()' and
        'find_package_modules()' do.)r   rM   ru   r   r:   rp   )r   rm   r   r   Úmr   r   r   Úfind_all_modules  s   

zbuild_py.find_all_modulesc                 C   s   dd„ |   ¡ D ƒS )Nc                 S   s   g | ]}|d  ‘qS )rY   r   )r5   rd   r   r   r   r9   -  s    z-build_py.get_source_files.<locals>.<listcomp>)rw   r   r   r   r   Úget_source_files,  s   zbuild_py.get_source_filesc                 C   s$   |gt |ƒ |d g }tjj|Ž S )Nrr   )Úlistr;   r(   r<   )r   rC   r   rd   Úoutfile_pathr   r   r   Úget_module_outfile/  s   zbuild_py.get_module_outfiler3   c                 C   sš   |   ¡ }g }|D ]8\}}}| d¡}|  | j||¡}| |¡ |r@| jr/| tjj|dd�¡ | j	dkr@| tjj|| j	d�¡ q|dd„ | j
D ƒ7 }|S )Nr2   rE   )Úoptimizationr   c                 S   s,   g | ]\}}}}|D ]	}t j ||¡‘q
qS r   )r;   r(   r<   )r5   r   rB   rC   rD   rW   r   r   r   r9   B  s    
ýþÿz(build_py.get_outputs.<locals>.<listcomp>)rw   r=   r{   r   r@   r	   Ú	importlibÚutilÚcache_from_sourcer   r!   )r   r+   rm   Úoutputsr   rd   re   rW   r   r   r   r0   3  s(   


ÿ

ÿ€þzbuild_py.get_outputsc                 C   sb   t |tƒr| d¡}nt |ttfƒstdƒ‚|  | j||¡}tj	 
|¡}|  |¡ | j||dd�S )Nr2   z:'package' must be a string (dot-separated), list, or tupler   rQ   )r"   Ústrr=   ry   ÚtupleÚ	TypeErrorr{   r   r;   r(   rT   rS   rU   )r   rd   re   r   ÚoutfileÚdirr   r   r   Úbuild_moduleJ  s   
ÿ
zbuild_py.build_modulec                 C   s*   |   ¡ }|D ]\}}}|  |||¡ qd S r4   )ru   r†   )r   rm   r   rd   re   r   r   r   r,   Y  s   ûzbuild_py.build_modulesc                 C   sP   | j D ]"}|  |¡}|  ||¡}|D ]\}}}||ksJ ‚|  |||¡ qqd S r4   )r   r:   rp   r†   )r   r   r   rm   Úpackage_rd   re   r   r   r   r-   b  s   


þñzbuild_py.build_packagesc                 C   s„   t jr
|  d¡ d S ddlm} | j}|d tjkr|tj }| jr-||d| j	|| j
d� | jdkr@||| j| j	|| j
d� d S d S )Nz%byte-compiling is disabled, skipping.r   )r/   rY   )r   r   ÚprefixÚdry_run)ÚsysÚdont_write_bytecodera   Údistutils.utilr/   r   r;   Úsepr	   r   r‰   r   )r   rI   r/   rˆ   r   r   r   r/   v  s    


ÿ


ÿÿzbuild_py.byte_compileN)r3   )Ú__name__Ú
__module__Ú__qualname__ÚdescriptionÚuser_optionsÚboolean_optionsÚnegative_optr   r)   r1   r    r?   r.   r:   rc   rf   rp   ru   rw   rx   r{   r0   r†   r,   r-   r/   r   r   r   r   r      s0    


'4
	r   c                   @   s   e Zd Zdd„ Zdd„ ZdS )Úbuild_py_2to3c                 C   sL   g | _ | jr
|  ¡  | jr|  ¡  |  ¡  |  | j ¡ |  | jdd�¡ d S r*   )	Úupdated_filesr   r,   r   r-   r.   Úrun_2to3r/   r0   r   r   r   r   r1   ‹  s   zbuild_py_2to3.runc                 C   s,   t  | |||¡}|d r| j |d ¡ |S )Nr3   r   )r   r†   r–   r@   )r   rd   re   r   Úresr   r   r   r†   ›  s   zbuild_py_2to3.build_moduleN)rŽ   r�   r�   r1   r†   r   r   r   r   r•   Š  s    r•   )Ú__doc__r;   Úimportlib.utilr}   rŠ   rK   Údistutils.corer   Údistutils.errorsrŒ   r   r   Ú	distutilsr   r   r•   r   r   r   r   Ú<module>   s      }