Skip to content

Commit b31ec9f

Browse files
committed
Fixes OPEN-3628 Conda not available in subprocess calls when running in Docker container
1 parent 869ebd7 commit b31ec9f

File tree

2 files changed

+38
-20
lines changed

2 files changed

+38
-20
lines changed

openlayer/models.py

+37-19
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,12 @@ def __init__(
9494
python_version_file_path: str,
9595
logger: Optional[logging.Logger] = None,
9696
):
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()
100100
self.env_name = env_name
101101
self.requirements_file_path = requirements_file_path
102102
self.python_version_file_path = python_version_file_path
103-
self._conda_prefix = self._get_conda_prefix()
104103
self.logger = logger or logging.getLogger("validators")
105104

106105
def __enter__(self):
@@ -115,18 +114,25 @@ def __enter__(self):
115114
def __exit__(self, exc_type, exc_value, traceback):
116115
self.deactivate()
117116

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
123129

124130
def _get_conda_prefix(self) -> str:
125131
"""Gets the conda base environment prefix.
126132
127133
E.g., '~/miniconda3' or '~/anaconda3'
128134
"""
129-
prefix = subprocess.check_output(["conda", "info", "--base"])
135+
prefix = subprocess.check_output([self._conda_exe, "info", "--base"])
130136
return prefix.decode("UTF-8").strip()
131137

132138
def create(self):
@@ -141,7 +147,7 @@ def create(self):
141147

142148
process = subprocess.Popen(
143149
[
144-
"conda",
150+
self._conda_exe,
145151
"create",
146152
"-n",
147153
f"{self.env_name}",
@@ -167,7 +173,14 @@ def delete(self):
167173
self.logger.info("Deleting conda environment '%s'...", self.env_name)
168174

169175
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+
],
171184
stdout=subprocess.PIPE,
172185
stderr=subprocess.STDOUT,
173186
)
@@ -183,8 +196,9 @@ def get_existing_envs(self) -> Set[str]:
183196
"""Gets the names of all existing conda environments."""
184197
self.logger.info("Checking existing conda environments...")
185198

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}'
188202
"""
189203

190204
try:
@@ -206,9 +220,10 @@ def activate(self):
206220
self.logger.info("Activating conda environment '%s'...", self.env_name)
207221

208222
activation_command = f"""
209-
eval $(conda shell.bash hook)
210223
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+
"""
212227

213228
try:
214229
subprocess.check_call(
@@ -228,14 +243,16 @@ def deactivate(self):
228243
self.logger.info("Deactivating conda environment '%s'...", self.env_name)
229244

230245
deactivation_command = f"""
231-
eval $(conda shell.bash hook)
232246
source {self._conda_prefix}/etc/profile.d/conda.sh
233-
conda deactivate"""
247+
eval $(conda shell.bash hook)
248+
conda deactivate
249+
"""
234250

235251
try:
236252
subprocess.check_call(
237253
deactivation_command,
238254
shell=True,
255+
executable=self._bash,
239256
stdout=subprocess.DEVNULL,
240257
stderr=subprocess.STDOUT,
241258
)
@@ -269,14 +286,15 @@ def run_commands(self, commands: List[str]):
269286
List of commands to run.
270287
"""
271288
full_command = f"""
272-
eval $(conda shell.bash hook)
273289
source {self._conda_prefix}/etc/profile.d/conda.sh
290+
eval $(conda shell.bash hook)
274291
conda activate {self.env_name}
275292
{" ".join(commands)}
276293
"""
277294
process = subprocess.Popen(
278295
full_command,
279296
shell=True,
297+
executable=self._bash,
280298
stdout=subprocess.PIPE,
281299
stderr=subprocess.STDOUT,
282300
)

openlayer/version.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
# Define the SDK version here so that the interal package can have access to this value.
22
# See https://stackoverflow.com/questions/2058802
3-
__version__ = "0.0.0a4"
3+
__version__ = "0.0.0a5"

0 commit comments

Comments
 (0)