o
    g&C                     @   sx   	 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 )
    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   P/Users/admin/.pyenv/versions/3.10.0/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sQz	t| j| _W d S  ttfyP   tdw d S )NZbuild)r   r   )r   r   zoptimize must be 0, 1, or 2)Zset_undefined_optionsdistributionpackagesr   r   r   itemsr   get_data_files
data_files
isinstancer   int
ValueErrorAssertionErrorZ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 )N.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   r3   r   r   a   s   



zbuild_py.get_data_filesc                    sf   	 | j dg | j |g  }g  |D ]}ttjt|t|}  fdd|D  q S )N c                    s$   g | ]}| vrt j|r|qS r   )r7   r$   isfile)r1   fnfilesr   r   r5      s    

z,build_py.find_data_files.<locals>.<listcomp>)	r   getglobr7   r$   r8   escaper   extend)r   r   r>   ZglobspatternZfilelistr   rD   r   r;   y   s   zbuild_py.find_data_filesc                 C   sb   	 d }| j D ](\}}}}|D ]}tj||}| tj| | jtj|||dd qqd S )NFZpreserve_mode)r   r7   r$   r8   mkpathdirname	copy_file)r   Zlastdirr   r>   r?   r@   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 }|rDz
| jd| }W n ty5   |d|d  |d= Y nw |d| tjj| S |s| jd}|d urT|d| |r\tjj| S dS )Nr.   rA   r   )r9   r   r7   r$   r8   KeyErrorinsertrF   )r   r   r$   tailZpdirr   r   r   r6      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 )NrA   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))	r7   r$   existsZDistutilsFileErrorisdirr8   rB   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)r7   r$   rB   r   rW   )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)rY   rG   r7   r$   r8   rH   abspathr   script_namesplitextbasenamer<   Zdebug_print)	r   r   r   Zmodule_filesmodulesZsetup_scriptr   Zabs_frZ   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y4   | |}d}Y nw |sM| ||}	|df||< |	rM||d|	f tj||d }
| 	||
s]q||||
f q|S )Nr.   r   rQ   r/   __init__.py)
r   r9   r8   rR   r6   rY   r<   r7   r$   r\   )r   r   ra   rZ   r$   r   Zmodule_baser   checkedrX   r[   r   r   r   find_modules   s.   


zbuild_py.find_modulesc                 C   sP   	 g }| j r||   | jr&| jD ]}| |}| ||}|| q|S r0   )r   rI   rf   r   r6   rb   )r   ra   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 )rQ   r   )r1   rZ   r   r   r   r5   -  s    z-build_py.get_source_files.<locals>.<listcomp>)rh   r   r   r   r   get_source_files,  s   zbuild_py.get_source_filesc                 C   s$   |gt | |d g }tjj| S )Nrd   )listr7   r$   r8   )r   r?   r   rZ   Zoutfile_pathr   r   r   get_module_outfile/  s   zbuild_py.get_module_outfiler/   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 )Nr.   rA   )optimizationr   c                 S   s,   g | ]\}}}}|D ]	}t j||q
qS r   )r7   r$   r8   )r1   r   r>   r?   r@   rO   r   r   r   r5   B  s    
z(build_py.get_outputs.<locals>.<listcomp>)rh   r9   rk   r   r<   r	   	importlibutilcache_from_sourcer   r   )r   r'   ra   Zoutputsr   rZ   r[   rO   r   r   r   r,   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 )Nr.   z:'package' must be a string (dot-separated), list, or tupler   rK   )r   strr9   rj   tuple	TypeErrorrk   r   r7   r$   rM   rL   rN   )r   rZ   r[   r   Zoutfiledirr   r   r   build_moduleJ  s   

zbuild_py.build_modulec                 C   s*   |   }|D ]\}}}| ||| qd S r0   )rf   rt   )r   ra   r   rZ   r[   r   r   r   r(   Y  s   zbuild_py.build_modulesc                 C   sD   | j D ]}| |}| ||}|D ]\}}}| ||| qqd S r0   )r   r6   rb   rt   )r   r   r   ra   Zpackage_rZ   r[   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+   rQ   )r   r   prefixdry_run)sysdont_write_bytecoderW   distutils.utilr+   r   r7   sepr	   r   rv   r   )r   rE   r+   ru   r   r   r   r+   v  s    





zbuild_py.byte_compileN)r/   )__name__
__module____qualname__descriptionZuser_optionsZboolean_optionsZnegative_optr   r%   r-   r   r;   r*   r6   rY   r\   rb   rf   rh   ri   rk   r,   rt   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*   Zrun_2to3r+   r,   r   r   r   r   r-     s   zbuild_py_2to3.runc                 C   s,   t | |||}|d r| j|d  |S )Nr/   r   )r   rt   r   r<   )r   rZ   r[   r   resr   r   r   rt     s   zbuild_py_2to3.build_moduleN)r{   r|   r}   r-   rt   r   r   r   r   r     s    r   )r7   importlib.utilrm   rw   rG   Zdistutils.corer   Zdistutils.errorsry   r   r   Z	distutilsr   r   r   r   r   r   r   <module>   s      }