Skip to content

Commit a3f47fc

Browse files
author
Thierry RAMORASOAVINA
committed
Ensure HOME env variable is corrected for OpenMPI 5+
If exists KHIOPS_MPI_HOME is prepended to HOME
1 parent 6aea158 commit a3f47fc

File tree

2 files changed

+86
-1
lines changed

2 files changed

+86
-1
lines changed

khiops/core/internals/runner.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -994,8 +994,12 @@ def _initialize_khiops_environment(self):
994994
var_value = ""
995995
else:
996996
continue
997+
998+
if var_name == "KHIOPS_MPI_HOME":
999+
# Prepend the corresponding value to HOME for OpenMPI 5+
1000+
os.environ["HOME"] = var_value + os.pathsep + os.environ["HOME"]
9971001
# Set paths to Khiops binaries
998-
if var_name == "KHIOPS_PATH":
1002+
elif var_name == "KHIOPS_PATH":
9991003
self.khiops_path = var_value
10001004
os.environ["KHIOPS_PATH"] = var_value
10011005
elif var_name == "KHIOPS_COCLUSTERING_PATH":

tests/test_khiops_integrations.py

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,12 +282,93 @@ def test_environment_error_on_bogus_khiops_env_script(self):
282282
output_msg = str(context.exception)
283283
self.assertEqual(output_msg, expected_msg)
284284

285+
def test_runner_environment_for_openmpi5(self):
286+
"""Test if HOME is actually corrected for OpenMPI 5+"""
287+
288+
with tempfile.TemporaryDirectory() as temp_dir:
289+
290+
# Save current HOME
291+
old_home = os.environ["HOME"]
292+
293+
# Create temporary khiops_env binary dir
294+
# Note: The "bin" subdir is needed for Windows.
295+
temp_khiops_env_dir = os.path.join(temp_dir, "bin")
296+
os.makedirs(temp_khiops_env_dir)
297+
298+
# Create temporary khiops_env path
299+
temp_khiops_env_file_path = os.path.join(temp_khiops_env_dir, "khiops_env")
300+
301+
# On Windows, set KHIOPS_HOME to the temp dir
302+
original_khiops_home_env_var = os.environ.get("KHIOPS_HOME")
303+
if platform.system() == "Windows":
304+
os.environ["KHIOPS_HOME"] = temp_dir
305+
temp_khiops_env_file_path += ".cmd"
306+
307+
# Replace the khiops_env with a crafted one
308+
with open(
309+
temp_khiops_env_file_path, "w", encoding="utf-8"
310+
) as temp_khiops_env_file:
311+
if platform.system() == "Windows":
312+
temp_khiops_env_file.write(
313+
"echo KHIOPS_PATH /usr/bin/MODL_openmpi\r\n"
314+
)
315+
temp_khiops_env_file.write(
316+
"echo KHIOPS_COCLUSTERING_PATH "
317+
"/usr/bin/MODL_Coclustering_openmpi\r\n"
318+
)
319+
temp_khiops_env_file.write("echo KHIOPS_MPI_HOME \tmp" + "\r\n")
320+
else:
321+
temp_khiops_env_file.write("#!/bin/bash\n")
322+
temp_khiops_env_file.write(
323+
"echo KHIOPS_PATH /usr/bin/MODL_openmpi\n"
324+
)
325+
temp_khiops_env_file.write(
326+
"echo KHIOPS_COCLUSTERING_PATH "
327+
"/usr/bin/MODL_Coclustering_openmpi\n"
328+
)
329+
temp_khiops_env_file.write("echo KHIOPS_MPI_HOME /tmp\n")
330+
331+
# Make the temporary khiops_env file executable
332+
os.chmod(
333+
temp_khiops_env_file_path,
334+
os.stat(temp_khiops_env_file_path).st_mode | stat.S_IEXEC,
335+
)
336+
337+
# Store initial PATH
338+
original_path_env_var = os.environ["PATH"]
339+
340+
# Prepend path to temporary khiops_env to PATH
341+
os.environ["PATH"] = (
342+
temp_khiops_env_dir + os.pathsep + original_path_env_var
343+
)
344+
345+
_ = KhiopsLocalRunner()
346+
347+
# Check `HOME` is corrected for OpenMPI 5+
348+
self.assertEqual(
349+
os.path.join("/", "tmp") + os.pathsep + old_home, os.environ["HOME"]
350+
)
351+
352+
# Restore HOME
353+
os.environ["HOME"] = old_home
354+
355+
# Restore initial PATH
356+
os.environ["PATH"] = original_path_env_var
357+
358+
# On Windows, restore initial KHIOPS_HOME
359+
if (
360+
platform.system() == "Windows"
361+
and original_khiops_home_env_var is not None
362+
):
363+
os.environ["KHIOPS_HOME"] = original_khiops_home_env_var
364+
285365
def test_runner_environment_initialization(self):
286366
"""Test that local runner initializes/ed its environment properly
287367
288368
.. note::
289369
To test a real initialization this test should be executed alone.
290370
"""
371+
291372
# Obtain the current runner
292373
runner = kh.get_runner()
293374

0 commit comments

Comments
 (0)