-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathlib.rs
More file actions
70 lines (62 loc) · 2.15 KB
/
lib.rs
File metadata and controls
70 lines (62 loc) · 2.15 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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
use pet_conda::utils::is_conda_env;
use pet_utils::path::normalize;
use std::{fs, path::PathBuf};
fn get_global_virtualenv_dirs(
work_on_home_env_var: Option<String>,
user_home: Option<PathBuf>,
) -> Vec<PathBuf> {
let mut venv_dirs: Vec<PathBuf> = vec![];
if let Some(work_on_home) = work_on_home_env_var {
let work_on_home = normalize(PathBuf::from(work_on_home));
if fs::metadata(&work_on_home).is_ok() {
venv_dirs.push(work_on_home);
}
}
if let Some(home) = user_home {
for dir in [
PathBuf::from("envs"),
PathBuf::from(".direnv"),
PathBuf::from(".venvs"), // Used by pipenv, https://pipenv.pypa.io/en/latest/virtualenv.html
PathBuf::from(".virtualenvs"), // Useb by virtualenvwrapper, https://virtualenvwrapper.readthedocs.io/en/latest/install.html#shell-startup-file
PathBuf::from(".local").join("share").join("virtualenvs"),
] {
let venv_dir = home.join(dir);
if fs::metadata(&venv_dir).is_ok() {
venv_dirs.push(venv_dir);
}
}
if cfg!(target_os = "linux") {
let envs = PathBuf::from("Envs");
if fs::metadata(&envs).is_ok() {
venv_dirs.push(envs);
}
let envs = PathBuf::from("envs");
if fs::metadata(&envs).is_ok() {
venv_dirs.push(envs);
}
}
}
venv_dirs
}
pub fn list_global_virtual_envs_paths(
work_on_home_env_var: Option<String>,
user_home: Option<PathBuf>,
) -> Vec<PathBuf> {
let mut python_envs: Vec<PathBuf> = vec![];
for root_dir in &get_global_virtualenv_dirs(work_on_home_env_var, user_home) {
if let Ok(dirs) = fs::read_dir(root_dir) {
python_envs.append(
&mut dirs
.filter_map(Result::ok)
.map(|e| e.path())
.filter(|p| !is_conda_env(p))
.collect(),
)
}
}
python_envs.sort();
python_envs.dedup();
python_envs
}