@@ -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 ( known_paths : & impl known:: KnownPaths ) -> Option < PathBuf > {
131
+ let paths = known_paths. 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,11 @@ 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 ( known_paths_provider : & impl known :: KnownPaths ) -> Vec < PathBuf > {
163
+ let user_profile = known_paths_provider . get_env_var ( "USERPROFILE" . to_string ( ) ) . unwrap ( ) ;
164
+ let program_data = known_paths_provider . get_env_var ( "PROGRAMDATA" . to_string ( ) ) . unwrap ( ) ;
165
+ let all_user_profile = known_paths_provider . get_env_var ( "ALLUSERSPROFILE" . to_string ( ) ) . unwrap ( ) ;
166
+ let home_drive = known_paths_provider . get_env_var ( "HOMEDRIVE" . to_string ( ) ) . unwrap ( ) ;
169
167
let mut known_paths = vec ! [
170
168
Path :: new( & user_profile) . join( "Anaconda3\\ Scripts" ) ,
171
169
Path :: new( & program_data) . join( "Anaconda3\\ Scripts" ) ,
@@ -176,12 +174,12 @@ fn get_known_conda_locations() -> Vec<PathBuf> {
176
174
Path :: new( & all_user_profile) . join( "Miniconda3\\ Scripts" ) ,
177
175
Path :: new( & home_drive) . join( "Miniconda3\\ Scripts" ) ,
178
176
] ;
179
- known_paths. append ( & mut known :: get_know_global_search_locations ( ) ) ;
177
+ known_paths. append ( & mut known_paths_provider . get_know_global_search_locations ( ) ) ;
180
178
known_paths
181
179
}
182
180
183
181
#[ cfg( unix) ]
184
- fn get_known_conda_locations ( ) -> Vec < PathBuf > {
182
+ fn get_known_conda_locations ( known_paths_provider : & impl known :: KnownPaths ) -> Vec < PathBuf > {
185
183
let mut known_paths = vec ! [
186
184
PathBuf :: from( "/opt/anaconda3/bin" ) ,
187
185
PathBuf :: from( "/opt/miniconda3/bin" ) ,
@@ -202,14 +200,16 @@ fn get_known_conda_locations() -> Vec<PathBuf> {
202
200
PathBuf :: from( "/anaconda3/bin" ) ,
203
201
PathBuf :: from( "/miniconda3/bin" ) ,
204
202
] ;
205
- known_paths. append ( & mut known :: get_know_global_search_locations ( ) ) ;
203
+ known_paths. append ( & mut known_paths_provider . get_know_global_search_locations ( ) ) ;
206
204
known_paths
207
205
}
208
206
209
207
/// Find conda binary in known locations
210
- fn find_conda_binary_in_known_locations ( ) -> Option < PathBuf > {
208
+ fn find_conda_binary_in_known_locations (
209
+ known_paths_provider : & impl known:: KnownPaths ,
210
+ ) -> 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 ( known_paths_provider ) ;
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,19 @@ 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 ( known_paths_provider : & impl known :: KnownPaths ) -> Option < PathBuf > {
227
+ let conda_binary_on_path = find_conda_binary_on_path ( known_paths_provider ) ;
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 ( known_paths_provider ) ,
231
231
}
232
232
}
233
233
234
- fn get_conda_envs_from_environment_txt ( ) -> Vec < String > {
234
+ fn get_conda_envs_from_environment_txt (
235
+ known_paths_provider : & impl known:: KnownPaths ,
236
+ ) -> Vec < String > {
235
237
let mut envs = vec ! [ ] ;
236
- let home = known :: get_user_home ( ) ;
238
+ let home = known_paths_provider . get_user_home ( ) ;
237
239
match home {
238
240
Some ( home) => {
239
241
let home = Path :: new ( & home) ;
@@ -252,9 +254,12 @@ fn get_conda_envs_from_environment_txt() -> Vec<String> {
252
254
envs
253
255
}
254
256
255
- fn get_known_env_locations ( conda_bin : PathBuf ) -> Vec < String > {
257
+ fn get_known_env_locations (
258
+ conda_bin : PathBuf ,
259
+ known_paths_provider : & impl known:: KnownPaths ,
260
+ ) -> Vec < String > {
256
261
let mut paths = vec ! [ ] ;
257
- let home = known :: get_user_home ( ) ;
262
+ let home = known_paths_provider . get_user_home ( ) ;
258
263
match home {
259
264
Some ( home) => {
260
265
let home = Path :: new ( & home) ;
@@ -284,9 +289,12 @@ fn get_known_env_locations(conda_bin: PathBuf) -> Vec<String> {
284
289
paths
285
290
}
286
291
287
- fn get_conda_envs_from_known_env_locations ( conda_bin : PathBuf ) -> Vec < String > {
292
+ fn get_conda_envs_from_known_env_locations (
293
+ conda_bin : PathBuf ,
294
+ known_paths_provider : & impl known:: KnownPaths ,
295
+ ) -> Vec < String > {
288
296
let mut envs = vec ! [ ] ;
289
- for location in get_known_env_locations ( conda_bin) {
297
+ for location in get_known_env_locations ( conda_bin, known_paths_provider ) {
290
298
if is_conda_environment ( & Path :: new ( & location) ) {
291
299
envs. push ( location. to_string ( ) ) ;
292
300
}
@@ -324,14 +332,18 @@ struct CondaEnv {
324
332
path : PathBuf ,
325
333
}
326
334
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 ( ) ) ;
335
+ fn get_distinct_conda_envs (
336
+ conda_bin : PathBuf ,
337
+ known_paths_provider : & impl known:: KnownPaths ,
338
+ ) -> Vec < CondaEnv > {
339
+ let mut envs = get_conda_envs_from_environment_txt ( known_paths_provider) ;
340
+ let mut known_envs =
341
+ get_conda_envs_from_known_env_locations ( conda_bin. to_path_buf ( ) , known_paths_provider) ;
330
342
envs. append ( & mut known_envs) ;
331
343
envs. sort ( ) ;
332
344
envs. dedup ( ) ;
333
345
334
- let locations = get_known_env_locations ( conda_bin) ;
346
+ let locations = get_known_env_locations ( conda_bin, known_paths_provider ) ;
335
347
let mut conda_envs = vec ! [ ] ;
336
348
for env in envs {
337
349
let env = Path :: new ( & env) ;
@@ -367,16 +379,19 @@ fn get_distinct_conda_envs(conda_bin: PathBuf) -> Vec<CondaEnv> {
367
379
conda_envs
368
380
}
369
381
370
- pub fn find_and_report ( ) {
371
- let conda_binary = find_conda_binary ( ) ;
382
+ pub fn find_and_report (
383
+ dispatcher : & mut impl messaging:: MessageDispatcher ,
384
+ known_paths_provider : & impl known:: KnownPaths ,
385
+ ) {
386
+ let conda_binary = find_conda_binary ( known_paths_provider) ;
372
387
match conda_binary {
373
388
Some ( conda_binary) => {
374
389
let params =
375
390
messaging:: EnvManager :: new ( vec ! [ conda_binary. to_string_lossy( ) . to_string( ) ] , None ) ;
376
391
let message = messaging:: EnvManagerMessage :: new ( params) ;
377
- messaging :: send_message ( message) ;
392
+ dispatcher . send_message ( message) ;
378
393
379
- let envs = get_distinct_conda_envs ( conda_binary. to_path_buf ( ) ) ;
394
+ let envs = get_distinct_conda_envs ( conda_binary. to_path_buf ( ) , known_paths_provider ) ;
380
395
for env in envs {
381
396
let executable = find_python_binary_path ( Path :: new ( & env. path ) ) ;
382
397
let params = messaging:: PythonEnvironment :: new (
@@ -407,7 +422,7 @@ pub fn find_and_report() {
407
422
Some ( env. path . to_string_lossy ( ) . to_string ( ) ) ,
408
423
) ;
409
424
let message = messaging:: PythonEnvironmentMessage :: new ( params) ;
410
- messaging :: send_message ( message) ;
425
+ dispatcher . send_message ( message) ;
411
426
}
412
427
}
413
428
None => ( ) ,
0 commit comments