-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathmanager.rs
More file actions
225 lines (204 loc) · 7.4 KB
/
manager.rs
File metadata and controls
225 lines (204 loc) · 7.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
use crate::{
conda_info::CondaInfo,
env_variables::EnvVariables,
environments::get_conda_installation_used_to_create_conda_env,
package::CondaPackageInfo,
utils::{is_conda_env, is_conda_install},
};
use log::trace;
use pet_core::{manager::EnvManager, manager::EnvManagerType};
use std::{
env,
path::{Path, PathBuf},
};
fn get_conda_executable(path: &Path) -> Option<PathBuf> {
#[cfg(windows)]
let relative_path_to_conda_exe = vec![
PathBuf::from("Scripts").join("conda.exe"),
PathBuf::from("Scripts").join("conda.bat"),
PathBuf::from("bin").join("conda.exe"),
PathBuf::from("bin").join("conda.bat"),
];
#[cfg(unix)]
let relative_path_to_conda_exe = vec![PathBuf::from("bin").join("conda")];
for relative_path in relative_path_to_conda_exe {
let exe = path.join(&relative_path);
if exe.exists() {
return Some(exe);
}
}
None
}
fn get_mamba_executable(path: &Path) -> Option<PathBuf> {
#[cfg(windows)]
let relative_paths = vec![
PathBuf::from("Scripts").join("mamba.exe"),
PathBuf::from("Scripts").join("micromamba.exe"),
PathBuf::from("bin").join("mamba.exe"),
PathBuf::from("bin").join("micromamba.exe"),
];
#[cfg(unix)]
let relative_paths = vec![
PathBuf::from("bin").join("mamba"),
PathBuf::from("bin").join("micromamba"),
];
for relative_path in relative_paths {
let exe = path.join(&relative_path);
if exe.exists() {
return Some(exe);
}
}
None
}
/// Specifically returns the file names that are valid for 'conda' on windows
#[cfg(windows)]
fn get_conda_bin_names() -> Vec<&'static str> {
vec!["conda.exe", "conda.bat"]
}
/// Specifically returns the file names that are valid for 'conda' on linux/Mac
#[cfg(unix)]
fn get_conda_bin_names() -> Vec<&'static str> {
vec!["conda"]
}
/// Specifically returns the file names that are valid for 'mamba'/'micromamba' on windows
#[cfg(windows)]
fn get_mamba_bin_names() -> Vec<&'static str> {
vec!["mamba.exe", "micromamba.exe"]
}
/// Specifically returns the file names that are valid for 'mamba'/'micromamba' on linux/Mac
#[cfg(unix)]
fn get_mamba_bin_names() -> Vec<&'static str> {
vec!["mamba", "micromamba"]
}
/// Find the conda binary on the PATH environment variable
pub fn find_conda_binary(env_vars: &EnvVariables) -> Option<PathBuf> {
let paths = env_vars.path.clone()?;
for path in env::split_paths(&paths) {
for bin in get_conda_bin_names() {
let conda_path = path.join(bin);
if conda_path.is_file() || conda_path.is_symlink() {
return Some(conda_path);
}
}
}
None
}
/// Find a mamba or micromamba binary on the PATH environment variable
pub fn find_mamba_binary(env_vars: &EnvVariables) -> Option<PathBuf> {
let paths = env_vars.path.clone()?;
for path in env::split_paths(&paths) {
for bin in get_mamba_bin_names() {
let mamba_path = path.join(bin);
if mamba_path.is_file() || mamba_path.is_symlink() {
return Some(mamba_path);
}
}
}
None
}
#[derive(Debug, Clone)]
pub struct CondaManager {
pub executable: PathBuf,
pub version: Option<String>,
pub conda_dir: Option<PathBuf>,
pub manager_type: EnvManagerType,
}
impl CondaManager {
pub fn to_manager(&self) -> EnvManager {
EnvManager {
tool: self.manager_type,
executable: self.executable.clone(),
version: self.version.clone(),
}
}
pub fn from(path: &Path) -> Option<CondaManager> {
if !is_conda_env(path) {
return None;
}
// If this environment is in a folder named `envs`, then the parent directory of `envs` is the root conda install folder.
if let Some(parent) = path.ancestors().nth(2) {
if is_conda_install(parent) {
if let Some(manager) =
get_conda_manager(parent).or_else(|| get_mamba_manager(parent))
{
return Some(manager);
}
}
}
// Possible this is a conda environment in some other location
// Such as global env folders location configured via condarc file
// Or a conda env created using `-p` flag.
// Get the conda install folder from the history file.
// Or its in a location such as `~/.conda/envs` or `~/miniconda3/envs` where the conda install folder is not a parent of this path.
if let Some(conda_install_folder) = get_conda_installation_used_to_create_conda_env(path) {
get_conda_manager(&conda_install_folder)
.or_else(|| get_mamba_manager(&conda_install_folder))
} else {
// If this is a conda env and the parent is `.conda/envs`, then this is definitely NOT a root conda install folder.
// Hence never use conda installs from these env paths.
if let Some(parent) = path.parent() {
if parent.ends_with(".conda/envs") || parent.ends_with(".conda\\envs") {
trace!(
"Parent path ends with .conda/envs, not a root conda install folder: {:?}",
parent
);
return None;
}
}
if let Some(manager) = get_conda_manager(path).or_else(|| get_mamba_manager(path)) {
Some(manager)
} else {
trace!("No conda or mamba manager found for path: {:?}", path);
None
}
}
}
pub fn from_info(
executable: &Path,
info: &CondaInfo,
manager_type: EnvManagerType,
) -> Option<CondaManager> {
Some(CondaManager {
executable: executable.to_path_buf(),
version: Some(info.conda_version.clone()),
conda_dir: info.conda_prefix.clone(),
manager_type,
})
}
}
fn get_conda_manager(path: &Path) -> Option<CondaManager> {
let conda_exe = get_conda_executable(path)?;
if let Some(conda_pkg) = CondaPackageInfo::from(path, &crate::package::Package::Conda) {
Some(CondaManager {
executable: conda_exe,
version: Some(conda_pkg.version),
conda_dir: Some(path.to_path_buf()),
manager_type: EnvManagerType::Conda,
})
} else {
None
}
}
/// Checks whether a given executable path refers to a mamba or micromamba binary.
pub fn is_mamba_executable(exe: &Path) -> bool {
if let Some(name) = exe.file_name().and_then(|n| n.to_str()) {
let name = name.to_lowercase();
name.starts_with("mamba") || name.starts_with("micromamba")
} else {
false
}
}
pub(crate) fn get_mamba_manager(path: &Path) -> Option<CondaManager> {
let mamba_exe = get_mamba_executable(path)?;
// We cannot reliably determine the mamba/micromamba version from package metadata alone.
// The conda package version in conda-meta is the conda version, not the mamba version.
// Determining the mamba version would require spawning the mamba process.
Some(CondaManager {
executable: mamba_exe,
version: None,
conda_dir: Some(path.to_path_buf()),
manager_type: EnvManagerType::Mamba,
})
}