
    
_d8                        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
Z
d dlZd dlmZ d dlmZ ej                  j!                  d      rd dlmc mc mc mZ nej&                  e j.                     Z	 eZeZg dZddZej>                  dd       Z ej>                  d        Z!ej>                  d	        Z"ej>                  d
        Z# G d de$      Z% G d d      Z&ej>                  d        Z'd Z(ej>                  d        Z)ej>                  d        Z*h dZ+d Z,d Z-d Z. G d d      Z/ e0e d      re jb                  gZ2ng Z2 G d de/      Z3 ejh                  ejj                  djm                         D  cg c]  }  e7e| d        c}       Z8 G d de      Z9y# e$ r dZY 3w xY wc c} w )     N)DistutilsError)working_setjava)AbstractSandboxDirectorySandboxSandboxViolation	run_setupc                     d}t        | |      5 }|j                         }ddd       ||}t        | d      }t        |||       y# 1 sw Y   (xY w)z.
    Python 3 implementation of execfile.
    rbNexec)openreadcompiler   )filenameglobalslocalsmodestreamscriptcodes          4/usr/lib/python3/dist-packages/setuptools/sandbox.py	_execfiler   $   sY     D	h	 ~68V,Dw s   AAc              #      K   t         j                  d d  }| | t         j                  d d  	 | |t         j                  d d  y # |t         j                  d d  w xY wwN)sysargv)replsaveds     r   	save_argvr   1   sJ     HHQKEes   )AA AAAc               #      K   t         j                  d d  } 	 |  | t         j                  d d  y # | t         j                  d d  w xY wwr   )r   pathr   s    r   	save_pathr#   <   s7     HHQKEes   A/ AAAc              #      K   t        j                  | d       t        j                  }| t        _        	 d |t        _        y# |t        _        w xY ww)zL
    Monkey-patch tempfile.tempdir with replacement, ensuring it exists
    T)exist_okN)osmakedirstempfiletempdir)replacementr   s     r   override_tempr+   E   sA     
 KKd+E"H! 5s   3AA AAAc              #      K   t        j                         }t        j                  |        	 | t        j                  |       y # t        j                  |       w xY wwr   )r&   getcwdchdir)targetr   s     r   pushdr0   V   s=     IIKEHHV
s   *A!A A!AA!c                        e Zd ZdZed        Zy)UnpickleableExceptionzP
    An exception representing another Exception that could not be pickled.
    c           	          	 t        j                  |       t        j                  |      fS # t        $ r* ddlm} |j                  | |t        |                  cY S w xY w)z
        Always return a dumped (pickled) type and exc. If exc can't be pickled,
        wrap it in UnpickleableException first.
        r   )r2   )pickledumps	Exceptionsetuptools.sandboxr2   dumprepr)typeexcclss      r   r8   zUnpickleableException.dumpe   sO    	1<<%v||C'888 	1G88CT#Y00		1s   ), 0AAN)__name__
