a
    IfN                  	   @   s   d Z 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 )a  Module/script to byte-compile all .py files to .pyc files.

When called as a script with arguments, this compiles the directories
given as arguments recursively; the -l option prevents it from
recursing into directories.

Without arguments, if compiles all modules on sys.path, without
recursing into subdirectories.  (Even though it should do so for
packages -- for now, you'll have to deal with packages separately.)

See module py_compile for details of the actual byte-compilation.
    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    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 )a  Byte-compile all modules in the given directory tree.

    Arguments (only dir is required):

    dir:       the directory to byte-compile
    maxlevels: maximum recursion level (default `sys.getrecursionlimit()`)
    ddir:      the directory that will be prepended to the path to the
               file as it is compiled into each byte-code file.
    force:     if True, force compilation, even if timestamps are up-to-date
    quiet:     full output with False or 0, errors only with 1,
               no output with 2
    legacy:    if True, produce legacy pyc paths instead of PEP 3147 paths
    optimize:  int or list of optimization levels or -1 for level of
               the interpreter. Multiple levels leads to multiple compiled
               files each with one optimization level.
    workers:   maximum number of parallel workers
    invalidation_mode: how the up-to-dateness of the pyc will be checked
    stripdir:  part of path to left-strip from source file path
    prependdir: path to prepend to beginning of original file path, applied
               after stripdir
    limit_sl_dest: ignore symlinks if they are pointing outside of
                   the defined path
    hardlink_dupes: hardlink duplicated pyc files
    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)Zmax_workers)ddirforcerxr   legacyoptimizeinvalidation_moder$   r%   r&   r'   )defaultr#   F)
ValueErrorZconcurrent.futuresr)   ImportErrorsysgetrecursionlimitr   mapr   r   min)r   r
   r*   r+   r,   r   r-   r.   workersr/   r$   r%   r&   r'   r)   filessuccessZexecutorZ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 )a  Byte-compile one file.

    Arguments (only fullname is required):

    fullname:  the file to byte-compile
    ddir:      if given, the directory name compiled in to the
               byte-code file.
    force:     if True, force compilation, even if timestamps are up-to-date
    quiet:     full output with False or 0, errors only with 1,
               no output with 2
    legacy:    if True, produce legacy pyc paths instead of PEP 3147 paths
    optimize:  int or list of optimization levels or -1 for level of
               the interpreter. Multiple levels leads to multiple compiled
               files each with one optimization level.
    invalidation_mode: how the up-to-dateness of the pyc will be checked
    stripdir:  part of path to left-strip from source file path
    prependdir: path to prepend to beginning of original file path, applied
               after stripdir
    limit_sl_dest: ignore symlinks if they are pointing outside of
                   the defined path.
    hardlink_dupes: hardlink duplicated pyc files
    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.   r/   F)Zshallowz*** Error compiling {!r}...z*** )endbackslashreplace)errors:)8r1   r   r   r   r   r   basenamer   splitseplistzipremoveintsortedsetlensearchr   r   Zresolveparentsisfile	importlibutilcache_from_sourcestatst_mtimestructZpackMAGIC_NUMBERvaluesopenreadr   r   r   	enumerate
py_compilecompilefilecmpZcmpunlinklinkPyCompileErrorr3   stdoutencodinggetdefaultencodingmsgencodedecodeSyntaxErrorUnicodeError	__class____name__)&r   r*   r+   r,   r   r-   r.   r/   r$   r%   r&   r'   r9   r   dfileZfullname_partsZstripdir_partsZ
ddir_partsZspartZopartZmoZ
opt_cfiles	opt_levelZoptcfileheadtailmtimeZexpectZchandleZactualindexokZprevious_cfileerrrd   rf   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 )a  Byte-compile all module on sys.path.

    Arguments (all optional):

    skip_curdir: if true, skip current directory (default True)
    maxlevels:   max recursion level (default 0)
    force: as for compile_dir() (default False)
    quiet: as for compile_dir() (default 0)
    legacy: as for compile_dir() (default False)
    optimize: as for compile_dir() (default -1)
    invalidation_mode: as for compiler_dir()
    Tr   zSkipping current directoryN)r   r-   r.   r/   )r3   r   r   r   r   r   )	Zskip_curdirr
   r+   r   r-   r.   r/   r9   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/d0 |jd1d2d3d4d5 |jd6d7d8td9d: d;d< tjD }|jd=t|d>d? |jd@dAtdBdCdD |jdEdFdGdHd0 |jdIddJdKd | }|j}|j	rddl
}||j	|_	|jdLkrd|_|jdur|j}n|j}|jdu rdMg|_t|jd8kr|jr|dN |jdur|jdus
|jdur|dO |jrz\|jdPkr0tjnt|j*}|D ]}||  q@W d   n1 sl0    Y  W n2 ty   |jdQk rtdR|j Y dSS 0 |j r|j !dPdT" }	tj|	 }
nd}
dU}z|r~|D ]}t#j$%|r4t&||j|j'|j	|j|j(|
|j|j|j|j|jdVstdS}n@t)|||j|j'|j	|j|j(|j*|
|j|j|j|j|jdWsdS}q|W S t+|j(|j'|j|
dXW S W n* t,y   |jdQk rtdY Y dSS 0 dUS )ZzScript main program.r   Nz1Utilities to support installing Python libraries.)Zdescriptionz-lZstore_constr
   z!don't recurse into subdirectories)actionZconstr0   desthelpz-r	recursionzhcontrol the maximum recursion level. if `-l` and `-r` options are specified, then `-r` takes precedence.)typerx   ry   z-f
store_truer+   z/force rebuild even if timestamps are up to date)rw   rx   ry   z-qcountr   zIoutput only error messages; -qq will suppress the error messages as well.)rw   rx   r0   ry   z-br-   z0use legacy (pre-PEP3147) compiled file locationsz-dZ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)metavarrx   r0   ry   z-sZSTRIPDIRr$   zpart of path to left-strip from path to source file - for example buildroot. `-d` and `-s` options cannot be specified together.z-pZ
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-xZREGEXPr,   zskip files matching the regular expression; the regexp is searched for in the full path of each file considered for compilationz-iZFILEflistzzadd all the files and directories listed in FILE to the list considered for compilation; if "-", names are read from stdin)r~   rx   ry   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~   nargsry   z-jz	--workersr	   zRun compileall concurrently)r0   r{   ry   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.)choicesry   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).)rw   r{   rx   ry   z-eZ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)r/   r$   r%   r.   r&   r'   )r7   r/   r$   r%   r.   r&   r'   )r-   r+   r   r/   z
[interrupted])-argparseArgumentParseradd_argumentrK   r]   PycInvalidationModerL   
parse_argsr   r,   rer^   r&   rz   r
   r   rN   r'   errorr*   r$   r%   r   r3   stdinrZ   r   stripr   r   r   r   r/   r   upperr   r   rQ   r   r+   r-   r   r7   r   KeyboardInterrupt)r   parserZinvalidation_modesargsZcompile_destsr   r
   flineZivl_moder/   r9   rx   r    r    r!   main2  s   











6
	
r   __main__)r   )	NNFNr   Fr"   r	   N)NFNr   Fr"   N)r	   r   Fr   Fr"   N)__doc__r   r3   importlib.utilrR   r]   rW   r_   	functoolsr   pathlibr   __all__r   r   r   r   r   rl   rK   Zexit_statusexitr    r    r    r!   <module>   sD   
   K       
" 