3

Q'ícÄ7ã@süddlZddlZddlZddlZddlZddlZddlZddlZddlZddl	Z	ddl
mZddlm
Z
mZddlZejjdƒr’ddljjjjZnejejZyeZWnek
r¾dZYnXeZddlm Z ddlm!Z!ddd	d
gZ"d-dd„Z#ej$d.d
d„ƒZ%ej$dd„ƒZ&ej$dd„ƒZ'ej$dd„ƒZ(Gdd„de)ƒZ*Gdd„dƒZ+ej$dd„ƒZ,dd„Z-ej$dd„ƒZ.ej$dd „ƒZ/d!d"„Z0d#d$„Z1d%d
„Z2Gd&d„dƒZ3e4ed'ƒr¸ej5gZ6ngZ6Gd(d„de3ƒZ7ej8ej9d)d*„d+j:ƒDƒƒZ;Gd,d	„d	e ƒZ<dS)/éN)Úsix)ÚbuiltinsÚmapÚjava)ÚDistutilsError)Úworking_setÚAbstractSandboxÚDirectorySandboxÚSandboxViolationÚ	run_setupcCsJd}t||ƒ}|jƒ}WdQRX|dkr.|}t||dƒ}t|||ƒdS)z.
    Python 3 implementation of execfile.
    ÚrbNÚexec)ÚopenÚreadÚcompiler
)ÚfilenameÚglobalsÚlocalsÚmodeÚstreamÚscriptÚcode©rú8/tmp/pip-build-l_bcah40/setuptools/setuptools/sandbox.pyÚ	_execfile#src
csDtjdd…}|dk	r$|tjdd…<z
|VWd|tjdd…<XdS)N)ÚsysÚargv)ÚreplÚsavedrrrÚ	save_argv0s
rc
cs.tjdd…}z
|VWd|tjdd…<XdS)N)rÚpath)rrrrÚ	save_path;s
r!ccs4tjj|ddtj}|t_z
dVWd|t_XdS)zL
    Monkey-patch tempfile.tempdir with replacement, ensuring it exists
    T)Úexist_okN)Ú
pkg_resourcesÚ
py31compatÚmakedirsÚtempfileÚtempdir)ÚreplacementrrrrÚ
override_tempDs
r)ccs.tjƒ}tj|ƒz
|VWdtj|ƒXdS)N)ÚosÚgetcwdÚchdir)ÚtargetrrrrÚpushdUs


r.c@seZdZdZedd„ƒZdS)ÚUnpickleableExceptionzP
    An exception representing another Exception that could not be pickled.
    cCsJytj|ƒ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)ÚpickleÚdumpsÚ	ExceptionÚsetuptools.sandboxr/ÚdumpÚrepr)ÚtypeÚexcÚclsrrrr4ds
zUnpickleableException.dumpN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ústaticmethodr4rrrrr/_sr/c@s(eZdZdZdd„Zdd„Zdd„ZdS)	ÚExceptionSaverz^
    A Context Manager that will save an exception, serialized, and restore it
    later.
    cCs|S)Nr)ÚselfrrrÚ	__enter__xszExceptionSaver.__enter__cCs |sdStj||ƒ|_||_dS)NT)r/r4Ú_savedÚ_tb)r?r6r7ÚtbrrrÚ__exit__{s
zExceptionSaver.__exit__cCs6dt|ƒkrdSttj|jƒ\}}tj|||jƒdS)z"restore and re-raise any exceptionrAN)Úvarsrr0ÚloadsrArÚreraiserB)r?r6r7rrrÚresume†szExceptionSaver.resumeN)r9r:r;r<r@rDrHrrrrr>rsr>c
#sVtjjƒ‰tƒ}ˆVWdQRXtjjˆƒ‡fdd„tjDƒ}t|ƒ|jƒdS)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)Ú.0Úmod_name)rrrú	<genexpr>Ÿszsave_modules.<locals>.<genexpr>)rÚmodulesÚcopyr>ÚupdateÚ_clear_modulesrH)Ú	saved_excZdel_modulesr)rrÚsave_moduless
rRcCsxt|ƒD]}tj|=q
WdS)N)ÚlistrrM)Zmodule_namesrKrrrrP©srPccs$tjƒ}z
|VWdtj|ƒXdS)N)r#Ú__getstate__Ú__setstate__)rrrrÚsave_pkg_resources_state®s
rVc,cs–tjj|dƒ}tƒxtƒftƒtƒNtƒ<t|ƒ(t	|ƒt
dƒdVWdQRXWdQRXWdQRXWdQRXWdQRXWdQRXdS)NÚtempÚ
setuptools)r*r ÚjoinrVrRÚhide_setuptoolsr!rr)r.Ú
__import__)Ú	setup_dirÚtemp_dirrrrÚ
setup_context·s

