-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathlib.rs
More file actions
399 lines (339 loc) · 12.8 KB
/
lib.rs
File metadata and controls
399 lines (339 loc) · 12.8 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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
use std::path::Path;
use pet_core::{
env::PythonEnv,
python_environment::{PythonEnvironment, PythonEnvironmentBuilder, PythonEnvironmentKind},
pyvenv_cfg::PyVenvCfg,
reporter::Reporter,
Locator, LocatorKind,
};
use pet_python_utils::executable::{find_executable_or_broken, find_executables, ExecutableResult};
use pet_python_utils::version;
fn is_venv_internal(env: &PythonEnv) -> Option<bool> {
// env path cannot be empty.
Some(
PyVenvCfg::find(env.executable.parent()?).is_some()
|| PyVenvCfg::find(&env.prefix.clone()?).is_some(),
)
}
pub fn is_venv(env: &PythonEnv) -> bool {
is_venv_internal(env).unwrap_or_default()
}
pub fn is_venv_dir(path: &Path) -> bool {
PyVenvCfg::find(path).is_some()
}
/// Tries to create a PythonEnvironment from a directory that might be a venv.
/// This function can detect broken environments (e.g., with broken symlinks)
/// and will return them with an error field set.
pub fn try_environment_from_venv_dir(path: &Path) -> Option<PythonEnvironment> {
// Check if this is a venv directory
let cfg = PyVenvCfg::find(path)?;
let prefix = path.to_path_buf();
let version = version::from_creator_for_virtual_env(&prefix).or(cfg.version.clone());
let name = cfg.prompt;
match find_executable_or_broken(path) {
ExecutableResult::Found(executable) => {
let symlinks = find_executables(&prefix);
Some(
PythonEnvironmentBuilder::new(Some(PythonEnvironmentKind::Venv))
.name(name)
.executable(Some(executable))
.version(version)
.prefix(Some(prefix))
.symlinks(Some(symlinks))
.build(),
)
}
ExecutableResult::Broken(executable) => Some(
PythonEnvironmentBuilder::new(Some(PythonEnvironmentKind::Venv))
.name(name)
.executable(Some(executable))
.version(version)
.prefix(Some(prefix))
.error(Some("Python executable is a broken symlink".to_string()))
.build(),
),
ExecutableResult::NotFound => {
// pyvenv.cfg exists but no Python executable found at all
Some(
PythonEnvironmentBuilder::new(Some(PythonEnvironmentKind::Venv))
.name(name)
.version(version)
.prefix(Some(prefix))
.error(Some("Python executable not found".to_string()))
.build(),
)
}
}
}
pub struct Venv {}
impl Venv {
pub fn new() -> Venv {
Venv {}
}
}
impl Default for Venv {
fn default() -> Self {
Self::new()
}
}
impl Locator for Venv {
fn get_kind(&self) -> LocatorKind {
LocatorKind::Venv
}
fn supported_categories(&self) -> Vec<PythonEnvironmentKind> {
vec![PythonEnvironmentKind::Venv]
}
fn try_from(&self, env: &PythonEnv) -> Option<PythonEnvironment> {
if is_venv(env) {
let mut prefix = env.prefix.clone();
if prefix.is_none() {
prefix = Some(env.executable.parent()?.parent()?.to_path_buf());
}
let version = match env.version {
Some(ref v) => Some(v.clone()),
None => match &prefix {
Some(prefix) => version::from_creator_for_virtual_env(prefix),
None => None,
},
};
let mut symlinks = vec![];
if let Some(ref prefix) = prefix {
symlinks.append(&mut find_executables(prefix));
}
// Get the name from the prefix if it exists.
let cfg = PyVenvCfg::find(env.executable.parent()?)
.or_else(|| PyVenvCfg::find(&env.prefix.clone()?));
let name = cfg.and_then(|cfg| cfg.prompt);
Some(
PythonEnvironmentBuilder::new(Some(PythonEnvironmentKind::Venv))
.name(name)
.executable(Some(env.executable.clone()))
.version(version)
.prefix(prefix)
.symlinks(Some(symlinks))
.build(),
)
} else {
None
}
}
fn find(&self, _reporter: &dyn Reporter) {
// There are no common global locations for virtual environments.
// We expect the user of this class to call `is_compatible`
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::fs;
use std::io::Write;
use tempfile::tempdir;
#[test]
fn test_try_environment_from_venv_dir_not_a_venv() {
// A directory without pyvenv.cfg should return None
let temp_dir = std::env::temp_dir().join("pet_test_not_a_venv");
let _ = fs::remove_dir_all(&temp_dir);
fs::create_dir_all(&temp_dir).unwrap();
let result = try_environment_from_venv_dir(&temp_dir);
assert!(result.is_none());
let _ = fs::remove_dir_all(&temp_dir);
}
#[test]
fn test_try_environment_from_venv_dir_missing_executable() {
// A venv with pyvenv.cfg but no Python executable
let temp_dir = std::env::temp_dir().join("pet_test_venv_no_exe");
let _ = fs::remove_dir_all(&temp_dir);
fs::create_dir_all(&temp_dir).unwrap();
// Create pyvenv.cfg
fs::write(
temp_dir.join("pyvenv.cfg"),
"version = 3.10.0\nprompt = test-env\n",
)
.unwrap();
let result = try_environment_from_venv_dir(&temp_dir);
assert!(result.is_some());
let env = result.unwrap();
assert_eq!(env.kind, Some(PythonEnvironmentKind::Venv));
assert!(env.error.is_some());
assert!(env.error.unwrap().contains("not found"));
assert_eq!(env.name, Some("test-env".to_string()));
let _ = fs::remove_dir_all(&temp_dir);
}
#[test]
fn test_try_environment_from_venv_dir_valid() {
// A valid venv with pyvenv.cfg and Python executable
let temp_dir = std::env::temp_dir().join("pet_test_venv_valid");
let _ = fs::remove_dir_all(&temp_dir);
#[cfg(windows)]
let bin_dir = temp_dir.join("Scripts");
#[cfg(unix)]
let bin_dir = temp_dir.join("bin");
fs::create_dir_all(&bin_dir).unwrap();
// Create pyvenv.cfg
fs::write(
temp_dir.join("pyvenv.cfg"),
"version = 3.11.0\nprompt = my-project\n",
)
.unwrap();
// Create python executable
#[cfg(windows)]
let python_exe = bin_dir.join("python.exe");
#[cfg(unix)]
let python_exe = bin_dir.join("python");
fs::write(&python_exe, "fake python").unwrap();
let result = try_environment_from_venv_dir(&temp_dir);
assert!(result.is_some());
let env = result.unwrap();
assert_eq!(env.kind, Some(PythonEnvironmentKind::Venv));
assert!(env.error.is_none());
assert!(env.executable.is_some());
assert_eq!(env.name, Some("my-project".to_string()));
let _ = fs::remove_dir_all(&temp_dir);
}
#[test]
#[cfg(unix)]
fn test_try_environment_from_venv_dir_broken_symlink() {
use std::os::unix::fs::symlink;
// A venv with pyvenv.cfg but a broken symlink for Python
let temp_dir = std::env::temp_dir().join("pet_test_venv_broken_symlink");
let _ = fs::remove_dir_all(&temp_dir);
let bin_dir = temp_dir.join("bin");
fs::create_dir_all(&bin_dir).unwrap();
// Create pyvenv.cfg
fs::write(
temp_dir.join("pyvenv.cfg"),
"version = 3.9.0\nprompt = broken-env\n",
)
.unwrap();
// Create a broken symlink
let python_exe = bin_dir.join("python");
let nonexistent_target = std::path::PathBuf::from("/nonexistent/python3.9");
symlink(&nonexistent_target, &python_exe).unwrap();
let result = try_environment_from_venv_dir(&temp_dir);
assert!(result.is_some());
let env = result.unwrap();
assert_eq!(env.kind, Some(PythonEnvironmentKind::Venv));
assert!(env.error.is_some());
assert!(env.error.as_ref().unwrap().contains("broken symlink"));
assert_eq!(env.name, Some("broken-env".to_string()));
assert!(env.executable.is_some());
let _ = fs::remove_dir_all(&temp_dir);
}
#[test]
fn test_is_venv_dir_with_pyvenv_cfg() {
let dir = tempdir().unwrap();
let cfg_path = dir.path().join("pyvenv.cfg");
let mut file = fs::File::create(&cfg_path).unwrap();
writeln!(file, "version = 3.11.4").unwrap();
assert!(is_venv_dir(dir.path()));
}
#[test]
fn test_is_venv_dir_without_pyvenv_cfg() {
let dir = tempdir().unwrap();
assert!(!is_venv_dir(dir.path()));
}
#[test]
fn test_is_venv_with_pyvenv_cfg_in_parent() {
let dir = tempdir().unwrap();
#[cfg(windows)]
let bin_dir = dir.path().join("Scripts");
#[cfg(unix)]
let bin_dir = dir.path().join("bin");
fs::create_dir_all(&bin_dir).unwrap();
let cfg_path = dir.path().join("pyvenv.cfg");
let mut file = fs::File::create(&cfg_path).unwrap();
writeln!(file, "version = 3.11.4").unwrap();
// Create a fake python executable
#[cfg(windows)]
let python_path = bin_dir.join("python.exe");
#[cfg(unix)]
let python_path = bin_dir.join("python");
fs::File::create(&python_path).unwrap();
let env = PythonEnv::new(python_path, Some(dir.path().to_path_buf()), None);
assert!(is_venv(&env));
}
#[test]
fn test_is_venv_without_pyvenv_cfg() {
let dir = tempdir().unwrap();
#[cfg(windows)]
let bin_dir = dir.path().join("Scripts");
#[cfg(unix)]
let bin_dir = dir.path().join("bin");
fs::create_dir_all(&bin_dir).unwrap();
#[cfg(windows)]
let python_path = bin_dir.join("python.exe");
#[cfg(unix)]
let python_path = bin_dir.join("python");
fs::File::create(&python_path).unwrap();
let env = PythonEnv::new(python_path, Some(dir.path().to_path_buf()), None);
assert!(!is_venv(&env));
}
#[test]
fn test_venv_locator_kind() {
let venv = Venv::new();
assert_eq!(venv.get_kind(), LocatorKind::Venv);
}
#[test]
fn test_venv_supported_categories() {
let venv = Venv::new();
let categories = venv.supported_categories();
assert_eq!(categories.len(), 1);
assert_eq!(categories[0], PythonEnvironmentKind::Venv);
}
#[test]
fn test_venv_default() {
let venv = Venv::default();
assert_eq!(venv.get_kind(), LocatorKind::Venv);
}
#[test]
fn test_venv_try_from_valid_venv() {
let dir = tempdir().unwrap();
#[cfg(windows)]
let bin_dir = dir.path().join("Scripts");
#[cfg(unix)]
let bin_dir = dir.path().join("bin");
fs::create_dir_all(&bin_dir).unwrap();
let cfg_path = dir.path().join("pyvenv.cfg");
let mut file = fs::File::create(&cfg_path).unwrap();
writeln!(file, "version = 3.11.4").unwrap();
writeln!(file, "prompt = my-test-env").unwrap();
#[cfg(windows)]
let python_path = bin_dir.join("python.exe");
#[cfg(unix)]
let python_path = bin_dir.join("python");
fs::File::create(&python_path).unwrap();
let env = PythonEnv::new(python_path.clone(), Some(dir.path().to_path_buf()), None);
let venv = Venv::new();
let result = venv.try_from(&env);
assert!(result.is_some());
let py_env = result.unwrap();
assert_eq!(py_env.kind, Some(PythonEnvironmentKind::Venv));
assert_eq!(py_env.name, Some("my-test-env".to_string()));
// Compare file names rather than full paths to avoid Windows 8.3 short path issues
assert!(py_env.executable.is_some());
assert_eq!(
py_env.executable.as_ref().unwrap().file_name(),
python_path.file_name()
);
}
#[test]
fn test_venv_try_from_non_venv() {
let dir = tempdir().unwrap();
#[cfg(windows)]
let bin_dir = dir.path().join("Scripts");
#[cfg(unix)]
let bin_dir = dir.path().join("bin");
fs::create_dir_all(&bin_dir).unwrap();
#[cfg(windows)]
let python_path = bin_dir.join("python.exe");
#[cfg(unix)]
let python_path = bin_dir.join("python");
fs::File::create(&python_path).unwrap();
let env = PythonEnv::new(python_path, Some(dir.path().to_path_buf()), None);
let venv = Venv::new();
let result = venv.try_from(&env);
assert!(result.is_none());
}
}