o
    (g8                     @   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
Z
d dlZd dlmZ d dlmZ ejdrRd dlm  m  m  mZ neje j ZzeZW n eyg   dZY nw eZg d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*h dZ+dd Z,dd  Z-d!d" Z.G d#d$ d$Z/e0e d%re j1gZ2ng Z2G d&d' d'e/Z3e4ej5d(d) d*6 D Z7G d+d, d,eZ8dS ).    N)DistutilsError)working_setjava)AbstractSandboxDirectorySandboxSandboxViolation	run_setupc                 C   s^   d}t | |}| }W d   n1 sw   Y  |du 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   Z/var/www/python.lazyprojects.co.uk/venv/lib/python3.10/site-packages/setuptools/sandbox.py	_execfile$   s   
r   c              
   c   sR    t jd d  }| d ur| t jd d < z|V  W |t jd d < d S |t jd d < w N)sysargv)replsavedr   r   r   	save_argv1   s   "r   c               
   c   s<    t jd d  } z| V  W | t jd d < d S | t jd d < w r   )r   pathr   r   r   r   	save_path<   s
   "r    c                 c   s8    t j| dd tj}| t_z	dV  W |t_dS |t_w )zL
    Monkey-patch tempfile.tempdir with replacement, ensuring it exists
    T)exist_okN)osmakedirstempfiletempdir)replacementr   r   r   r   override_tempE   s   r'   c              	   c   s8    t  }t |  z|V  W t | d S t | w r   )r"   getcwdchdir)targetr   r   r   r   pushdV   s   
r+   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   sJ   zt | t |fW S  ty$   ddlm} |||t| Y S w )z
        Always return a dumped (pickled) type and exc. If exc can't be pickled,
        wrap it in UnpickleableException first.
        r   )r,   )pickledumps	Exceptionsetuptools.sandboxr,   dumprepr)typeexcclsr   r   r   r1   e   s   zUnpickleableException.dumpN)__name__
__module____qualname____doc__staticmethodr1   r   r   r   r   r,   `   s    r,   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__z   s   zExceptionSaver.__enter__c                 C   s    |sd S t ||| _|| _dS NT)r,   r1   _saved_tb)r=   r3   r4   tbr   r   r   __exit__}   s
   zExceptionSaver.__exit__c                 C   s.   dt | vrdS ttj| j\}}|| j)z"restore and re-raise any exceptionr@   N)varsmapr-   loadsr@   with_tracebackrA   )r=   r3   r4   r   r   r   resume   s   zExceptionSaver.resumeN)r6   r7   r8   r9   r>   rC   rH   r   r   r   r   r;   t   s
    r;   c                  #   sl    t j  t }  V  W d   n1 sw   Y  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.
    Nc                 3   s&    | ]}| vr| d s|V  qdS )z
encodings.N
startswith).0mod_namer   r   r   	<genexpr>   s    zsave_modules.<locals>.<genexpr>)r   modulescopyr;   update_clear_modulesrH   )	saved_excdel_modulesr   r   r   save_modules   s   

rT   c                 C   s   t | D ]}tj|= qd S r   )listr   rN   )module_namesrL   r   r   r   rQ      s   
rQ   c               	   c   s.    t  } z| V  W t |  d S t |  w r   )pkg_resources__getstate____setstate__r   r   r   r   save_pkg_resources_state   s
   rZ   c                 c   s"   t j| d}t | t ` t M t  t 7 t|# t	|  t
d d V  W d    n1 s6w   Y  W d    n1 sEw   Y  W d    n1 sTw   Y  W d    n1 scw   Y  W d    n1 srw   Y  W d    d S W d    d S 1 sw   Y  d S )Ntemp
setuptools)r"   r   joinrZ   rT   r    hide_setuptoolsr   r'   r+   
__import__)	setup_dirtemp_dirr   r   r   setup_context   s.   

"rb   >   Cython	distutilsr\   rW   _distutils_hackc                 C   s   |  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)rL   base_moduler   r   r   _needs_hiding   s   rk   c                  C   s6   t jdd} | dur|   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.
    re   N)r   rN   getremove_shimfilterrk   rQ   )re   rN   r   r   r   r^      s
   r^   c                 C   s
  t jt j| }t|m z@| gt| tjdd< tjd| t	
  t	jdd  t| t| dd}t| | W d   n1 sIw   Y  W n tyj } z|jr`|jd r` W Y d}~nd}~ww W d   dS W d   dS 1 s~w   Y  dS )z8Run a distutils setup script, sandboxed in its directoryNr   c                 S   s   |   S r   )activate)distr   r   r   <lambda>   s    zrun_setup.<locals>.<lambda>__main__)__file__r6   )r"   r   abspathdirnamerb   rU   r   r   insertr   __init__	callbacksappendr   dictr   
