3 öLÖjˆ)ã@sddlZddlZddlZddlZddlZddlZddlZddlZddlZddl Z ddl Z ddl m Z ddl mZmZdadd„ZGdd„dejdd#ƒƒZd d „Zd d„Zdd„Zdd„Zejejdœdd„Zdd„Zd$dd„Zd%dd„Zd&dd„Zd'd!d"„ZdS)(éN)Úsource_from_cache)Úmake_legacy_pycÚstrip_python_stderrc CsVtdkrRdtjkrdadSytjtjdddgƒWntjk rLdaYnXdatS)a  Returns True if our sys.executable interpreter requires environment variables in order to be able to run at all. This is designed to be used with @unittest.skipIf() to annotate tests that need to use an assert_python*() function to launch an isolated mode (-I) or no environment mode (-E) sub-interpreter process. A normal build & test does not run into this situation but it can happen when trying to run the standard library test suite from an interpreter that doesn't have an obvious home with Python's current home finding logic. Setting PYTHONHOME is one way to get most of the testsuite to run in that situation. PYTHONPATH or PYTHONUSERSITE are other common environment variables that might impact whether or not the interpreter can start. NZ PYTHONHOMETz-Ez-czimport sys; sys.exit(0)F)Ú$__cached_interp_requires_environmentÚosÚenvironÚ subprocessZ check_callÚsysÚ executableZCalledProcessError©r r ú2/usr/lib64/python3.6/test/support/script_helper.pyÚ interpreter_requires_environments   r c@seZdZdZdd„ZdS)Ú_PythonRunResultz2Helper for reporting Python subprocess run resultscCsˆd }|j|j}}t|ƒ|kr0d|| d…}t|ƒ|krNd|| d…}|jddƒjƒ}|jddƒjƒ}td|j|||fƒ‚dS) z4Provide helpful details about failed subcommand runséPéds(... truncated stdout ...)Ns(... truncated stderr ...)ÚasciiÚreplacezRProcess return code is %d command line: %r stdout: --- %s --- stderr: --- %s ---i@)ÚoutÚerrÚlenÚdecodeÚrstripÚAssertionErrorÚrc)ÚselfÚcmd_lineÚmaxlenrrr r r Úfail>s   z_PythonRunResult.failN)Ú__name__Ú __module__Ú __qualname__Ú__doc__rr r r r r;srrrrc Ostƒ}d|kr|jdƒ}n | o$| }tjddg}|rB|jdƒn| rX| rX|jdƒ|jddƒr‚i}tjdkrŒtjd|d<n tjjƒ}d |krœd |d <|j |ƒ|j |ƒt j |t j t j t j |d �}|�*z|jƒ\}}Wd|jƒt jƒXWdQRX|j} t|ƒ}t| ||ƒ|fS) NZ __isolatedz-XZ faulthandlerz-Iz-EZ __cleanenvZwin32Z SYSTEMROOTÚTERMÚ)ÚstdinÚstdoutÚstderrÚenv)r Úpopr r ÚappendÚplatformrrÚcopyÚupdateÚextendrÚPopenÚPIPEZ communicateÚkillÚ_cleanupÚ returncoderr) ÚargsÚenv_varsZ env_requiredÚisolatedrr'Úprocrrrr r r Úrun_python_until_end[s:            r7cOs4t||Ž\}}|jr|s&|j r0| r0|j|ƒ|S)N)r7rr)Zexpected_successr3r4Úresrr r r Ú_assert_pythonŽs r9cOstd|ž|ŽS)a| Assert that running the interpreter with `args` and optional environment variables `env_vars` succeeds (rc == 0) and return a (return code, stdout, stderr) tuple. If the __cleanenv keyword is set, env_vars is used as a fresh environment. Python is started in isolated mode (command line option -I), except if the __isolated keyword is set to False. T)T)r9)r3r4r r r Úassert_python_ok”s r:cOstd|ž|ŽS)zß Assert that running the interpreter with `args` and optional environment variables `env_vars` fails (rc != 0) and return a (return code, stdout, stderr) tuple. See assert_python_ok() for more options. F)F)r9)r3r4r r r Úassert_python_failure¡sr;)r%r&cOsXtjg}tƒs|jdƒ|j|ƒ|jdttjƒƒ}d|d<t j |ft j ||dœ|—ŽS)z�Run a Python subprocess with the given arguments. kw is extra keyword args to pass to subprocess.Popen. Returns a Popen object. z-Er'Zvt100r")r$r%r&) r r r r)r-Ú setdefaultÚdictrrrr.r/)r%r&r3Úkwrr'r r r Ú spawn_python«s   r?cCs2|jjƒ|jjƒ}|jjƒ|jƒtjƒ|S)z?Run the given Popen process until completion and return stdout.)r$Úcloser%ÚreadÚwaitrr1)ÚpÚdatar r r Ú kill_pythonÁs    rEFcCsP|}|s|tjd7}tjj||ƒ}t|ddd�}|j|ƒ|jƒtjƒ|S)NÚpyÚwzutf-8)Úencoding) rÚextsepÚpathÚjoinÚopenÚwriter@Ú importlibÚinvalidate_caches)Z script_dirÚscript_basenameÚsourceZ omit_suffixZscript_filenameÚ script_nameZ script_filer r r Ú make_scriptÌs rSc Cs¤|tjd}tjj||ƒ}tj|dƒ}|dkr~|jtjƒ}t|ƒdkrr|ddkrrt t |ƒƒ}tjj |ƒ}|}n tjj |ƒ}|j ||ƒ|j ƒ|tjj||ƒfS)NÚziprGéÚ __pycache__éþÿÿÿ)rrIrJrKÚzipfileÚZipFileÚsplitÚseprrrÚbasenamerMr@) Úzip_dirÚ zip_basenamerRZ name_in_zipÚ zip_filenameÚzip_nameÚzip_fileÚpartsZ legacy_pycr r r Úmake_zip_scriptØs      rcr#cCstj|ƒt|d|ƒdS)NÚ__init__)rÚmkdirrS)Zpkg_dirZ init_sourcer r r Úmake_pkgís rfécs0g}t|ddƒ}|j|ƒtjj|ƒ} t|||ƒ} |j| ƒ|rjtj|dd�}tj| dd�} |j|| fƒ‡fdd„td|dƒDƒ} tjj | d tjj| ƒƒ} |tj d} tjj || ƒ}t j |d ƒ}x&| D]}tjj || ƒ}|j ||ƒqÎW|j | | ƒ|jƒx|D]}tj|ƒ�q W|tjj || ƒfS) Nrdr#T)Údoraisecsg|]}tjjˆg|ƒ‘qSr )rr[rK)Ú.0Úi)Úpkg_namer r ú ýsz make_zip_pkg..rgrTrGéÿÿÿÿ)rSr)rrJr\Ú py_compileÚcompiler-ÚrangerKrIrXrYrMr@Úunlink)r]r^rkrPrQZdepthZcompiledrqZ init_nameZ init_basenamerRZ pkg_namesZscript_name_in_zipr_r`raÚnameZinit_name_in_zipr )rkr Ú make_zip_pkgñs.         rs)rrr)F)N)r#)rgF) Ú collectionsrNr rZos.pathZtempfilerrnÚ contextlibZshutilrXÚimportlib.utilrZ test.supportrrrr Ú namedtuplerr7r9r:r;r/ZSTDOUTr?rErSrcrfrsr r r r Ús4 $3