-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathexecutable.rs
More file actions
228 lines (213 loc) · 6.86 KB
/
executable.rs
File metadata and controls
228 lines (213 loc) · 6.86 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
// 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::{
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
if env_path.ends_with(".pyenv/shims") {
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)
}
}
#[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()
));
}
}
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
}