r^cCstjdƒ}t|j|ƒƒ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
    z1(setuptools|pkg_resources|distutils|Cython)(\.|$))ÚrerÚboolÚmatch)rKÚpatternrrrÚ
_needs_hidingÆs
rccCstttjƒ}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.
    N)ÚfilterrcrrMrP)rMrrrrZÛsrZcCsêtjjtjj|ƒƒ}t|ƒÄyˆ|gt|ƒtjdd…<tjjd|ƒt	j
ƒt	jjdd„ƒt
|tƒrl|n|jtjƒƒ}t|ƒt|dd}t||ƒWdQRXWn4tk
rÚ}z|jrÊ|jdrʂWYdd}~XnXWdQRXdS)z8Run a distutils setup script, sandboxed in its directoryNrcSs|jƒS)N)Úactivate)ÚdistrrrÚ<lambda>ïszrun_setup.<locals>.<lambda>Ú__main__)Ú__file__r9)r*r ÚabspathÚdirnamer^rSrrÚinsertrÚ__init__Ú	callbacksÚappendÚ
isinstanceÚstrÚencodeÚgetfilesystemencodingr	ÚdictrÚ
SystemExitÚargs)Zsetup_scriptrvr\Zdunder_fileÚnsÚvrrrræs

c@s2eZdZdZdZdd„Zdd„Zdd„Zd	d
„Zdd„Z	d
d„Z
x$d9D]Zee
eƒrFe
eƒeƒe<qFWd:dd„Zer~edeƒZedeƒZx$d;D]Zee
eƒrŽeeƒeƒe<qŽWd)d*„Zx$d<D]Zee
eƒr¼eeƒeƒe<q¼Wd-d.„Zx(d=D] Zee
eƒrêeeƒeƒe<qêWd1d2„Zd3d4„Zd5d6„Zd7d8„ZdS)>rzDWrap 'os' module and 'open()' builtin for virtualizing setup scriptsFcs‡fdd„ttƒDƒˆ_dS)Ncs&g|]}|jdƒrtˆ|ƒr|‘qS)Ú_)rIÚhasattr)rJÚname)r?rrú
<listcomp>sz,AbstractSandbox.__init__.<locals>.<listcomp>)ÚdirÚ_osÚ_attrs)r?r)r?rrmszAbstractSandbox.__init__cCs&x |jD]}tt|t||ƒƒqWdS)N)rÚsetattrr*Úgetattr)r?Úsourcer{rrrÚ_copyszAbstractSandbox._copycCs(|j|ƒtr|jt_|jt_d|_dS)NT)rƒÚ_filerÚfileÚ_openrÚ_active)r?rrrr@s

