zfc@sdZddlZddlZddlmZyddlmZWn!ek reddlmZnXddgZdefdYZ d efd YZ dddddd Z dddddd Z d ZedZedZdefdYZedZddedZdZdZdddZedkreejdkruejdIJqejd=eejdndS(sZrunpy.py - locating and running Python code using the module namespace Provides support for locating and running Python scripts using the Python module namespace instead of the native filesystem. This allows Python code to play nicely with non-filesystem based PEP 302 importers when locating support scripts as well as when importing modules. iN(t read_code(t get_loadert run_moduletrun_patht _TempModulecBs)eZdZdZdZdZRS(sCTemporarily replace a module in sys.modules with an empty namespacecCs(||_tj||_g|_dS(N(tmod_nametimpt new_moduletmodulet _saved_module(tselfR((s/usr/lib64/python2.7/runpy.pyt__init__s cCsL|j}y|jjtj|Wntk r7nX|jtj|<|S(N(RR tappendtsystmodulestKeyErrorR(R R((s/usr/lib64/python2.7/runpy.pyt __enter__s  cGs=|jr#|jdtj|js cCsZt|B}t|-|jj}t|||||||WdQXWdQX|jS(s5Helper to run code in new namespace with sys modifiedN(RRRt__dict__R(tcopy(R"R$RR%R&R't temp_modulet mod_globals((s/usr/lib64/python2.7/runpy.pyt_run_module_codeKs    cCs=x6dD].}t||d}|dk r||SqWdS(Nt get_filenamet _get_filename(R.R/(tgetattrR (tloaderRtattrtmeth((s/usr/lib64/python2.7/runpy.pyR/\s   cCsdy>t|}|dkr.|d|n|j|}Wn%tk re}|t|nX|r|dks|jdr|dnt|y|d}t|SWqtk r}|dd||fqXny|j|}Wn%tk r%}|t|nX|dkrE|d|nt ||}||||fS(NsNo module named %st__main__s .__main__s%Cannot use package as __main__ modules%s; %r is a package and cannot sbe directly executedsNo code object available for %s( RR t is_packaget ImportErrortformattendswitht __import__t_get_module_detailstget_codeR/(RterrorR1tispkgtet pkg_main_nameR"tfilename((s/usr/lib64/python2.7/runpy.pyR:ds2     cCscd}yt|SWnHtk r^}|t|krX|d|tjdfnnXdS(NR4scan't find %r module in %ri(R:R6tstrR tpath(R<t main_nametexc((s/usr/lib64/python2.7/runpy.pyt_get_main_module_detailsst_ErrorcBseZdZRS(sBError that _run_module_as_main() should report without a traceback(RRR(((s/usr/lib64/python2.7/runpy.pyRFsc CsyL|s|dkr3t|t\}}}}ntt\}}}}Wn3tk r}dtj|f}tj|nX|jdd}tjdj}|r|tj diR4Nt(R RYt isinstanceRRTRaR-R RBtinsertRRERRRR)R(R*tremovet ValueError( RUR$RPRWR"RCt saved_mainRR1RLR'R+R,((s/usr/lib64/python2.7/runpy.pyRs0         & R4is!No module specified for executioni(RR RtpkgutilRRR6t__all__RRRR R(R-R/R:REt ExceptionRFtTrueROtFalseRRYRaRRtlenRtstderr(((s/usr/lib64/python2.7/runpy.pyts>           1