o
    fO                     @   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rhee  Zee dS 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| } |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   sZ  d}|dur|
dus|durt d|dur| }
|}d}|dk r$t d|dkrEddlm} z|  W n ty>   d}Y nw ddlm} |du rMt }t| ||d}d	}|dkr|dur|pad}||d
#}|	t
t|||||||	|
|||d|}t|d	d}W d   |S 1 sw   Y  |S |D ]}t||||||||	|
|||dsd}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	   )_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   sh   


r   c          &   
   C   sn  |dur|dus|	durt dd}t| } |dur t|nd}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,/ }#|"j0j1|#dd2|#}$t"|$ W Y d}"~"|S d}"~"w t3t4t!fy- }% z0d}|dkr|W  Y d}%~%S |rt"d#|  nt"dd	d t"|%j5j6d |% W Y d}%~%|S d}%~%ww | dkr5d}|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}...)r0   r1   F)shallowz*** Error compiling {!r}...z*** )endbackslashreplace)errors:)7r3   r   r   r   basenamer   splitseplistzipremover   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   sT   d}t jD ]"}|r|tjkr| r|dk rtd q|o&t||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/   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/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rQz0|jdPkrtjnt|jdQdR}|D ]
}||  qW d   n	1 s1w   Y  W n tyP   |jdSk rMtdT|j Y dUS w |j rd|j !dPdV" }	tj|	 }
nd}
dW}z^|r|D ]I}t#j$%|rt&||j|j'|j	|j|j(|
|j|j|j|j|jdXsdU}qnt)|||j|j'|j	|j|j(|j*|
|j|j|j|j|jdYsdU}qn|W S t+|j(|j'|j|
dZW S  t,y   |jdSk rtd[ Y dUS w )\zScript main program.r   Nz1Utilities 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)__doc__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 
" 