U ec @sZdZddlmZddlZddlZddlZddlZddlZddlZddl Z ddl Z ddl m Z m Z ddlmZe dddd d d gZd d ZeZedZGdddZedkrVejddZeedkredZeddZndZeedkredZeddZndZejddee_eZejdedddeedddfdS)z The PythonInfo contains information about a concrete instance of a Python interpreter. Note: this file is also used to query target interpreters, so can only use standard library methods ) annotationsN) OrderedDict namedtuple)digits VersionInfomajorminormicro releaselevelserialcCs(ttdtjddtjS)NPATHEXT)r ) listrfromkeysosenvirongetlowersplitpathseprrJ/opt/hc_python/lib/python3.8/site-packages/virtualenv/discovery/py_info.py_get_path_extensionssrz\{\w+\}c@seZdZdZddddZddZdd Zed d Ze d d Z e ddZ e ddZ e ddZ e ddZdejfddZdJddZe ddZe ddZe d d!Zd"dd#d$Zd"dd%d&Ze d'd(Zed)d*Zd+d,ZdZdZedKd-d.ZedLd/d0Zd1d2Z d3d4Z!edMd6d7Z"ed8d9Z#ed:d;Z$edd?Z'd@dAZ(edBdCZ)dDdEZ*dFdGZ+dHdIZ,dS)O PythonInfoz.Contains information for a Python interpreter.None)returnc sdd}tj_t_jdkr0ttj_ttj_tjdkrJdnd_ t d_ tj _ tj_|ttdd_|ttd d_|ttd d_|ttd d_|ttd d_|tj_|j__ztd d}Wntk rd}YnX|_tj_t_ttj dd_!t "}d |krtd _#fddt $D_%i_&nltjdddkrd|krd_#fddt $D_%i_&n(d_#ddt $D_%'(_&tt dtt dd}ddd|ffD_ t)}j%*D]*}t+,|D]}|-|ddq*q|-dd d|D_.fd!dj./D}0d"|_10d#|_2ttd$ttd%d_3d_4dS)&NcSs|dkr dStj|SN)rpathabspath)vrrrabs_path#sz%PythonInfo.__init__..abs_pathPyPyl@ py_version_nodotprefix base_prefix real_prefixbase_exec_prefix exec_prefixvenvTFencodingcs i|]}|tj|djdqSF)expandscheme sysconfigget_pathsysconfig_scheme.0iselfrr Rsz'PythonInfo.__init__..) deb_system posix_prefixcs i|]}|tj|djdqSr,r/r3r6rrr8\scSsi|]}|tj|ddqS)F)r-)r0r1r3rrrr8csget_makefile_filenameZ_get_makefile_filenamecSsi|]\}}|dk r||qSrrr4krrrrr8hsZmakefile_filenamePYTHONFRAMEWORKcSsi|]}|t|pdqSr )r0get_config_varr3rrrr8wscs0i|](\}}||dk r(|jr(jn|qSr startswithr% system_prefixr?r6rrr8ysstdlib platstdlibmaxsizeZmaxint)5sysplatformpython_implementationimplementationtuplepypy_version_infor version_inforK architecturer0rEZ version_nodotversionrnamegetattrr%r&r'r(r) executableoriginal_executable_fast_get_system_executablesystem_executable __import__ ImportErrorZhas_venvrgetfilesystemencodingfile_system_encodingstdoutstdout_encodingget_scheme_namesr2get_path_namessysconfig_pathsdistutils_install_distutils_installcopysetvalues _CONF_VAR_REfindalladdsysconfig_varsitemssysconfig_pathZ system_stdlibZsystem_stdlib_platformmax_size _creators) r7r hasZ scheme_namesmakefileZconfig_var_keyselementr@Zconfsrr6r__init__"sz                zPythonInfo.__init__cs|js|jdk r|j|jkr|jdkrttdd}|dk rtj|krtj|rT|S|j j |j j }}|jdkr||fdkrtj |fddd|d|d|fDD]}tj|r|SqdS|j S) z?Try to get the system executable by just looking at properties.N_base_executableposix)r: csg|]}tj|qSr)rrjoin)r4exebase_dirrr sz:PythonInfo._fast_get_system_executable..python.)r'r&r%rVrLrWrrexistsrRrrdirnamerX)r7Zbase_executablerrrrzrrYs*          z&PythonInfo._fast_get_system_executablecsZ|j|}|dkrV|j|j|j|jffdd|jD}|j||d t j }|S)Ncs"i|]\}}||krdn|qSrDrr?prefixesrrr8sz+PythonInfo.install_path..) config_var) rdrr%r)r&r(rlrmrnlstriprsep)r7keyresultrrrr install_paths  zPythonInfo.install_pathc stRtdzddlm}ddlm}Wn$tk rTiYW5QRSXW5QRX|ddi}t t dr~dt _ ttd|j dd d W5QRXt j_fd d |DS) Nignorer)dist) SCHEME_KEYS script_argsz --no-user-cfg _frameworkinstallT)createcs.i|]&}|td|ddtjqS)install_rAN)rVrrr)r4rr5rrr8sz1PythonInfo._distutils_install..)warningscatch_warnings simplefilter distutilsrdistutils.command.installrr\ DistributionhasattrrLrget_command_objrrr%finalize_options)rrdrrrres        zPythonInfo._distutils_installcCsddd|jddDS)Nr~css|]}t|VqdSrstrr3rrr sz)PythonInfo.version_str..rr:rxrRr6rrr version_strszPythonInfo.version_strcCsddd|jddDS)Nr~css|]}t|VqdSrrr3rrrrsz1PythonInfo.version_release_str..rr9rr6rrrversion_release_strszPythonInfo.version_release_strcCs|j}d|jd|jS)Nr}r~)rRrr)r7rRrrr python_nameszPythonInfo.python_namecCs |jdk Sr)r'r6rrris_old_virtualenvszPythonInfo.is_old_virtualenvcCs |jdk Sr)r&r6rrris_venvszPythonInfo.is_venvNcCsF|j|}|dkr|j}n|j}|||}|jf|d|S)N/)rcrlrfupdateformatreplace)r7rrrpatternbaserrrrns   zPythonInfo.sysconfig_pathFcCs0|jdks|dkr*ddlm}|||_|jS)NTr)CreatorSelector)rpZvirtualenv.run.plugin.creatorsrZfor_interpreter)r7refreshrrrrcreatorss  zPythonInfo.creatorscs^dfddjD}tj|sZtjjtj d}tj|rZ|}|S)Nincludecs0i|](\}}||dk r(|jr(jn|qSrrFr?r6rrr8sz-PythonInfo.system_include..headers) rnrlrmrrrrxr%rr)r7rfallbackrr6rsystem_includes   zPythonInfo.system_includecCs|jp|jp|jSr)r'r&r%r6rrrrHszPythonInfo.system_prefixcCs|jp|jp|jSr)r'r(r)r6rrrsystem_exec_prefixszPythonInfo.system_exec_prefixrcCs d|jjdd|jDS)Nz{}({!r})cSs i|]\}}|ds||qS)_)rGr?rrrr8s z'PythonInfo.__repr__..)r __class____name____dict__rmr6rrr__repr__szPythonInfo.__repr__cCsd|jjdddd|jf|jdk r6|j|jkr6dnd|jf|j|j|jhkrTdnd|jfd|jfd |jfd t |j fd |j d |j ffDS) Nz{}({})z, css(|] \}}|dk r|d|VqdS)N=rr?rrrr sz%PythonInfo.__str__..specsystemoriginalryrMrTZencoding_fs_io-) rrrrxrrZrWrXrMreprrTr^r`r6rrr__str__s.   zPythonInfo.__str__cCs$d|jddd|jD|jS)Nz{}{}-{}r~css|]}t|VqdSrrr3rrrr%sz"PythonInfo.spec..)rrOrxrRrSr6rrrr#szPythonInfo.speccCs"ddlm}|||jdS)Nr)clear)#virtualenv.discovery.cached_py_infor_cache_exe_discovery)clsapp_datarrrr clear_cache's zPythonInfo.clear_cachecCs|jrz|jtj|jkrdS|jsztj|j}|j}tjdkrntj |\}}| |rn|dt | }||krzdS|r|j dk r|j |j krdS|jdk r|j|jkrdSt|jdd|j|j|jfD]&\}}|dk r|dk r||krdSqdS)zXCheck if a given specification can be satisfied by the this python interpreter instance.Twin32NFrr:)rrWrrZis_absbasenamerXrLrMsplitextendswithlenrOrrSziprRrrr )r7rZimpl_must_matchrZ spec_pathsuffixZourreqrrr satisfies/s2  (zPythonInfo.satisfiescCs&|jdkr |jtj|ddd|_|jS) This locates the current host interpreter information. This might be different than what we run into in case the host python has been upgraded from underneath us. NTFraise_on_errorresolve_to_host)_currentfrom_exerLrWrrrrrcurrentQs zPythonInfo.currentcCs&|jdkr |jtj|ddd|_|jS)rNTr)_current_systemrrLrWrrrrcurrent_system[s zPythonInfo.current_systemcCstj|ddS)Nr9)indent)jsondumps_to_dictr6rrr_to_jsoneszPythonInfo._to_jsoncs*fddtD}|d|d<|S)Ncs$i|]}||dkrt|ndqS)rpN)rV)r4varr6rrr8jsz'PythonInfo._to_dict..rR)vars_asdict)r7datarr6rriszPythonInfo._to_dictTc Csddlm}|dkrtjn|}|||||||d}t|tr|rz|||}Wn<tk r} z|rht d|j | d}W5d} ~ XYnX|S)z9Given a path to an executable get the python information.r)rN)envr ignore_cachez-ignore %s due cannot resolve system due to %r) rrrr isinstancer_resolve_to_system ExceptionlogginginforX) rryrrrrrrZproposed exceptionrrrros zPythonInfo.from_execCst|}||Sr)rloads _from_dictrf)rpayloadrawrrr _from_jsons zPythonInfo._from_jsoncCs&tf|d|d<|}||_|S)NrR)rrfr)rrrrrrrs zPythonInfo._from_dictc Cs|j}t}|jdkr|jp&|jp&|j}||krt|dkrRtd||j|_qt | ddD]\}\}}t d|||qbt dt|d||d d |} t| |||<|j||dd}q |j|jkr||j|}||_|S) NrAz$%r links back to itself via prefixes)startz%d: prefix=%s, info=%rz prefixes are causing a circle {}|F)r%exact)rWrrZr'r&r%rrr enumeratermerrorrrxkeys RuntimeError discover_exer) rrtargetZstart_executablerr%atptmsgrrrrs(    zPythonInfo._resolve_to_systemc Cs||f}||jkr6|r6td|||j||j|Std|||}||}g}|dkrhtjn|}|D]@} |D]6} ||| | |||} | dk rx| |j|<| Sqxqp|dkr|r|||} tj |} | |j|<td| | | Sd d |tj |} t | dS)Nz)discover exe from cache %s - exact %s: %rzdiscover exe for %s in %sFzFno exact match found, chosen most similar of %s within base folders %szfailed to detect {} in {}r) rrdebug_find_possible_exe_names_find_possible_foldersrr _check_exe_select_most_likelyrrxrr)r7rr%rrrZpossible_namesZpossible_folders discoveredfolderrUrfoldersrrrrrs.       zPythonInfo.discover_exec Cstj||}tj|sdS|j||dd|d}|dkr>dSdD]} t|| } t|| } | | krB| dkrddd| Dddd| D} } |j} td| | | | |dkr| |qqB|SdS) NF)rrr)rOrSrRrRr~css|]}t|VqdSrrr3rrrrsz(PythonInfo._check_exe..z2refused interpreter %s because %s differs %s != %s) rrrxrrrVrWrrappend) r7rrrUrrrZexe_pathritemfoundZsearchedrWrrrrs&   * zPythonInfo._check_execs"fdd}t||dd}|dS)Ncsx|jjk|jjjjk|jjjjk|jjk|jjjjk|jjjjk|jjjjkg}tddt t |DS)Ncss"|]\}}|rd|>ndVqdS)rArNr)r4posmatchrrrrszBPythonInfo._select_most_likely..sort_by..) rOrRrrrSr r r sumrreversed)rmatchesrrrsort_bys   z/PythonInfo._select_most_likely..sort_byT)rreverser)sorted)rrr Zsorted_discoveredrr rrs zPythonInfo._select_most_likelycCst}t}d|tj|j<d||j<d|tj|j<d||j<|D]>}tj|}||jrH|t |jd}d|||<qHd||<dd|DS)NcSsg|]}tj|r|qSr)rrrr3rrrr|s z5PythonInfo._find_possible_folders..) rrrrealpathrWrXrrGr%r)r7Z inside_folderZcandidate_folderZ executablesryrrelativerrrrs    z!PythonInfo._find_possible_folderscCst}|D]h}dD]^}ddd|jd|D}d|jdfD]*}tD] }||||}d||<qPqHqqt|S)N)r:r9rArr~css|]}t|VqdSrrr3rrrr sz6PythonInfo._find_possible_exe_names..rr )r_possible_baserxrRrS EXTENSIONSrr)r7Zname_candidaterUrrTarchext candidaterrrrs z#PythonInfo._find_possible_exe_namesccst}tjtj|jdt}d||<d||j<d|krF|d=d|d<|D]H}| }|Vddl m }|rR||kr|V| }||krR|VqRdS)Nrr})fs_is_case_sensitive) rrrrrrWrstriprrOrZvirtualenv.inforupper)r7Z possible_baserrrrrrrrrs"   zPythonInfo._possible_base)F)N)N)NTFTN)TN)-r __module__ __qualname____doc__rtrYr staticmethodrepropertyrrrrrrrrnrrrHrrrr classmethodrrrrrrrrrrrrrrrrrrrrrrrrsx`                      r__main__rAr rB) r __future__rrrrrMrerLr0r collectionsrrstringrrrrcompilerirrargvrZ start_cookieZ end_cookierrr_writerxrrrrsB