o
    Ni4                     @   s  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mZ ddlmZ ddlmZ zd dlZW n eyE   dZY nw d	d
 Zdd ZdefddZe	dd Zdd Zdd Zd!ddZdd Zdd Zdd Zd"ddZed kre  dS dS )#    N)defaultdict)contextmanager   )	cythonizeextended_iglob   )is_package_dir)Optionsc                 C   sD   t j| \}}t|rt j|\}}d||f }t|s||fS )Nz%s/%s)ospathsplitr   )r   base_dirpackage_pathparent r   J/home/ubuntu/.local/lib/python3.10/site-packages/Cython/Build/Cythonize.pyfind_package_base   s   r   c                 C   s4   t tjjt| }t||}tt| |j	 d S N)
mapr
   r   abspathr   _cython_compile_files_buildlistitemsparallel)path_patternoptions	all_pathsext_modules_by_basedirr   r   r   cython_compile   s   
r   returnc                 C   s   t t}| D ]_}|jr(|}tj|rt|r'tj|}tj|rt|snd }tj|r:tj|ddg}n|g}t	|f|j
|j|j|j|j|j|j|j|jd	|j}|re|jre|| | qt|S )Nz**z
*.{py,pyx})	nthreadsexclude_failuresexcludecompiler_directivescompile_time_envforcequietdepfilelanguage)r   r   build_inplacer
   r   isdirr   dirnamejoinr   r   
keep_goingexcludes
directivesr%   r&   r'   r(   r)   r   buildextenddict)r   r   ext_modules_to_buildr   r   pathsext_modulesr   r   r   r   !   s>   
r   c              	   c   s\    | !}z|V  W n t y   |  |jdd  w W d    d S 1 s'w   Y  d S )NT)cancel_futures)KeyboardInterruptterminate_workersshutdown)pool_cm	proc_poolr   r   r   _interruptible_poolF   s   
"r=   c                    s  t dd | D }|sd S |dkp|d uo|dk }z|rd ntjj|d}W n ttfy3   d}Y nw |rA| D ]}t| q8d S t|t  fdd| D }tjj|tjj	d	 g }|D ]}z|
  W q] ty| }	 z||	 W Y d }	~	q]d }	~	ww |rd
}
|
ddgtt|R 7 }
|
d7 }
dd |D }tjd d dkr|rt|
|t|
|d W d    d S 1 sw   Y  d S )Nc                 s   s    | ]	\}}t |V  qd S r   )len).0_modulesr   r   r   	<genexpr>R   s    z_build.<locals>.<genexpr>r   r   )max_workersTc                    s,   g | ]\}}|D ]}  t||gfqqS r   )submitrun_distutils)r?   r   rA   extr<   r   r   
<listcomp>i   s    z_build.<locals>.<listcomp>)return_whenz4Compiling Cython modules failed with these errors:

z
	*  z

c                 S   s   g | ]	}t |tr|qS r   )
isinstance	Exception)r?   excr   r   r   rH   }   s    )      r   )sum
concurrentfuturesProcessPoolExecutorOSErrorImportErrorrE   r=   waitFIRST_EXCEPTIONresultBaseExceptionappendr-   r   strsysversion_infoExceptionGroupRuntimeError)r6   r   modcountserial_execution_moder;   rF   compiler_tasksworker_exceptionstaskproc_errexc_msgnon_base_exceptionsr   rG   r   r   Q   sT   



"r   c                 C   s
  zddl m} W n ty%   zddlm} W n ty"   tdw Y nw | \}}ddg}t }d }z8|rIt| tj|d}|	d|g |d||d	 W |rgt| |ritj
|rkt| d S d S d S d S |rt| |rtj
|rt| w w w w )
Nr   )setupzL'distutils' is not available. Please install 'setuptools' for binary builds.	build_ext-i)dirz--build-tempzsetup.py)script_namescript_argsr6   )distutils.corerh   rU   
setuptoolsr
   getcwdchdirtempfilemkdtempr2   r   r+   shutilrmtree)argsrh   r   r6   rm   cwdtemp_dirr   r   r   rE      sH   


rE   c              	      s   ddl m} || |||dd\}}ddddd	}d
d t| D   fdd}|  t|d@ dks7J |d |t|d  |d }	}
}t| dt| d||	 d||
 d ||	d krvtd|| d||	 d d S d S )Nr   )cymeit	   )repeatg&.>gư>gMbP?g      ?)nsecusecmsecsecc                 S   s   g | ]\}}||fqS r   r   )r?   unitscaler   r   r   rH          zbenchmark.<locals>.<listcomp>c                    s6    D ]
\}}| |kr nqt d| | dd| S )Nz"Timing is below nanoseconds: {t:f}z.3f )r_   )tr   r   scalesr   r   format_time   s   zbenchmark.<locals>.format_timer   r   z loops, best of z: z per loop (median: )   z3The timings are likely unreliable. The worst time (z6) was more than four times slower than the best time (z).)Cython.Build.Inlinery   reversedr   sortr>   print)code
setup_codeimport_moduler0   ry   timingsnumberunitsr   fastestmedianslowestr   r   r   	benchmark   s$   $,r   c               
   C   s  ddl m} m} ddlm}m}m} | |dd}|jddd	d
