Skip to content

Commit fea0149

Browse files
committed
Remove sysPrefixPath from native locator (#23427)
1 parent 79e1423 commit fea0149

17 files changed

+132
-678
lines changed

native_locator/src/common_python.rs

-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ impl Locator for PythonOnPath<'_> {
4343
python_executable_path: Some(env.executable.clone()),
4444
version: env.version.clone(),
4545
category: crate::messaging::PythonEnvironmentCategory::System,
46-
sys_prefix_path: None,
4746
env_path: env.path.clone(),
4847
env_manager: None,
4948
project_path: None,

native_locator/src/conda.rs

+87-116
Original file line numberDiff line numberDiff line change
@@ -63,21 +63,24 @@ fn get_conda_package_json_path(path: &Path, package: &str) -> Option<CondaPackag
6363
let path = path.join("conda-meta");
6464
let package_name = format!("{}-", package);
6565
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
7682
}
77-
} else {
78-
None
79-
}
80-
})
83+
})
8184
}
8285

8386
fn get_conda_executable(path: &PathBuf) -> Option<PathBuf> {
@@ -109,13 +112,10 @@ fn find_conda_binary_on_path(environment: &dyn known::Environment) -> Option<Pat
109112
for path in env::split_paths(&paths) {
110113
for bin in get_conda_bin_names() {
111114
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);
117118
}
118-
Err(_) => (),
119119
}
120120
}
121121
}
@@ -213,40 +213,37 @@ struct CondaEnvironment {
213213
}
214214
fn get_conda_environment_info(env_path: &PathBuf, named: bool) -> Option<CondaEnvironment> {
215215
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+
});
238228
} else {
239229
return Some(CondaEnvironment {
240230
name: path.file_name()?.to_string_lossy().to_string(),
241231
path,
242232
named,
243-
python_executable_path: None,
233+
python_executable_path: Some(python_binary),
244234
version: None,
245235
});
246236
}
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+
});
247245
}
248246
}
249-
Err(_) => (),
250247
}
251248

252249
None
@@ -258,14 +255,12 @@ fn get_environments_from_envs_folder_in_conda_directory(
258255
// iterate through all sub directories in the env folder
259256
// for each sub directory, check if it has a python executable
260257
// 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);
269264
}
270265
}
271266

@@ -274,21 +269,14 @@ fn get_environments_from_envs_folder_in_conda_directory(
274269

275270
fn get_conda_envs_from_environment_txt(environment: &dyn known::Environment) -> Vec<String> {
276271
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());
289278
}
290279
}
291-
None => (),
292280
}
293281
envs
294282
}
@@ -309,31 +297,28 @@ fn get_conda_conda_rc(environment: &dyn known::Environment) -> Option<Condarc> {
309297
if let Some(home) = environment.get_user_home() {
310298
let conda_rc = Path::new(&home).join(".condarc");
311299
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);
327313
}
328-
continue;
329-
} else {
330-
break;
331314
}
315+
continue;
316+
} else {
317+
break;
332318
}
333319
}
334-
return Some(Condarc { env_dirs });
335320
}
336-
Err(_) => (),
321+
return Some(Condarc { env_dirs });
337322
}
338323
}
339324
None
@@ -346,21 +331,16 @@ fn get_conda_envs_from_conda_rc(
346331
let mut envs: Vec<CondaEnvironment> = vec![];
347332
for env in get_conda_conda_rc(environment)?.env_dirs {
348333
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);
362343
}
363-
Err(_) => (),
364344
}
365345
}
366346
}
@@ -383,23 +363,17 @@ fn was_conda_environment_created_by_specific_conda(
383363
root_conda_path: &PathBuf,
384364
) -> bool {
385365
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;
396374
}
397375
}
398376
}
399-
Err(_) => warn!(
400-
"Error reading conda-meta/history file {:?}",
401-
conda_meta_history
402-
),
403377
}
404378

405379
false
@@ -555,7 +529,6 @@ fn get_root_python_environment(path: &PathBuf, manager: &EnvManager) -> Option<P
555529
python_executable_path: Some(python_exe),
556530
version: Some(package_info.version),
557531
env_path: Some(path.clone()),
558-
sys_prefix_path: Some(path.clone()),
559532
env_manager: Some(manager.clone()),
560533
python_run_command: Some(vec![
561534
conda_exe,
@@ -601,7 +574,6 @@ pub fn get_conda_environments_in_specified_path(
601574
messaging::PythonEnvironmentCategory::Conda,
602575
env.version.clone(),
603576
Some(env.path.clone()),
604-
Some(env.path.clone()),
605577
Some(manager.clone()),
606578
get_activation_command(env, &manager),
607579
);
@@ -746,7 +718,6 @@ fn get_conda_environments_from_environments_txt_that_have_not_been_discovered(
746718
messaging::PythonEnvironmentCategory::Conda,
747719
env.version.clone(),
748720
Some(env.path.clone()),
749-
Some(env.path.clone()),
750721
Some(manager.clone()),
751722
get_activation_command(&env, &manager),
752723
);

0 commit comments

Comments
 (0)