zAbstractSandbox.__enter__cCs$d|_trtt_tt_|jtƒdS)NF)r‡r„rr…r†rrƒr~)r?Úexc_typeÚ	exc_valueÚ	tracebackrrrrDs
zAbstractSandbox.__exit__c	Cs||ƒSQRXdS)zRun 'func' under os sandboxingNr)r?ÚfuncrrrÚrunszAbstractSandbox.runcsttˆƒ‰‡‡fdd„}|S)Ncs2|jr |jˆ||f|ž|Ž\}}ˆ||f|ž|ŽS)N)r‡Ú_remap_pair)r?ÚsrcÚdstrvÚkw)r{ÚoriginalrrÚwrap&sz3AbstractSandbox._mk_dual_path_wrapper.<locals>.wrap)rr~)r{r’r)r{r‘rÚ_mk_dual_path_wrapper#s
z%AbstractSandbox._mk_dual_path_wrapperÚrenameÚlinkÚsymlinkNcs ˆpttˆƒ‰‡‡fdd„}|S)Ncs*|jr|jˆ|f|ž|Ž}ˆ|f|ž|ŽS)N)r‡Ú_remap_input)r?r rvr)r{r‘rrr’4sz5AbstractSandbox._mk_single_path_wrapper.<locals>.wrap)rr~)r{r‘r’r)r{r‘rÚ_mk_single_path_wrapper1sz'AbstractSandbox._mk_single_path_wrapperr…rÚstatÚlistdirr,ÚchmodÚchownÚmkdirÚremoveÚunlinkÚrmdirÚutimeÚlchownÚchrootÚlstatÚ	startfileÚmkfifoÚmknodÚpathconfÚaccesscsttˆƒ‰‡‡fdd„}|S)NcsB|jr2|jˆ|f|ž|Ž}|jˆˆ|f|ž|ŽƒSˆ|f|ž|ŽS)N)r‡r—Ú
_remap_output)r?r rvr)r{r‘rrr’Isz4AbstractSandbox._mk_single_with_return.<locals>.wrap)rr~)r{r’r)r{r‘rÚ_mk_single_with_returnFs
z&AbstractSandbox._mk_single_with_returnÚreadlinkÚtempnamcsttˆƒ‰‡‡fdd„}|S)Ncs ˆ||Ž}|jr|jˆ|ƒS|S)N)r‡rª)r?rvrÚretval)r{r‘rrr’Xs
z'AbstractSandbox._mk_query.<locals>.wrap)rr~)r{r’r)r{r‘rÚ	_mk_queryUs
zAbstractSandbox._mk_queryr+ÚtmpnamcCs|S)z=Called to remap or validate any path, whether input or outputr)r?r rrrÚ_validate_pathdszAbstractSandbox._validate_pathcOs
|j|ƒS)zCalled for path inputs)r±)r?Ú	operationr rvrrrrr—hszAbstractSandbox._remap_inputcCs
|j|ƒS)zCalled for path outputs)r±)r?r²r rrrrªlszAbstractSandbox._remap_outputcOs0|j|d|f|ž|Ž|j|d|f|ž|ŽfS)z?Called for path pairs like rename, link, and symlink operationsz-fromz-to)r—)r?r²rŽrrvrrrrrpszAbstractSandbox._remap_pair)r”r•r–)N)r™ršr,rr›rœrržrŸr r¡r¢r£r¤r¥r¦r§r¨r©)r¬r­)r+r°)r9r:r;r<r‡rmrƒr@rDrŒr“r{rzr~rr˜r„r†r«r¯r±r—rªrrrrrrsB










Údevnullc@s˜eZdZdZejdddddddd	d
ddd
dg
ƒZdgZefdd„Z	dd„Z
erXd'dd„Zd(dd„Zdd„Z
dd„Zdd„Zdd „Zd!d"„Zd)d$d%„Zd&S)*r	z<Restrict operations to a single subdirectory - pseudo-chrootrr›rœrržrŸr r¡r¢r£r¦r§r­z.*lib2to3.*\.pickle$cCsFtjjtjj|ƒƒ|_tjj|jdƒ|_dd„|Dƒ|_tj	|ƒdS)NÚcSs g|]}tjjtjj|ƒƒ‘qSr)r*r ÚnormcaseÚrealpath)rJr rrrr|sz-DirectorySandbox.__init__.<locals>.<listcomp>)
r*r rµr¶Ú_sandboxrYÚ_prefixÚ_exceptionsrrm)r?ZsandboxÚ
exceptionsrrrrmŒs

