Skip to content

Commit 748bc9b

Browse files
authored
Run discovery on CI and adjust Conda env discovery (#9)
1 parent 78739b3 commit 748bc9b

File tree

6 files changed

+109
-20
lines changed

6 files changed

+109
-20
lines changed

.github/workflows/pr-check.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,23 @@ jobs:
1919
include:
2020
- os: windows-latest
2121
target: x86_64-pc-windows-msvc
22+
run_cli: "yes"
2223
- os: windows-latest
2324
target: aarch64-pc-windows-msvc
25+
run_cli: "no"
2426
- os: ubuntu-latest
2527
target: x86_64-unknown-linux-musl
28+
run_cli: "yes"
2629
# - os: ubuntu-latest
2730
# target: aarch64-unknown-linux-gnu
2831
# - os: ubuntu-latest
2932
# target: arm-unknown-linux-gnueabihf
3033
- os: macos-latest
3134
target: x86_64-apple-darwin
35+
run_cli: "yes"
3236
- os: macos-14
3337
target: aarch64-apple-darwin
38+
run_cli: "yes"
3439
# - os: ubuntu-latest
3540
# target: x86_64-unknown-linux-gnu
3641
# - os: ubuntu-latest
@@ -130,6 +135,11 @@ jobs:
130135
run: cargo test --frozen --all-features
131136
shell: bash
132137

138+
- name: Find Environments
139+
if: matrix.run_cli == 'yes'
140+
run: cargo run --release --target ${{ matrix.target }}
141+
shell: bash
142+
133143
- name: Build
134144
run: cargo build --release --target ${{ matrix.target }}
135145
shell: bash

crates/pet-conda/src/conda_rc.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ impl Condarc {
1717
}
1818

1919
#[cfg(windows)]
20+
// Search paths documented here
21+
// https://conda.io/projects/conda/en/latest/user-guide/configuration/use-condarc.html#searching-for-condarc
2022
fn get_conda_rc_search_paths(env_vars: &EnvVariables) -> Vec<PathBuf> {
2123
let mut search_paths: Vec<PathBuf> = [
2224
"C:\\ProgramData\\conda\\.condarc",
@@ -60,6 +62,8 @@ fn get_conda_rc_search_paths(env_vars: &EnvVariables) -> Vec<PathBuf> {
6062
}
6163

6264
#[cfg(unix)]
65+
// Search paths documented here
66+
// https://conda.io/projects/conda/en/latest/user-guide/configuration/use-condarc.html#searching-for-condarc
6367
fn get_conda_rc_search_paths(env_vars: &EnvVariables) -> Vec<PathBuf> {
6468
let mut search_paths: Vec<PathBuf> = [
6569
"/etc/conda/.condarc",

crates/pet-conda/src/env_variables.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ pub struct EnvVariables {
1717
pub programdata: Option<String>,
1818
pub homedrive: Option<String>,
1919
pub conda_root: Option<String>,
20+
pub conda: Option<String>,
2021
pub conda_prefix: Option<String>,
2122
pub condarc: Option<String>,
2223
pub xdg_config_home: Option<String>,
@@ -33,7 +34,8 @@ impl EnvVariables {
3334
allusersprofile: env.get_env_var("ALLUSERSPROFILE".to_string()),
3435
programdata: env.get_env_var("PROGRAMDATA".to_string()),
3536
homedrive: env.get_env_var("HOMEDRIVE".to_string()),
36-
conda_root: env.get_env_var("ALLUSERSPROFILE".to_string()),
37+
conda_root: env.get_env_var("CONDA_ROOT".to_string()),
38+
conda: env.get_env_var("CONDA".to_string()),
3739
conda_prefix: env.get_env_var("CONDA_PREFIX".to_string()),
3840
condarc: env.get_env_var("CONDARC".to_string()),
3941
xdg_config_home: env.get_env_var("XDG_CONFIG_HOME".to_string()),

crates/pet-conda/src/environment_locations.rs

Lines changed: 73 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -107,15 +107,29 @@ pub fn get_environments(conda_dir: &Path) -> Vec<PathBuf> {
107107
envs.push(conda_dir.to_path_buf());
108108

109109
if let Ok(entries) = fs::read_dir(conda_dir.join("envs")) {
110-
for entry in entries.filter_map(Result::ok) {
111-
let path = entry.path();
112-
if is_conda_env(&path) {
113-
envs.push(path);
114-
}
115-
}
110+
envs.append(
111+
&mut entries
112+
.filter_map(Result::ok)
113+
.map(|e| e.path())
114+
.filter(|p| is_conda_env(p))
115+
.collect(),
116+
);
116117
}
117118
} else if is_conda_env(conda_dir) {
118119
envs.push(conda_dir.to_path_buf());
120+
} else if fs::metadata(conda_dir.join("envs")).is_ok() {
121+
// This could be a directory where conda environments are stored.
122+
// I.e. its not necessarily the root conda install directory.
123+
// E.g. C:\Users\donjayamanne\.conda
124+
if let Ok(entries) = fs::read_dir(conda_dir.join("envs")) {
125+
envs.append(
126+
&mut entries
127+
.filter_map(Result::ok)
128+
.map(|e| e.path())
129+
.filter(|p| is_conda_env(p))
130+
.collect(),
131+
);
132+
}
119133
}
120134

121135
envs.sort();
@@ -161,33 +175,73 @@ pub fn get_known_conda_install_locations(env_vars: &EnvVariables) -> Vec<PathBuf
161175
known_paths.push(Path::new(&home_drive).join("miniconda"));
162176
known_paths.push(Path::new(&home_drive).join("miniforge3"));
163177
}
178+
if let Some(ref conda_root) = env_vars.conda_root {
179+
known_paths.push(PathBuf::from(conda_root.clone()));
180+
}
181+
if let Some(ref conda_prefix) = env_vars.conda_prefix {
182+
known_paths.push(PathBuf::from(conda_prefix.clone()));
183+
}
184+
if let Some(ref conda) = env_vars.conda {
185+
let conda = PathBuf::from(conda);
186+
if let Some(parent) = conda.parent() {
187+
known_paths.push(parent.to_path_buf());
188+
}
189+
}
164190
if let Some(home) = env_vars.clone().home {
165191
known_paths.push(home.clone().join("anaconda3"));
166192
known_paths.push(home.clone().join("miniconda3"));
167193
known_paths.push(home.clone().join("miniforge3"));
194+
// E.g. C:\Users\user name\.conda where we have `envs`` under this directory.
168195
known_paths.push(home.join(".conda"));
196+
// E.g. C:\Users\user name\AppData\Local\conda\conda\envs
197+
known_paths.push(
198+
home.join("AppData")
199+
.join("Local")
200+
.join("conda")
201+
.join("conda"),
202+
);
169203
}
204+
known_paths.sort();
205+
known_paths.dedup();
170206
known_paths
171207
}
172208

173209
#[cfg(unix)]
174210
pub fn get_known_conda_install_locations(env_vars: &EnvVariables) -> Vec<PathBuf> {
175-
let mut known_paths = vec![
176-
PathBuf::from("/opt/anaconda3"),
177-
PathBuf::from("/opt/miniconda3"),
178-
PathBuf::from("/usr/local/anaconda3"),
179-
PathBuf::from("/usr/local/miniconda3"),
180-
PathBuf::from("/usr/anaconda3"),
181-
PathBuf::from("/usr/miniconda3"),
182-
PathBuf::from("/home/anaconda3"),
183-
PathBuf::from("/home/miniconda3"),
184-
PathBuf::from("/anaconda3"),
185-
PathBuf::from("/miniconda3"),
186-
PathBuf::from("/miniforge3"),
187-
PathBuf::from("/miniforge3"),
211+
let mut known_paths = vec![];
212+
let directories_to_look_in = [
213+
"/opt",
214+
"/opt",
215+
"/usr/share",
216+
"/usr/local",
217+
"/usr",
218+
"/home",
219+
"", // We need to look in `/anaconda3` and `/miniconda3` as well.
188220
];
221+
for directory in directories_to_look_in.iter() {
222+
known_paths.push(PathBuf::from(format!("{}/anaconda", directory)));
223+
known_paths.push(PathBuf::from(format!("{}/anaconda3", directory)));
224+
known_paths.push(PathBuf::from(format!("{}/miniconda", directory)));
225+
known_paths.push(PathBuf::from(format!("{}/miniconda3", directory)));
226+
known_paths.push(PathBuf::from(format!("{}/miniforge", directory)));
227+
known_paths.push(PathBuf::from(format!("{}/miniforge3", directory)));
228+
}
229+
if let Some(ref conda_root) = env_vars.conda_root {
230+
known_paths.push(PathBuf::from(conda_root.clone()));
231+
}
232+
if let Some(ref conda_prefix) = env_vars.conda_prefix {
233+
known_paths.push(PathBuf::from(conda_prefix.clone()));
234+
}
235+
if let Some(ref conda) = env_vars.conda {
236+
let conda = PathBuf::from(conda);
237+
if let Some(parent) = conda.parent() {
238+
known_paths.push(parent.to_path_buf());
239+
}
240+
}
189241
if let Some(ref home) = env_vars.home {
242+
known_paths.push(home.clone().join("anaconda"));
190243
known_paths.push(home.clone().join("anaconda3"));
244+
known_paths.push(home.clone().join("miniconda"));
191245
known_paths.push(home.clone().join("miniconda3"));
192246
known_paths.push(home.clone().join("miniforge3"));
193247
known_paths.push(home.join(".conda"));

crates/pet-conda/tests/ci_test.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
mod common;
5+
6+
#[cfg(unix)]
7+
#[test]
8+
fn conda_ci() {
9+
use pet_conda::Conda;
10+
use pet_core::{os_environment::EnvironmentApi, Locator};
11+
12+
let env = EnvironmentApi::new();
13+
14+
let conda = Conda::from(&env);
15+
let result = conda.find();
16+
println!("SERVER CI Started");
17+
println!("SERVER CI REsults{:?}", result);
18+
}

crates/pet-conda/tests/common.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ pub fn create_env_variables(home: PathBuf, root: PathBuf) -> EnvVariables {
2222
allusersprofile: None,
2323
conda_prefix: None,
2424
conda_root: None,
25+
conda: None,
2526
condarc: None,
2627
homedrive: None,
2728
known_global_search_locations: vec![],

0 commit comments

Comments
 (0)