a
    zeN                  	   @   s   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
 g dZdddZdddddd
ddZdddddd
ddZdddZdd Zedkree  Zee dS )    N)partial)Path)compile_dircompile_filecompile_pathc                 c   s   |dk rt | tjrt| } |s0td|  zt| }W n, tyj   |dk rbtd|  g }Y n0 |  |D ]|}|dkrqxtj	
| |}tj	|s|V  qx|dkrx|tjkrx|tjkrxtj	|rxtj	|sxt||d |dE d H  qxd S )N   zListing {!r}...zCan't list {!r}__pycache__r      )	maxlevelsquiet)
isinstanceosPathLikefspathprintformatlistdirOSErrorsortpathjoinisdircurdirpardirislink	_walk_dir)dirr
   r   namesnamefullname r     /usr/lib/python3.9/compileall.pyr      s2    




r   Fr	   stripdir
prependdirlimit_sl_desthardlink_dupesc
                C   sN  d }|d ur$|
d us|d ur$t d|d ur8| }
|}d }|dk rHt d|dkrxzddlm} W n tyv   d}Y n0 |d u rt }t| ||d}d}|dkr|d ur|pd }||dD}|tt	|||||||	|
|||d	|}t
|dd
}W d    n1 s0    Y  n2|D ],}t	||||||||	|
|||dsd}q|S )NPDestination dir (ddir) cannot be used in combination with stripdir or prependdirr   z%workers must be greater or equal to 0r	   )ProcessPoolExecutor)r   r
   T)max_workers)ddirforcerxr   legacyoptimizeinvalidation_moder$   r%   r&   r'   )defaultr#   F)
ValueErrorconcurrent.futuresr)   ImportErrorsysgetrecursionlimitr   mapr   r   min)r   r
   r+   r,   r-   r   r.   r/   workersr0   r$   r%   r&   r'   r)   filessuccessexecutorresultsfiler    r    r!   r   0   sX    

.r   c          &   
   C   s  |d ur |d us|	d ur t dd}|dk rBt| tjrBt| } tj| }d }|d urhtj||}|d ur| tjj	}|tjj	}t
|}t||D ]\}}||kr|| qtjj| }|	d ur|d u rtj|	| }ntj|	|}t|tr|g}tt|}|r.t|dk r.t d|d urL|| }|rL|S |
d urtj| rt|
 t|  jvr|S i }tj| r|D ]d}|r| d ||< nJ|dkr|dkr|nd}tjj| |d	}|||< ntj| }|||< q|d d
 |d
d   }}|dkr|sztt| j}tdtjjd|d@ }| D ]L}t |d}|!d}W d    n1 s0    Y  ||kr\ qq\|W S W n t"y   Y n0 |st#d$|  zt%|D ]p\}}|| }t&j'| ||d||d} |dkr|r|||d   }!t(j)||!ddrt*| t+|!| qW n t&j,y }" z|d}|dkr|W  Y d }"~"S |rt#d$|  nt#ddd t-j.j/pt-0 }#|"j1j2|#dd3|#}$t#|$ W Y d }"~"nd }"~"0  t4t5t"fyp }% z^d}|dkr(|W  Y d }%~%S |r>t#d$|  nt#ddd t#|%j6j7d |% W Y d }%~%nd }%~%0 0 | dkrd}|S )Nr(   Tr   zXHardlinking of duplicated bytecode makes sense only for more than one optimization levelcr   r	    )optimizationz.pyz<4sLLl    rb   zCompiling {!r}...)r/   r0   F)shallowz*** Error compiling {!r}...z*** )endbackslashreplace)errors:)8r2   r   r   r   r   r   basenamer   splitseplistzipremoveintsortedsetlensearchr   r   resolveparentsisfile	importlibutilcache_from_sourcestatst_mtimestructpackMAGIC_NUMBERvaluesopenreadr   r   r   	enumerate
py_compilecompilefilecmpcmpunlinklinkPyCompileErrorr5   stdoutencodinggetdefaultencodingmsgencodedecodeSyntaxErrorUnicodeError	__class____name__)&r   r+   r,   r-   r   r.   r/   r0   r$   r%   r&   r'   r;   r   dfilefullname_partsstripdir_parts
ddir_partsspartopartmo
opt_cfiles	opt_leveloptcfileheadtailmtimeexpectchandleactualindexokprevious_cfileerrrl   rn   er    r    r!   r   {   s    





*





(
r   c           	      C   sT   d}t jD ]D}|r|tjkr2| r2|dk rNtd q
|oLt||d |||||d}q
|S )NTr   zSkipping current directory)r   r.   r/   r0   )r5   r   r   r   r   r   )	skip_curdirr
   r,   r   r.   r/   r0   r;   r   r    r    r!   r     s     