zDirectorySandbox.__init__cOsddlm}||||ƒ‚dS)Nr)r
)r3r
)r?r²rvrr
rrrÚ
_violation•szDirectorySandbox._violationÚrcOs<|dkr*|j|ƒr*|jd||f|ž|Žt||f|ž|ŽS)Nr¼ÚrtrÚrUÚUr…)r¼r½rr¾r¿)Ú_okr»r„)r?r rrvrrrrr„›szDirectorySandbox._filecOs<|dkr*|j|ƒr*|jd||f|ž|Žt||f|ž|ŽS)Nr¼r½rr¾r¿r)r¼r½rr¾r¿)rÀr»r†)r?r rrvrrrrr† szDirectorySandbox._opencCs|jdƒdS)Nr°)r»)r?rrrr°¥szDirectorySandbox.tmpnamcCsN|j}z:d|_tjjtjj|ƒƒ}|j|ƒp@||jkp@|j|jƒS||_XdS)NF)	r‡r*r rµr¶Ú	_exemptedr·rIr¸)r?r Úactiver¶rrrrÀ¨s

zDirectorySandbox._okcs<‡fdd„|jDƒ}‡fdd„|jDƒ}tj||ƒ}t|ƒS)Nc3s|]}ˆj|ƒVqdS)N)rI)rJÚ	exception)ÚfilepathrrrL·sz-DirectorySandbox._exempted.<locals>.<genexpr>c3s|]}tj|ˆƒVqdS)N)r_ra)rJrb)rÄrrrL»s)r¹Ú_exception_patternsÚ	itertoolsÚchainÚany)r?rÄZ
start_matchesZpattern_matchesÚ
candidatesr)rÄrrÁµs



zDirectorySandbox._exemptedcOs6||jkr2|j|ƒr2|j|tjj|ƒf|ž|Ž|S)zCalled for path inputs)Ú	write_opsrÀr»r*r r¶)r?r²r rvrrrrr—ÁszDirectorySandbox._remap_inputcOs6|j|ƒs|j|ƒr.|j|||f|ž|Ž||fS)z?Called for path pairs like rename, link, and symlink operations)rÀr»)r?r²rŽrrvrrrrrÇszDirectorySandbox._remap_pairéÿcOsB|t@r,|j|ƒr,|jd|||f|ž|Žtj|||f|ž|ŽS)zCalled for low-level os.open()zos.open)ÚWRITE_FLAGSrÀr»r~r)r?r…ÚflagsrrvrrrrrÍszDirectorySandbox.openN)r¼)r¼)rË)r9r:r;r<rtÚfromkeysrÊrÅÚ_EXCEPTIONSrmr»r„r†r°rÀrÁr—rrrrrrr	~s 	


cCsg|]}tt|dƒ‘qS)r)rr~)rJÚarrrr|Õsr|z4O_WRONLY O_RDWR O_APPEND O_CREAT O_TRUNC O_TEMPORARYc@s&eZdZdZejdƒjƒZdd„ZdS)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.
        cCs|j\}}}|jjftƒŽS)N)rvÚtmplÚformatr)r?ÚcmdrvÚkwargsrrrÚ__str__észSandboxViolation.__str__N)	r9r:r;r<ÚtextwrapÚdedentÚlstriprÑrÕrrrrr
Ús

)N)N)=r*rr&ÚoperatorÚ	functoolsrÆr_Ú
contextlibr0rÖZsetuptools.externrZsetuptools.extern.six.movesrrZpkg_resources.py31compatr#ÚplatformrIZ$org.python.modules.posix.PosixModuleÚpythonrMÚposixZPosixModuler~r{r…r„Ú	NameErrorrr†Zdistutils.errorsrrÚ__all__rÚcontextmanagerrr!r)r.r2r/r>rRrPrVr^rcrZrrrzr³rÏr	ÚreduceÚor_ÚsplitrÌr
rrrrÚ<module>s^



	
	w
V