
.Wc           @   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	 m
 Z
 d  d l m Z m Z d  d l Z e j j d  r d  d l j j j j Z n e j e  j Z y
 e Z Wn e k
 r d Z n Xe Z d  d l m Z d  d l m  Z  d d d	 d
 g Z! d d  Z" e j# d d   Z$ e j# d    Z% e j# d    Z& e j# d    Z' d e( f d     YZ) d d" d     YZ* e j# d    Z+ d   Z, e j# d    Z- e j# d    Z. d   Z/ d   Z0 d   Z1 d d# d     YZ2 e3 e  d  rBe  j4 g Z5 n g  Z5 y' d  d l6 m7 Z7 e5 j8 e7    [7 Wn e9 k
 rn Xd e2 f d     YZ: e j; e j< g  d j=   D] Z> e? e e> d   ^ q Z@ d	 e f d!     YZA d S($   iN(   t   six(   t   builtinst   mapt   java(   t   DistutilsError(   t   working_sett   AbstractSandboxt   DirectorySandboxt   SandboxViolationt	   run_setupc         B   s   d } e  |  |   } | j   } Wd QXe j d  d k  sc e j d  d k r e j d  d k  r | j d d  } | j d	 d  } n  | d k r | } n  e | |  d
  } | | | Ud S(   s.   
    Python 3 implementation of execfile.
    t   rbNi   i   i   i    s   
s   
s   t   exec(   i   i   (   i   i    (   i   i   (   t   opent   readt   syst   version_infot   replacet   Nonet   compile(   t   filenamet   globalst   localst   modet   streamt   scriptt   code(    (    s6   /tmp/pip-build-Q3rTTP/setuptools/setuptools/sandbox.pyt	   _execfile!   s    9	c         c   s>   t  j } |  d  k	 r# |  t  j (n  z	 | VWd  | t  j (Xd  S(   N(   R   t   argvR   (   t   replt   saved(    (    s6   /tmp/pip-build-Q3rTTP/setuptools/setuptools/sandbox.pyt	   save_argv2   s    
	c          c   s%   t  j }  z	 |  VWd  |  t  j (Xd  S(   N(   R   t   path(   R   (    (    s6   /tmp/pip-build-Q3rTTP/setuptools/setuptools/sandbox.pyt	   save_path=   s    
	c         c   sN   t  j j |   s" t  j |   n  t j } |  t _ z	 d VWd | t _ Xd S(   sL   
    Monkey-patch tempfile.tempdir with replacement, ensuring it exists
    N(   t   osR   t   isdirt   makedirst   tempfilet   tempdir(   t   replacementR   (    (    s6   /tmp/pip-build-Q3rTTP/setuptools/setuptools/sandbox.pyt   override_tempF   s    			c         c   s7   t  j   } t  j |   z	 | VWd  t  j |  Xd  S(   N(   R!   t   getcwdt   chdir(   t   targetR   (    (    s6   /tmp/pip-build-Q3rTTP/setuptools/setuptools/sandbox.pyt   pushdX   s
    	t   UnpickleableExceptionc           B   s   e  Z d  Z e d    Z RS(   sP   
    An exception representing another Exception that could not be pickled.
    c         C   sa   y  t  j |   t  j |  f SWn: t k
 r\ d d l m } | j | | t |    SXd S(   s   
        Always return a dumped (pickled) type and exc. If exc can't be pickled,
        wrap it in UnpickleableException first.
        i(   R,   N(   t   picklet   dumpst	   Exceptiont   setuptools.sandboxR,   t   dumpt   repr(   t   typet   exct   cls(    (    s6   /tmp/pip-build-Q3rTTP/setuptools/setuptools/sandbox.pyR1   f   s
     (   t   __name__t
   __module__t   __doc__t   staticmethodR1   (    (    (    s6   /tmp/pip-build-Q3rTTP/setuptools/setuptools/sandbox.pyR,   b   s   t   ExceptionSaverc           B   s)   e  Z d  Z d   Z d   Z d   Z RS(   s^   
    A Context Manager that will save an exception, serialized, and restore it
    later.
    c         C   s   |  S(   N(    (   t   self(    (    s6   /tmp/pip-build-Q3rTTP/setuptools/setuptools/sandbox.pyt	   __enter__z   s    c         C   s,   | s
 d  St  j | |  |  _ | |  _ t S(   N(   R,   R1   t   _savedt   _tbt   True(   R;   R3   R4   t   tb(    (    s6   /tmp/pip-build-Q3rTTP/setuptools/setuptools/sandbox.pyt   __exit__}   s
    	c         C   sK   d t  |   k r d St t j |  j  \ } } t j | | |  j  d S(   s"   restore and re-raise any exceptionR=   N(   t   varsR   R-   t   loadsR=   R    t   reraiseR>   (   R;   R3   R4   (    (    s6   /tmp/pip-build-Q3rTTP/setuptools/setuptools/sandbox.pyt   resume   s    (   R6   R7   R8   R<   RA   RE   (    (    (    s6   /tmp/pip-build-Q3rTTP/setuptools/setuptools/sandbox.pyR:   t   s   		c          #   sg   t  j j     t    }    VWd QXt  j j      f d   t  j D } t |  |  j   d S(   s   
    Context in which imported modules are saved.

    Translates exceptions internal to the context into the equivalent exception
    outside the context.
    Nc         3   s1   |  ]' } |   k r | j  d   r | Vq d S(   s
   encodings.N(   t
   startswith(   t   .0t   mod_name(   R   (    s6   /tmp/pip-build-Q3rTTP/setuptools/setuptools/sandbox.pys	   <genexpr>   s    (   R   t   modulest   copyR:   t   updatet   _clear_modulesRE   (   t	   saved_exct   del_modules(    (   R   s6   /tmp/pip-build-Q3rTTP/setuptools/setuptools/sandbox.pyt   save_modules   s    
c         C   s%   x t  |   D] } t j | =q Wd  S(   N(   t   listR   RI   (   t   module_namesRH   (    (    s6   /tmp/pip-build-Q3rTTP/setuptools/setuptools/sandbox.pyRL      s    c          c   s*   t  j   }  z	 |  VWd  t  j |   Xd  S(   N(   t   pkg_resourcest   __getstate__t   __setstate__(   R   (    (    s6   /tmp/pip-build-Q3rTTP/setuptools/setuptools/sandbox.pyt   save_pkg_resources_state   s    	c         c   s   t  j j |  d  } t   q t   a t   t   J t   : t |  ' t	 |    t
 d  d  VWd  QXWd  QXWd  QXWd  QXWd  QXWd  QXd  S(   Nt   tempt
   setuptools(   R!   R   t   joinRU   RO   t   hide_setuptoolsR    R   R'   R+   t
   __import__(   t	   setup_dirt   temp_dir(    (    s6   /tmp/pip-build-Q3rTTP/setuptools/setuptools/sandbox.pyt   setup_context   s    




c         C   s"   t  j d  } t | j |    S(   sH  
    >>> _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
    s1   (setuptools|pkg_resources|distutils|Cython)(\.|$)(   t   reR   t   boolt   match(   RH   t   pattern(    (    s6   /tmp/pip-build-Q3rTTP/setuptools/setuptools/sandbox.pyt   _needs_hiding   s    c          C   s    t  t t j  }  t |   d S(   s%  
    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(   t   filterRb   R   RI   RL   (   RI   (    (    s6   /tmp/pip-build-Q3rTTP/setuptools/setuptools/sandbox.pyRY      s    c      	      s   t  j j t  j j     } t |   ym   g t |  t j (t j j d |  t	 j
   t	 j j d      f d   } t |  j |  Wn/ t k
 r } | j r | j d r   q n XWd QXd S(   s8   Run a distutils setup script, sandboxed in its directoryi    c         S   s
   |  j    S(   N(   t   activate(   t   dist(    (    s6   /tmp/pip-build-Q3rTTP/setuptools/setuptools/sandbox.pyt   <lambda>   s    c             s&   t  d   d d  }  t   |   d  S(   Nt   __file__R6   t   __main__(   t   dictR   (   t   ns(   t   setup_script(    s6   /tmp/pip-build-Q3rTTP/setuptools/setuptools/sandbox.pyt   runner   s    N(   R!   R   t   abspatht   dirnameR]   RP   R   R   t   insertR   t   __init__t	   callbackst   appendR   t   runt
   SystemExitt   args(   Rk   Ru   R[   Rl   t   v(    (   Rk   s6   /tmp/pip-build-Q3rTTP/setuptools/setuptools/sandbox.pyR	      s    
c           B   s  e  Z d  Z e Z d   Z d   Z d   Z d   Z x< d d d g D]+ Z	 e
 e e	  rF e e	  e   e	 <qF qF Wd' d  Z e r e d	 e  Z n  e d
 e  Z xl d d d d
 d d d d d d d d d d d d d d d g D]+ Z	 e
 e e	  r e e	  e   e	 <q q Wd   Z x9 d d g D]+ Z	 e
 e e	  r-e e	  e   e	 <q-q-Wd    Z x9 d! d" g D]+ Z	 e
 e e	  rre e	  e   e	 <qrqrWd#   Z d$   Z d%   Z d&   Z RS((   sD   Wrap 'os' module and 'open()' builtin for virtualizing setup scriptsc         C   sE   g  t  t  D]+ } | j d  r t |  |  r | ^ q |  _ d  S(   Nt   _(   t   dirt   _osRF   t   hasattrt   _attrs(   R;   t   name(    (    s6   /tmp/pip-build-Q3rTTP/setuptools/setuptools/sandbox.pyRp     s    c         C   s1   x* |  j  D] } t t | t | |   q
 Wd  S(   N(   R{   t   setattrR!   t   getattr(   R;   t   sourceR|   (    (    s6   /tmp/pip-build-Q3rTTP/setuptools/setuptools/sandbox.pyt   _copy  s    c         C   s{   zB |  j  |   t r% |  j t _ n  |  j t _ t |  _ |   SWd t |  _ t r` t t _ n  t t _ |  j  t	  Xd S(   s   Run 'func' under os sandboxingN(
   R   t   _fileR   t   filet   _openR   R?   t   _activet   FalseRy   (   R;   t   func(    (    s6   /tmp/pip-build-Q3rTTP/setuptools/setuptools/sandbox.pyRs     s    			c            s%   t  t        f d   } | S(   Nc            s@   |  j  r- |  j   | | | |  \ } } n   | | | |  S(   N(   R   t   _remap_pair(   R;   t   srct   dstRu   t   kw(   R|   t   original(    s6   /tmp/pip-build-Q3rTTP/setuptools/setuptools/sandbox.pyt   wrap  s    	$(   R~   Ry   (   R|   R   (    (   R|   R   s6   /tmp/pip-build-Q3rTTP/setuptools/setuptools/sandbox.pyt   _mk_dual_path_wrapper  s    t   renamet   linkt   symlinkc            s+    p t  t        f d   } | S(   Nc            s4   |  j  r$ |  j   | | |  } n   | | |  S(   N(   R   t   _remap_input(   R;   R   Ru   R   (   R|   R   (    s6   /tmp/pip-build-Q3rTTP/setuptools/setuptools/sandbox.pyR   ,  s    	(   R~   Ry   (   R|   R   R   (    (   R|   R   s6   /tmp/pip-build-Q3rTTP/setuptools/setuptools/sandbox.pyt   _mk_single_path_wrapper)  s    R   R   t   statt   listdirR)   t   chmodt   chownt   mkdirt   removet   unlinkt   rmdirt   utimet   lchownt   chroott   lstatt	   startfilet   mkfifot   mknodt   pathconft   accessc            s%   t  t        f d   } | S(   Nc            sM   |  j  r= |  j   | | |  } |  j    | | |   S | | |  S(   N(   R   R   t   _remap_output(   R;   R   Ru   R   (   R|   R   (    s6   /tmp/pip-build-Q3rTTP/setuptools/setuptools/sandbox.pyR   @  s    	(   R~   Ry   (   R|   R   (    (   R|   R   s6   /tmp/pip-build-Q3rTTP/setuptools/setuptools/sandbox.pyt   _mk_single_with_return=  s    t   readlinkt   tempnamc            s%   t  t        f d   } | S(   Nc            s,    | |   } |  j  r( |  j   |  S| S(   N(   R   R   (   R;   Ru   R   t   retval(   R|   R   (    s6   /tmp/pip-build-Q3rTTP/setuptools/setuptools/sandbox.pyR   N  s    	(   R~   Ry   (   R|   R   (    (   R|   R   s6   /tmp/pip-build-Q3rTTP/setuptools/setuptools/sandbox.pyt	   _mk_queryK  s    R(   t   tmpnamc         C   s   | S(   s=   Called to remap or validate any path, whether input or output(    (   R;   R   (    (    s6   /tmp/pip-build-Q3rTTP/setuptools/setuptools/sandbox.pyt   _validate_pathY  s    c         O   s   |  j  |  S(   s   Called for path inputs(   R   (   R;   t	   operationR   Ru   R   (    (    s6   /tmp/pip-build-Q3rTTP/setuptools/setuptools/sandbox.pyR   ]  s    c         C   s   |  j  |  S(   s   Called for path outputs(   R   (   R;   R   R   (    (    s6   /tmp/pip-build-Q3rTTP/setuptools/setuptools/sandbox.pyR   a  s    c         O   s6   |  j  | d | | |  |  j  | d | | |  f S(   s?   Called for path pairs like rename, link, and symlink operationss   -froms   -to(   R   (   R;   R   R   R   Ru   R   (    (    s6   /tmp/pip-build-Q3rTTP/setuptools/setuptools/sandbox.pyR   e  s    N(   R6   R7   R8   R   R   Rp   R   Rs   R   R|   Rz   Ry   R   R   R   R   R   R   R   R   R   R   R   (    (    (    s6   /tmp/pip-build-Q3rTTP/setuptools/setuptools/sandbox.pyR      s>   							
	
			t   devnull(   t   GetGeneratePathc           B   s   e  Z d  Z e j d d d d d d d d d	 d
 d d d g  Z d g Z e d  Z d   Z	 e
 ru d d  Z
 n  d d  Z d   Z d   Z d   Z d   Z d   Z d d  Z RS(   s<   Restrict operations to a single subdirectory - pseudo-chrootR   R   R   R   R   R   R   R   R   R   R   R   R   s   .*lib2to3.*\.pickle$c         C   s   t  j j t  j j |   |  _ t  j j |  j d  |  _ g  | D]$ } t  j j t  j j |   ^ qC |  _ t j	 |   d  S(   Nt    (
   R!   R   t   normcaset   realpatht   _sandboxRX   t   _prefixt   _exceptionsR   Rp   (   R;   t   sandboxt
   exceptionsR   (    (    s6   /tmp/pip-build-Q3rTTP/setuptools/setuptools/sandbox.pyRp     s
    !1c         O   s&   d d l  m } | | | |   d  S(   Ni(   R   (   R0   R   (   R;   R   Ru   R   R   (    (    s6   /tmp/pip-build-Q3rTTP/setuptools/setuptools/sandbox.pyt
   _violation  s    t   rc         O   sK   | d k r8 |  j  |  r8 |  j d | | | |  n  t | | | |  S(   NR   t   rtR
   t   rUt   UR   (   R   R   s   rbs   rUR   (   t   _okR   R   (   R;   R   R   Ru   R   (    (    s6   /tmp/pip-build-Q3rTTP/setuptools/setuptools/sandbox.pyR     s    c         O   sK   | d k r8 |  j  |  r8 |  j d | | | |  n  t | | | |  S(   NR   R   R
   R   R   R   (   R   s   rts   rbs   rUR   (   R   R   R   (   R;   R   R   Ru   R   (    (    s6   /tmp/pip-build-Q3rTTP/setuptools/setuptools/sandbox.pyR     s    c         C   s   |  j  d  d  S(   NR   (   R   (   R;   (    (    s6   /tmp/pip-build-Q3rTTP/setuptools/setuptools/sandbox.pyR     s    c         C   ss   |  j  } zY t |  _  t j j t j j |   } |  j |  p` | |  j k p` | j |  j	  SWd  | |  _  Xd  S(   N(
   R   R   R!   R   R   R   t	   _exemptedR   RF   R   (   R;   R   t   activeR   (    (    s6   /tmp/pip-build-Q3rTTP/setuptools/setuptools/sandbox.pyR     s    		c            sN     f d   |  j  D }   f d   |  j D } t j | |  } t |  S(   Nc         3   s   |  ] }   j  |  Vq d  S(   N(   RF   (   RG   t	   exception(   t   filepath(    s6   /tmp/pip-build-Q3rTTP/setuptools/setuptools/sandbox.pys	   <genexpr>  s   c         3   s!   |  ] } t  j |    Vq d  S(   N(   R^   R`   (   RG   Ra   (   R   (    s6   /tmp/pip-build-Q3rTTP/setuptools/setuptools/sandbox.pys	   <genexpr>  s   (   R   t   _exception_patternst	   itertoolst   chaint   any(   R;   R   t   start_matchest   pattern_matchest
   candidates(    (   R   s6   /tmp/pip-build-Q3rTTP/setuptools/setuptools/sandbox.pyR     s    c         O   sH   | |  j  k rD |  j |  rD |  j | t j j |  | |  n  | S(   s   Called for path inputs(   t	   write_opsR   R   R!   R   R   (   R;   R   R   Ru   R   (    (    s6   /tmp/pip-build-Q3rTTP/setuptools/setuptools/sandbox.pyR     s    %c         O   sF   |  j  |  s  |  j  |  r< |  j | | | | |  n  | | f S(   s?   Called for path pairs like rename, link, and symlink operations(   R   R   (   R;   R   R   R   Ru   R   (    (    s6   /tmp/pip-build-Q3rTTP/setuptools/setuptools/sandbox.pyR     s     i  c         O   sR   | t  @r9 |  j |  r9 |  j d | | | | |  n  t j | | | | |  S(   s   Called for low-level os.open()s   os.open(   t   WRITE_FLAGSR   R   Ry   R   (   R;   R   t   flagsR   Ru   R   (    (    s6   /tmp/pip-build-Q3rTTP/setuptools/setuptools/sandbox.pyR     s    (   R6   R7   R8   Ri   t   fromkeysR   R   t   _EXCEPTIONSRp   R   R   R   R   R   R   R   R   R   (    (    (    s6   /tmp/pip-build-Q3rTTP/setuptools/setuptools/sandbox.pyR   {  s    								s4   O_WRONLY O_RDWR O_APPEND O_CREAT O_TRUNC O_TEMPORARYi    c           B   s   e  Z d  Z d   Z RS(   sE   A setup script attempted to modify the filesystem outside the sandboxc         C   s   d |  j  S(   Ns  SandboxViolation: %s%r %s

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.(   Ru   (   R;   (    (    s6   /tmp/pip-build-Q3rTTP/setuptools/setuptools/sandbox.pyt   __str__  s    	(   R6   R7   R8   R   (    (    (    s6   /tmp/pip-build-Q3rTTP/setuptools/setuptools/sandbox.pyR     s   (    (    (B   R!   R   R$   t   operatort	   functoolsR   R^   t
   contextlibR-   t   setuptools.externR    t   setuptools.extern.six.movesR   R   RR   t   platformRF   t$   org.python.modules.posix.PosixModulet   pythonRI   t   posixt   PosixModuleRy   R|   R   R   t	   NameErrorR   R   R   t   distutils.errorsR   R   t   __all__R   t   contextmanagerR   R    R'   R+   R/   R,   R:   RO   RL   RU   R]   Rb   RY   R	   R   Rz   R   R   t   win32com.client.gencacheR   Rr   t   ImportErrorR   t   reducet   or_t   splitt   aR~   R   R   (    (    (    s6   /tmp/pip-build-Q3rTTP/setuptools/setuptools/sandbox.pyt   <module>   sh   


	
					pU	+