
 X
'                 @   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 e  j d k r d  d l	 j
 j j j Z n e j e  j Z y
 e Z Wn e k
 r d Z Yn Xe Z d  d l m Z d  d l m Z d  d l m Z m Z d d d d	 g Z d
 d	   Z Gd d   d  Z e e  d  rJe  j g Z n g  Z y' d  d l  m! Z! e j" e!    [! Wn e# k
 rYn XGd d   d e  Z$ e j% e j& d d   d j'   D  Z( Gd d   d e  Z) d S)    Njava)DistutilsError)working_set)builtinsexecfileAbstractSandboxDirectorySandboxSandboxViolation	run_setupc                s  t  j   } t j d d  } t j d d  } t  j j t  j j    } t  j j | d  } t  j j |  s t  j	 |  n  t
 j } t j j     t j   } z | t
 _ t  j |  yv  g t |  t j d d  <t j j d |  t j   t j j d d    t |  j  f d d    Wn> t k
 rzt j   d }	 |	 j rv|	 j d rv  n  Yn XWd t j |  t j j      f d d	   t j D }
 t t t j j |
   t  j |  | t j d d  <| t j d d  <| t
 _ Xd S)
z8Run a distutils setup script, sandboxed in its directoryNtempr   c             S   s
   |  j    S)N)activate)dist r   D/home/ubuntu/projects/ifolica/build/setuptools/setuptools/sandbox.py<lambda>0   s    zrun_setup.<locals>.<lambda>c                  s   t  d i   d 6d d 6 S)Nzsetup.py__file____main____name__)r   r   )setup_scriptr   r   r   2   s      c                s2   g  |  ]( } |   k r | j  d   r |  q S)z
encodings.)
startswith).0mod_name)save_modulesr   r   
<listcomp>A   s   	 zrun_setup.<locals>.<listcomp>) osgetcwdsysargvpathabspathdirnamejoinisdirmakedirstempfiletempdirmodulescopypkg_resources__getstate__chdirlistinsertr   __init__	callbacksappendr   run
SystemExitexc_infoargs__setstate__updatemap__delitem__)r   r4   Zold_dir	save_argvZ	save_pathZ	setup_dirtemp_dirZsave_tmpZpr_statevZdel_modulesr   )r   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 x< d d d g D]+ Z	 e
 e e	  rX e e	  e   e	 <qX qX Wd d 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% d&   Z x9 d' d( g D]+ Z	 e
 e e	  rEe e	  e   e	 <qEqEWd) d*   Z x9 d+ d, g D]+ Z	 e
 e e	  re e	  e   e	 <qqWd- d.   Z d/ d0   Z d1 d2   Z d3 d4   Z d S)5r   zDWrap 'os' module and 'open()' builtin for virtualizing setup scriptsFc                s&     f d d   t  t  D   _ d  S)Nc                s5   g  |  ]+ } | j  d   r t   |  r |  q S)_)r   hasattr)r   name)selfr   r   r   T   s   	 z,AbstractSandbox.__init__.<locals>.<listcomp>)dir_os_attrs)r?   r   )r?   r   r.   R   s    zAbstractSandbox.__init__c             C   s1   x* |  j  D] } t t | t | |   q
 Wd  S)N)rB   setattrr   getattr)r?   sourcer>   r   r   r   _copyX   s    zAbstractSandbox._copyc             C   s{   zB |  j  |   t r% |  j t _ n  |  j t _ d |  _ |   SWd d |  _ t r` t t _ n  t t _ |  j  t  Xd S)zRun 'func' under os sandboxingTNF)rF   _filer   file_openopen_activerA   )r?   funcr   r   r   r1   \   s    			zAbstractSandbox.runc                s(   t  t        f d d   } | S)Nc                s@   |  j  r- |  j   | | | |  \ } } n   | | | |  S)N)rK   _remap_pair)r?   srcdstr4   kw)r>   originalr   r   wrapn   s    	$z3AbstractSandbox._mk_dual_path_wrapper.<locals>.wrap)rD   rA   )r>   rR   r   )r>   rQ   r   _mk_dual_path_wrapperl   s    z%AbstractSandbox._mk_dual_path_wrapperrenamelinksymlinkNc                s.    p t  t        f d d   } | S)Nc                s4   |  j  r$ |  j   | | |  } n   | | |  S)N)rK   _remap_input)r?   r   r4   rP   )r>   rQ   r   r   rR   y   s    	z5AbstractSandbox._mk_single_path_wrapper.<locals>.wrap)rD   rA   )r>   rQ   rR   r   )r>   rQ   r   _mk_single_path_wrapperw   s    z'AbstractSandbox._mk_single_path_wrapperrH   rJ   statlistdirr+   chmodchownmkdirremoveunlinkrmdirutimelchownchrootlstatZ	startfilemkfifomknodpathconfaccessc                s(   t  t        f d d   } | S)Nc                sM   |  j  r= |  j   | | |  } |  j    | | |   S | | |  S)N)rK   rW   _remap_output)r?   r   r4   rP   )r>   rQ   r   r   rR      s    	z4AbstractSandbox._mk_single_with_return.<locals>.wrap)rD   rA   )r>   rR   r   )r>   rQ   r   _mk_single_with_return   s    z&AbstractSandbox._mk_single_with_returnreadlinktempnamc                s(   t  t        f d d   } | S)Nc                s,    | |   } |  j  r( |  j   |  S| S)N)rK   ri   )r?   r4   rP   retval)r>   rQ   r   r   rR      s    	z'AbstractSandbox._mk_query.<locals>.wrap)rD   rA   )r>   rR   r   )r>   rQ   r   	_mk_query   s    zAbstractSandbox._mk_queryr   tmpnamc             C   s   | S)z=Called to remap or validate any path, whether input or outputr   )r?   r   r   r   r   _validate_path   s    zAbstractSandbox._validate_pathc             O   s   |  j  |  S)zCalled for path inputs)rp   )r?   	operationr   r4   rP   r   r   r   rW      s    zAbstractSandbox._remap_inputc             C   s   |  j  |  S)zCalled for path outputs)rp   )r?   rq   r   r   r   r   ri      s    zAbstractSandbox._remap_outputc             O   s6   |  j  | d | | |  |  j  | d | | |  f S)z?Called for path pairs like rename, link, and symlink operationsz-fromz-to)rW   )r?   rq   rN   rO   r4   rP   r   r   r   rM      s    zAbstractSandbox._remap_pair)r   
__module____qualname____doc__rK   r.   rF   r1   rS   r>   r=   rA   localsrX   rG   rI   rj   rn   rp   rW   ri   rM   r   r   r   r   r   M   s>     	 	 devnull)GetGeneratePathc               @   s   e  Z d  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 d  Z	 d d   Z
 e r d d d  Z n  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-chrootrJ   r[   r\   r]   r^   r_   r`   ra   rb   rc   re   rf   rl   z.*lib2to3.*\.pickle$c             C   sc   t  j j t  j j |   |  _ t  j j |  j d  |  _ d d   | D |  _ t j	 |   d  S)N c             S   s.   g  |  ]$ } t  j j t  j j |    q Sr   )r   r   normcaserealpath)r   r   r   r   r   r      s   	z-DirectorySandbox.__init__.<locals>.<listcomp>)
