@@ -63,21 +63,24 @@ fn get_conda_package_json_path(path: &Path, package: &str) -> Option<CondaPackag
63
63
let path = path. join ( "conda-meta" ) ;
64
64
let package_name = format ! ( "{}-" , package) ;
65
65
let regex = Regex :: new ( format ! ( "^{}-((\\ d+\\ .*)*)-.*.json$" , package) . as_str ( ) ) ;
66
- std:: fs:: read_dir ( path) . ok ( ) ?. find_map ( |entry| {
67
- let path = entry. ok ( ) ?. path ( ) ;
68
- let file_name = path. file_name ( ) ?. to_string_lossy ( ) ;
69
- if file_name. starts_with ( & package_name) && file_name. ends_with ( ".json" ) {
70
- match regex. clone ( ) . ok ( ) ?. captures ( & file_name) ?. get ( 1 ) {
71
- Some ( version) => Some ( CondaPackage {
72
- path : path. clone ( ) ,
73
- version : version. as_str ( ) . to_string ( ) ,
74
- } ) ,
75
- None => None ,
66
+ std:: fs:: read_dir ( path)
67
+ . ok ( ) ?
68
+ . filter_map ( Result :: ok)
69
+ . find_map ( |entry| {
70
+ let path = entry. path ( ) ;
71
+ let file_name = path. file_name ( ) ?. to_string_lossy ( ) ;
72
+ if file_name. starts_with ( & package_name) && file_name. ends_with ( ".json" ) {
73
+ match regex. clone ( ) . ok ( ) . unwrap ( ) . captures ( & file_name) ?. get ( 1 ) {
74
+ Some ( version) => Some ( CondaPackage {
75
+ path : path. clone ( ) ,
76
+ version : version. as_str ( ) . to_string ( ) ,
77
+ } ) ,
78
+ None => None ,
79
+ }
80
+ } else {
81
+ None
76
82
}
77
- } else {
78
- None
79
- }
80
- } )
83
+ } )
81
84
}
82
85
83
86
fn get_conda_executable ( path : & PathBuf ) -> Option < PathBuf > {
@@ -109,13 +112,10 @@ fn find_conda_binary_on_path(environment: &dyn known::Environment) -> Option<Pat
109
112
for path in env:: split_paths ( & paths) {
110
113
for bin in get_conda_bin_names ( ) {
111
114
let conda_path = path. join ( bin) ;
112
- match std:: fs:: metadata ( & conda_path) {
113
- Ok ( metadata) => {
114
- if metadata. is_file ( ) || metadata. is_symlink ( ) {
115
- return Some ( conda_path) ;
116
- }
115
+ if let Ok ( metadata) = std:: fs:: metadata ( & conda_path) {
116
+ if metadata. is_file ( ) || metadata. is_symlink ( ) {
117
+ return Some ( conda_path) ;
117
118
}
118
- Err ( _) => ( ) ,
119
119
}
120
120
}
121
121
}
@@ -213,40 +213,37 @@ struct CondaEnvironment {
213
213
}
214
214
fn get_conda_environment_info ( env_path : & PathBuf , named : bool ) -> Option < CondaEnvironment > {
215
215
let metadata = env_path. metadata ( ) ;
216
- match metadata {
217
- Ok ( metadata) => {
218
- if metadata. is_dir ( ) {
219
- let path = env_path. clone ( ) ;
220
- if let Some ( python_binary) = find_python_binary_path ( & path) {
221
- if let Some ( package_info) = get_conda_package_json_path ( & path, "python" ) {
222
- return Some ( CondaEnvironment {
223
- name : path. file_name ( ) ?. to_string_lossy ( ) . to_string ( ) ,
224
- path,
225
- named,
226
- python_executable_path : Some ( python_binary) ,
227
- version : Some ( package_info. version ) ,
228
- } ) ;
229
- } else {
230
- return Some ( CondaEnvironment {
231
- name : path. file_name ( ) ?. to_string_lossy ( ) . to_string ( ) ,
232
- path,
233
- named,
234
- python_executable_path : Some ( python_binary) ,
235
- version : None ,
236
- } ) ;
237
- }
216
+ if let Ok ( metadata) = metadata {
217
+ if metadata. is_dir ( ) {
218
+ let path = env_path. clone ( ) ;
219
+ if let Some ( python_binary) = find_python_binary_path ( & path) {
220
+ if let Some ( package_info) = get_conda_package_json_path ( & path, "python" ) {
221
+ return Some ( CondaEnvironment {
222
+ name : path. file_name ( ) ?. to_string_lossy ( ) . to_string ( ) ,
223
+ path,
224
+ named,
225
+ python_executable_path : Some ( python_binary) ,
226
+ version : Some ( package_info. version ) ,
227
+ } ) ;
238
228
} else {
239
229
return Some ( CondaEnvironment {
240
230
name : path. file_name ( ) ?. to_string_lossy ( ) . to_string ( ) ,
241
231
path,
242
232
named,
243
- python_executable_path : None ,
233
+ python_executable_path : Some ( python_binary ) ,
244
234
version : None ,
245
235
} ) ;
246
236
}
237
+ } else {
238
+ return Some ( CondaEnvironment {
239
+ name : path. file_name ( ) ?. to_string_lossy ( ) . to_string ( ) ,
240
+ path,
241
+ named,
242
+ python_executable_path : None ,
243
+ version : None ,
244
+ } ) ;
247
245
}
248
246
}
249
- Err ( _) => ( ) ,
250
247
}
251
248
252
249
None
@@ -258,14 +255,12 @@ fn get_environments_from_envs_folder_in_conda_directory(
258
255
// iterate through all sub directories in the env folder
259
256
// for each sub directory, check if it has a python executable
260
257
// if it does, create a PythonEnvironment object and add it to the list
261
- for entry in std:: fs:: read_dir ( path. join ( "envs" ) ) . ok ( ) ? {
262
- match entry {
263
- Ok ( entry) => {
264
- if let Some ( env) = get_conda_environment_info ( & entry. path ( ) , true ) {
265
- envs. push ( env) ;
266
- }
267
- }
268
- Err ( _) => ( ) ,
258
+ for entry in std:: fs:: read_dir ( path. join ( "envs" ) )
259
+ . ok ( ) ?
260
+ . filter_map ( Result :: ok)
261
+ {
262
+ if let Some ( env) = get_conda_environment_info ( & entry. path ( ) , true ) {
263
+ envs. push ( env) ;
269
264
}
270
265
}
271
266
@@ -274,21 +269,14 @@ fn get_environments_from_envs_folder_in_conda_directory(
274
269
275
270
fn get_conda_envs_from_environment_txt ( environment : & dyn known:: Environment ) -> Vec < String > {
276
271
let mut envs = vec ! [ ] ;
277
- let home = environment. get_user_home ( ) ;
278
- match home {
279
- Some ( home) => {
280
- let home = Path :: new ( & home) ;
281
- let environment_txt = home. join ( ".conda" ) . join ( "environments.txt" ) ;
282
- match std:: fs:: read_to_string ( environment_txt) {
283
- Ok ( reader) => {
284
- for line in reader. lines ( ) {
285
- envs. push ( line. to_string ( ) ) ;
286
- }
287
- }
288
- Err ( _) => ( ) ,
272
+ if let Some ( home) = environment. get_user_home ( ) {
273
+ let home = Path :: new ( & home) ;
274
+ let environment_txt = home. join ( ".conda" ) . join ( "environments.txt" ) ;
275
+ if let Ok ( reader) = std:: fs:: read_to_string ( environment_txt) {
276
+ for line in reader. lines ( ) {
277
+ envs. push ( line. to_string ( ) ) ;
289
278
}
290
279
}
291
- None => ( ) ,
292
280
}
293
281
envs
294
282
}
@@ -309,31 +297,28 @@ fn get_conda_conda_rc(environment: &dyn known::Environment) -> Option<Condarc> {
309
297
if let Some ( home) = environment. get_user_home ( ) {
310
298
let conda_rc = Path :: new ( & home) . join ( ".condarc" ) ;
311
299
let mut start_consuming_values = false ;
312
- match std:: fs:: read_to_string ( conda_rc) {
313
- Ok ( reader) => {
314
- let mut env_dirs = vec ! [ ] ;
315
- for line in reader. lines ( ) {
316
- if line. starts_with ( "envs_dirs:" ) && !start_consuming_values {
317
- start_consuming_values = true ;
318
- continue ;
319
- }
320
- if start_consuming_values {
321
- if line. trim ( ) . starts_with ( "-" ) {
322
- if let Some ( env_dir) = line. splitn ( 2 , '-' ) . nth ( 1 ) {
323
- let env_dir = PathBuf :: from ( env_dir. trim ( ) ) ;
324
- if env_dir. exists ( ) {
325
- env_dirs. push ( env_dir) ;
326
- }
300
+ if let Ok ( reader) = std:: fs:: read_to_string ( conda_rc) {
301
+ let mut env_dirs = vec ! [ ] ;
302
+ for line in reader. lines ( ) {
303
+ if line. starts_with ( "envs_dirs:" ) && !start_consuming_values {
304
+ start_consuming_values = true ;
305
+ continue ;
306
+ }
307
+ if start_consuming_values {
308
+ if line. trim ( ) . starts_with ( "-" ) {
309
+ if let Some ( env_dir) = line. splitn ( 2 , '-' ) . nth ( 1 ) {
310
+ let env_dir = PathBuf :: from ( env_dir. trim ( ) ) ;
311
+ if env_dir. exists ( ) {
312
+ env_dirs. push ( env_dir) ;
327
313
}
328
- continue ;
329
- } else {
330
- break ;
331
314
}
315
+ continue ;
316
+ } else {
317
+ break ;
332
318
}
333
319
}
334
- return Some ( Condarc { env_dirs } ) ;
335
320
}
336
- Err ( _ ) => ( ) ,
321
+ return Some ( Condarc { env_dirs } ) ;
337
322
}
338
323
}
339
324
None
@@ -346,21 +331,16 @@ fn get_conda_envs_from_conda_rc(
346
331
let mut envs: Vec < CondaEnvironment > = vec ! [ ] ;
347
332
for env in get_conda_conda_rc ( environment) ?. env_dirs {
348
333
if let Ok ( reader) = std:: fs:: read_dir ( env) {
349
- for entry in reader {
350
- match entry {
351
- Ok ( entry) => {
352
- if entry. path ( ) . is_dir ( )
353
- && was_conda_environment_created_by_specific_conda (
354
- & entry. path ( ) ,
355
- root_conda_path,
356
- )
357
- {
358
- if let Some ( env) = get_conda_environment_info ( & entry. path ( ) , false ) {
359
- envs. push ( env) ;
360
- }
361
- }
334
+ for entry in reader. filter_map ( Result :: ok) {
335
+ if entry. path ( ) . is_dir ( )
336
+ && was_conda_environment_created_by_specific_conda (
337
+ & entry. path ( ) ,
338
+ root_conda_path,
339
+ )
340
+ {
341
+ if let Some ( env) = get_conda_environment_info ( & entry. path ( ) , false ) {
342
+ envs. push ( env) ;
362
343
}
363
- Err ( _) => ( ) ,
364
344
}
365
345
}
366
346
}
@@ -383,23 +363,17 @@ fn was_conda_environment_created_by_specific_conda(
383
363
root_conda_path : & PathBuf ,
384
364
) -> bool {
385
365
let conda_meta_history = env_path. join ( "conda-meta" ) . join ( "history" ) ;
386
- match std:: fs:: read_to_string ( conda_meta_history. clone ( ) ) {
387
- Ok ( reader) => {
388
- for line in reader. lines ( ) {
389
- let line = line. to_lowercase ( ) ;
390
- if line. starts_with ( "# cmd:" ) && line. contains ( " create " ) {
391
- if line. contains ( & root_conda_path. to_str ( ) . unwrap ( ) . to_lowercase ( ) ) {
392
- return true ;
393
- } else {
394
- return false ;
395
- }
366
+ if let Ok ( reader) = std:: fs:: read_to_string ( conda_meta_history. clone ( ) ) {
367
+ for line in reader. lines ( ) {
368
+ let line = line. to_lowercase ( ) ;
369
+ if line. starts_with ( "# cmd:" ) && line. contains ( " create " ) {
370
+ if line. contains ( & root_conda_path. to_str ( ) . unwrap ( ) . to_lowercase ( ) ) {
371
+ return true ;
372
+ } else {
373
+ return false ;
396
374
}
397
375
}
398
376
}
399
- Err ( _) => warn ! (
400
- "Error reading conda-meta/history file {:?}" ,
401
- conda_meta_history
402
- ) ,
403
377
}
404
378
405
379
false
@@ -555,7 +529,6 @@ fn get_root_python_environment(path: &PathBuf, manager: &EnvManager) -> Option<P
555
529
python_executable_path : Some ( python_exe) ,
556
530
version : Some ( package_info. version ) ,
557
531
env_path : Some ( path. clone ( ) ) ,
558
- sys_prefix_path : Some ( path. clone ( ) ) ,
559
532
env_manager : Some ( manager. clone ( ) ) ,
560
533
python_run_command : Some ( vec ! [
561
534
conda_exe,
@@ -601,7 +574,6 @@ pub fn get_conda_environments_in_specified_path(
601
574
messaging:: PythonEnvironmentCategory :: Conda ,
602
575
env. version . clone ( ) ,
603
576
Some ( env. path . clone ( ) ) ,
604
- Some ( env. path . clone ( ) ) ,
605
577
Some ( manager. clone ( ) ) ,
606
578
get_activation_command ( env, & manager) ,
607
579
) ;
@@ -746,7 +718,6 @@ fn get_conda_environments_from_environments_txt_that_have_not_been_discovered(
746
718
messaging:: PythonEnvironmentCategory :: Conda ,
747
719
env. version . clone ( ) ,
748
720
Some ( env. path . clone ( ) ) ,
749
- Some ( env. path . clone ( ) ) ,
750
721
Some ( manager. clone ( ) ) ,
751
722
get_activation_command ( & env, & manager) ,
752
723
) ;
0 commit comments