U
    @{?^7                     @   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Zd dlZd dlZd dl	Z	d dl
mZ d dlmZmZ d dlZd dlmZ d dlmZ ejdrd dlm  m  m  mZ neje j ZzeZW n ek
r   dZY nX e Z!ddd	d
gZ"d-ddZ#ej$d.ddZ%ej$dd Z&ej$dd Z'ej$dd Z(G dd de)Z*G dd dZ+ej$dd Z,dd Z-ej$dd Z.ej$dd  Z/d!d" Z0d#d$ Z1d%d
 Z2G d&d dZ3e4e d're j5gZ6ng Z6G d(d de3Z7e8ej9d)d* d+: D Z;G d,d	 d	eZ<dS )/    N)six)builtinsmap)DistutilsError)working_setjavaAbstractSandboxDirectorySandboxSandboxViolation	run_setupc              	   C   sJ   d}t | |}| }W 5 Q R X |dkr.|}t|| d}t||| dS )z.
    Python 3 implementation of execfile.
    rbNexec)openreadcompiler   )filenameglobalslocalsmodestreamZscriptcode r   4/usr/lib/python3/dist-packages/setuptools/sandbox.py	_execfile#   s    r   c              
   c   sD   t jd d  }| d k	r$| t jd d < z
|V  W 5 |t jd d < X d S N)sysargv)replsavedr   r   r   	save_argv0   s    
r   c               
   c   s.   t jd d  } z
| V  W 5 | t jd d < X d S r   )r   pathr   r   r   r   	save_path;   s    
r"   c                 c   s4   t jj| dd tj}| t_z
dV  W 5 |t_X dS )zL
    Monkey-patch tempfile.tempdir with replacement, ensuring it exists
    T)exist_okN)pkg_resourcesZ
py31compatmakedirstempfileZtempdir)Zreplacementr   r   r   r   override_tempD   s    
r'   c              	   c   s.   t  }t |  z
|V  W 5 t | X d S r   )osgetcwdchdir)targetr   r   r   r   pushdU   s
    

r,   c                   @   s   e Zd ZdZedd ZdS )UnpickleableExceptionzP
    An exception representing another Exception that could not be pickled.
    c              	   C   sP   zt | t |fW S  tk
rJ   ddlm} |||t| Y S X dS )z
        Always return a dumped (pickled) type and exc. If exc can't be pickled,
        wrap it in UnpickleableException first.
        r   )r-   N)pickledumps	Exceptionsetuptools.sandboxr-   dumprepr)typeexcclsr   r   r   r2   d   s
    zUnpickleableException.dumpN)__name__
__module____qualname____doc__staticmethodr2   r   r   r   r   r-   _   s   r-   c                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	ExceptionSaverz^
    A Context Manager that will save an exception, serialized, and restore it
    later.
    c                 C   s   | S r   r   selfr   r   r   	__enter__x   s    zExceptionSaver.__enter__c                 C   s    |sd S t ||| _|| _dS NT)r-   r2   _saved_tb)r>   r4   r5   tbr   r   r   __exit__{   s
    zExceptionSaver.__exit__c                 C   s6   dt | krdS ttj| j\}}t||| j dS )z"restore and re-raise any exceptionrA   N)varsr   r.   loadsrA   r   ZreraiserB   )r>   r4   r5   r   r   r   resume   s    zExceptionSaver.resumeN)r7   r8   r9   r:   r?   rD   rG   r   r   r   r   r<   r   s   r<   c               	   #   sV   t j  t }  V  W 5 Q R X t j   fddt jD }t| |   dS )z
    Context in which imported modules are saved.

    Translates exceptions internal to the context into the equivalent exception
    outside the context.
    c                 3   s$   | ]}| kr| d s|V  qdS )z
encodings.N
startswith).0mod_namer!   r   r   	<genexpr>   s   
zsave_modules.<locals>.<genexpr>N)r   modulescopyr<   update_clear_modulesrG   )	saved_excZdel_modulesr   r!   r   save_modules   s    

