o
    IAfO                     @   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rgee  Zee dS dS )    N)partial)Path)compile_dircompile_filecompile_pathc                 c   s    |dk rt | tjrt| } |std|  zt| }W n ty6   |dk r2td|  g }Y nw |  |D ]>}|dkrDq=tj	
| |}tj	|sU|V  q=|dkr{|tjkr{|tjkr{tj	|r{tj	|s{t||d |dE d H  q=d 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.10/compileall.pyr      s:   



r   Fr	   stripdir
prependdirlimit_sl_desthardlink_dupesc
                C   s\  	 d }|d ur|
d us|d urt d|d ur| }
|}d }|dk r%t d|dkrFddlm} z|  W n ty?   d}Y nw ddlm} |d u rNt }t| ||d}d}|dkr|d ur|pbd }||d	#}|	t
t|||||||	|
|||d
|}t|dd}W d    |S 1 sw   Y  |S |D ]}t||||||||	|
|||dsd}q|S )NPDestination dir (ddir) cannot be used in combination with stripdir or prependdirr   z%workers must be greater or equal to 0r	   )_check_system_limits)ProcessPoolExecutor)r   r
   T)max_workers)ddirforcerxr   legacyoptimizeinvalidation_moder$   r%   r&   r'   )defaultr#   F)
ValueErrorconcurrent.futures.processr)   NotImplementedErrorconcurrent.futuresr*   sysgetrecursionlimitr   mapr   r   min)r   r
   r,   r-   r.   r   r/   r0   workersr1   r$   r%   r&   r'   r*   r)   filessuccessexecutorresultsfiler    r    r!   r   0   sj   


r   c          &   
   C   sn  	 |d ur|d us|	d urt dd}|dk r"t| tjr"t| } tj| }d }|d ur5tj||}|d urd| tjj	}|tjj	}t
|}t||D ]\}}||kr]|| qPtjj| }|	d ur{|d u rttj|	| }ntj|	|}t|tr|g}tt|}|rt|dk rt d|d ur|| }|r|S |
d urtj| rt|
 t|  jvr|S i }tj| r5|D ].}|r| d ||< q|dkr|dkr|nd}tjj| |d	}|||< qtj| }|||< q|d d
 |d
d  }}|dkr5|sXzCtt| j}tdtjjd|d@ }| D ]%}t |d}|!d}W d    n	1 s;w   Y  ||krG nq#|W S W n
 t"yW   Y nw |sbt#d$|  z?t%|D ]8\}}|| }t&j'| ||d||d} |dkr|r|||d   }!t(j)||!ddrt*| t+|!| qgW 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 }"~"|S d }"~"w t4t5t"fy- }% z0d}|dkr|W  Y d }%~%S |rt#d$|  nt#ddd t#|%j6j7d |% W Y d }%~%|S d }%~%ww | dkr5d}|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}...)r0   r1   F)shallowz*** Error compiling {!r}...z*** )endbackslashreplace)errors:)8r3   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PyCompileErrorr7   stdoutencodinggetdefaultencodingmsgencodedecodeSyntaxErrorUnicodeError	__class____name__)&r   r,   r-   r.   r   r/   r0   r1   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errrn   rp   er    r    r!   r   }   s   













r   c           	      C   sV   	 d}t jD ]"}|r|tjkr| r|dk rtd q|o't||d |||||d}q|S )NTr   zSkipping current directory)r   r/   r0   r1   )r7   r   r   r   r   r   )	skip_curdirr
   r-   r   r/   r0   r1   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rRz0|jdOkrtjnt|jdPdQ}|D ]
}||  qW d    n	1 s2w   Y  W n tyQ   |jdRk rNtdS|j Y dTS w |j re|j !dOdU" }	tj|	 }
nd }
dV}z^|r|D ]I}t#j$%|rt&||j|j'|j	|j|j(|
|j|j|j|j|jdWsdT}qot)|||j|j'|j	|j|j(|j*|
|j|j|j|j|jdXsdT}qo|W S t+|j(|j'|j|
dYW S  t,y   |jdRk rtdZ Y dTS w )[Nr   z1Utilities to support installing Python libraries.)descriptionz-lstore_constr
   z!don't recurse into subdirectories)actionconstr2   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   r2   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   r2   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)r2   r   r   c                 S   s   g | ]}|j  d dqS )_-)r   lowerreplace).0moder    r    r!   
<listcomp>i  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 filesrB   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   zutf-8)rn   r   zError reading file list {}Fr   T)r1   r$   r%   r0   r&   r'   )r;   r1   r$   r%   r0   r&   r'   )r/   r-   r   r1   z
[interrupted])-argparseArgumentParseradd_argumentrR   rf   PycInvalidationModerS   
parse_argsr   r.   rerg   r&   r   r
   r   rU   r'   errorr,   r$   r%   r   r7   stdinrc   r   stripr   r   r   r   r1   r   upperr   r   rY   r   r-   r/   r   r;   r   KeyboardInterrupt)r   parserinvalidation_modesargscompile_destsr   r
   flineivl_moder1   r=   r   r    r    r!   main4  s  















	
r   __main__)r   )	NNFNr   Fr"   r	   N)NFNr   Fr"   N)r	   r   Fr   Fr"   N)r   r7   importlib.utilrZ   rf   r_   rh   	functoolsr   pathlibr   __all__r   r   r   r   r   rv   rR   exit_statusexitr    r    r    r!   <module>   sH    
M 
" 