SystemExitargs)setup_scriptr|   r`   nsvr   r   r   r      s*   

" r   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r0e
ee e< q"d$ddZer=edeZedeZdD ]ZeeerReee e< qDdd ZdD ]Zeeergeee e< qYdd ZdD ]Zeeer|eee e< qnd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 )_)rJ   hasattr)rK   namer<   r   r   
<listcomp>  s    z,AbstractSandbox.__init__.<locals>.<listcomp>)dir_os_attrsr<   r   r<   r   rw     s   
zAbstractSandbox.__init__c                 C   s"   | j D ]}tt|t|| qd S r   )r   setattrr"   getattr)r=   sourcer   r   r   r   _copy  s   
zAbstractSandbox._copyc                 C   s(   |  |  tr| jt_| jt_d| _d S r?   )r   _filebuiltinsfile_openr   _activer<   r   r   r   r>     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   )r=   exc_type	exc_value	tracebackr   r   r   rC   !  s
   zAbstractSandbox.__exit__c                 C   s.   |  | W  d   S 1 sw   Y  dS )zRun 'func' under os sandboxingNr   )r=   funcr   r   r   run(  s   $zAbstractSandbox.runc                       t t  fdd}|S )Nc                    s>   | j r| j ||g|R i |\}}||g|R i |S r   )r   _remap_pair)r=   srcdstr|   kwr   originalr   r   wrap0  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                    s6   | j r| j |g|R i |}|g|R i |S r   )r   _remap_inputr=   r   r|   r   r   r   r   r   >  s   z5AbstractSandbox._mk_single_path_wrapper.<locals>.wrapr   )r   r   r   r   r   r   _mk_single_path_wrapper;  s   z'AbstractSandbox._mk_single_path_wrapperr   r   )statlistdirr)   r   chmodchownmkdirremoveunlinkrmdirutimelchownchrootlstat	startfilemkfifomknodpathconfaccessc                    r   )Nc                    sT   | j r| j |g|R i |}|  |g|R i |S |g|R i |S r   )r   r   _remap_outputr   r   r   r   r   c  s   z4AbstractSandbox._mk_single_with_return.<locals>.wrapr   r   r   r   r   _mk_single_with_return`     
z&AbstractSandbox._mk_single_with_return)readlinktempnamc                    r   )Nc                    s$   |i |}| j r|  |S |S r   )r   r   )r=   r|   r   retvalr   r   r   r   r  s   z'AbstractSandbox._mk_query.<locals>.wrapr   r   r   r   r   	_mk_queryo  r   zAbstractSandbox._mk_query)r(   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 zCalled for path inputsr   r=   	operationr   r|   r   r   r   r   r        
zAbstractSandbox._remap_inputc                 C   r   )zCalled for path outputsr   )r=   r   r   r   r   r   r     r   zAbstractSandbox._remap_outputc                 O   s<   | j |d |g|R i || j |d |g|R i |fS )?Called for path pairs like rename, link, and symlink operationsz-fromz-to)r   r=   r   r   r   r|   r   r   r   r   r     s   zAbstractSandbox._remap_pairr   )r6   r7   r8   r9   r   rw   r   r>   rC   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   
  sF    








r   devnullc                   @   s   e Zd ZdZeg dZg Z	 efddZ	dd Z
er!d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-chroot)r   r   r   r   r   r   r   r   r   r   r   r   r   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)rK   r   r   r   r   r     s    z-DirectorySandbox.__init__.<locals>.<listcomp>)
r"   r   r   r   _sandboxr]   _prefix_exceptionsr   rw   )r=   sandbox
exceptionsr   r   r   rw     s   zDirectorySandbox.__init__c                 O   s   ddl m} ||||)Nr   )r   )r0   r   )r=   r   r|   r   r   r   r   r   
_violation  s   zDirectorySandbox._violationrc                 O   F   |dvr|  |s| jd||g|R i | t||g|R i |S )Nr   rtr	   rUUr   )_okr   r   r=   r   r   r|   r   r   r   r   r        zDirectorySandbox._filec                 O   r   )Nr   r   )r   r   r   r   r   r   r   r     r   zDirectorySandbox._openc                 C   s   |  d d S )Nr   )r   r<   r   r   r   r     s   zDirectorySandbox.tmpnamc                 C   sR   | j }z!d| _ tjtj|}| |p || jkp || jW || _ S || _ w r   )	r   r"   r   r   r   	_exemptedr   rJ   r   )r=   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   rI   )rK   	exceptionfilepathr   r   rM     s    

z-DirectorySandbox._exempted.<locals>.<genexpr>c                 3   s    | ]	}t | V  qd S r   )rematch)rK   patternr   r   r   rM     s    
)r   _exception_patterns	itertoolschainany)r=   r   start_matchespattern_matches
candidatesr   r   r   r     s   

zDirectorySandbox._exemptedc                 O   s:   || j v r| |s| j|tj|g|R i | |S r   )	write_opsr   r   r"   r   r   r   r   r   r   r     s   "zDirectorySandbox._remap_inputc                 O   s8   |  |r
|  |s| j|||g|R i | ||fS )r   )r   r   r   r   r   r   r     s   zDirectorySandbox._remap_pair  c                 O   sL   |t @ r| |s| jd|||g|R i | tj|||g|R i |S )zCalled for low-level os.open()zos.open)WRITE_FLAGSr   r   r   r   )r=   r   flagsr   r|   r   r   r   r   r     s   zDirectorySandbox.openN)r   )r   )r6   r7   r8   r9   rz   fromkeysr   r   _EXCEPTIONSrw   r   r   r   r   r   r   r   r   r   r   r   r   r   r     s$    


r   c                 C   s   g | ]}t t|d qS )r   r   )rK   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di t S )Nr   )r|   tmplformatr   )r=   cmdr|   kwargsr   r   r   __str__  s   zSandboxViolation.__str__N)	r6   r7   r8   r9   textwrapdedentlstripr   r   r   r   r   r   r     s    r   r   )9r"   r   r$   operator	functoolsr   r   
contextlibr-   r   r   rW   distutils.errorsr   r   platformrJ   $org.python.modules.posix.PosixModulepythonrN   posixPosixModuler   r   r   r   	NameErrorr   r   __all__r   contextmanagerr   r    r'   r+   r/   r,   r;   rT   rQ   rZ   rb   ri   rk   r^   r   r   r   r   r   r   reduceor_rh   r   r   r   r   r   r   <module>   sv     




	


	 
	
^	