r   r   ry   rz   _sandboxr"   _prefix_exceptionsr   r.   )r?   Zsandbox
exceptionsr   r   r   r.      s
    !	zDirectorySandbox.__init__c             O   s   t  | | |   d  S)N)r	   )r?   rq   r4   rP   r   r   r   
_violation   s    zDirectorySandbox._violationrc             O   sK   | d k r8 |  j  |  r8 |  j d | | | |  n  t | | | |  S)Nr   rtrbrUUrH   )r   zrtzrbzrUr   )_okr   rG   )r?   r   moder4   rP   r   r   r   rG      s    zDirectorySandbox._filec             O   sK   | d k r8 |  j  |  r8 |  j d | | | |  n  t | | | |  S)Nr   r   r   r   r   rJ   )r   zrtzrbzrUr   )r   r   rI   )r?   r   r   r4   rP   r   r   r   rI      s    zDirectorySandbox._openc             C   s   |  j  d  d  S)Nro   )r   )r?   r   r   r   ro      s    zDirectorySandbox.tmpnamc             C   ss   |  j  } zY d |  _  t j j t j j |   } |  j |  p` | |  j k p` | j |  j  SWd  | |  _  Xd  S)NF)	rK   r   r   ry   rz   	_exemptedr{   r   r|   )r?   r   Zactiverz   r   r   r   r      s    		zDirectorySandbox._okc                sT     f d d   |  j  D }   f d d   |  j D } t j | |  } t |  S)Nc             3   s   |  ] }   j  |  Vq d  S)N)r   )r   	exception)filepathr   r   	<genexpr>   s   z-DirectorySandbox._exempted.<locals>.<genexpr>c             3   s!   |  ] } t  j |    Vq d  S)N)rematch)r   pattern)r   r   r   r      s   )r}   _exception_patterns	itertoolschainany)r?   r   Zstart_matchesZpattern_matchesZ
candidatesr   )r   r   r      s    zDirectorySandbox._exemptedc             O   sH   | |  j  k rD |  j |  rD |  j | t j j |  | |  n  | S)zCalled for path inputs)	write_opsr   r   r   r   rz   )r?   rq   r   r4   rP   r   r   r   rW     s    %zDirectorySandbox._remap_inputc             O   sF   |  j  |  s  |  j  |  r< |  j | | | | |  n  | | f S)z?Called for path pairs like rename, link, and symlink operations)r   r   )r?   rq   rN   rO   r4   rP   r   r   r   rM   	  s     zDirectorySandbox._remap_pairi  c             O   sR   | t  @r9 |  j |  r9 |  j d | | | | |  n  t j | | | | |  S)zCalled for low-level os.open()zos.open)WRITE_FLAGSr   r   rA   rJ   )r?   rH   flagsr   r4   rP   r   r   r   rJ     s    zDirectorySandbox.openN)r   rr   rs   rt   dictfromkeysr   r   _EXCEPTIONSr.   r   rG   rI   ro   r   r   rW   rM   rJ   r   r   r   r   r      s    		c             C   s"   g  |  ] } t  t | d    q S)r   )rD   rA   )r   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 d d   Z d S)r	   zEA setup script attempted to modify the filesystem outside the sandboxc             C   s   d |  j  S)Na  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.)r4   )r?   r   r   r   __str__  s    	zSandboxViolation.__str__N)r   rr   rs   rt   r   r   r   r   r   r	     s   )*r   r   r%   operator	functoolsr   r   r)   r>   Z$org.python.modules.posix.PosixModulepythonr'   posixZPosixModulerA   rH   rG   	NameErrorrJ   rI   distutils.errorsr   r   Zsetuptools.compatr   r   __all__r
   r   r=   rv   r   Zwin32com.client.gencacherw   r0   ImportErrorr   reduceor_splitr   r	   r   r   r   r   <module>   sF   
0hS