__module____qualname____doc__staticmethodr8        r   r2   r2   `   s     1 1rC   r2   c                   "    e Zd ZdZd Zd Zd Zy)ExceptionSaverz^
    A Context Manager that will save an exception, serialized, and restore it
    later.
    c                     | S r   rB   selfs    r   	__enter__zExceptionSaver.__enter__z   s    rC   c                 N    |sy t         j                  ||      | _        || _        yNT)r2   r8   _saved_tb)rH   r:   r;   tbs       r   __exit__zExceptionSaver.__exit__}   s*     ,00s; rC   c                     dt        |       vryt        t        j                  | j                        \  }}|j                  | j                        )z"restore and re-raise any exceptionrL   N)varsmapr4   loadsrL   with_tracebackrM   )rH   r:   r;   s      r   resumezExceptionSaver.resume   s@     4:%dkk2	c  **rC   N)r=   r>   r?   r@   rI   rO   rU   rB   rC   r   rE   rE   t   s    
	+rC   rE   c               #   6  K   t         j                  j                         t               5 }  ddd       t         j                  j	                         fdt         j                  D        }t        |        j                          y# 1 sw Y   ]xY ww)z
    Context in which imported modules are saved.

    Translates exceptions internal to the context into the equivalent exception
    outside the context.
    Nc              3   L   K   | ]  }|vr|j                  d       s|  yw)z
encodings.N
startswith).0mod_namer   s     r   	<genexpr>zsave_modules.<locals>.<genexpr>   s/      5 ##L1	 	s   !$)r   modulescopyrE   update_clear_modulesrU   )	saved_excdel_modulesr   s     @r   save_modulesrc      sy      KKE		 Y KKuK ; s   )BBABBBc                 F    t        |       D ]  }t        j                  |=  y r   )listr   r]   )module_namesr[   s     r   r`   r`      s"    & "KK!"rC   c               #      K   t        j                         } 	 |  t        j                  |        y # t        j                  |        w xY wwr   )pkg_resources__getstate____setstate__r"   s    r   save_pkg_resources_staterk      s:     &&(E*""5)""5)s   A2 AA		Ac              #     K   t         j                  j                  | d      }t               5  t	               5  t               5  t                t               5  t        |      5  t        |       5  t        d       d  d d d        d d d        d d d        d d d        d d d        d d d        y # 1 sw Y   2xY w# 1 sw Y   6xY w# 1 sw Y   :xY w# 1 sw Y   >xY w# 1 sw Y   BxY w# 1 sw Y   y xY ww)Ntemp
setuptools)r&   r!   joinrk   rc   r#   hide_setuptoolsr   r+   r0   
__import__)	setup_dirtemp_dirs     r   setup_contextrt      s     ww||Iv.H	!	# 	"^ 	" "![ "&x0 ""9- "&|4!""""	"	" 	"" "" "" "" "	" 	"	" 	"s   +C9C-C!CC		$B=0B1 B=C		CC! C-(	C91B:6B==CC			CCCC!!C*	&C--C62C9>   Cython	distutilsrn   rh   _distutils_hackc                 <    | j                  dd      d   }|t        v 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
    .   r   )split_MODULES_TO_HIDE)r[   base_modules     r   _needs_hidingr~      s%    " ..a(+K***rC   c                      t         j                  j                  dd      } | | j                          t	        t
        t         j                        }t        |       y)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.
    rw   N)r   r]   get_remove_shimfilterr~   r`   )rw   r]   s     r   rp   rp      sC     kkoo&7>O"$$&]CKK0G7rC   c                 |   t         j                  j                  t         j                  j                  |             }t	        |      5  	 | gt        |      z   t        j                  dd t        j                  j                  d|       t        j                          t        j                  j                  d        t        |      5  t        | d      }t        | |       ddd       ddd       y# 1 sw Y   xY w# t         $ r&}|j"                  r|j"                  d   r Y d}~?d}~ww xY w# 1 sw Y   yxY w)z8Run a distutils setup script, sandboxed in its directoryNr   c                 "    | j                         S r   )activate)dists    r   <lambda>zrun_setup.<locals>.<lambda>   s    dmmo rC   __main__)__file__r=   )r&   r!   abspathdirnamert   re   r   r   insertr   __init__	callbacksappendr   dictr   
SystemExitargs)setup_scriptr   rr   nsvs        r   r	   r	      s     =>I	y	! 	'.4:5CHHQKHHOOAy)  "!!(()EF!), ,<*E,+, , ,  	vv!&&)	 sI   D2
A?D 	C4#D 4C=	9D  	D/	D*%D2*D//D22D;c                      e Zd ZdZdZd Zd Zd Zd Zd Z	d Z
d	D ]  Z eee      s e
e       e       e<    ddZer	 ede      Z ede      ZdD ]  Z eee      s ee       e       e<    d ZdD ]  Z eee      s ee       e       e<    d ZdD ]  Z eee      s ee       e       e<    d Zd Zd Zd Zy
)r   zDWrap 'os' module and 'open()' builtin for virtualizing setup scriptsFc                     t        t              D cg c]!  }|j                  d      st        | |      r|# c}| _        y c c}w )N_)dir_osrY   hasattr_attrs)rH   names     r   r   zAbstractSandbox.__init__  s<     C
??3'GD$,? 
 
s   &Ac           	      \    | j                   D ]  }t        t        |t        ||              y r   )r   setattrr&   getattr)rH   sourcer   s      r   _copyzAbstractSandbox._copy  s(    KK 	5DBgfd34	5rC   c                     | j                  |        t        r| j                  t        _        | j                  t        _        d| _        y rK   )r   _filebuiltinsfile_openr   _activerG   s    r   rI   zAbstractSandbox.__enter__  s.    

4 JJHM

