-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathexecutable.rs
More file actions
309 lines (283 loc) · 9.69 KB
/
executable.rs
File metadata and controls
309 lines (283 loc) · 9.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
use lazy_static::lazy_static;
use log::trace;
use regex::Regex;
use std::ffi::OsStr;
use std::{
fs,
path::{Path, PathBuf},
};
lazy_static! {
static ref WINDOWS_EXE: Regex =
Regex::new(r"python(\d+\.?)*.exe").expect("error parsing Windows executable regex");
static ref UNIX_EXE: Regex =
Regex::new(r"python(\d+\.?)*$").expect("error parsing Unix executable regex");
}
#[cfg(windows)]
pub fn find_executable(env_path: &Path) -> Option<PathBuf> {
[
env_path.join("Scripts").join("python.exe"),
env_path.join("Scripts").join("python3.exe"),
env_path.join("bin").join("python.exe"),
env_path.join("bin").join("python3.exe"),
env_path.join("python.exe"),
env_path.join("python3.exe"),
]
.into_iter()
.find(|path| path.is_file())
}
#[cfg(unix)]
pub fn find_executable(env_path: &Path) -> Option<PathBuf> {
[
env_path.join("bin").join("python"),
env_path.join("bin").join("python3"),
env_path.join("python"),
env_path.join("python3"),
]
.into_iter()
.find(|path| path.is_file())
}
pub fn find_executables<T: AsRef<Path>>(env_path: T) -> Vec<PathBuf> {
let mut env_path = env_path.as_ref().to_path_buf();
// Never find exes in pyenv shims folder, they are not valid exes.
// Pyenv can be installed at custom locations (e.g., ~/.pl/pyenv via PYENV_ROOT),
// not just ~/.pyenv, so we check for any path ending with "shims" that has a
// parent directory containing "pyenv".
if is_pyenv_shims_dir(&env_path) {
return vec![];
}
let mut python_executables = vec![];
if cfg!(windows) {
// Only windows can have a Scripts folder
let bin = "Scripts";
if env_path.join(bin).exists() {
env_path = env_path.join(bin);
}
}
let bin = "bin"; // Windows can have bin as well, https://github.com/microsoft/vscode-python/issues/24792
if env_path.join(bin).exists() {
env_path = env_path.join(bin);
}
// If we have python.exe or python3.exe, then enumerator files in this directory
// We might have others like python 3.10 and python 3.11
// If we do not find python or python3, then do not enumerate, as its very expensive.
// This fn gets called from a number of places, e.g. to look scan all folders that are in PATH variable,
// & a few others, and scanning all of those dirs is every expensive.
let python_exe = if cfg!(windows) {
"python.exe"
} else {
"python"
};
let python3_exe = if cfg!(windows) {
"python3.exe"
} else {
"python3"
};
// On linux /home/linuxbrew/.linuxbrew/bin does not contain a `python` file
// If you install python@3.10, then only a python3.10 exe is created in that bin directory.
// As a compromise, we only enumerate if this is a bin directory and there are no python exes
// Else enumerating entire directories is very expensive.
if env_path.join(python_exe).exists()
|| env_path.join(python3_exe).exists()
|| env_path.ends_with(bin)
{
// Enumerate this directory and get all `python` & `pythonX.X` files.
if let Ok(entries) = fs::read_dir(env_path) {
for entry in entries.filter_map(Result::ok) {
let file = entry.path();
if file.is_file() && is_python_executable_name(&file) {
python_executables.push(file);
}
}
}
}
// Ensure the exe `python` is first, instead of `python3.10`
python_executables.sort();
python_executables
}
fn is_python_executable_name(exe: &Path) -> bool {
let name = exe
.file_name()
.unwrap_or_default()
.to_str()
.unwrap_or_default()
.to_lowercase();
if !name.starts_with("python") {
return false;
}
// Regex to match pythonX.X.exe
if cfg!(windows) {
WINDOWS_EXE.is_match(&name)
} else {
UNIX_EXE.is_match(&name)
}
}
/// Checks if the given path is a pyenv shims directory.
/// Pyenv shims are not valid Python executables - they are wrapper scripts that
/// redirect to the actual Python installation based on pyenv configuration.
/// Pyenv can be installed at custom locations via PYENV_ROOT (e.g., ~/.pl/pyenv),
/// not just the default ~/.pyenv location.
fn is_pyenv_shims_dir(path: &Path) -> bool {
// Must end with "shims"
if !path.ends_with("shims") {
return false;
}
// Check if parent directory name contains "pyenv" (case-insensitive)
// This handles: ~/.pyenv/shims, ~/.pl/pyenv/shims, /opt/pyenv/shims, etc.
if let Some(parent) = path.parent() {
if let Some(parent_name) = parent.file_name() {
if let Some(name_str) = parent_name.to_str() {
return name_str.to_lowercase().contains("pyenv");
}
}
}
false
}
pub fn should_search_for_environments_in_path<P: AsRef<Path>>(path: &P) -> bool {
// Never search in the .git folder
// Never search in the node_modules folder
// Mostly copied from https://github.com/github/gitignore/blob/main/Python.gitignore
let folders_to_ignore = [
"node_modules",
".cargo",
".devcontainer",
".github",
".git",
".tox",
".nox",
".hypothesis",
".ipynb_checkpoints",
".eggs",
".coverage",
".cache",
".pyre",
".ptype",
".pytest_cache",
".vscode",
"__pycache__",
"__pypackages__",
".mypy_cache",
"cython_debug",
"env.bak",
"venv.bak",
"Scripts", // If the folder ends bin/scripts, then ignore it, as the parent is most likely an env.
"bin", // If the folder ends bin/scripts, then ignore it, as the parent is most likely an env.
];
for folder in folders_to_ignore.iter() {
if path.as_ref().ends_with(folder) {
trace!("Ignoring folder: {:?}", path.as_ref());
return false;
}
}
true
}
#[cfg(target_os = "windows")]
pub fn new_silent_command(program: impl AsRef<OsStr>) -> std::process::Command {
use std::os::windows::process::CommandExt;
const CREATE_NO_WINDOW: u32 = 0x08000000;
let mut command = std::process::Command::new(program);
command.creation_flags(CREATE_NO_WINDOW);
command
}
#[cfg(not(target_os = "windows"))]
pub fn new_silent_command(program: impl AsRef<OsStr>) -> std::process::Command {
std::process::Command::new(program)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn is_python_executable_test() {
#[cfg(unix)]
assert!(is_python_executable_name(PathBuf::from("python").as_path()));
#[cfg(unix)]
assert!(is_python_executable_name(
PathBuf::from("python3").as_path()
));
#[cfg(unix)]
assert!(is_python_executable_name(
PathBuf::from("python3.1").as_path()
));
#[cfg(unix)]
assert!(is_python_executable_name(
PathBuf::from("python3.10").as_path()
));
#[cfg(unix)]
assert!(is_python_executable_name(
PathBuf::from("python4.10").as_path()
));
#[cfg(windows)]
assert!(is_python_executable_name(
PathBuf::from("python.exe").as_path()
));
#[cfg(windows)]
assert!(is_python_executable_name(
PathBuf::from("python3.exe").as_path()
));
#[cfg(windows)]
assert!(is_python_executable_name(
PathBuf::from("python3.1.exe").as_path()
));
#[cfg(windows)]
assert!(is_python_executable_name(
PathBuf::from("python3.10.exe").as_path()
));
#[cfg(windows)]
assert!(is_python_executable_name(
PathBuf::from("python4.10.exe").as_path()
));
}
#[test]
fn is_not_python_executable_test() {
#[cfg(unix)]
assert!(!is_python_executable_name(
PathBuf::from("pythonw").as_path()
));
#[cfg(unix)]
assert!(!is_python_executable_name(
PathBuf::from("pythonw3").as_path()
));
#[cfg(windows)]
assert!(!is_python_executable_name(
PathBuf::from("pythonw.exe").as_path()
));
#[cfg(windows)]
assert!(!is_python_executable_name(
PathBuf::from("pythonw3.exe").as_path()
));
}
#[test]
fn test_is_pyenv_shims_dir() {
// Standard pyenv location
assert!(is_pyenv_shims_dir(
PathBuf::from("/home/user/.pyenv/shims").as_path()
));
// Custom pyenv location (issue #238)
assert!(is_pyenv_shims_dir(
PathBuf::from("/home/user/.pl/pyenv/shims").as_path()
));
// Other custom locations
assert!(is_pyenv_shims_dir(
PathBuf::from("/opt/pyenv/shims").as_path()
));
assert!(is_pyenv_shims_dir(
PathBuf::from("/usr/local/pyenv/shims").as_path()
));
// pyenv-win style (parent contains "pyenv")
assert!(is_pyenv_shims_dir(
PathBuf::from("/home/user/.pyenv/pyenv-win/shims").as_path()
));
// Not pyenv shims (should return false)
assert!(!is_pyenv_shims_dir(
PathBuf::from("/home/user/.pyenv/versions/3.10.0/bin").as_path()
));
assert!(!is_pyenv_shims_dir(PathBuf::from("/usr/bin").as_path()));
assert!(!is_pyenv_shims_dir(
PathBuf::from("/home/user/shims").as_path()
)); // "shims" but parent is not pyenv
assert!(!is_pyenv_shims_dir(
PathBuf::from("/home/user/project/shims").as_path()
));
}
}