33import os
44import platform
55import sys
6+ import posixpath
7+ import ntpath
8+ import re
9+ import shutil
610
711
812def is_type_checking ():
@@ -13,16 +17,19 @@ def is_type_checking():
1317 return TYPE_CHECKING
1418
1519
16- PYENV_INSTALLED = bool (os .environ .get ("PYENV_SHELL" )) or bool (
17- os .environ .get ("PYENV_ROOT" )
18- )
19- ASDF_INSTALLED = bool (os .environ .get ("ASDF_DIR" ))
2020PYENV_ROOT = os .path .expanduser (
2121 os .path .expandvars (os .environ .get ("PYENV_ROOT" , "~/.pyenv" ))
2222)
23+ # Check if the path is in Unix-style (Git Bash)
24+ if PYENV_ROOT .startswith ('/' ) and os .name == 'nt' :
25+ # Convert to Windows-style path
26+ drive , tail = re .match (r"^/([a-zA-Z])/(.*)" , PYENV_ROOT ).groups ()
27+ PYENV_ROOT = drive .upper () + ":\\ " + tail .replace ('/' , '\\ ' )
28+ PYENV_INSTALLED = shutil .which ("pyenv" ) != None
2329ASDF_DATA_DIR = os .path .expanduser (
2430 os .path .expandvars (os .environ .get ("ASDF_DATA_DIR" , "~/.asdf" ))
2531)
32+ ASDF_INSTALLED = shutil .which ("asdf" ) != None
2633IS_64BIT_OS = None
2734SYSTEM_ARCH = platform .architecture ()[0 ]
2835
@@ -41,10 +48,50 @@ def is_type_checking():
4148"""
4249
4350
44- def get_shim_paths ():
45- shim_paths = []
51+ def join_path_for_platform (path , path_parts ):
52+ # If we're on Unix or Unix-like system
53+ if os .name == 'posix' or sys .platform == 'linux' :
54+ return posixpath .join (path , * path_parts )
55+ # If we're on Windows
56+ elif os .name == 'nt' or sys .platform == 'win32' :
57+ return ntpath .join (path , * path_parts )
58+ else :
59+ raise Exception ("Unknown environment" )
60+
61+
62+ def set_asdf_paths ():
4663 if ASDF_INSTALLED :
47- shim_paths .append (os .path .join (ASDF_DATA_DIR , "shims" ))
64+ python_versions = join_path_for_platform (ASDF_DATA_DIR , ["installs" , "python" ])
65+ try :
66+ # Get a list of all files and directories in the given path
67+ all_files_and_dirs = os .listdir (python_versions )
68+ # Filter out files and keep only directories
69+ for name in all_files_and_dirs :
70+ if os .path .isdir (os .path .join (python_versions , name )):
71+ asdf_path = os .path .join (python_versions , name )
72+ asdf_path = os .path .join (asdf_path , "bin" )
73+ os .environ ['PATH' ] = asdf_path + os .pathsep + os .environ ['PATH' ]
74+ except FileNotFoundError :
75+ pass
76+
77+
78+ def set_pyenv_paths ():
4879 if PYENV_INSTALLED :
49- shim_paths .append (os .path .join (PYENV_ROOT , "shims" ))
50- return [os .path .normpath (os .path .normcase (p )) for p in shim_paths ]
80+ is_windows = False
81+ if os .name == "nt" :
82+ python_versions = join_path_for_platform (PYENV_ROOT , ["pyenv-win" , "versions" ])
83+ is_windows = True
84+ else :
85+ python_versions = join_path_for_platform (PYENV_ROOT , ["versions" ])
86+ try :
87+ # Get a list of all files and directories in the given path
88+ all_files_and_dirs = os .listdir (python_versions )
89+ # Filter out files and keep only directories
90+ for name in all_files_and_dirs :
91+ if os .path .isdir (os .path .join (python_versions , name )):
92+ pyenv_path = os .path .join (python_versions , name )
93+ if not is_windows :
94+ pyenv_path = os .path .join (pyenv_path , "bin" )
95+ os .environ ['PATH' ] = pyenv_path + os .pathsep + os .environ ['PATH' ]
96+ except FileNotFoundError :
97+ pass
0 commit comments