o nnh@sdZddlmZddlmZddlZddlZddlZddl Z ddl Z ddl m Z ddlmZmZmZmZmZmZmZmZddlmZmZddlmZmZdd lmZmZd%ddZ  d&d'ddZ!d(ddZ"d)d d!Z#d*d#d$Z$dS)+zVirtual environment management.) annotationsN)EnvironmentConfig) find_pythonSubprocessErrorANSIBLE_TEST_TARGET_TOOLS_ROOTdisplay remove_treeApplicationErrorstr_to_version raw_command) run_command ResultType)VirtualPythonConfig PythonConfig)collect_bootstraprun_pipargsrpythonrreturncCs|jrd}nd}tjtjjdd|j|}tj|d}t|jtj|dd|jd}tj|rAt j d|j|fd d n t |t ||||jsVt d |jd t|}t|||d t||S)zRCreate a virtual environment for the given Python and return the path to its root.z-ssp delegationrz marker.txtbin)versionpathsystem_site_packagesz0Using existing Python %s virtual environment: %sr verbosityzPython z. does not provide virtual environment support.N)rosrjoinrTMPrrexistsrinfor create_virtual_environmentr rrpathlibPathtouch)rrsuffixvirtual_environment_pathvirtual_environment_markervirtual_environment_pythoncommandsr,F/usr/local/lib/python3.10/dist-packages/ansible_test/_internal/venv.pyget_virtual_python*s&  r.FrrstrrboolpipcCsTtj|js dSt|jD]}t|||||r'tjd|j|fdddSqdS)zICreate a virtual environment using venv for the requested Python version.Fz6Created Python %s virtual environment using "venv": %srrT)rrr!iterate_real_pythonsrrun_venvrr")rrrrr1 real_pythonr,r,r-r#Psr#rc.Iterable[str]ccst|}d}|tjdt|kr*tj}t|}|r%t|tj |d}|r*|Vtj dtjj }|s7dSt||}|s@dS||krFdSt|}|rVt|tj |d}|r]|VdSdS)z Iterate through available real python interpreters of the requested version. The current interpreter will be checked and then the path will be searched. NrPATH) r sys version_infolen executableget_python_real_prefixrrrrenvirongetdefpath)rr8current_python real_prefixr found_pythonr,r,r-r2is0  r2 python_patht.Optional[str]cCs<|tjtjtdg}tt|ddd}|d}|S)z Return the real prefix of the specified interpreter or None if the interpreter is not a virtual environment created by 'virtualenv'. zvirtualenvcheck.pyTcapturerr@)rrrrjsonloadsr )rBcmd check_resultr@r,r,r-r;sr; run_pythonc Cs|ddg}|r |d|s|d||z t||ddWdStyD}zt||jdkr9t|jWYd}~d Sd}~ww) z5Create a virtual environment using the 'venv' module.z-mvenvz--system-site-packagesz --without-pipTrDrNF)appendr rr rrerrormessage)rrJrr1rrHexr,r,r-r3s       r3)rrrrrr)FF) rrrrrr/rr0r1r0rr0)rr/rr5)rBr/rrC) rrrJr/rr0r1r0rr/rr0)%__doc__ __future__rcollections.abcabccrFrr$r7typingtconfigrutilrrrrr r r r util_commonr r host_configsrrpython_requirementsrrr.r#r2r;r3r,r,r,r-s(   (  *  (