3 e 8 @sddlZddlZddlZddlZddlZddlZddlZddlZddlZddl Z ddl Z ddl Z ddl m Z ddl mZejjdrddljjjjZn ejejZyeZWnek rdZYnXeZddddgZd0d d Zejd1d d Z ejd dZ!ejddZ"ejddZ#Gddde$Z%GdddZ&ejddZ'ddZ(ejddZ)ejddZ*dd d!d"d#hZ+d$d%Z,d&d'Z-d(dZ.Gd)ddZ/e0ed*rej1gZ2ngZ2Gd+dde/Z3ej4ej5d,d-d.j6DZ7Gd/dde Z8dS)2N)DistutilsError) working_setjavaAbstractSandboxDirectorySandboxSandboxViolation run_setupc CsJd}t||}|j}WdQRX|dkr.|}t||d}t|||dS)z. Python 3 implementation of execfile. rbNexec)openreadcompiler )filenameglobalslocalsmodestreamscriptcoder8/tmp/pip-build-vlmub77e/setuptools/setuptools/sandbox.py _execfile$s  rc csDtjdd}|dk r$|tjdd<z |VWd|tjdd<XdS)N)sysargv)replsavedrrr save_argv1s  rc cs.tjdd}z |VWd|tjdd<XdS)N)rpath)rrrr save_path<s rc cs2tj|ddtj}|t_z dVWd|t_XdS)zL Monkey-patch tempfile.tempdir with replacement, ensuring it exists T)exist_okN)osmakedirstempfiletempdir) replacementrrrr override_tempEs  r%c cs.tj}tj|z |VWdtj|XdS)N)r getcwdchdir)targetrrrrpushdVs   r)c@seZdZdZeddZdS)UnpickleableExceptionzP An exception representing another Exception that could not be pickled. c CsJytj|tj|fStk rDddlm}|j||t|SXdS)z Always return a dumped (pickled) type and exc. If exc can't be pickled, wrap it in UnpickleableException first. r)r*N)pickledumps Exceptionsetuptools.sandboxr*dumprepr)typeexcclsrrrr/es  zUnpickleableException.dumpN)__name__ __module__ __qualname____doc__ staticmethodr/rrrrr*`sr*c@s(eZdZdZddZddZddZdS) ExceptionSaverz^ A Context Manager that will save an exception, serialized, and restore it later. cCs|S)Nr)selfrrr __enter__zszExceptionSaver.__enter__cCs |sdStj|||_||_dS)NT)r*r/_saved_tb)r:r1r2tbrrr__exit__}s zExceptionSaver.__exit__cCs2dt|krdSttj|j\}}|j|jdS)z"restore and re-raise any exceptionr<N)varsmapr+loadsr<with_tracebackr=)r:r1r2rrrresumes zExceptionSaver.resumeN)r4r5r6r7r;r?rDrrrrr9ts r9c #sVtjjt }VWdQRXtjjfddtjD}t||jdS)z Context in which imported modules are saved. Translates exceptions internal to the context into the equivalent exception outside the context. Nc3s&|]}|kr|jd r|VqdS)z encodings.N) startswith).0mod_name)rrr szsave_modules..)rmodulescopyr9update_clear_modulesrD) saved_excZ del_modulesr)rr save_moduless    rNcCsxt|D] }tj|=q WdS)N)listrrI)Z module_namesrGrrrrLsrLc cs$tj}z |VWdtj|XdS)N) pkg_resources __getstate__ __setstate__)rrrrsave_pkg_resources_states rSc,cstjj|d}txtftTtt<t|(t |t ddVWdQRXWdQRXWdQRXWdQRXWdQRXWdQRXdS)Ntemp setuptools) r rjoinrSrNrhide_setuptoolsrr%r) __import__) setup_dirtemp_dirrrr setup_contexts  r[rU distutilsrPZCython_distutils_hackcCs|jddd}|tkS)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)rGZ base_modulerrr _needs_hidingsrbcCs6tjjdd}|dk r|jtttj}t|dS)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. r]N)rrIgetZ remove_shimfilterrbrL)r]rIrrrrWs  rWcCstjjtjj|}t|yl|gt|tjdd<tjjd|t j t j j ddt |t|dd}t||WdQRXWn4tk r}z|jr|jdrWYdd}~XnXWdQRXdS)z8Run a distutils setup script, sandboxed in its directoryNrcSs|jS)N)activate)distrrrszrun_setup..__main__)__file__r4)r rabspathdirnamer[rOrrinsertr__init__ callbacksappendrdictr SystemExitargs)Z setup_scriptrrrYnsvrrrrs   c@s2eZdZdZdZddZddZddZd d Zd d Z d dZ x$d9D]Z e e e rFe e ee <qFWd:ddZer~edeZedeZx$d;D]Z e e e ree ee <qWd)d*Zx$drzDWrap 'os' module and 'open()' builtin for virtualizing setup scriptsFcsfddttD_dS)Ncs&g|]}|jd rt|r|qS)_)rEhasattr)rFname)r:rr sz,AbstractSandbox.__init__..)dir_os_attrs)r:r)r:rrms zAbstractSandbox.__init__cCs&x |jD]}tt|t||qWdS)N)r{setattrr getattr)r:sourcerwrrr_copys zAbstractSandbox._copycCs(|j|tr|jt_|jt_d|_dS)NT)r_filebuiltinsfile_openr _active)r:rrrr;s  zAbstractSandbox.__enter__cCs$d|_trtt_tt_|jtdS)NF)rrrrrr rrz)r:exc_type exc_value tracebackrrrr?!s zAbstractSandbox.__exit__c Cs||SQRXdS)zRun 'func' under os sandboxingNr)r:funcrrrrun(szAbstractSandbox.runcsttfdd}|S)Ncs2|jr |j||f||\}}||f||S)N)r _remap_pair)r:srcdstrrkw)rworiginalrrwrap0sz3AbstractSandbox._mk_dual_path_wrapper..wrap)r}rz)rwrr)rwrr_mk_dual_path_wrapper-s z%AbstractSandbox._mk_dual_path_wrapperrenamelinksymlinkNcs p ttfdd}|S)Ncs*|jr|j|f||}|f||S)N)r _remap_input)r:rrrr)rwrrrr>sz5AbstractSandbox._mk_single_path_wrapper..wrap)r}rz)rwrrr)rwrr_mk_single_path_wrapper;sz'AbstractSandbox._mk_single_path_wrapperrr statlistdirr'chmodchownmkdirremoveunlinkrmdirutimelchownchrootlstat startfilemkfifomknodpathconfaccesscsttfdd}|S)NcsB|jr2|j|f||}|j|f||S|f||S)N)rr _remap_output)r:rrrr)rwrrrrcsz4AbstractSandbox._mk_single_with_return..wrap)r}rz)rwrr)rwrr_mk_single_with_return`s z&AbstractSandbox._mk_single_with_returnreadlinktempnamcsttfdd}|S)Ncs ||}|jr|j|S|S)N)rr)r:rrrretval)rwrrrrrs  z'AbstractSandbox._mk_query..wrap)r}rz)rwrr)rwrr _mk_queryos zAbstractSandbox._mk_queryr&tmpnamcCs|S)z=Called to remap or validate any path, whether input or outputr)r:rrrr_validate_path~szAbstractSandbox._validate_pathcOs |j|S)zCalled for path inputs)r)r: operationrrrrrrrrszAbstractSandbox._remap_inputcCs |j|S)zCalled for path outputs)r)r:rrrrrrszAbstractSandbox._remap_outputcOs0|j|d|f|||j|d|f||fS)z?Called for path pairs like rename, link, and symlink operationsz-fromz-to)r)r:rrrrrrrrrrszAbstractSandbox._remap_pair)rrr)N)rrr'r rrrrrrrrrrrrrrr)rr)r&r)r4r5r6r7rrmrr;r?rrrwrvrzrrrrrrrrrrrrrrr sb          devnullc@seZdZdZejdddddddd d d d d dg ZgZefddZ ddZ e rVd&ddZ d'ddZ ddZ ddZddZddZd d!Zd(d#d$Zd%S))rz.) r rrr_sandboxrV_prefix _exceptionsrrm)r:Zsandbox exceptionsrrrrmszDirectorySandbox.__init__cOsddlm}||||dS)Nr)r)r.r)r:rrrrrrrr _violations zDirectorySandbox._violationrcOs<|dkr*|j| r*|jd||f||t||f||S)Nrrtr rUUr)rrr rr)_okrr)r:rrrrrrrrrszDirectorySandbox._filecOs<|dkr*|j| r*|jd||f||t||f||S)Nrrr rrr )rrr rr)rrr)r:rrrrrrrrrszDirectorySandbox._opencCs|jddS)Nr)r)r:rrrrszDirectorySandbox.tmpnamc CsN|j}z:d|_tjjtjj|}|j|p@||jkp@|j|jS||_XdS)NF) rr rrr _exemptedrrEr)r:ractiverrrrrs   zDirectorySandbox._okcs<fdd|jD}fdd|jD}tj||}t|S)Nc3s|]}j|VqdS)N)rE)rF exception)filepathrrrHsz-DirectorySandbox._exempted..c3s|]}tj|VqdS)N)rematch)rFpattern)rrrrHs)r_exception_patterns itertoolschainany)r:rZ start_matchesZpattern_matches candidatesr)rrrs zDirectorySandbox._exemptedcOs6||jkr2|j| r2|j|tjj|f|||S)zCalled for path inputs) write_opsrrr rr)r:rrrrrrrrrszDirectorySandbox._remap_inputcOs6|j| s|j| r.|j|||f||||fS)z?Called for path pairs like rename, link, and symlink operations)rr)r:rrrrrrrrrrszDirectorySandbox._remap_paircOsB|t@r,|j| r,|jd|||f||tj|||f||S)zCalled for low-level os.open()zos.open) WRITE_FLAGSrrrzr )r:rflagsrrrrrrrr szDirectorySandbox.openN)r)r)r)r4r5r6r7rpfromkeysrr _EXCEPTIONSrmrrrrrrrrr rrrrrs6     cCsg|]}tt|dqS)r)r}rz)rFarrrrxsrxz4O_WRONLY O_RDWR O_APPEND O_CREAT O_TRUNC O_TEMPORARYc@s&eZdZdZejdjZddZdS)rzEA 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. cCs|j\}}}|jjftS)N)rrtmplformatr)r:cmdrrkwargsrrr__str__s zSandboxViolation.__str__N) r4r5r6r7textwrapdedentlstriprrrrrrrs )N)N)9r rr"operator functoolsrr contextlibr+rrrPZdistutils.errorsrrplatformrEZ$org.python.modules.posix.PosixModulepythonrIposixZ PosixModulerzrwrr NameErrorr r__all__rcontextmanagerrrr%r)r-r*r9rNrLrSr[rarbrWrrrvrrrreduceor_r`rrrrrrsp            ^