rC   c                     d| _         t        rt        t        _        t        t        _        | j                  t               y NF)r   r   r   r   r   r   r   r   )rH   exc_type	exc_value	tracebacks       r   rO   zAbstractSandbox.__exit__!  s%    !HM

3rC   c                 @    | 5   |       cddd       S # 1 sw Y   yxY w)zRun 'func' under os sandboxingNrB   )rH   funcs     r   runzAbstractSandbox.run(  s     	6	 	 	s   c                 6     t        t                fd}|S )Nc                 l    | j                   r | j                  ||g|i |\  }} ||g|i |S r   )r   _remap_pair)rH   srcdstr   kwr   originals        r   wrapz3AbstractSandbox._mk_dual_path_wrapper.<locals>.wrap0  sE    ||+4++D#sHTHRHSC2t2r22rC   r   r   r   r   r   s   ` @r   _mk_dual_path_wrapperz%AbstractSandbox._mk_dual_path_wrapper-  s    3%	3
 rC   )renamelinksymlinkNc                 >     xs t        t                fd}|S )Nc                 b    | j                   r | j                  |g|i |} |g|i |S r   )r   _remap_inputrH   r!   r   r   r   r   s       r   r   z5AbstractSandbox._mk_single_path_wrapper.<locals>.wrap>  s=    ||(t((tAdAbAD.4.2..rC   r   )r   r   r   s   `` r   _mk_single_path_wrapperz'AbstractSandbox._mk_single_path_wrapper;  s     1wsD1	/
 rC   r   r   )statlistdirr.   r   chmodchownmkdirremoveunlinkrmdirutimelchownchrootlstat	startfilemkfifomknodpathconfaccessc                 6     t        t                fd}|S )Nc                     | j                   r3 | j                  |g|i |}| j                   |g|i |      S  |g|i |S r   )r   r   _remap_outputr   s       r   r   z4AbstractSandbox._mk_single_with_return.<locals>.wrapc  s_    ||(t((tAdAbA))$0K0K0KLLD.4.2..rC   r   r   s   ` @r   _mk_single_with_returnz&AbstractSandbox._mk_single_with_return`  s    3%	/ rC   )readlinktempnamc                 6     t        t                fd}|S )Nc                 T     |i |}| j                   r| j                  |      S |S r   )r   r   )rH   r   r   retvalr   r   s       r   r   z'AbstractSandbox._mk_query.<locals>.wrapr  s1    t*r*F||))$77MrC   r   r   s   ` @r   	_mk_queryzAbstractSandbox._mk_queryo  s    3%	 rC   )r-   tmpnamc                     |S )z=Called to remap or validate any path, whether input or outputrB   )rH   r!   s     r   _validate_pathzAbstractSandbox._validate_path~  s    rC   c                 $    | j                  |      S zCalled for path inputsr   rH   	operationr!   r   r   s        r   r   zAbstractSandbox._remap_input      ""4((rC   c                 $    | j                  |      S )zCalled for path outputsr   )rH   r   r!   s      r   r   zAbstractSandbox._remap_output  r   rC   c                 j     | j                   |dz   |g|i | | j                   |dz   |g|i |fS )?Called for path pairs like rename, link, and symlink operationsz-fromz-to)r   rH   r   r   r   r   r   s         r   r   zAbstractSandbox._remap_pair  sP     Di'13DDDDi%/BtBrB
 	
rC   r   )r=   r>   r?   r@   r   r   r   rI   rO   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rB   rC   r   r   r   
  s   NG
5
 . 93248FHTN9 '6#FE2E ;* 34T:FHTN-;0	 ( :33D9FHTN:	 % -3&t_FHTN-))
rC   r   devnullc                       e Zd ZdZej                  g d      Zg Z	 efdZ	d Z
erddZddZd Zd Zd	 Zd
 Zd ZddZy)r   z<Restrict operations to a single subdirectory - pseudo-chroot)r   r   r   r   r   r   r   r   r   r   r   r   r   c                    t         j                  j                  t         j                  j                  |            | _        t         j                  j                  | j                  d      | _        |D cg c]>  }t         j                  j                  t         j                  j                  |            @ c}| _        t        j                  |        y c c}w )N )
r&   r!   normcaserealpath_sandboxro   _prefix_exceptionsr   r   )rH   sandbox
exceptionsr!   s       r   r   zDirectorySandbox.__init__  s    (()9)9')BCww||DMM26AK
9=BGGRWW--d34
 	  &