rR   c                 C   s   t | D ]}tj|= qd S r   )listr   rM   )Zmodule_namesrK   r   r   r   rP      s    rP   c               	   c   s$   t  } z
| V  W 5 t |  X d S r   )r$   __getstate____setstate__r!   r   r   r   save_pkg_resources_state   s    
rV   c                 c   s   t j| d}t x t f t  t N t < t|( t	|  t
d d V  W 5 Q R X W 5 Q R X W 5 Q R X W 5 Q R X W 5 Q R X W 5 Q R X d S )NZtempZ
setuptools)r(   r    joinrV   rR   hide_setuptoolsr"   r   r'   r,   
__import__)	setup_dirZtemp_dirr   r   r   setup_context   s    

r[   c                 C   s   t d}t|| S )aH  
    >>> _needs_hiding('setuptools')
    True
    >>> _needs_hiding('pkg_resources')
    True
    >>> _needs_hiding('setuptools_plugin')
    False
    >>> _needs_hiding('setuptools.__init__')
    True
    >>> _needs_hiding('distutils')
    True
    >>> _needs_hiding('os')
    False
    >>> _needs_hiding('Cython')
    True
    z1(setuptools|pkg_resources|distutils|Cython)(\.|$))rer   boolmatch)rK   patternr   r   r   _needs_hiding   s    
r`   c                  C   s   t ttj} t|  dS )a%  
    Remove references to setuptools' modules from sys.modules to allow the
    invocation to import the most appropriate setuptools. This technique is
    necessary to avoid issues such as #315 where setuptools upgrading itself
    would fail to find a function declared in the metadata.
    N)filterr`   r   rM   rP   )rM   r   r   r   rX      s    rX   c                 C   s   t jt j| }t| z| gt| tjdd< tjd| t	
  t	jdd  t| trl| n| t }t| t|dd}t| | W 5 Q R X W n4 tk
r } z|jr|jd rʂ W 5 d}~X Y nX W 5 Q R X dS )z8Run a distutils setup script, sandboxed in its directoryNr   c                 S   s   |   S r   )Zactivate)Zdistr   r   r   <lambda>       zrun_setup.<locals>.<lambda>__main__)__file__r7   )r(   r    abspathdirnamer[   rS   r   r   insertr   __init__Z	callbacksappend
isinstancestrencodegetfilesystemencodingr	   dictr   
SystemExitargs)Zsetup_scriptrq   rZ   Zdunder_filensvr   r   r   r      s"    

c                   @   s  e Zd ZdZdZdd Zdd Zdd Zd	d
 Zdd Z	dd Z
dD ]ZeeerDe
ee e< qDd$ddZerzedeZedeZdD ]Zeeereee e< qdd ZdD ]Zeeereee e< qdd ZdD ]Zeeereee e< qdd Zdd Zd d! Zd"d# ZdS )%r   zDWrap 'os' module and 'open()' builtin for virtualizing setup scriptsFc                    s    fddt tD  _d S )Nc                    s$   g | ]}| d st |r|qS )_)rI   hasattr)rJ   namer=   r   r   
<listcomp>  s   
 
z,AbstractSandbox.__init__.<locals>.<listcomp>)dir_os_attrsr=   r   r=   r   ri     s    
zAbstractSandbox.__init__c                 C   s"   | j D ]}tt|t|| qd S r   )rz   setattrr(   getattr)r>   sourcerv   r   r   r   _copy  s    
zAbstractSandbox._copyc                 C   s(   |  |  tr| jt_| jt_d| _d S r@   )r~   _filer   file_openr   _activer=   r   r   r   r?     s
    
