U
    ٲg7                     @   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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lm Z  d dlm!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java)DistutilsError)working_set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streamscriptcode r   6/tmp/pip-unpacked-wheel-aqb5epr1/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_resources
py31compatmakedirstempfiletempdir)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
    

r0   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   )r1   N)pickledumps	Exceptionsetuptools.sandboxr1   dumprepr)typeexcclsr   r   r   r6   d   s
    zUnpickleableException.dumpN)__name__
__module____qualname____doc__staticmethodr6   r   r   r   r   r1   _   s   r1   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)r1   r6   _saved_tb)rB   r8   r9   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 exceptionrE   N)varsr   r2   loadsrE   r   reraiserF   )rB   r8   r9   r   r   r   resume   s    zExceptionSaver.resumeN)r;   r<   r=   r>   rC   rH   rL   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_modulesrL   )	saved_excZdel_modulesr   r"   r   save_modules   s    

rW   c                 C   s   t | D ]}tj|= qd S r   )listr   rR   )Zmodule_namesrP   r   r   r   rU      s    rU   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    
r[   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 )Ntemp
setuptools)r,   r!   joinr[   rW   hide_setuptoolsr#   r    r+   r0   
__import__)	setup_dirtemp_dirr   r   r   setup_context   s    

rc   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)rP   patternr   r   r   _needs_hiding   s    
rh   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)filterrh   r   rR   rU   )rR   r   r   r   r_      s    r_   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   )activate)distr   r   r   <lambda>       zrun_setup.<locals>.<lambda>__main__)__file__r;   )r,   r!   abspathdirnamerc   rX   r   r   insertr   __init__	callbacksappend
isinstancestrencodegetfilesystemencodingr	   dictr   
SystemExitargs)Zsetup_scriptr|   ra   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 )_)rN   hasattr)rO   namerA   r   r   
<listcomp>  s   
 
z,AbstractSandbox.__init__.<locals>.<listcomp>)dir_os_attrsrA   r   rA   r   rs     s    
zAbstractSandbox.__init__c                 C   s"   | j D ]}tt|t|| qd S r   )r   setattrr,   getattr)rB   sourcer   r   r   r   _copy  s    
zAbstractSandbox._copyc                 C   s(   |  |  tr| jt_| jt_d| _d S rD   )r   _filer   file_openr   _activerA   r   r   r   rC     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   r   )rB   exc_type	exc_value	tracebackr   r   r   rH     s
    zAbstractSandbox.__exit__c              
   C   s"   |  | W  5 Q R  S Q R X dS )zRun 'func' under os sandboxingNr   )rB   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)rB   srcdstr|   kwr   originalr   r   wrap&  s    z3AbstractSandbox._mk_dual_path_wrapper.<locals>.wrapr   r   r   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rB   r!   r|   r   r   r   r   r   4  s    z5AbstractSandbox._mk_single_path_wrapper.<locals>.wrapr   )r   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   )rB   r|   r   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   )rB   r!   r   r   r   _validate_pathd  s    zAbstractSandbox._validate_pathc                 O   s
   |  |S zCalled for path inputsr   rB   	operationr!   r|   r   r   r   r   r   h  s    zAbstractSandbox._remap_inputc                 C   s
   |  |S )zCalled for path outputsr   )rB   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   rB   r   r   r   r|   r   r   r   r   r   p  s    zAbstractSandbox._remap_pair)N)r;   r<   r=   r>   r   rs   r   rC   rH   r   r   r   r   r   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)rO   r!   r   r   r   r     s   z-DirectorySandbox.__init__.<locals>.<listcomp>)
r,   r!   r   r   _sandboxr^   _prefix_exceptionsr   rs   )rB   Zsandbox
exceptionsr   r   r   rs     s    zDirectorySandbox.__init__c                 O   s   ddl m} ||||d S )Nr   )r
   )r5   r
   )rB   r   r|   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   rtr   ZrUUr   )_okr   r   rB   r!   r   r|   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   rA   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   rN   r   )rB   r!   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   rM   )rO   	exceptionfilepathr   r   rQ     s   z-DirectorySandbox._exempted.<locals>.<genexpr>c                 3   s   | ]}t | V  qd S r   )rd   rf   )rO   rg   r   r   r   rQ     s   )r   _exception_patterns	itertoolschainany)rB   r   Zstart_matchesZpattern_matches
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   r   r   )rB   r   flagsr   r|   r   r   r   r   r     s    zDirectorySandbox.openN)r   )r   )r   )r;   r<   r=   r>   rz   fromkeysr   r   _EXCEPTIONSrs   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   )rO   ar   r   r   r     s     r   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   )r|   tmplformatr   )rB   cmdr|   kwargsr   r   r   __str__  s    zSandboxViolation.__str__N)	r;   r<   r=   r>   textwrapdedentlstripr   r   r   r   r   r   r
     s   )N)N)=r,   r   r(   operator	functoolsr   rd   
contextlibr2   r   Zsetuptools.externr   Zsetuptools.extern.six.movesr   r   Zpkg_resources.py31compatr%   platformrN   Z$org.python.modules.posix.PosixModulepythonrR   posixZPosixModuler   r   r   r   	NameErrorr   r   distutils.errorsr   r   __all__r   contextmanagerr    r#   r+   r0   r4   r1   r@   rW   rU   r[   rc   rh   r_   r   r   r   r   r   r	   reduceor_splitr   r
   r   r   r   r   <module>   sx    
   




	


w
V 