3
eb]/                 @   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mZ dddgZ	ddd	Z
dddZdddZdddZdd Zedkree  Zej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compile_dircompile_filecompile_path
   c             c   s   |dk rt | tjrtj| } |s0tdj|  ytj| }W n. tk
rl   |dk rdtdj|  g }Y nX |j  x|D ]}|dkrq|tj	j
| |}|d k	rtj	j
||}nd }tj	j|s|V  q||dkr||tjkr||tjkr|tj	j|r|tj	j| r|t|||d |dE d H  q|W d S )N   zListing {!r}...zCan't list {!r}__pycache__r      )ddir	maxlevelsquiet)
isinstanceosPathLikefspathprintformatlistdirOSErrorsortpathjoinisdircurdirpardirislink	_walk_dir)dirr
   r   r   namesnamefullnamedfile r"    /usr/lib/python3.6/compileall.pyr      s0    


r   Fr	   c	             C   s   d}	|dk	rP|dk rt dn2|dkrPyddlm}	 W n tk
rN   d}Y nX t| |||d}
d}|dk	r|dkr|	dk	r|pd}|	|d0}|jtt||||||d	|
}t|dd
}W dQ R X n&x$|
D ]}t|||||||sd}qW |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 10)
    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:  optimization level or -1 for level of the interpreter
    workers:   maximum number of parallel workers
    Nr   z%workers must be greater or equal to 0r	   )ProcessPoolExecutor)r   r   r
   T)Zmax_workers)r
   forcerxr   legacyoptimize)defaultF)	
ValueErrorZconcurrent.futuresr$   ImportErrorr   mapr   r   min)r   r   r
   r%   r&   r   r'   r(   workersr$   ZfilessuccessZexecutorZresultsfiler"   r"   r#   r   2   s6    


c             C   s  d}|dk r"t | tjr"tj| } tjj| }|dk	rFtjj||}	nd}	|dk	rd|j| }
|
rd|S tjj| r|r| d }nB|dkr|dkr|nd}t	j
j| |d}nt	j
j| }tjj|}|dd |dd  }}|d
kr|sXyTttj| j}tjdt	j
j|}t|d}|jd}W dQ R X ||kr>|S W n tk
rV   Y nX |sltdj|  ytj| ||	d|d}W n tjk
r } zdd}|dkr|S |rtdj|  ntddd |jjtjjdd}|j tjj}t| W Y dd}~X n~ t!t"tfk
rv } zLd}|dkr2|S |rHtdj|  ntddd t|j#j$d | W Y dd}~X nX |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:  optimization level or -1 for level of the interpreter
    Tr   Ncr   r	    )optimization   z.pyz<4slrb   zCompiling {!r}...)r(   Fz*** Error compiling {!r}...z*** )endbackslashreplace)errors:r;   )%r   r   r   r   r   basenamer   searchisfile	importlibutilcache_from_sourcedirnameintstatst_mtimestructZpackMAGIC_NUMBERopenreadr   r   r   
py_compilecompilePyCompileErrormsgencodesysstdoutencodingdecodeSyntaxErrorUnicodeError	__class____name__)r    r
   r%   r&   r   r'   r(   r/   r   r!   ZmocfileZoptZ	cache_dirheadtailmtimeZexpectZchandleZactualokerrrM   er"   r"   r#   r   b   sx    






$
c          
   C   sX   d}xNt jD ]D}| s |tjkr6| r6|dk rPtd q|oNt||d||||d}qW |S )aw  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)
    Tr   zSkipping current directoryN)r   r'   r(   )rO   r   r   r   r   r   )Zskip_curdirr   r%   r   r'   r(   r/   r   r"   r"   r#   r      s    
c        
      C   sz  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) |jd*d+d,d-d. |jd/d0d1td2d3 |j }|j}|jrddl}|j|j|_|j	dk	r|j	}n|j
}|jryJ|jd4kr4tjnt|j$}x|D ]}|j|j  qFW W dQ R X W n2 tk
r   |jd5k rtd6j|j d7S X |jdk	r|jpd|_d8}y|r4xj|D ]b}	tjj|	r t|	|j|j|j|j|js*d7}n*t|	||j|j|j|j|j|jd9sd7}qW |S t|j|j|jd:S W n* tk
rt   |jd5k rptd; d7S X d8S )<zScript main program.r   Nz1Utilities to support installing Python libraries.)Zdescriptionz-lZstore_constr   r   z!don't recurse into subdirectories)actionZconstr)   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_   r)   r`   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)metavarr_   r)   r`   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)re   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)re   nargsr`   z-jz	--workersr	   zRun compileall concurrently)r)   rb   r`   -r   zError reading file list {}FT)r.   )r'   r%   r   z
[interrupted]) argparseArgumentParseradd_argumentrC   
parse_argsrg   r&   rerK   ra   r   rf   rO   stdinrH   appendstripr   r   r   r   r.   r   r   r>   r   r
   r%   r'   r   r   KeyboardInterrupt)
rk   parserargsZcompile_destsro   r   fliner/   r_   r"   r"   r#   main   s~    








"



rx   __main__)Nr   r   )r   NFNr   Frz   r	   rz   )NFNr   Frz   rz   )r	   r   Fr   Frz   )__doc__r   rO   importlib.utilr?   rJ   rF   	functoolsr   __all__r   r   r   r   rx   rV   rC   Zexit_statusexitr"   r"   r"   r#   <module>   s$   

 
/ 
R 
[