|
6 | 6 | import sys
|
7 | 7 | from pathlib import Path
|
8 | 8 |
|
| 9 | +# Environment variables and constants |
9 | 10 | PYENV_ROOT = os.path.expanduser(
|
10 | 11 | os.path.expandvars(os.environ.get("PYENV_ROOT", "~/.pyenv"))
|
11 | 12 | )
|
12 | 13 | PYENV_ROOT = Path(PYENV_ROOT)
|
13 | 14 | PYENV_INSTALLED = shutil.which("pyenv") is not None
|
| 15 | + |
14 | 16 | ASDF_DATA_DIR = os.path.expanduser(
|
15 | 17 | os.path.expandvars(os.environ.get("ASDF_DATA_DIR", "~/.asdf"))
|
16 | 18 | )
|
17 | 19 | ASDF_INSTALLED = shutil.which("asdf") is not None
|
18 |
| -IS_64BIT_OS = None |
| 20 | + |
19 | 21 | SYSTEM_ARCH = platform.architecture()[0]
|
| 22 | +IS_64BIT_OS = None |
20 | 23 |
|
21 | 24 | if sys.maxsize > 2**32:
|
22 | 25 | IS_64BIT_OS = platform.machine() == "AMD64"
|
23 | 26 | else:
|
24 | 27 | IS_64BIT_OS = False
|
25 | 28 |
|
26 |
| - |
27 | 29 | IGNORE_UNSUPPORTED = bool(os.environ.get("PYTHONFINDER_IGNORE_UNSUPPORTED", False))
|
28 |
| -SUBPROCESS_TIMEOUT = os.environ.get("PYTHONFINDER_SUBPROCESS_TIMEOUT", 5) |
29 |
| -"""The default subprocess timeout for determining python versions |
| 30 | +SUBPROCESS_TIMEOUT = int(os.environ.get("PYTHONFINDER_SUBPROCESS_TIMEOUT", 5)) |
30 | 31 |
|
31 |
| -Set to **5** by default. |
32 |
| -""" |
33 | 32 |
|
34 |
| - |
35 |
| -def set_asdf_paths(): |
| 33 | +def get_python_paths() -> list[str]: |
| 34 | + """ |
| 35 | + Get a list of paths where Python executables might be found. |
| 36 | + |
| 37 | + Returns: |
| 38 | + A list of paths to search for Python executables. |
| 39 | + """ |
| 40 | + paths = [] |
| 41 | + |
| 42 | + # Add paths from PATH environment variable |
| 43 | + if "PATH" in os.environ: |
| 44 | + paths.extend(os.environ["PATH"].split(os.pathsep)) |
| 45 | + |
| 46 | + # Add pyenv paths if installed |
| 47 | + if PYENV_INSTALLED: |
| 48 | + pyenv_paths = get_pyenv_paths() |
| 49 | + paths.extend(pyenv_paths) |
| 50 | + |
| 51 | + # Add asdf paths if installed |
36 | 52 | if ASDF_INSTALLED:
|
37 |
| - python_versions = os.path.join(ASDF_DATA_DIR, "installs", "python") |
38 |
| - try: |
39 |
| - # Get a list of all files and directories in the given path |
40 |
| - all_files_and_dirs = os.listdir(python_versions) |
41 |
| - # Filter out files and keep only directories |
42 |
| - for name in all_files_and_dirs: |
43 |
| - if os.path.isdir(os.path.join(python_versions, name)): |
44 |
| - asdf_path = os.path.join(python_versions, name) |
45 |
| - asdf_path = os.path.join(asdf_path, "bin") |
46 |
| - os.environ["PATH"] = asdf_path + os.pathsep + os.environ["PATH"] |
47 |
| - except FileNotFoundError: |
48 |
| - pass |
| 53 | + asdf_paths = get_asdf_paths() |
| 54 | + paths.extend(asdf_paths) |
| 55 | + |
| 56 | + # Add Windows registry paths if on Windows |
| 57 | + if os.name == "nt": |
| 58 | + from .finders.windows_registry import get_registry_python_paths |
| 59 | + registry_paths = get_registry_python_paths() |
| 60 | + paths.extend(registry_paths) |
| 61 | + |
| 62 | + return paths |
49 | 63 |
|
50 | 64 |
|
51 |
| -def set_pyenv_paths(): |
52 |
| - if PYENV_INSTALLED: |
53 |
| - python_versions = os.path.join(PYENV_ROOT, "versions") |
54 |
| - is_windows = os.name == "nt" |
55 |
| - try: |
56 |
| - # Get a list of all files and directories in the given path |
57 |
| - all_files_and_dirs = os.listdir(python_versions) |
58 |
| - # Filter out files and keep only directories |
59 |
| - for name in all_files_and_dirs: |
60 |
| - if os.path.isdir(os.path.join(python_versions, name)): |
61 |
| - pyenv_path = os.path.join(python_versions, name) |
62 |
| - if not is_windows: |
63 |
| - pyenv_path = os.path.join(pyenv_path, "bin") |
64 |
| - os.environ["PATH"] = pyenv_path + os.pathsep + os.environ["PATH"] |
65 |
| - except FileNotFoundError: |
66 |
| - pass |
| 65 | +def get_pyenv_paths() -> list[str]: |
| 66 | + """ |
| 67 | + Get a list of paths where pyenv Python executables might be found. |
| 68 | + |
| 69 | + Returns: |
| 70 | + A list of paths to search for pyenv Python executables. |
| 71 | + """ |
| 72 | + paths = [] |
| 73 | + python_versions = os.path.join(PYENV_ROOT, "versions") |
| 74 | + is_windows = os.name == "nt" |
| 75 | + |
| 76 | + try: |
| 77 | + # Get a list of all files and directories in the given path |
| 78 | + all_files_and_dirs = os.listdir(python_versions) |
| 79 | + # Filter out files and keep only directories |
| 80 | + for name in all_files_and_dirs: |
| 81 | + version_path = os.path.join(python_versions, name) |
| 82 | + if os.path.isdir(version_path): |
| 83 | + if not is_windows: |
| 84 | + version_path = os.path.join(version_path, "bin") |
| 85 | + paths.append(version_path) |
| 86 | + except FileNotFoundError: |
| 87 | + pass |
| 88 | + |
| 89 | + return paths |
| 90 | + |
| 91 | + |
| 92 | +def get_asdf_paths() -> list[str]: |
| 93 | + """ |
| 94 | + Get a list of paths where asdf Python executables might be found. |
| 95 | + |
| 96 | + Returns: |
| 97 | + A list of paths to search for asdf Python executables. |
| 98 | + """ |
| 99 | + paths = [] |
| 100 | + python_versions = os.path.join(ASDF_DATA_DIR, "installs", "python") |
| 101 | + |
| 102 | + try: |
| 103 | + # Get a list of all files and directories in the given path |
| 104 | + all_files_and_dirs = os.listdir(python_versions) |
| 105 | + # Filter out files and keep only directories |
| 106 | + for name in all_files_and_dirs: |
| 107 | + version_path = os.path.join(python_versions, name) |
| 108 | + if os.path.isdir(version_path): |
| 109 | + bin_path = os.path.join(version_path, "bin") |
| 110 | + paths.append(bin_path) |
| 111 | + except FileNotFoundError: |
| 112 | + pass |
| 113 | + |
| 114 | + return paths |
0 commit comments