-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathtelemetry.rs
More file actions
326 lines (302 loc) · 9.76 KB
/
telemetry.rs
File metadata and controls
326 lines (302 loc) · 9.76 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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
use std::{collections::HashSet, path::PathBuf, vec};
use log::warn;
use pet_core::{
python_environment::{PythonEnvironment, PythonEnvironmentKind},
reporter::Reporter,
telemetry::{missing_conda_info::MissingCondaEnvironments, TelemetryEvent},
};
use crate::{
conda_info::CondaInfo, conda_rc::Condarc, env_variables::EnvVariables,
environments::get_conda_environment_info, manager::CondaManager, utils::is_conda_install,
};
pub fn report_missing_envs(
reporter: &dyn Reporter,
env_vars: &EnvVariables,
possibly_missing_envs: &[PathBuf],
known_envs: &[PythonEnvironment],
conda_info: &CondaInfo,
user_provided_conda_exe: bool,
) -> Option<()> {
let missing_envs = log_and_find_missing_envs(possibly_missing_envs, known_envs, conda_info)
.unwrap_or_default();
let known_conda_rcs = get_all_known_conda_rc(env_vars, known_envs);
let conda_manager_not_found = !known_envs
.iter()
.any(|e| e.kind == Some(PythonEnvironmentKind::Conda) && e.manager.is_some());
let mut discovered_conda_rcs: HashSet<_> = known_conda_rcs
.iter()
.flat_map(|rc| rc.files.clone().into_iter())
.collect();
let mut discovered_env_dirs: HashSet<_> = known_conda_rcs
.iter()
.flat_map(|rc| rc.env_dirs.clone().into_iter())
.collect();
let known_env_prefixes: HashSet<_> =
known_envs.iter().filter_map(|e| e.prefix.clone()).collect();
let mut conda_env_dirs = vec![];
let mut root_prefix_not_found = false;
let mut conda_prefix_not_found = false;
if let Some(prefix) = conda_info.root_prefix.as_ref() {
if !known_env_prefixes.contains(prefix) {
warn!("Root prefix {:?} not found", prefix);
root_prefix_not_found = true;
}
}
if let Some(prefix) = conda_info.conda_prefix.as_ref() {
if !known_env_prefixes.contains(prefix) {
warn!("Conda prefix {:?} not found", prefix);
conda_prefix_not_found = true;
}
}
let (
sys_conda_rc_not_found,
missing_from_sys_rc_env_dirs,
missing_env_dirs_from_sys_rc,
mut env_dirs,
) = count_missing_envs(
&mut discovered_conda_rcs,
&mut discovered_env_dirs,
&missing_envs,
&conda_info
.sys_rc_path
.clone()
.map(|x| [x])
.unwrap_or_default(),
"sys",
);
conda_env_dirs.append(&mut env_dirs);
let (
user_conda_rc_not_found,
missing_from_user_rc_env_dirs,
missing_env_dirs_from_user_rc,
mut env_dirs,
) = count_missing_envs(
&mut discovered_conda_rcs,
&mut discovered_env_dirs,
&missing_envs,
&conda_info
.user_rc_path
.clone()
.map(|x| [x])
.unwrap_or_default(),
"user",
);
conda_env_dirs.append(&mut env_dirs);
let (
other_conda_rc_not_found,
missing_from_other_rc_env_dirs,
missing_env_dirs_from_other_rc,
mut env_dirs,
) = count_missing_envs(
&mut discovered_conda_rcs,
&mut discovered_env_dirs,
&missing_envs,
&conda_info.config_files,
"other",
);
conda_env_dirs.append(&mut env_dirs);
let conda_env_dirs: HashSet<_> = conda_env_dirs.into_iter().collect();
let missing_conda_env_dirs = conda_env_dirs.difference(&discovered_env_dirs).count();
let missing_info = MissingCondaEnvironments {
missing: missing_envs.len() as u16,
env_dirs_not_found: if missing_conda_env_dirs > 0 {
Some(missing_conda_env_dirs as u16)
} else {
None
},
user_provided_conda_exe: if user_provided_conda_exe {
Some(true)
} else {
None
},
root_prefix_not_found: if root_prefix_not_found {
Some(true)
} else {
None
},
conda_prefix_not_found: if conda_prefix_not_found {
Some(true)
} else {
None
},
conda_manager_not_found: if conda_manager_not_found {
Some(true)
} else {
None
},
sys_rc_not_found: if sys_conda_rc_not_found > 0 {
Some(true)
} else {
None
},
user_rc_not_found: if user_conda_rc_not_found > 0 {
Some(true)
} else {
None
},
other_rc_not_found: if other_conda_rc_not_found > 0 {
Some(other_conda_rc_not_found)
} else {
None
},
missing_env_dirs_from_sys_rc: if missing_env_dirs_from_sys_rc > 0 {
Some(missing_env_dirs_from_sys_rc)
} else {
None
},
missing_env_dirs_from_user_rc: if missing_env_dirs_from_user_rc > 0 {
Some(missing_env_dirs_from_user_rc)
} else {
None
},
missing_env_dirs_from_other_rc: if missing_env_dirs_from_other_rc > 0 {
Some(missing_env_dirs_from_other_rc)
} else {
None
},
missing_from_sys_rc_env_dirs: if missing_from_sys_rc_env_dirs > 0 {
Some(missing_from_sys_rc_env_dirs)
} else {
None
},
missing_from_user_rc_env_dirs: if missing_from_user_rc_env_dirs > 0 {
Some(missing_from_user_rc_env_dirs)
} else {
None
},
missing_from_other_rc_env_dirs: if missing_from_other_rc_env_dirs > 0 {
Some(missing_from_other_rc_env_dirs)
} else {
None
},
};
reporter.report_telemetry(&TelemetryEvent::MissingCondaEnvironments(missing_info));
Some(())
}
pub fn get_conda_rcs_and_env_dirs(
env_vars: &EnvVariables,
known_envs: &[PythonEnvironment],
) -> (Vec<PathBuf>, Vec<PathBuf>) {
let known_conda_rcs = get_all_known_conda_rc(env_vars, known_envs);
let discovered_conda_rcs = known_conda_rcs
.iter()
.flat_map(|rc| rc.files.clone().into_iter())
.collect();
let discovered_env_dirs = known_conda_rcs
.iter()
.flat_map(|rc| rc.env_dirs.clone().into_iter())
.collect();
(discovered_conda_rcs, discovered_env_dirs)
}
fn log_and_find_missing_envs(
possibly_missing_envs: &[PathBuf],
known_envs: &[PythonEnvironment],
conda_info: &CondaInfo,
) -> Option<Vec<PathBuf>> {
let mut missing_envs = possibly_missing_envs.to_vec();
if missing_envs.is_empty() {
return None;
}
let known_env_prefixes = known_envs
.iter()
.filter_map(|e| e.prefix.clone())
.collect::<Vec<_>>();
// Oh oh, we have new envs, lets see what they are.
let manager = CondaManager::from_info(
&conda_info.executable,
conda_info,
pet_core::manager::EnvManagerType::Conda,
)?;
for path in missing_envs
.clone()
.iter()
.filter(|p| !known_env_prefixes.contains(p))
{
let mgr = manager.clone();
if let Some(env) = get_conda_environment_info(path, &Some(mgr.clone())) {
warn!(
"Failed to find conda env {:?} without spawning conda {:?}",
env.prefix, conda_info.executable
);
} else {
missing_envs.retain(|p| p != path);
}
}
if missing_envs.is_empty() {
None
} else {
Some(missing_envs)
}
}
fn get_all_known_conda_rc(
env_vars: &EnvVariables,
known_envs: &[PythonEnvironment],
) -> Vec<Condarc> {
let mut conda_rcs = vec![];
if let Some(rc) = Condarc::from(env_vars) {
conda_rcs.push(rc);
}
for env in known_envs.iter() {
if let Some(prefix) = env.prefix.as_ref() {
if !is_conda_install(prefix) {
continue;
}
if let Some(rc) = Condarc::from_path(prefix) {
conda_rcs.push(rc);
}
}
}
conda_rcs
}
fn count_missing_envs(
discovered_conda_rcs: &mut HashSet<PathBuf>,
discovered_env_dirs: &mut HashSet<PathBuf>,
missing_envs: &[PathBuf],
config_files: &[PathBuf],
config_type: &str,
) -> (u16, u16, u16, Vec<PathBuf>) {
let mut conda_rc_not_found = 0;
let mut missing_from_rc_env_dirs = 0;
let mut missing_env_dirs_from_rc = 0;
let mut env_dirs = vec![];
for rc in config_files.iter() {
// We are not interested in the rc if it does not exist.
if !rc.exists() {
continue;
}
if !discovered_conda_rcs.contains(rc) {
discovered_conda_rcs.insert(rc.clone());
conda_rc_not_found += 1;
warn!("{} Conda condarc not found: {:?}", config_type, rc);
if let Some(cfg) = Condarc::from_path(rc) {
for env_dir in cfg.env_dirs.iter().filter(|d| d.exists()) {
env_dirs.push(env_dir.clone());
if !discovered_env_dirs.contains(env_dir) {
missing_env_dirs_from_rc += 1;
warn!(
"Environment dir {:?} is missing from {} rc env dirs",
env_dir, config_type
);
}
for env in missing_envs.iter() {
if env.starts_with(env_dir) {
missing_from_rc_env_dirs += 1;
warn!(
"Environment {:?} is missing from {} rc env dirs",
env, config_type
);
}
}
}
}
}
}
(
conda_rc_not_found,
missing_from_rc_env_dirs,
missing_env_dirs_from_rc,
env_dirs,
)
}