zAbstractSandbox.__enter__c                 C   s$   d| _ trtt_tt_| t d S NF)r   r   r   r   r   r   r~   ry   )r>   exc_type	exc_value	tracebackr   r   r   rD     s
    zAbstractSandbox.__exit__c              
   C   s"   |  | W  5 Q R  S Q R X dS )zRun 'func' under os sandboxingNr   )r>   funcr   r   r   run  s    zAbstractSandbox.runc                    s   t t  fdd}|S )Nc                    s2   | j r | j ||f||\}}||f||S r   )r   _remap_pair)r>   srcdstrq   kwrv   originalr   r   wrap&  s    z3AbstractSandbox._mk_dual_path_wrapper.<locals>.wrapr|   ry   rv   r   r   r   r   _mk_dual_path_wrapper#  s    
z%AbstractSandbox._mk_dual_path_wrapper)renamelinksymlinkNc                    s    pt t  fdd}|S )Nc                    s*   | j r| j |f||}|f||S r   )r   _remap_inputr>   r    rq   r   r   r   r   r   4  s    z5AbstractSandbox._mk_single_path_wrapper.<locals>.wrapr   )rv   r   r   r   r   r   _mk_single_path_wrapper1  s    z'AbstractSandbox._mk_single_path_wrapperr   r   )statlistdirr*   r   chmodchownmkdirremoveunlinkrmdirutimelchownchrootlstatZ	startfilemkfifomknodpathconfaccessc                    s   t t  fdd}|S )Nc                    sB   | j r2| j |f||}|  |f||S |f||S r   )r   r   _remap_outputr   r   r   r   r   I  s    z4AbstractSandbox._mk_single_with_return.<locals>.wrapr   r   r   r   r   _mk_single_with_returnF  s    
z&AbstractSandbox._mk_single_with_return)readlinktempnamc                    s   t t  fdd}|S )Nc                    s    ||}| j r|  |S |S r   )r   r   )r>   rq   r   Zretvalr   r   r   r   X  s    
z'AbstractSandbox._mk_query.<locals>.wrapr   r   r   r   r   	_mk_queryU  s    
zAbstractSandbox._mk_query)r)   tmpnamc                 C   s   |S )z=Called to remap or validate any path, whether input or outputr   )r>   r    r   r   r   _validate_pathd  s    zAbstractSandbox._validate_pathc                 O   s
   |  |S zCalled for path inputsr   r>   	operationr    rq   r   r   r   r   r   h  s    zAbstractSandbox._remap_inputc                 C   s
   |  |S )zCalled for path outputsr   )r>   r   r    r   r   r   r   l  s    zAbstractSandbox._remap_outputc                 O   s0   | j |d |f||| j |d |f||fS )?Called for path pairs like rename, link, and symlink operationsz-fromz-to)r   r>   r   r   r   rq   r   r   r   r   r   p  s    zAbstractSandbox._remap_pair)N)r7   r8   r9   r:   r   ri   r~   r?   rD   r   r   rv   ru   ry   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r     s<   








devnullc                   @   s   e Zd ZdZedddddddd	d
ddddgZdgZefddZ	dd Z
erXd'ddZd(ddZdd Zdd Zdd Zdd  Zd!d" Zd)d$d%Zd&S )*r	   z<Restrict operations to a single subdirectory - pseudo-chrootr   r   r   r   r   r   r   r   r   r   r   r   r   z.*lib2to3.*\.pickle$c                 C   sF   t jt j|| _t j| jd| _dd |D | _t	|  d S )N c                 S   s    g | ]}t jt j|qS r   )r(   r    normcaserealpath)rJ   r    r   r   r   rw     s   z-DirectorySandbox.__init__.<locals>.<listcomp>)
r(   r    r   r   _sandboxrW   _prefix_exceptionsr   ri   )r>   Zsandbox
exceptionsr   r   r   ri     s    zDirectorySandbox.__init__c                 O   s   ddl m} ||||d S )Nr   )r
   )r1   r
   )r>   r   rq   r   r
   r   r   r   
