@@ -32,23 +32,8 @@ impl CondaEnvironment {
32
32
33
33
pub fn to_python_environment ( & self , conda_manager : Option < EnvManager > ) -> PythonEnvironment {
34
34
#[ allow( unused_assignments) ]
35
- let mut name: Option < String > = None ;
36
- if is_conda_install ( & self . prefix ) {
37
- name = Some ( "base" . to_string ( ) ) ;
38
- } else {
39
- name = self
40
- . prefix
41
- . file_name ( )
42
- . map ( |name| name. to_str ( ) . unwrap_or_default ( ) . to_string ( ) ) ;
43
- }
44
- // if the conda install folder is parent of the env folder, then we can use named activation.
45
- // E.g. conda env is = <conda install>/envs/<env name>
46
- // Then we can use `<conda install>/bin/conda activate -n <env name>`
47
- if let Some ( conda_dir) = & self . conda_dir {
48
- if !self . prefix . starts_with ( conda_dir) {
49
- name = None ;
50
- }
51
- }
35
+ let name = get_conda_env_name ( & self . prefix , & self . prefix , & self . conda_dir ) ;
36
+
52
37
// This is a root env.
53
38
let builder = PythonEnvironmentBuilder :: new ( Some ( PythonEnvironmentKind :: Conda ) )
54
39
. executable ( self . executable . clone ( ) )
@@ -143,29 +128,93 @@ pub fn get_conda_installation_used_to_create_conda_env(env_path: &Path) -> Optio
143
128
144
129
// First look for the conda-meta/history file in the environment folder.
145
130
// This could be a conda envirment (not root) but has `conda` installed in it.
131
+ if let Some ( line) = get_conda_creation_line_from_history ( env_path) {
132
+ // Sample lines
133
+ // # cmd: <conda install directory>\Scripts\conda-script.py create -n samlpe1
134
+ // # cmd: <conda install directory>\Scripts\conda-script.py create -p <full path>
135
+ // # cmd: /Users/donjayamanne/miniconda3/bin/conda create -n conda1
136
+ if let Some ( conda_dir) = get_conda_dir_from_cmd ( line) {
137
+ if is_conda_install ( & conda_dir) {
138
+ return Some ( conda_dir) ;
139
+ }
140
+ }
141
+ }
142
+
143
+ // Possible the env_path is the root conda install folder.
144
+ if is_conda_install ( env_path) {
145
+ Some ( env_path. to_path_buf ( ) )
146
+ } else {
147
+ None
148
+ }
149
+ }
150
+
151
+ pub fn get_conda_creation_line_from_history ( env_path : & Path ) -> Option < String > {
146
152
let conda_meta_history = env_path. join ( "conda-meta" ) . join ( "history" ) ;
147
153
if let Ok ( reader) = std:: fs:: read_to_string ( conda_meta_history. clone ( ) ) {
148
154
if let Some ( line) = reader. lines ( ) . map ( |l| l. trim ( ) ) . find ( |l| {
149
155
l. to_lowercase ( ) . starts_with ( "# cmd:" ) && l. to_lowercase ( ) . contains ( " create -" )
150
156
} ) {
157
+ trace ! (
158
+ "Conda creation line for {:?} is from history file is {:?}" ,
159
+ env_path,
160
+ line
161
+ ) ;
162
+ return Some ( line. into ( ) ) ;
163
+ }
164
+ }
165
+
166
+ None
167
+ }
168
+
169
+ fn get_conda_env_name (
170
+ env_path : & Path ,
171
+ prefix : & Path ,
172
+ conda_dir : & Option < PathBuf > ,
173
+ ) -> Option < String > {
174
+ let mut name: Option < String > ;
175
+ if is_conda_install ( prefix) {
176
+ name = Some ( "base" . to_string ( ) ) ;
177
+ } else {
178
+ name = prefix
179
+ . file_name ( )
180
+ . map ( |name| name. to_str ( ) . unwrap_or_default ( ) . to_string ( ) ) ;
181
+ }
182
+ // if the conda install folder is parent of the env folder, then we can use named activation.
183
+ // E.g. conda env is = <conda install>/envs/<env name>
184
+ // Then we can use `<conda install>/bin/conda activate -n <env name>`
185
+ if let Some ( conda_dir) = conda_dir {
186
+ if !prefix. starts_with ( conda_dir) {
187
+ name = get_conda_env_name_from_history_file ( env_path, prefix) ;
188
+ }
189
+ }
190
+
191
+ name
192
+ }
193
+
194
+ /**
195
+ * The conda-meta/history file in conda environments contain the command used to create the conda environment.
196
+ * And example is `# cmd: <conda install directory>\Scripts\conda-script.py create -n sample``
197
+ * And example is `# cmd: conda create -n sample``
198
+ *
199
+ * This function returns the name of the conda environment.
200
+ */
201
+ fn get_conda_env_name_from_history_file ( env_path : & Path , prefix : & Path ) -> Option < String > {
202
+ let name = prefix
203
+ . file_name ( )
204
+ . map ( |name| name. to_str ( ) . unwrap_or_default ( ) . to_string ( ) ) ;
205
+
206
+ if let Some ( name) = name {
207
+ if let Some ( line) = get_conda_creation_line_from_history ( env_path) {
151
208
// Sample lines
152
209
// # cmd: <conda install directory>\Scripts\conda-script.py create -n samlpe1
153
210
// # cmd: <conda install directory>\Scripts\conda-script.py create -p <full path>
154
211
// # cmd: /Users/donjayamanne/miniconda3/bin/conda create -n conda1
155
- if let Some ( conda_dir) = get_conda_dir_from_cmd ( line. into ( ) ) {
156
- if is_conda_install ( & conda_dir) {
157
- return Some ( conda_dir) ;
158
- }
212
+ if is_conda_env_name_in_cmd ( line, & name) {
213
+ return Some ( name) ;
159
214
}
160
215
}
161
216
}
162
-
163
- // Possible the env_path is the root conda install folder.
164
- if is_conda_install ( env_path) {
165
- Some ( env_path. to_path_buf ( ) )
166
- } else {
167
- None
168
- }
217
+ None
169
218
}
170
219
171
220
fn get_conda_dir_from_cmd ( cmd_line : String ) -> Option < PathBuf > {
@@ -229,6 +278,16 @@ fn get_conda_dir_from_cmd(cmd_line: String) -> Option<PathBuf> {
229
278
None
230
279
}
231
280
281
+ fn is_conda_env_name_in_cmd ( cmd_line : String , name : & str ) -> bool {
282
+ // Sample lines
283
+ // # cmd: <conda install directory>\Scripts\conda-script.py create -n samlpe1
284
+ // # cmd: <conda install directory>\Scripts\conda-script.py create -p <full path>
285
+ // # cmd: /Users/donjayamanne/miniconda3/bin/conda create -n conda1
286
+ // # cmd_line: "# cmd: /usr/bin/conda create -p ./prefix-envs/.conda1 python=3.12 -y"
287
+ // Look for "-n <name>" in the command line
288
+ cmd_line. contains ( format ! ( "-n {:?}" , name) . as_str ( ) )
289
+ }
290
+
232
291
pub fn get_activation_command (
233
292
env : & CondaEnvironment ,
234
293
manager : & EnvManager ,
0 commit comments