@@ -94,13 +94,12 @@ def __init__(
94
94
python_version_file_path : str ,
95
95
logger : Optional [logging .Logger ] = None ,
96
96
):
97
- if not self ._conda_available ():
98
- raise Exception ( "Conda is not available on this machine." )
99
-
97
+ self . _conda_exe = self ._get_executable ()
98
+ self . _conda_prefix = self . _get_conda_prefix ( )
99
+ self . _bash = self . _get_bash ()
100
100
self .env_name = env_name
101
101
self .requirements_file_path = requirements_file_path
102
102
self .python_version_file_path = python_version_file_path
103
- self ._conda_prefix = self ._get_conda_prefix ()
104
103
self .logger = logger or logging .getLogger ("validators" )
105
104
106
105
def __enter__ (self ):
@@ -115,18 +114,25 @@ def __enter__(self):
115
114
def __exit__ (self , exc_type , exc_value , traceback ):
116
115
self .deactivate ()
117
116
118
- def _conda_available (self ) -> bool :
119
- """Checks if conda is available on the machine."""
120
- if os .environ .get ("CONDA_EXE" ) is None :
121
- return False
122
- return True
117
+ def _get_executable (self ) -> str :
118
+ conda_exe = os .environ .get ("CONDA_EXE" )
119
+ if conda_exe is None :
120
+ raise Exception ("Conda is not available on this machine." )
121
+ return conda_exe
122
+
123
+ def _get_bash (self ) -> str :
124
+ """Gets the bash executable."""
125
+ shell_path = shutil .which ("bash" )
126
+ if shell_path is None :
127
+ raise Exception ("Bash is not available on this machine." )
128
+ return shell_path
123
129
124
130
def _get_conda_prefix (self ) -> str :
125
131
"""Gets the conda base environment prefix.
126
132
127
133
E.g., '~/miniconda3' or '~/anaconda3'
128
134
"""
129
- prefix = subprocess .check_output (["conda" , "info" , "--base" ])
135
+ prefix = subprocess .check_output ([self . _conda_exe , "info" , "--base" ])
130
136
return prefix .decode ("UTF-8" ).strip ()
131
137
132
138
def create (self ):
@@ -141,7 +147,7 @@ def create(self):
141
147
142
148
process = subprocess .Popen (
143
149
[
144
- "conda" ,
150
+ self . _conda_exe ,
145
151
"create" ,
146
152
"-n" ,
147
153
f"{ self .env_name } " ,
@@ -167,7 +173,14 @@ def delete(self):
167
173
self .logger .info ("Deleting conda environment '%s'..." , self .env_name )
168
174
169
175
process = subprocess .Popen (
170
- ["conda" , "env" , "remove" , "-n" , f"{ self .env_name } " , "--yes" ],
176
+ [
177
+ self ._conda_exe ,
178
+ "env" ,
179
+ "remove" ,
180
+ "-n" ,
181
+ f"{ self .env_name } " ,
182
+ "--yes" ,
183
+ ],
171
184
stdout = subprocess .PIPE ,
172
185
stderr = subprocess .STDOUT ,
173
186
)
@@ -183,8 +196,9 @@ def get_existing_envs(self) -> Set[str]:
183
196
"""Gets the names of all existing conda environments."""
184
197
self .logger .info ("Checking existing conda environments..." )
185
198
186
- list_envs_command = """
187
- conda env list | awk '{print $1}'
199
+ awk_command = "awk '{print $1}"
200
+ list_envs_command = f"""
201
+ { self ._conda_exe } env list | { awk_command } '
188
202
"""
189
203
190
204
try :
@@ -206,9 +220,10 @@ def activate(self):
206
220
self .logger .info ("Activating conda environment '%s'..." , self .env_name )
207
221
208
222
activation_command = f"""
209
- eval $(conda shell.bash hook)
210
223
source { self ._conda_prefix } /etc/profile.d/conda.sh
211
- conda activate { self .env_name } """
224
+ eval $(conda shell.bash hook)
225
+ conda activate { self .env_name }
226
+ """
212
227
213
228
try :
214
229
subprocess .check_call (
@@ -228,14 +243,16 @@ def deactivate(self):
228
243
self .logger .info ("Deactivating conda environment '%s'..." , self .env_name )
229
244
230
245
deactivation_command = f"""
231
- eval $(conda shell.bash hook)
232
246
source { self ._conda_prefix } /etc/profile.d/conda.sh
233
- conda deactivate"""
247
+ eval $(conda shell.bash hook)
248
+ conda deactivate
249
+ """
234
250
235
251
try :
236
252
subprocess .check_call (
237
253
deactivation_command ,
238
254
shell = True ,
255
+ executable = self ._bash ,
239
256
stdout = subprocess .DEVNULL ,
240
257
stderr = subprocess .STDOUT ,
241
258
)
@@ -269,14 +286,15 @@ def run_commands(self, commands: List[str]):
269
286
List of commands to run.
270
287
"""
271
288
full_command = f"""
272
- eval $(conda shell.bash hook)
273
289
source { self ._conda_prefix } /etc/profile.d/conda.sh
290
+ eval $(conda shell.bash hook)
274
291
conda activate { self .env_name }
275
292
{ " " .join (commands )}
276
293
"""
277
294
process = subprocess .Popen (
278
295
full_command ,
279
296
shell = True ,
297
+ executable = self ._bash ,
280
298
stdout = subprocess .PIPE ,
281
299
stderr = subprocess .STDOUT ,
282
300
)
0 commit comments