r   c                  C   s  dd l } | jdd}|jdddd ddd |jd	td
dd |jddddd |jdddddd |j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 |jd'd(d)d d*d |jd+d,d-d.d/ |jd0d1d2d3d4 |jd5d6d7td8d9 d:d; tjD }|jd<t|d=d> |jd?d@tdAdBdC |jdDdEdFdGd/ |jdHddIdJd | }|j}|j	rdd l
}||j	|_	|jdKkrd |_|jd ur|j}n|j}|jd u rdLg|_t|jd7kr|jr|dM |jd ur|jd us
|jd ur|dN |jrz\|jdOkr0tjnt|j*}|D ]}||  q@W d    n1 sl0    Y  W n2 ty   |jdPk rtdQ|j Y dRS 0 |j r|j !dOdS" }	tj|	 }
nd }
dT}z|r~|D ]}t#j$%|r4t&||j|j'|j	|j|j(|
|j|j|j|j|jdUstdR}n@t)|||j|j'|j	|j|j(|j*|
|j|j|j|j|jdVsdR}q|W S t+|j(|j'|j|
dWW S W n* t,y   |jdPk rtdX Y dRS 0 dTS )YNr   z1Utilities to support installing Python libraries.)descriptionz-lstore_constr
   z!don't recurse into subdirectories)actionconstr1   desthelpz-r	recursionzhcontrol the maximum recursion level. if `-l` and `-r` options are specified, then `-r` takes precedence.)typer   r   z-f
store_truer,   z/force rebuild even if timestamps are up to date)r   r   r   z-qcountr   zIoutput only error messages; -qq will suppress the error messages as well.)r   r   r1   r   z-br.   z0use legacy (pre-PEP3147) compiled file locationsz-dDESTDIRr+   zdirectory to prepend to file paths for use in compile-time tracebacks and in runtime tracebacks in cases where the source file is unavailable)metavarr   r1   r   z-sSTRIPDIRr$   zpart of path to left-strip from path to source file - for example buildroot. `-d` and `-s` options cannot be specified together.z-p
PREPENDDIRr%   zpath to add as prefix to path to source file - for example / to make it absolute when some part is removed by `-s` option. `-d` and `-p` options cannot be specified together.z-xREGEXPr-   zskip files matching the regular expression; the regexp is searched for in the full path of each file considered for compilationz-iFILEflistzzadd all the files and directories listed in FILE to the list considered for compilation; if "-", names are read from stdin)r   r   r   compile_destzFILE|DIR*zrzero or more file and directory names to compile; if no arguments given, defaults to the equivalent of -l sys.path)r   nargsr   z-jz	--workersr	   zRun compileall concurrently)r1   r   r   c                 S   s   g | ]}|j  d dqS )_-)r   lowerreplace).0moder    r    r!   
<listcomp>g  s   zmain.<locals>.<listcomp>z--invalidation-modezset .pyc invalidation mode; defaults to "checked-hash" if the SOURCE_DATE_EPOCH environment variable is set, and "timestamp" otherwise.)choicesr   z-oappend
opt_levelszOptimization levels to run compilation with. Default is -1 which uses the optimization level of the Python interpreter itself (see -O).)r   r   r   r   z-eDIRr&   z+Ignore symlinks pointing outsite of the DIRz--hardlink-dupesr'   zHardlink duplicated pyc filesr@   r"   zYHardlinking of duplicated bytecode makes sense only for more than one optimization level.z.-d cannot be used in combination with -s or -pr   r   zError reading file list {}Fr   T)r0   r$   r%   r/   r&   r'   )r9   r0   r$   r%   r/   r&   r'   )r.   r,   r   r0   z
[interrupted])-argparseArgumentParseradd_argumentrP   rd   PycInvalidationModerQ   
parse_argsr   r-   rere   r&   r   r
   r   rS   r'   errorr+   r$   r%   r   r5   stdinra   r   stripr   r   r   r   r0   r   upperr   r   rW   r   r,   r.   r   r9   r   KeyboardInterrupt)r   parserinvalidation_modesargscompile_destsr   r
   flineivl_moder0   r;   r   r    r    r!   main2  s   











6
	
r   __main__)r   )	NNFNr   Fr"   r	   N)NFNr   Fr"   N)r	   r   Fr   Fr"   N)r   r5   importlib.utilrX   rd   r]   rf   	functoolsr   pathlibr   __all__r   r   r   r   r   rt   rP   exit_statusexitr    r    r    r!   <module>   sB   
   K       
" 