_violation  s    zDirectorySandbox._violationrc                 O   s:   |dkr(|  |s(| jd||f|| t||f||S )Nr   Zrtr   ZrUUr   )_okr   r   r>   r    r   rq   r   r   r   r   r     s    zDirectorySandbox._filec                 O   s:   |dkr(|  |s(| jd||f|| t||f||S )Nr   r   )r   r   r   r   r   r   r   r     s    zDirectorySandbox._openc                 C   s   |  d d S )Nr   )r   r=   r   r   r   r     s    zDirectorySandbox.tmpnamc                 C   sR   | j }z>d| _ tjtj|}| |p@|| jkp@|| jW S || _ X d S r   )	r   r(   r    r   r   	_exemptedr   rI   r   )r>   r    Zactiver   r   r   r   r     s    

zDirectorySandbox._okc                    s<    fdd| j D } fdd| jD }t||}t|S )Nc                 3   s   | ]}  |V  qd S r   rH   )rJ   Z	exceptionfilepathr   r   rL     s   z-DirectorySandbox._exempted.<locals>.<genexpr>c                 3   s   | ]}t | V  qd S r   )r\   r^   )rJ   r_   r   r   r   rL     s   )r   _exception_patterns	itertoolschainany)r>   r   Zstart_matchesZpattern_matchesZ
candidatesr   r   r   r     s    

zDirectorySandbox._exemptedc                 O   s4   || j kr0| |s0| j|tj|f|| |S r   )	write_opsr   r   r(   r    r   r   r   r   r   r     s    zDirectorySandbox._remap_inputc                 O   s2   |  |r|  |s*| j|||f|| ||fS )r   )r   r   r   r   r   r   r     s    zDirectorySandbox._remap_pair  c                 O   s@   |t @ r*| |s*| jd|||f|| tj|||f||S )zCalled for low-level os.open()zos.open)WRITE_FLAGSr   r   ry   r   )r>   r   flagsr   rq   r   r   r   r   r     s    zDirectorySandbox.openN)r   )r   )r   )r7   r8   r9   r:   ro   fromkeysr   r   _EXCEPTIONSri   r   r   r   r   r   r   r   r   r   r   r   r   r   r	   ~  s:              	

c                 C   s   g | ]}t t|d qS )r   r   )rJ   ar   r   r   rw     s    rw   z4O_WRONLY O_RDWR O_APPEND O_CREAT O_TRUNC O_TEMPORARYc                   @   s&   e Zd ZdZed Zdd ZdS )r
   zEA setup script attempted to modify the filesystem outside the sandboxa  
        SandboxViolation: {cmd}{args!r} {kwargs}

        The package setup script has attempted to modify files on your system
        that are not within the EasyInstall build area, and has been aborted.

        This package cannot be safely installed by EasyInstall, and may not
        support alternate installation locations even if you run its setup
        script by hand.  Please inform the package's author and the EasyInstall
        maintainers to find out if a fix or workaround is available.
        c                 C   s   | j \}}}| jjf t S r   )rq   tmplformatr   )r>   cmdrq   kwargsr   r   r   __str__  s    zSandboxViolation.__str__N)	r7   r8   r9   r:   textwrapdedentlstripr   r   r   r   r   r   r
     s   )N)N)=r(   r   r&   operator	functoolsr   r\   
contextlibr.   r   Zsetuptools.externr   Zsetuptools.extern.six.movesr   r   Zpkg_resources.py31compatr$   Zdistutils.errorsr   r   platformrI   Z$org.python.modules.posix.PosixModulepythonrM   posixZPosixModulery   rv   r   r   	NameErrorr   r   __all__r   contextmanagerr   r"   r'   r,   r0   r-   r<   rR   rP   rV   r[   r`   rX   r   r   ru   r   r   r	   reduceor_splitr   r
   r   r   r   r   <module>   sx    
   




	


w
V 