Skip to content

Commit f7c3cb1

Browse files
committed
be a bit more careful with sysroot in run_cmd, add logging + add dedicated test for run_cmd using with_sysroot=True
1 parent 165fe79 commit f7c3cb1

File tree

2 files changed

+35
-5
lines changed

2 files changed

+35
-5
lines changed

easybuild/tools/run.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -227,14 +227,17 @@ def run_cmd(cmd, log_ok=True, log_all=False, simple=False, inp=None, regexp=True
227227
if cmd_log:
228228
cmd_log.write("# output for command: %s\n\n" % cmd_msg)
229229

230+
exec_cmd = "/bin/bash"
231+
232+
# if EasyBuild is configured to use an alternate sysroot,
233+
# we should also run shell commands using the bash shell provided in there,
234+
# since /bin/bash may not be compatible with the alternate sysroot
230235
if with_sysroot:
231236
sysroot = build_option('sysroot')
232237
if sysroot:
233-
exec_cmd = "%s/bin/bash" % sysroot
234-
else:
235-
exec_cmd = "/bin/bash"
236-
else:
237-
exec_cmd = "/bin/bash"
238+
sysroot_bin_bash = os.path.join(sysroot, 'bin', 'bash')
239+
if os.path.exists(sysroot_bin_bash):
240+
exec_cmd = sysroot_bin_bash
238241

239242
if not shell:
240243
if isinstance(cmd, list):
@@ -245,6 +248,8 @@ def run_cmd(cmd, log_ok=True, log_all=False, simple=False, inp=None, regexp=True
245248
else:
246249
raise EasyBuildError("Don't know how to prefix with /usr/bin/env for commands of type %s", type(cmd))
247250

251+
_log.info("Using %s as shell for running cmd: %s", exec_cmd, cmd)
252+
248253
if with_hooks:
249254
hooks = load_hooks(build_option('hooks'))
250255
hook_res = run_hook(RUN_SHELL_CMD, hooks, pre_step_hook=True, args=[cmd], kwargs={'work_dir': os.getcwd()})

test/framework/run.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -796,6 +796,31 @@ def post_run_shell_cmd_hook(cmd, *args, **kwargs):
796796
])
797797
self.assertEqual(stdout, expected_stdout)
798798

799+
def test_run_cmd_sysroot(self):
800+
"""Test with_sysroot option of run_cmd function."""
801+
802+
# put fake /bin/bash in place that will be picked up when using run_cmd with with_sysroot=True
803+
bin_bash = os.path.join(self.test_prefix, 'bin', 'bash')
804+
bin_bash_txt = '\n'.join([
805+
"#!/bin/bash",
806+
"echo 'Hi there I am a fake /bin/bash in %s'" % self.test_prefix,
807+
'/bin/bash "$@"',
808+
])
809+
write_file(bin_bash, bin_bash_txt)
810+
adjust_permissions(bin_bash, stat.S_IXUSR)
811+
812+
update_build_option('sysroot', self.test_prefix)
813+
814+
(out, ec) = run_cmd("echo hello")
815+
self.assertEqual(ec, 0)
816+
self.assertTrue(out.startswith("Hi there I am a fake /bin/bash in"))
817+
self.assertTrue(out.endswith("\nhello\n"))
818+
819+
# picking up on alternate sysroot is enabled by default, but can be disabled via with_sysroot=False
820+
(out, ec) = run_cmd("echo hello", with_sysroot=False)
821+
self.assertEqual(ec, 0)
822+
self.assertEqual(out, "hello\n")
823+
799824

800825
def suite():
801826
""" returns all the testcases in this module """

0 commit comments

Comments
 (0)