o nh::@s ddlmZddlZddlZddlZddlZddlZddlZddlZddl Z ddl Z ddl Z ddl Z ddl mZddlmZmZmZddlZddlmZddlmZerYddlZne jdroddlmmmmZne jejZe Z!erddl"m#Z#gd Z$d1d d Z%ej&d1d d Z'ej&ddZ(ej&ddZ)ej&ddZ*Gddde+Z,GdddZ-ej&ddZ.ddZ/ej&ddZ0ej&ddZ1hd Z2d!d"Z3d#d$Z4d%d&Z5Gd'd(d(Z6e7ed)rej8gZ9ngZ9Gd*d+d+e6Z:e;ejGd/d0d0eZ?dS)2) annotationsN) TracebackType) TYPE_CHECKINGAnyClassVar) working_set)DistutilsErrorjava)Self)AbstractSandboxDirectorySandboxSandboxViolation run_setupcCs^d}t|| }|}Wdn1swY|dur!|}t||d}t|||dS)z. Python 3 implementation of execfile. rbNexec)openreadcompiler)filenameglobalslocalsmodestreamscriptcoderU/opt/prometheus-monitoring-scripts/lib/python3.10/site-packages/setuptools/sandbox.py _execfile*s   rc csRtjdd}|dur|tjdd<z |VW|tjdd<dS|tjdd<wN)sysargv)replsavedrrr save_argv7s"r#c cs<tjdd}z |VW|tjdd<dS|tjdd<wr)rpathr"rrr save_pathBs "r&ccs8tj|ddtj}|t_z dVW|t_dS|t_w)zL Monkey-patch tempfile.tempdir with replacement, ensuring it exists T)exist_okN)osmakedirstempfiletempdir) replacementr"rrr override_tempKsr-c cs8t}t|z |VWt|dSt|wr)r(getcwdchdir)targetr"rrrpushd\s  r1c@seZdZdZeddZdS)UnpickleableExceptionzP An exception representing another Exception that could not be pickled. c CsJz t|t|fWSty$ddlm}|||t|YSw)z Always return a dumped (pickled) type and exc. If exc can't be pickled, wrap it in UnpickleableException first. r)r2)pickledumps Exceptionsetuptools.sandboxr2dumprepr)typeexcclsrrrr7ks   zUnpickleableException.dumpN)__name__ __module__ __qualname____doc__ staticmethodr7rrrrr2fsr2c@s,eZdZdZdddZdd dZddZdS)ExceptionSaverz] A Context Manager that will save an exception, serialize, and restore it later. returnr cCs|Srrselfrrr __enter__szExceptionSaver.__enter__r9type[BaseException] | Noner:BaseException | NonetbTracebackType | NoneboolcCs |sdSt|||_||_dS)NFT)r2r7_saved_tb)rDr9r:rHrrr__exit__s zExceptionSaver.__exit__cCs.dt|vrdSttj|j\}}||j)z"restore and re-raise any exceptionrKN)varsmapr3loadsrKwith_tracebackrL)rD_typer:rrrresumes  zExceptionSaver.resumeN)rBr )r9rFr:rGrHrIrBrJ)r<r=r>r?rErMrSrrrrrAzs    rAc#sltjt }VWdn1swYtjfddtjD}t||dS)z Context in which imported modules are saved. Translates exceptions internal to the context into the equivalent exception outside the context. Nc3s&|]}|vr|ds|VqdS)z encodings.N startswith).0mod_namer%rr szsave_modules..)rmodulescopyrAupdate_clear_modulesrS) saved_exc del_modulesrr%r save_moduless    r_cCst|D]}tj|=qdSr)listrrY) module_namesrWrrrr\s  r\c cs.t}z |VWt|dSt|wr) pkg_resources __getstate__ __setstate__r%rrrsave_pkg_resources_states rec cs"tj|d}t|t`tMtt7t|#t |t ddVWdn1s6wYWdn1sEwYWdn1sTwYWdn1scwYWdn1srwYWddSWddS1swYdS)Ntemp setuptools) r(r$joinrer_r&hide_setuptoolsr#r-r1 __import__) setup_dirtemp_dirrrr setup_contexts.  "rm>Cython distutilsrgrb_distutils_hackcCs|ddd}|tvS)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)rW base_modulerrr _needs_hidingsrvcCs6tjdd}|dur|tttj}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. rpN)rrYget _remove_shimfilterrvr\)rprYrrrris   ric Cs tjtj|}t|mz@|gt|tjdd<tjd|t t j ddt |t|dd}t||Wdn1sIwYWntyj}z|jr`|jdr`WYd}~n d}~wwWddSWddS1s~wYdS)z8Run a distutils setup script, sandboxed in its directoryNrcSs|Sr)activate)distrrr szrun_setup..__main__)__file__r<)r(r$abspathdirnamermr`rr insertr__init__ callbacksappendr dictr SystemExitargs) setup_scriptrrknsvrrrrs*    "rc@s0eZdZdZdZd0ddZddZd0d d Zd1ddZddZ d2ddZ dD]Z e e e r4e e ee <q&d3d2ddZedeZdD]Z e e e rPee ee <qBd2dd Zd!D]Z e e e rfee ee <qXd2d"d#Zd$D]Z e e e r|ee ee <qnd%d&Zd'd(Zd)d*Zd+d,Zerd4d.d/ZdSdS)5r zDWrap 'os' module and 'open()' builtin for virtualizing setup scriptsFrBNonecsfddttD_dS)Ncs$g|]}|dst|r|qS)_)rUhasattr)rVnamerCrr sz,AbstractSandbox.__init__..)dir_os_attrsrCrrCrrs  zAbstractSandbox.__init__cCs"|jD] }tt|t||qdSr)rsetattrr(getattr)rDsourcerrrr_copy!s zAbstractSandbox._copycCs|||jt_d|_dS)NT)r_openbuiltinsr_activerCrrrrE%s  zAbstractSandbox.__enter__exc_typerF exc_valuerG tracebackrIcCsd|_tt_|tdSNF)rrrrrr)rDrrrrrrrM*szAbstractSandbox.__exit__cCs.| |WdS1swYdS)zRun 'func' under os sandboxingNr)rDfuncrrrrun4s$zAbstractSandbox.runrstrcttfdd}|S)Ncs>|jr|j||g|Ri|\}}||g|Ri|Sr)r _remap_pair)rDsrcdstrkwroriginalrrwrap<s z3AbstractSandbox._mk_dual_path_wrapper..wraprrrrrrr_mk_dual_path_wrapper9s z%AbstractSandbox._mk_dual_path_wrapper)renamelinksymlinkNcs pttfdd}|S)Ncs6|jr|j|g|Ri|}|g|Ri|Sr)r _remap_inputrDr$rrrrrrJsz5AbstractSandbox._mk_single_path_wrapper..wrapr)rrrrrr_mk_single_path_wrapperGsz'AbstractSandbox._mk_single_path_wrapperr)statlistdirr/rchmodchownmkdirremoveunlinkrmdirutimelchownchrootlstat startfilemkfifomknodpathconfaccesscr)NcsT|jr|j|g|Ri|}||g|Ri|S|g|Ri|Sr)rr _remap_outputrrrrrmsz4AbstractSandbox._mk_single_with_return..wraprrrrr_mk_single_with_returnj z&AbstractSandbox._mk_single_with_return)readlinktempnamcr)Ncs$|i|}|jr||S|Sr)rr)rDrrretvalrrrr|s z'AbstractSandbox._mk_query..wraprrrrr _mk_queryyrzAbstractSandbox._mk_query)r.tmpnamcCs|S)z=Called to remap or validate any path, whether input or outputr)rDr$rrr_validate_pathszAbstractSandbox._validate_pathcO ||SzCalled for path inputsrrD operationr$rrrrrr zAbstractSandbox._remap_inputcCr)zCalled for path outputsr)rDrr$rrrrrzAbstractSandbox._remap_outputcOs<|j|d|g|Ri||j|d|g|Ri|fS)?Called for path pairs like rename, link, and symlink operationsz-fromz-to)rrDrrrrrrrrrszAbstractSandbox._remap_pairrcCsdSrr)rDrrrr__getattribute__sz AbstractSandbox.__getattribute__rBr)rrFrrGrrI)rrr)rrrBr)r<r=r>r?rrrrErMrr_AbstractSandbox__namerrrrrrrrrrrrrrrrrr sH            r devnullc@seZdZUdZegdZded<gZded< e fd d d Z d d Z d!ddZ d ddZ ddZddZddZddZd"d#ddZdS)$r z.) r(r$rr_sandboxrh_prefix _exceptionsr r)rDsandbox exceptionsrrrrs zDirectorySandbox.__init__cOsddlm}||||)Nr)r )r6r )rDrrrr rrr _violations  zDirectorySandbox._violationrcOsF|dvr||s|jd||g|Ri|t||g|Ri|S)N)rrtrrUUr)_okrr)rDr$rrrrrrrszDirectorySandbox._opencCs|ddS)Nr)rrCrrrrszDirectorySandbox.tmpnamcCsR|j}z!d|_tjtj|}||p ||jkp ||jW||_S||_wr) rr(r$rr _exemptedrrUr)rDr$activerrrrrs  zDirectorySandbox._okcs<fdd|jD}fdd|jD}t||}t|S)Nc3s|]}|VqdSrrT)rV exceptionfilepathrrrXs  z-DirectorySandbox._exempted..c3s|] }t|VqdSr)rematch)rVpatternrrrrXs  )rr itertoolschainany)rDr start_matchespattern_matches candidatesrrrrs   zDirectorySandbox._exemptedcOs:||jvr||s|j|tj|g|Ri||Sr)rrrr(r$rrrrrrs"zDirectorySandbox._remap_inputcOs8||r ||s|j|||g|Ri|||fS)r)rrrrrrrszDirectorySandbox._remap_pairrintcOsL|t@r||s|jd|||g|Ri|tj|||g|Ri|S)zCalled for low-level os.open()zos.open) WRITE_FLAGSrrrr)rDfileflagsrrrrrrrszDirectorySandbox.openNr)r)r)rrrBr)r<r=r>r?rfromkeysr__annotations__r _EXCEPTIONSrrrrrrrrrrrrrr s      r cCsg|]}tt|dqS)rr)rVarrrrs rz4O_WRONLY O_RDWR O_APPEND O_CREAT O_TRUNC O_TEMPORARYc@s(eZdZdZedZdddZdS) 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. rBrcCs |j\}}}|jjditS)Nr)rtmplformatr)rDcmdrkwargsrrr__str__s zSandboxViolation.__str__N)rBr) r<r=r>r?textwrapdedentlstriprrrrrrr s r r)@ __future__rr contextlib functoolsroperatorr(r3rrr*rtypesrtypingrrrrbrdistutils.errorsrrplatformrU$org.python.modules.posix.PosixModulepythonrYposix PosixModulerrrtyping_extensionsr __all__rcontextmanagerr#r&r-r1r5r2rAr_r\rermrtrvrirr rrrr reduceor_rsrr rrrrsz               #     U