@@ -84,7 +84,6 @@ fn get_version_from_meta_json(json_file: &Path) -> Option<String> {
84
84
fn get_conda_package_json_path ( any_path : & Path , package : & str ) -> Option < PathBuf > {
85
85
let package_name = format ! ( "{}-" , package) ;
86
86
let conda_meta_path = get_conda_meta_path ( any_path) ?;
87
-
88
87
std:: fs:: read_dir ( conda_meta_path) . ok ( ) ?. find_map ( |entry| {
89
88
let path = entry. ok ( ) ?. path ( ) ;
90
89
let file_name = path. file_name ( ) ?. to_string_lossy ( ) ;
@@ -97,6 +96,7 @@ fn get_conda_package_json_path(any_path: &Path, package: &str) -> Option<PathBuf
97
96
}
98
97
99
98
/// Checks if the `python` package is installed in the conda environment
99
+ #[ allow( dead_code) ]
100
100
pub fn is_python_conda_env ( any_path : & Path ) -> bool {
101
101
let conda_python_json_path = get_conda_package_json_path ( any_path, "python" ) ;
102
102
match conda_python_json_path {
@@ -127,11 +127,9 @@ fn get_conda_bin_names() -> Vec<&'static str> {
127
127
}
128
128
129
129
/// Find the conda binary on the PATH environment variable
130
- fn find_conda_binary_on_path ( ) -> Option < PathBuf > {
131
- let paths = env:: var ( "PATH" ) . ok ( ) ?;
132
- let paths = env:: split_paths ( & paths) ;
133
- for path in paths {
134
- let path = Path :: new ( & path) ;
130
+ fn find_conda_binary_on_path ( environment : & impl known:: Environment ) -> Option < PathBuf > {
131
+ let paths = environment. get_env_var ( "PATH" . to_string ( ) ) ?;
132
+ for path in env:: split_paths ( & paths) {
135
133
for bin in get_conda_bin_names ( ) {
136
134
let conda_path = path. join ( bin) ;
137
135
match std:: fs:: metadata ( & conda_path) {
@@ -161,11 +159,13 @@ fn find_python_binary_path(env_path: &Path) -> Option<PathBuf> {
161
159
}
162
160
163
161
#[ cfg( windows) ]
164
- fn get_known_conda_locations ( ) -> Vec < PathBuf > {
165
- let user_profile = env:: var ( "USERPROFILE" ) . unwrap ( ) ;
166
- let program_data = env:: var ( "PROGRAMDATA" ) . unwrap ( ) ;
167
- let all_user_profile = env:: var ( "ALLUSERSPROFILE" ) . unwrap ( ) ;
168
- let home_drive = env:: var ( "HOMEDRIVE" ) . unwrap ( ) ;
162
+ fn get_known_conda_locations ( environment : & impl known:: Environment ) -> Vec < PathBuf > {
163
+ let user_profile = environment. get_env_var ( "USERPROFILE" . to_string ( ) ) . unwrap ( ) ;
164
+ let program_data = environment. get_env_var ( "PROGRAMDATA" . to_string ( ) ) . unwrap ( ) ;
165
+ let all_user_profile = environment
166
+ . get_env_var ( "ALLUSERSPROFILE" . to_string ( ) )
167
+ . unwrap ( ) ;
168
+ let home_drive = environment. get_env_var ( "HOMEDRIVE" . to_string ( ) ) . unwrap ( ) ;
169
169
let mut known_paths = vec ! [
170
170
Path :: new( & user_profile) . join( "Anaconda3\\ Scripts" ) ,
171
171
Path :: new( & program_data) . join( "Anaconda3\\ Scripts" ) ,
@@ -176,12 +176,12 @@ fn get_known_conda_locations() -> Vec<PathBuf> {
176
176
Path :: new( & all_user_profile) . join( "Miniconda3\\ Scripts" ) ,
177
177
Path :: new( & home_drive) . join( "Miniconda3\\ Scripts" ) ,
178
178
] ;
179
- known_paths. append ( & mut known :: get_know_global_search_locations ( ) ) ;
179
+ known_paths. append ( & mut environment . get_know_global_search_locations ( ) ) ;
180
180
known_paths
181
181
}
182
182
183
183
#[ cfg( unix) ]
184
- fn get_known_conda_locations ( ) -> Vec < PathBuf > {
184
+ fn get_known_conda_locations ( environment : & impl known :: Environment ) -> Vec < PathBuf > {
185
185
let mut known_paths = vec ! [
186
186
PathBuf :: from( "/opt/anaconda3/bin" ) ,
187
187
PathBuf :: from( "/opt/miniconda3/bin" ) ,
@@ -202,14 +202,14 @@ fn get_known_conda_locations() -> Vec<PathBuf> {
202
202
PathBuf :: from( "/anaconda3/bin" ) ,
203
203
PathBuf :: from( "/miniconda3/bin" ) ,
204
204
] ;
205
- known_paths. append ( & mut known :: get_know_global_search_locations ( ) ) ;
205
+ known_paths. append ( & mut environment . get_know_global_search_locations ( ) ) ;
206
206
known_paths
207
207
}
208
208
209
209
/// Find conda binary in known locations
210
- fn find_conda_binary_in_known_locations ( ) -> Option < PathBuf > {
210
+ fn find_conda_binary_in_known_locations ( environment : & impl known :: Environment ) -> Option < PathBuf > {
211
211
let conda_bin_names = get_conda_bin_names ( ) ;
212
- let known_locations = get_known_conda_locations ( ) ;
212
+ let known_locations = get_known_conda_locations ( environment ) ;
213
213
for location in known_locations {
214
214
for bin in & conda_bin_names {
215
215
let conda_path = location. join ( bin) ;
@@ -223,17 +223,17 @@ fn find_conda_binary_in_known_locations() -> Option<PathBuf> {
223
223
}
224
224
225
225
/// Find the conda binary on the system
226
- pub fn find_conda_binary ( ) -> Option < PathBuf > {
227
- let conda_binary_on_path = find_conda_binary_on_path ( ) ;
226
+ pub fn find_conda_binary ( environment : & impl known :: Environment ) -> Option < PathBuf > {
227
+ let conda_binary_on_path = find_conda_binary_on_path ( environment ) ;
228
228
match conda_binary_on_path {
229
229
Some ( conda_binary_on_path) => Some ( conda_binary_on_path) ,
230
- None => find_conda_binary_in_known_locations ( ) ,
230
+ None => find_conda_binary_in_known_locations ( environment ) ,
231
231
}
232
232
}
233
233
234
- fn get_conda_envs_from_environment_txt ( ) -> Vec < String > {
234
+ fn get_conda_envs_from_environment_txt ( environment : & impl known :: Environment ) -> Vec < String > {
235
235
let mut envs = vec ! [ ] ;
236
- let home = known :: get_user_home ( ) ;
236
+ let home = environment . get_user_home ( ) ;
237
237
match home {
238
238
Some ( home) => {
239
239
let home = Path :: new ( & home) ;
@@ -252,9 +252,12 @@ fn get_conda_envs_from_environment_txt() -> Vec<String> {
252
252
envs
253
253
}
254
254
255
- fn get_known_env_locations ( conda_bin : PathBuf ) -> Vec < String > {
255
+ fn get_known_env_locations (
256
+ conda_bin : PathBuf ,
257
+ environment : & impl known:: Environment ,
258
+ ) -> Vec < String > {
256
259
let mut paths = vec ! [ ] ;
257
- let home = known :: get_user_home ( ) ;
260
+ let home = environment . get_user_home ( ) ;
258
261
match home {
259
262
Some ( home) => {
260
263
let home = Path :: new ( & home) ;
@@ -284,9 +287,12 @@ fn get_known_env_locations(conda_bin: PathBuf) -> Vec<String> {
284
287
paths
285
288
}
286
289
287
- fn get_conda_envs_from_known_env_locations ( conda_bin : PathBuf ) -> Vec < String > {
290
+ fn get_conda_envs_from_known_env_locations (
291
+ conda_bin : PathBuf ,
292
+ environment : & impl known:: Environment ,
293
+ ) -> Vec < String > {
288
294
let mut envs = vec ! [ ] ;
289
- for location in get_known_env_locations ( conda_bin) {
295
+ for location in get_known_env_locations ( conda_bin, environment ) {
290
296
if is_conda_environment ( & Path :: new ( & location) ) {
291
297
envs. push ( location. to_string ( ) ) ;
292
298
}
@@ -324,14 +330,18 @@ struct CondaEnv {
324
330
path : PathBuf ,
325
331
}
326
332
327
- fn get_distinct_conda_envs ( conda_bin : PathBuf ) -> Vec < CondaEnv > {
328
- let mut envs = get_conda_envs_from_environment_txt ( ) ;
329
- let mut known_envs = get_conda_envs_from_known_env_locations ( conda_bin. to_path_buf ( ) ) ;
333
+ fn get_distinct_conda_envs (
334
+ conda_bin : PathBuf ,
335
+ environment : & impl known:: Environment ,
336
+ ) -> Vec < CondaEnv > {
337
+ let mut envs = get_conda_envs_from_environment_txt ( environment) ;
338
+ let mut known_envs =
339
+ get_conda_envs_from_known_env_locations ( conda_bin. to_path_buf ( ) , environment) ;
330
340
envs. append ( & mut known_envs) ;
331
341
envs. sort ( ) ;
332
342
envs. dedup ( ) ;
333
343
334
- let locations = get_known_env_locations ( conda_bin) ;
344
+ let locations = get_known_env_locations ( conda_bin, environment ) ;
335
345
let mut conda_envs = vec ! [ ] ;
336
346
for env in envs {
337
347
let env = Path :: new ( & env) ;
@@ -367,16 +377,19 @@ fn get_distinct_conda_envs(conda_bin: PathBuf) -> Vec<CondaEnv> {
367
377
conda_envs
368
378
}
369
379
370
- pub fn find_and_report ( ) {
371
- let conda_binary = find_conda_binary ( ) ;
380
+ pub fn find_and_report (
381
+ dispatcher : & mut impl messaging:: MessageDispatcher ,
382
+ environment : & impl known:: Environment ,
383
+ ) {
384
+ let conda_binary = find_conda_binary ( environment) ;
372
385
match conda_binary {
373
386
Some ( conda_binary) => {
374
387
let params =
375
388
messaging:: EnvManager :: new ( vec ! [ conda_binary. to_string_lossy( ) . to_string( ) ] , None ) ;
376
389
let message = messaging:: EnvManagerMessage :: new ( params) ;
377
- messaging :: send_message ( message) ;
390
+ dispatcher . send_message ( message) ;
378
391
379
- let envs = get_distinct_conda_envs ( conda_binary. to_path_buf ( ) ) ;
392
+ let envs = get_distinct_conda_envs ( conda_binary. to_path_buf ( ) , environment ) ;
380
393
for env in envs {
381
394
let executable = find_python_binary_path ( Path :: new ( & env. path ) ) ;
382
395
let params = messaging:: PythonEnvironment :: new (
@@ -407,7 +420,7 @@ pub fn find_and_report() {
407
420
Some ( env. path . to_string_lossy ( ) . to_string ( ) ) ,
408
421
) ;
409
422
let message = messaging:: PythonEnvironmentMessage :: new ( params) ;
410
- messaging :: send_message ( message) ;
423
+ dispatcher . send_message ( message) ;
411
424
}
412
425
}
413
426
None => ( ) ,
0 commit comments