s   5ACc                 "    ddl m}  ||||      )Nr   )r   )r7   r   )rH   r   r   r   r   s        r   
_violationzDirectorySandbox._violation  s    7y$33rC   c                 |    |dvr)| j                  |      s | j                  d||g|i | t        ||g|i |S )Nrrtr   rUUr   )_okr   r   rH   r!   r   r   r   s        r   r   zDirectorySandbox._file  sH    77d@T@R@t1d1b11rC   c                 |    |dvr)| j                  |      s | j                  d||g|i | t        ||g|i |S )Nr   r   )r   r   r   r   s        r   r   zDirectorySandbox._open  sH    33DHHTNDOOFD$<<<T4-$-"--rC   c                 &    | j                  d       y )Nr   )r   rG   s    r   r   zDirectorySandbox.tmpnam  s    !rC   c                 H   | j                   }	 d| _         t        j                  j                  t        j                  j	                  |            }| j                  |      xs, || j                  k(  xs |j                  | j                        	 || _         S # || _         w xY wr   )	r   r&   r!   r   r   	_exemptedr   rY   r   )rH   r!   activer   s       r   r   zDirectorySandbox._ok  s    		" DLww''(8(8(>?Hx( 5t}},5&&t||4 "DL6DLs   BB 	B!c                     fd| j                   D        }fd| j                  D        }t        j                  ||      }t	        |      S )Nc              3   @   K   | ]  }j                  |        y wr   rX   )rZ   	exceptionfilepaths     r   r\   z-DirectorySandbox._exempted.<locals>.<genexpr>  s!      
/8H	*
s   c              3   J   K   | ]  }t        j                  |        y wr   )rematch)rZ   patternr	  s     r   r\   z-DirectorySandbox._exempted.<locals>.<genexpr>  s!      
,3BHHWh'
s    #)r   _exception_patterns	itertoolschainany)rH   r	  start_matchespattern_matches
candidatess    `   r   r  zDirectorySandbox._exempted  sI    
<@<L<L

7;7O7O
 __]OD
:rC   c                     || j                   v rE| j                  |      s4 | j                  |t        j                  j                  |      g|i | |S r   )	write_opsr   r   r&   r!   r   r   s        r   r   zDirectorySandbox._remap_input  sF    &txx~DOOIrww'7'7'=KKKrC   c                 ~    | j                  |      r| j                  |      s | j                  |||g|i | ||fS )r   )r   r   r   s         r   r   zDirectorySandbox._remap_pair  s=    xx}DHHSMDOOIsC=$="=SzrC   c                     |t         z  r*| j                  |      s | j                  d|||g|i | t        j                  |||g|i |S )zCalled for low-level os.open()zos.open)WRITE_FLAGSr   r   r   r   )rH   r   flagsr   r   r   s         r   r   zDirectorySandbox.open  sO    ;txx~DOOItUDF4F2FxxeT7D7B77rC   N)r   )i  )r=   r>   r?   r@   r   fromkeysr  r  _EXCEPTIONSr   r   r   r   r   r   r  r   r   r   rB   rC   r   r   r     s]    F	
I$ 4+6 '4
 	2
.
""8rC   r   z4O_WRONLY O_RDWR O_APPEND O_CREAT O_TRUNC O_TEMPORARYc                   V    e Zd ZdZ ej
                  d      j                         Zd Zy)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                 j    | j                   \  }}} | j                  j                  di t               S )NrB   )r   tmplformatr   )rH   cmdr   kwargss       r   __str__zSandboxViolation.__str__  s.     IIT6tyy+&(++rC   N)	r=   r>   r?   r@   textwrapdedentlstripr  r#  rB   rC   r   r   r     s+    O8??
	 fh 	,rC   r   r   ):r&   r   r(   operator	functoolsr  r  
contextlibr4   r$  r   rh   distutils.errorsr   r   platformrY   $org.python.modules.posix.PosixModulepythonr]   posixPosixModuler   r   r   r   	NameErrorr   r   __all__r   contextmanagerr   r#   r+   r0   r6   r2   rE   rc   r`   rk   rt   r|   r~   rp   r	   r   r   r   r  r   reduceor_r{   r   r  r   )as   0r   <module>r6     s   	 
     	      + %<<6"66
++bgg
CE 	
      ! !   1I 1(+ +<  2"
 * * " " +*(E
 E
P 2y::,KK[8 [8| iLL HMMO 	Q,~ ,Q  EBs   F- 	F;
-F87F8