ó
¨9Vc           @   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	 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 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 „  Z/ d „  Z0 d d# d „  ƒ  YZ1 e2 e  d ƒ r<e  j3 g Z4 n g  Z4 y' d  d l5 m6 Z6 e4 j7 e6 ƒ  ƒ [6 Wn e8 k
 r|n Xd e1 f d „  ƒ  YZ9 e j: e j; g  d j< ƒ  D] Z= e> e e= d  ƒ ^ q¬ƒ Z? d	 e f d! „  ƒ  YZ@ d S($   iÿÿÿÿNt   java(   t   DistutilsError(   t   working_set(   t   compat(   t   builtinst   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
 ƒ } | | | f 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-5Z5nTX/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-5Z5nTX/setuptools/setuptools/sandbox.pyt	   save_argv1   s    
	c          c   s%   t  j }  z	 |  VWd  |  t  j (Xd  S(   N(   R   t   path(   R   (    (    s6   /tmp/pip-build-5Z5nTX/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-5Z5nTX/setuptools/setuptools/sandbox.pyt   override_tempE   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-5Z5nTX/setuptools/setuptools/sandbox.pyt   pushdW   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   sQ   y  t  j | ƒ t  j | ƒ f SWn* t k
 rL |  j |  |  t | ƒ ƒ ƒ SXd S(   s‰   
        Always return a dumped (pickled) type and exc. If exc can't be pickled,
        wrap it in UnpickleableException first.
        N(   t   picklet   dumpst	   Exceptiont   dumpt   repr(   t   clst   typet   exc(    (    s6   /tmp/pip-build-5Z5nTX/setuptools/setuptools/sandbox.pyR/   e   s     (   t   __name__t
   __module__t   __doc__t   classmethodR/   (    (    (    s6   /tmp/pip-build-5Z5nTX/setuptools/setuptools/sandbox.pyR+   a   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-5Z5nTX/setuptools/setuptools/sandbox.pyt	   __enter__v   s    c         C   s,   | s
 d  St  j | | ƒ |  _ | |  _ t S(   N(   R+   R/   t   _savedt   _tbt   True(   R9   R2   R3   t   tb(    (    s6   /tmp/pip-build-5Z5nTX/setuptools/setuptools/sandbox.pyt   __exit__y   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   varst   mapR,   t   loadsR;   R   t   reraiseR<   (   R9   R2   R3   (    (    s6   /tmp/pip-build-5Z5nTX/setuptools/setuptools/sandbox.pyt   resume„   s    (   R4   R5   R6   R:   R?   RD   (    (    (    s6   /tmp/pip-build-5Z5nTX/setuptools/setuptools/sandbox.pyR8   q   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-5Z5nTX/setuptools/setuptools/sandbox.pys	   <genexpr>   s    (   R   t   modulest   copyR8   t   updatet   _clear_modulesRD   (   t	   saved_exct   del_modules(    (   R   s6   /tmp/pip-build-5Z5nTX/setuptools/setuptools/sandbox.pyt   save_modulesŽ   s    
c         C   s%   x t  |  ƒ D] } t j | =q Wd  S(   N(   t   listR   RH   (   t   module_namesRG   (    (    s6   /tmp/pip-build-5Z5nTX/setuptools/setuptools/sandbox.pyRK   §   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-5Z5nTX/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   joinRT   RN   t   hide_setuptoolsR   R   R&   R*   t
   __import__(   t	   setup_dirt   temp_dir(    (    s6   /tmp/pip-build-5Z5nTX/setuptools/setuptools/sandbox.pyt   setup_contextµ   s    




c         C   s"   t  j d ƒ } t | j |  ƒ ƒ S(   sù   
    >>> _needs_hiding('setuptools')
    True
    >>> _needs_hiding('pkg_resources')
    True
    >>> _needs_hiding('setuptools_plugin')
    False
    >>> _needs_hiding('setuptools.__init__')
    True
    >>> _needs_hiding('distutils')
    True
    s*   (setuptools|pkg_resources|distutils)(\.|$)(   t   reR   t   boolt   match(   RG   t   pattern(    (    s6   /tmp/pip-build-5Z5nTX/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   filterRa   R   RH   RK   (   RH   (    (    s6   /tmp/pip-build-5Z5nTX/setuptools/setuptools/sandbox.pyRX   Õ   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-5Z5nTX/setuptools/setuptools/sandbox.pyt   <lambda>é   s    c             s&   t  d ˆ  d d ƒ }  t ˆ  |  ƒ d  S(   Nt   __file__R4   t   __main__(   t   dictR   (   t   ns(   t   setup_script(    s6   /tmp/pip-build-5Z5nTX/setuptools/setuptools/sandbox.pyt   runnerê   s    N(   R    R   t   abspatht   dirnameR\   RO   R   R   t   insertR   t   __init__t	   callbackst   appendR   t   runt
   SystemExitt   args(   Rj   Rt   RZ   Rk   t   v(    (   Rj   s6   /tmp/pip-build-5Z5nTX/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   _osRE   t   hasattrt   _attrs(   R9   t   name(    (    s6   /tmp/pip-build-5Z5nTX/setuptools/setuptools/sandbox.pyRo   ù   s    c         C   s1   x* |  j  D] } t t | t | | ƒ ƒ q
 Wd  S(   N(   Rz   t   setattrR    t   getattr(   R9   t   sourceR{   (    (    s6   /tmp/pip-build-5Z5nTX/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   FalseRx   (   R9   t   func(    (    s6   /tmp/pip-build-5Z5nTX/setuptools/setuptools/sandbox.pyRr     s    			c            s%   t  t ˆ  ƒ ‰ ‡  ‡ f d †  } | S(   Nc            s@   |  j  r- |  j ˆ  | | | | Ž \ } } n  ˆ | | | | Ž S(   N(   Rƒ   t   _remap_pair(   R9   t   srct   dstRt   t   kw(   R{   t   original(    s6   /tmp/pip-build-5Z5nTX/setuptools/setuptools/sandbox.pyt   wrap  s    	$(   R}   Rx   (   R{   R‹   (    (   R{   RŠ   s6   /tmp/pip-build-5Z5nTX/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(   R9   R   Rt   R‰   (   R{   RŠ   (    s6   /tmp/pip-build-5Z5nTX/setuptools/setuptools/sandbox.pyR‹      s    	(   R}   Rx   (   R{   RŠ   R‹   (    (   R{   RŠ   s6   /tmp/pip-build-5Z5nTX/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(   R9   R   Rt   R‰   (   R{   RŠ   (    s6   /tmp/pip-build-5Z5nTX/setuptools/setuptools/sandbox.pyR‹   2  s    	(   R}   Rx   (   R{   R‹   (    (   R{   RŠ   s6   /tmp/pip-build-5Z5nTX/setuptools/setuptools/sandbox.pyt   _mk_single_with_return0  s    t   readlinkt   tempnamc            s%   t  t ˆ  ƒ ‰ ‡  ‡ f d †  } | S(   Nc            s,   ˆ | | Ž  } |  j  r( |  j ˆ  | ƒ S| S(   N(   Rƒ   R£   (   R9   Rt   R‰   t   retval(   R{   RŠ   (    s6   /tmp/pip-build-5Z5nTX/setuptools/setuptools/sandbox.pyR‹   >  s    	(   R}   Rx   (   R{   R‹   (    (   R{   RŠ   s6   /tmp/pip-build-5Z5nTX/setuptools/setuptools/sandbox.pyt	   _mk_query<  s    R'   t   tmpnamc         C   s   | S(   s=   Called to remap or validate any path, whether input or output(    (   R9   R   (    (    s6   /tmp/pip-build-5Z5nTX/setuptools/setuptools/sandbox.pyt   _validate_pathH  s    c         O   s   |  j  | ƒ S(   s   Called for path inputs(   Rª   (   R9   t	   operationR   Rt   R‰   (    (    s6   /tmp/pip-build-5Z5nTX/setuptools/setuptools/sandbox.pyR   L  s    c         C   s   |  j  | ƒ S(   s   Called for path outputs(   Rª   (   R9   R«   R   (    (    s6   /tmp/pip-build-5Z5nTX/setuptools/setuptools/sandbox.pyR£   P  s    c         O   s6   |  j  | d | | | Ž |  j  | d | | | Ž f S(   s?   Called for path pairs like rename, link, and symlink operationss   -froms   -to(   R   (   R9   R«   R‡   Rˆ   Rt   R‰   (    (    s6   /tmp/pip-build-5Z5nTX/setuptools/setuptools/sandbox.pyR†   T  s    N(   R4   R5   R6   R„   Rƒ   Ro   R   Rr   RŒ   R{   Ry   Rx   R   R   R‘   R€   R‚   R¤   R¨   Rª   R   R£   R†   (    (    (    s6   /tmp/pip-build-5Z5nTX/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   _sandboxRW   t   _prefixt   _exceptionsR   Ro   (   R9   t   sandboxt
   exceptionsR   (    (    s6   /tmp/pip-build-5Z5nTX/setuptools/setuptools/sandbox.pyRo   w  s
    !1c         O   s   t  | | | ƒ ‚ d  S(   N(   R   (   R9   R«   Rt   R‰   (    (    s6   /tmp/pip-build-5Z5nTX/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€   (   R9   R   R   Rt   R‰   (    (    s6   /tmp/pip-build-5Z5nTX/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‚   (   R9   R   R   Rt   R‰   (    (    s6   /tmp/pip-build-5Z5nTX/setuptools/setuptools/sandbox.pyR‚   ‰  s    c         C   s   |  j  d ƒ d  S(   NR©   (   R¶   (   R9   (    (    s6   /tmp/pip-build-5Z5nTX/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±   RE   R²   (   R9   R   t   activeR°   (    (    s6   /tmp/pip-build-5Z5nTX/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(   RE   (   RF   t	   exception(   t   filepath(    s6   /tmp/pip-build-5Z5nTX/setuptools/setuptools/sandbox.pys	   <genexpr>   s   c         3   s!   |  ] } t  j | ˆ  ƒ Vq d  S(   N(   R]   R_   (   RF   R`   (   R¿   (    s6   /tmp/pip-build-5Z5nTX/setuptools/setuptools/sandbox.pys	   <genexpr>¤  s   (   R³   t   _exception_patternst	   itertoolst   chaint   any(   R9   R¿   t   start_matchest   pattern_matchest
   candidates(    (   R¿   s6   /tmp/pip-build-5Z5nTX/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°   (   R9   R«   R   Rt   R‰   (    (    s6   /tmp/pip-build-5Z5nTX/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¶   (   R9   R«   R‡   Rˆ   Rt   R‰   (    (    s6   /tmp/pip-build-5Z5nTX/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¶   Rx   R   (   R9   R   t   flagsR   Rt   R‰   (    (    s6   /tmp/pip-build-5Z5nTX/setuptools/setuptools/sandbox.pyR   ¶  s    (   R4   R5   R6   Rh   t   fromkeysRÇ   RÀ   t   _EXCEPTIONSRo   R¶   R€   R‚   R©   R»   R¼   R   R†   R   (    (    (    s6   /tmp/pip-build-5Z5nTX/setuptools/setuptools/sandbox.pyR   i  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.(   Rt   (   R9   (    (    s6   /tmp/pip-build-5Z5nTX/setuptools/setuptools/sandbox.pyt   __str__Ä  s    	(   R4   R5   R6   RÌ   (    (    (    s6   /tmp/pip-build-5Z5nTX/setuptools/setuptools/sandbox.pyR   Á  s   (    (    (A   R    R   R#   t   operatort	   functoolsRÁ   R]   t
   contextlibR,   RQ   t   platformRE   t$   org.python.modules.posix.PosixModulet   pythonRH   t   posixt   PosixModuleRx   R{   R   R€   t	   NameErrorR   R   R‚   t   distutils.errorsR   R   RV   R   t   setuptools.compatR   t   __all__R   t   contextmanagerR   R   R&   R*   R.   R+   R8   RN   RK   RT   R\   Ra   RX   R   R   Ry   R¬   RË   t   win32com.client.gencacheR­   Rq   t   ImportErrorR   t   reducet   or_t   splitt   aR}   RÈ   R   (    (    (    s6   /tmp/pip-build-5Z5nTX/setuptools/setuptools/sandbox.pyt   <module>   sh   


	
					hS	+