-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathenvironment_locations.rs
More file actions
289 lines (270 loc) · 10.3 KB
/
environment_locations.rs
File metadata and controls
289 lines (270 loc) · 10.3 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
use crate::{
conda_rc::Condarc,
env_variables::EnvVariables,
utils::{is_conda_env, is_conda_install},
};
use log::trace;
use pet_fs::path::norm_case;
use std::{
fs,
path::{Path, PathBuf},
thread,
};
pub fn get_conda_environment_paths(
env_vars: &EnvVariables,
additional_env_dirs: &Vec<PathBuf>,
) -> Vec<PathBuf> {
let mut env_paths = thread::scope(|s| {
let mut envs = vec![];
for thread in [
s.spawn(|| get_conda_envs_from_environment_txt(env_vars)),
s.spawn(|| get_conda_environment_paths_from_conda_rc(env_vars)),
s.spawn(|| get_conda_environment_paths_from_known_paths(env_vars)),
s.spawn(|| get_conda_environment_paths_from_additional_paths(additional_env_dirs)),
s.spawn(|| get_known_conda_install_locations(env_vars)),
] {
if let Ok(mut env_paths) = thread.join() {
envs.append(&mut env_paths);
}
}
envs
});
env_paths = env_paths.iter().map(norm_case).collect();
env_paths.sort();
env_paths.dedup();
// For each env, check if we have a conda install directory in them and
// & then iterate through the list of envs in the envs directory.
// let env_paths = vec![];
let mut threads = vec![];
for path in env_paths {
let path = path.clone();
threads.push(thread::spawn(move || get_environments(&path)));
}
let mut result = vec![];
for thread in threads {
if let Ok(envs) = thread.join() {
result.extend(envs);
}
}
result.sort();
result.dedup();
result
}
/**
* Get the list of conda environments found in other locations such as
* <user home>/.conda/envs
* <user home>/AppData/Local/conda/conda/envs
*/
fn get_conda_environment_paths_from_conda_rc(env_vars: &EnvVariables) -> Vec<PathBuf> {
if let Some(conda_rc) = Condarc::from(env_vars) {
trace!("Conda environments in .condarc {:?}", conda_rc.env_dirs);
conda_rc.env_dirs
} else {
trace!("No Conda environments in .condarc");
vec![]
}
}
fn get_conda_environment_paths_from_known_paths(env_vars: &EnvVariables) -> Vec<PathBuf> {
let mut env_paths: Vec<PathBuf> = vec![];
if let Some(ref home) = env_vars.home {
let known_conda_paths = [
PathBuf::from(".conda").join("envs"),
PathBuf::from("AppData")
.join("Local")
.join("conda")
.join("conda")
.join("envs"),
];
for path in known_conda_paths {
// We prefix with home only for testing purposes.
let full_path = home.join(path);
if let Ok(entries) = fs::read_dir(full_path) {
for entry in entries.filter_map(Result::ok) {
let path = entry.path();
if let Ok(meta) = fs::metadata(&path) {
if meta.is_dir() {
env_paths.push(path);
}
}
}
}
}
}
env_paths.append(&mut env_vars.known_global_search_locations.clone());
trace!("Conda environments in known paths {:?}", env_paths);
env_paths
}
fn get_conda_environment_paths_from_additional_paths(
additional_env_dirs: &Vec<PathBuf>,
) -> Vec<PathBuf> {
let mut env_paths: Vec<PathBuf> = vec![];
for path in additional_env_dirs {
if let Ok(entries) = fs::read_dir(path) {
for entry in entries.filter_map(Result::ok) {
let path = entry.path();
if let Ok(meta) = fs::metadata(&path) {
if meta.is_dir() {
env_paths.push(path);
}
}
}
}
}
env_paths.append(&mut additional_env_dirs.clone());
trace!("Conda environments in additional paths {:?}", env_paths);
env_paths
}
pub fn get_environments(conda_dir: &Path) -> Vec<PathBuf> {
let mut envs: Vec<PathBuf> = vec![];
if is_conda_install(conda_dir) {
envs.push(conda_dir.to_path_buf());
if let Ok(entries) = fs::read_dir(conda_dir.join("envs")) {
envs.append(
&mut entries
.filter_map(Result::ok)
.map(|e| e.path())
.filter(|p| is_conda_env(p))
.collect(),
);
}
// Then read the .condarc in the conda install folder as well.
if let Some(mut conda_rc) = Condarc::from_path(conda_dir) {
envs.append(&mut conda_rc.env_dirs);
}
} else if is_conda_env(conda_dir) {
envs.push(conda_dir.to_path_buf());
} else if fs::metadata(conda_dir.join("envs")).is_ok() {
// This could be a directory where conda environments are stored.
// I.e. its not necessarily the root conda install directory.
// E.g. C:\Users\donjayamanne\.conda
if let Ok(entries) = fs::read_dir(conda_dir.join("envs")) {
envs.append(
&mut entries
.filter_map(Result::ok)
.map(|e| e.path())
.filter(|p| is_conda_env(p))
.collect(),
);
}
}
envs.sort();
envs.dedup();
envs
}
pub fn get_conda_envs_from_environment_txt(env_vars: &EnvVariables) -> Vec<PathBuf> {
let mut envs: Vec<PathBuf> = vec![];
if let Some(ref home) = env_vars.home {
let home = Path::new(&home);
let environment_txt = home.join(".conda").join("environments.txt");
if let Ok(reader) = fs::read_to_string(environment_txt.clone()) {
trace!("Found environments.txt file {:?}", environment_txt);
for line in reader.lines() {
let line = norm_case(&PathBuf::from(line.to_string()));
trace!("Conda env in environments.txt file {:?}", line);
envs.push(line);
}
}
}
envs
}
#[cfg(windows)]
pub fn get_known_conda_install_locations(env_vars: &EnvVariables) -> Vec<PathBuf> {
use pet_fs::path::norm_case;
let user_profile = env_vars.userprofile.clone().unwrap_or_default();
let program_data = env_vars.programdata.clone().unwrap_or_default();
let all_user_profile = env_vars.allusersprofile.clone().unwrap_or_default();
let mut home_drive = env_vars.homedrive.clone().unwrap_or_default();
let mut known_paths = vec![];
for env_variable in &[program_data, all_user_profile, user_profile] {
if !env_variable.is_empty() {
known_paths.push(Path::new(&env_variable).join("anaconda3"));
known_paths.push(Path::new(&env_variable).join("miniconda3"));
known_paths.push(Path::new(&env_variable).join("miniforge3"));
}
}
if !home_drive.is_empty() {
if home_drive.ends_with(':') {
home_drive = format!("{}\\", home_drive);
}
known_paths.push(Path::new(&home_drive).join("anaconda3"));
known_paths.push(Path::new(&home_drive).join("miniconda"));
known_paths.push(Path::new(&home_drive).join("miniforge3"));
}
if let Some(ref conda_root) = env_vars.conda_root {
known_paths.push(PathBuf::from(conda_root.clone()));
}
if let Some(ref conda_prefix) = env_vars.conda_prefix {
known_paths.push(PathBuf::from(conda_prefix.clone()));
}
if let Some(ref conda) = env_vars.conda {
known_paths.push(PathBuf::from(conda));
}
if let Some(home) = env_vars.clone().home {
known_paths.push(home.clone().join("anaconda3"));
known_paths.push(home.clone().join("miniconda3"));
known_paths.push(home.clone().join("miniforge3"));
// E.g. C:\Users\user name\.conda where we have `envs`` under this directory.
known_paths.push(home.join(".conda"));
// E.g. C:\Users\user name\AppData\Local\conda\conda\envs
known_paths.push(
home.join("AppData")
.join("Local")
.join("conda")
.join("conda"),
);
}
known_paths.sort();
known_paths.dedup();
// Ensure the casing of the paths are correct.
// Its possible the actual path is in a different case.
// E.g. instead of C:\username\miniconda it might bt C:\username\Miniconda
// We use lower cases above, but it could be in any case on disc.
// We do not want to have duplicates in different cases.
// & we'd like to preserve the case of the original path as on disc.
known_paths = known_paths.iter().map(norm_case).collect();
known_paths.sort();
known_paths.dedup();
known_paths
}
#[cfg(unix)]
pub fn get_known_conda_install_locations(env_vars: &EnvVariables) -> Vec<PathBuf> {
let mut known_paths = vec![];
let directories_to_look_in = [
"/opt",
"/usr/share",
"/usr/local",
"/usr",
"/home",
"", // We need to look in `/anaconda3` and `/miniconda3` as well.
];
for directory in directories_to_look_in.iter() {
known_paths.push(PathBuf::from(format!("{directory}/anaconda")));
known_paths.push(PathBuf::from(format!("{directory}/anaconda3")));
known_paths.push(PathBuf::from(format!("{directory}/miniconda")));
known_paths.push(PathBuf::from(format!("{directory}/miniconda3")));
known_paths.push(PathBuf::from(format!("{directory}/miniforge")));
known_paths.push(PathBuf::from(format!("{directory}/miniforge3")));
}
if let Some(ref conda_root) = env_vars.conda_root {
known_paths.push(PathBuf::from(conda_root.clone()));
}
if let Some(ref conda_prefix) = env_vars.conda_prefix {
known_paths.push(PathBuf::from(conda_prefix.clone()));
}
if let Some(ref conda) = env_vars.conda {
known_paths.push(PathBuf::from(conda));
}
if let Some(ref home) = env_vars.home {
known_paths.push(home.clone().join("anaconda"));
known_paths.push(home.clone().join("anaconda3"));
known_paths.push(home.clone().join("miniconda"));
known_paths.push(home.clone().join("miniconda3"));
known_paths.push(home.clone().join("miniforge3"));
known_paths.push(home.join(".conda"));
}
known_paths.sort();
known_paths.dedup();
known_paths
}