3
/*^?                 @   sL  d dl m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	m
Z
mZ d dlmZmZ d dlmZ d dlmZ d dlmZ d dlmZ d d	lmZ d d	lmZ d d
lmZ d dlmZ dd Zddi dddi dddddfddZ dd Z!d!ddZ"d"ddZ#ej$j%dr8ej& d dd d#kr8dd Z'ndd Z'dd  Z(dS )$    )print_functionN)verifyIS_PYPYPYTHON_VERSION)iscodesysinfo2float)check_object_path)pysource)ParserError)VERSION)code_deparse)deparse_code_with_map)load_modulec             C   st   t jj| }| d }t jj|r*t j| yt j| W n tk
rL   Y nX tdk rbt| ddS t| dddS d S )NZ_failedg      @wb)modewzutf-8)r   encoding)	ospathdirnameexistsremovemakedirsOSErrorr   open)outfiledirZfailed_file r   L/home/psgendb/BIRCHDEV/python/lib/python3.6/site-packages/uncompyle6/main.py_get_outstream"   s    
r   Fc                s  | dkrt  } |ptjfdd}t|s0t|
r8dnd}trDdnd}tjjd}|rd|d|  |dt|| |r~d	t	| nd|d
j
|f  tdk r| dkr|d |jr|d|j  |r|dtjj|  |	r|d|	  |||d}y|r`t|t	rt|}t| |||||||
d dt|  fddt jj D }|jd|  n"|rlt}nt}|||| ||
d  S  tjk
r } ztjt	|W Y dd}~X nX dS )z
    ingests and deparses a given code block 'co'

    if `bytecode_version` is None, use the current Python intepreter
    version.

    Caller is responsible for closing `out` and `mapstream`
    Nc                s   | d7 }  j |  d S )N
)write)s)real_outr   r   r!   O   s    zdecompile.<locals>.writezPyPy  r    z# -*- coding: %s -*-zO# uncompyle6 version %s
# %sPython bytecode %s%s
# Decompiled from: %sPython %sz (%s)z
# g      @z^# Warning: this version has problems handling the Python 3 "byte" type in constants properly.
z# Embedded file name: %sz# Compiled at: %sz$# Size of source mod 2**32: %d bytes)Zasmastgrammar)code_objectsis_pypy   c                s   g | ]}| j |  fqS r   )source_linemap).0Zline_no)deparsedheader_countr   r   
<listcomp>   s   zdecompile.<locals>.<listcomp>z

# %s
)
debug_optsr(   )r   sysstdoutr   AssertionErrorr   versionsplitr   strjoinr   co_filenamedatetimefromtimestamp
isinstancer   r   lensortedr*   keysr!   code_deparse_fragmentsr   r	   SourceWalkerError)Zbytecode_versioncooutshowasmshowast	timestampshowgrammarsource_encodingr'   source_sizer(   	magic_int	mapstreamdo_fragmentsr!   Zco_pypy_strZrun_pypy_strZsys_version_linesr/   ZlinemapZ
deparse_fner   )r,   r-   r#   r   	decompile1   sj    
rL   c             C   sb   | j dr| d d }n| }ttdr4d|tf }nd|tf }td| |f  tj| |d |S )	Nz.pyr)   Zpypy_version_infoz%s-pypy%s.pycz	%s-%s.pyczcompiling %s to %sexec)endswithhasattrr0   r   print
py_compilecompile)source_pathbasenamebytecode_pathr   r   r   compile_file   s    

rW   c             C   s   t | } i }t| |\}	}
}}}}}t|trjg }x\|D ],}|jt|	|||||
|||||d|d q8W n&t|	|||||
||||||||dg}d}|S )zx
    decompile Python byte-code file (.pyc). Return objects to
    all of the deparsed objects found in `filename`.
    )r'   r(   rH   )rI   )r'   rG   r(   rH   rI   rJ   N)r   r   r:   listappendrL   )filename	outstreamrB   rC   rE   rF   rI   rJ   r'   r3   rD   rH   r@   r(   rG   Zsip_hashr,   Zconr   r   r   decompile_file   sL    


r\   c       %   !   C   s  d } } }}|}d}x|D ]}|j t| qW x |D ]}tjj| |}tjj|sntjjd|  q<|rz|d }|rt	|}n,|dkrxtj
}|rtj
}|rtjj|d }|jdr|dtd  }tj
j rd$nd}tdkrtjd	|d|d
}ntjd	d|d}|j}tjtj
j d|t_
tjd|gtjd}tj|jj tj
j  tj|jj tjj  n>|jdrtjj||dd% }ntjj||d }t	|}yt||||||	||}|rx|D ]}d}|j}xtdd |j D D ]}|d |kr:dt|d  } |jd| |d | f  |d }|| }!|j|!}"|jd|!jj  j!  d  |j|"j"d  |j|"j#d  q W qW |d7 }W nL t$t%t&t'j(fk
r } z6tj
jd tjjd||f  |d7 }|d7 }W Y dd}~X n
 t)k
rT   |r0|j*  tj+| tj
jd tjjd|   Y n t,k
r } ztj
jdt-|  t-|j.drtj
jd tjjd||f  n6|r|j*  tj+| tj
jd tjjd|   W Y dd}~X nX |r|j*  |ryRt/j0|||}#|sR|#s<t1d|  |d7 }n|d7 }t1d||# n|d7 }W nx t/j2k
r } zXt1| |d7 }tj3||d  tjjd |  tjjt-|d  |s|
rĂ W Y dd}~X nX n|d7 }n0|rtjjd! n|d7 }|sd"}$t1|$| |r<tj
jd#|t4||||||f  ytj
j5  W q<   Y q<X q<W |rtj
jd ytj
j5  W n   Y nX ||||fS )&a  
    in_base	base directory for input files
    out_base	base directory for output files (ignored when
    files	list of filenames to be uncompyled (relative to in_base)
    outfile	write output to this filename (overwrites out_base)

    For redirecting output to
    - <filename>		outfile=<filename> (out_base is ignored)
    - files below out_base	out_base=...
    - stdout			out_base=None, outfile=None
    r   Nz!File '%s' doesn't exist. Skipped
z.pymap-z.py   g      @zw+b)r   	bufferingsuffixprefix)r   r`   ra   r   tee)stdinz.pycZ_disc             S   s   g | ]}t |d  tr|qS )r^   )r:   int)r+   kr   r   r   r.   M  s    zmain.<locals>.<listcomp>=z	%s
%s
%s
z%sr    z

z
# file %s
# %s
z
Last file: %s   z
%s
zUnsupported Pythonz(
# Unsupported bytecode in file %s
# %s
z
# okay decompiling %sz	
# %s
	%sZ_unverifiedz### Error Verifying %s
z:
### uncompile successful, but no file to compare against
z
# okay decompilingz	%s -- %srg   )6rY   rW   r   r   r6   r   r0   stderrr!   r   r1   rU   rO   r;   isattyr   tempfileNamedTemporaryFilenamefdopenfileno
subprocessPopenPIPEdup2rc   r\   offsetsr<   r=   Zextract_node_infonodeformatstripZselectedLineZ
markerLine
ValueErrorSyntaxErrorr
   r	   r?   KeyboardInterruptcloser   RuntimeErrorr5   
startswithr   Zcompare_code_with_srcfilerQ   ZVerifyCmpErrorrename
status_msgflush)%Zin_baseZout_baseZcompiled_filesZsource_filesr   rB   rC   	do_verifyrE   rF   Zraise_on_errorZdo_linemapsrJ   	tot_files
okay_filesfailed_filesverify_failed_filesZcurrent_outfileZlinemap_streamrT   rZ   infiler[   ra   r_   trb   r,   dZlast_modrs   rK   lineinfoZextractInfomsgmessr   r   r   main   s$   











r   linux   2.3.4.c              C   s.   t dd} | j j d }| j  t|d S )Nz/proc/self/statr   i@B )r   readliner4   rz   rd   )mimur   r   r   
__memUsage  s    
r   c               C   s   dS )Nr$   r   r   r   r   r   r     s    c             C   sf   |dkrd}n|dkrd}nd}|dkr@|r0dS |r<d| S d	S d
|||f }| rb|d||f 7 }|S )NZweakzweak z
verify-runzrun r$   r^   z
# decompile failedz"
# decompile %sverification failedz
# Successfully decompiled filez'decompiled %i files: %i okay, %i failedz, %i %sverification failedr   )r   r   r   r   r   Zweak_verifyZverification_typer   r   r   r   r~     s$    r~   )NNFFNNF)	NNFFFNFFF)r   r   r   ))
__future__r   r8   rR   r   ro   r0   rj   
uncompyle6r   r   r   Zxdisr   r   Zuncompyle6.disasr   Zuncompyle6.semanticsr	   Zuncompyle6.parserr
   Zuncompyle6.versionr   Zuncompyle6.semantics.pysourcer   Zuncompyle6.semantics.fragmentsr>   Zuncompyle6.semantics.linemapr   Z	xdis.loadr   r   rL   rW   r\   r   platformr|   unamer   r~   r   r   r   r   <module>   s\   0Y      
=        
 Y(
	