i t|dd |jddd	di t|dd |jddddi t|dd |jddddd dd |jdddddd |jddddd d |jd!d"d#dd$d d%d |jd&d'dd(d)d*d+ |jd,dd-d)d.d+ |jd/d0d1d2d3g d4d5 |jd6d7d8d9d d:d; |jd<d=d>d9d d?d; |jd@dAdBtd dCdD |jdEdFdBtd dGdD |jdHdIdJdKt	d dLdD |jdMdNdOd9d dPd; |jdQdRdSd9d dTd; |jdUdVd9d dWd; |jdXdYdZd9d d[d; |jd\d]d9d d^d; |jd_d`d9dadb |jdcddde |S )fNr   )ArgumentParserRawDescriptionHelpFormatterr   )ParseDirectivesActionParseOptionsActionParseCompileTimeEnvActiona  Environment variables:
  CYTHON_FORCE_REGEN: if set to 1, forces cythonize to regenerate the output files regardless
        of modification times and changes.
  CYTHON_CACHE_DIR: the base directory containing Cython's caches.
  Environment variables accepted by setuptools are supported to configure the C compiler and build:
  https://setuptools.pypa.io/en/latest/userguide/ext_modules.html#compiler-and-linker-options)formatter_classepilogz-Xz--directivezNAME=VALUE,...r0   zset a compiler directive)metavardestdefaulttypeactionhelpz-Ez--compile-time-envr%   z'set a compile time environment variablez-sz--optionz
NAME=VALUEr   zset a cythonize optionz-2language_levelstore_constz#use Python 2 syntax mode by default)r   r   constr   r   z-3rN   z#use Python 3 syntax mode by default)r   r   r   r   z--3strz=use Python 3 syntax mode by default (deprecated alias for -3)z-+z--cplusr)   zc++zCompile as C++ rather than Cz-az
--annotater   annotatez/Produce a colorized HTML version of the source.)r   r   r   r   z--annotate-fullcfullczZProduce a colorized HTML version of the source which includes entire generated C/C++-code.z-xz	--excludePATTERNr/   rZ   z2exclude certain file patterns from the compilation)r   r   r   r   r   z-bz--buildr1   
store_truez2build extension modules using distutils/setuptools)r   r   r   r   rj   z	--inplacer*   zHbuild extension modules in place using distutils/setuptools (implies -b)z--timeitr   
CODESTRINGzabuild in place, then compile+run CODESTRING as benchmark in first module's namespace (implies -i))r   r   r   r   r   z--setupbenchmark_setupz6use CODESTRING as pre-benchmark setup code for --benchz-jz
--parallelr   Nz2run builds in N parallel jobs (default: CPU count)z-fz--forcer&   zforce recompilationz-qz--quietr'   z"be less verbose during compilationz	--lenientlenientzBincrease Python compatibility by ignoring some compile time errorsz-kz--keep-goingr.   z8compile as much as possible, ignore compilation failuresz--no-docstringsno_docstringszstrip docstringsz-Mz	--depfilez produce depfiles for the sources)r   r   sources*)nargs)
argparser   r   Compiler.CmdLiner   r   r   add_argumentr[   int)r   r   r   r   r   parserr   r   r   create_args_parser   s   


r   c                 C   sN   |  |\}}|j}|D ]}|dr| d|  q|| q|`||fS )N-zunknown option )parse_known_argsr   
startswitherrorrZ   )r   rv   r   unknownr   optionr   r   r   parse_args_raw  s   
r   c                 C   s   t  }t|| \}} |jd urd|_n| s|d |jr d|_td u r'd|_|jr7|jdv s1J |j|j	d< |j
r@dt_dt_|jrG|jt_|jrMdt_|| fS )NTzno source files providedr   )r   rN   3strr   F)r   r   r   r*   r   r1   multiprocessingr   r   r   r   r	   error_on_unknown_nameserror_on_uninitializedr   r   
docstrings)rv   r   r   r   r   r   
parse_args  s*   

r   c           
      C   s   t | \}}g }|D ]%}dd t|D }|s*tdtjd |tjd td || q
t	||}|rD|j
rDtt| |j |jd ur|d  }}|r\| \}}	|	r\|	d j}|d urgtjd| t|j|j|d |d ur~tj| d S d S d S )Nc                 S   s   g | ]}t j|qS r   )r
   r   r   )r?   pr   r   r   rH   >  r   zmain.<locals>.<listcomp>z#{}: No such file or directory: '{}'r   )filer   )r   )r   r   r   formatr\   argvstderrexitr2   r   r1   r   r   r   r   r   popitemnamer   insertr   remove)
rv   r   r5   r   r   expanded_pathr   r   r   first_extensionsr   r   r   main9  s6   




r   __main__)NNNr   ) concurrent.futuresrQ   r
   rt   r\   rr   collectionsr   
contextlibr   Dependenciesr   r   Utilsr   Compilerr	   r   rU   r   r   r3   r   r=   r   rE   r   r   r   r   r   __name__r   r   r   r   <module>   s<    %

6
I
#
