Skip to content

Commit 3b7295f

Browse files
authored
Merge pull request #4646 from casparvl/exec_command_with_sysroot
take into account alternate sysroot for `/bin/bash` used by `run_cmd`
2 parents f7035e9 + f7c3cb1 commit 3b7295f

File tree

4 files changed

+59
-16
lines changed

4 files changed

+59
-16
lines changed

easybuild/tools/options.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1969,7 +1969,7 @@ def set_tmpdir(tmpdir=None, raise_error=False):
19691969
os.close(fd)
19701970
os.chmod(tmptest_file, 0o700)
19711971
if not run_cmd(tmptest_file, simple=True, log_ok=False, regexp=False, force_in_dry_run=True, trace=False,
1972-
stream_output=False, with_hooks=False):
1972+
stream_output=False, with_hooks=False, with_sysroot=False):
19731973
msg = "The temporary directory (%s) does not allow to execute files. " % tempfile.gettempdir()
19741974
msg += "This can cause problems in the build process, consider using --tmpdir."
19751975
if raise_error:

easybuild/tools/run.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ def get_output_from_process(proc, read_size=None, asynchronous=False):
134134
@run_cmd_cache
135135
def run_cmd(cmd, log_ok=True, log_all=False, simple=False, inp=None, regexp=True, log_output=False, path=None,
136136
force_in_dry_run=False, verbose=True, shell=None, trace=True, stream_output=None, asynchronous=False,
137-
with_hooks=True):
137+
with_hooks=True, with_sysroot=True):
138138
"""
139139
Run specified command (in a subshell)
140140
:param cmd: command to run
@@ -152,6 +152,7 @@ def run_cmd(cmd, log_ok=True, log_all=False, simple=False, inp=None, regexp=True
152152
:param stream_output: enable streaming command output to stdout
153153
:param asynchronous: run command asynchronously (returns subprocess.Popen instance if set to True)
154154
:param with_hooks: trigger pre/post run_shell_cmd hooks (if defined)
155+
:param with_sysroot: prepend sysroot to exec_cmd (if defined)
155156
"""
156157
cwd = os.getcwd()
157158

@@ -228,6 +229,16 @@ def run_cmd(cmd, log_ok=True, log_all=False, simple=False, inp=None, regexp=True
228229

229230
exec_cmd = "/bin/bash"
230231

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
235+
if with_sysroot:
236+
sysroot = build_option('sysroot')
237+
if sysroot:
238+
sysroot_bin_bash = os.path.join(sysroot, 'bin', 'bash')
239+
if os.path.exists(sysroot_bin_bash):
240+
exec_cmd = sysroot_bin_bash
241+
231242
if not shell:
232243
if isinstance(cmd, list):
233244
exec_cmd = None
@@ -237,6 +248,8 @@ def run_cmd(cmd, log_ok=True, log_all=False, simple=False, inp=None, regexp=True
237248
else:
238249
raise EasyBuildError("Don't know how to prefix with /usr/bin/env for commands of type %s", type(cmd))
239250

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

easybuild/tools/systemtools.py

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,8 @@ def get_avail_core_count():
275275
core_cnt = int(sum(sched_getaffinity()))
276276
else:
277277
# BSD-type systems
278-
out, _ = run_cmd('sysctl -n hw.ncpu', force_in_dry_run=True, trace=False, stream_output=False, with_hooks=False)
278+
out, _ = run_cmd('sysctl -n hw.ncpu', force_in_dry_run=True, trace=False, stream_output=False,
279+
with_hooks=False, with_sysroot=False)
279280
try:
280281
if int(out) > 0:
281282
core_cnt = int(out)
@@ -312,7 +313,8 @@ def get_total_memory():
312313
elif os_type == DARWIN:
313314
cmd = "sysctl -n hw.memsize"
314315
_log.debug("Trying to determine total memory size on Darwin via cmd '%s'", cmd)
315-
out, ec = run_cmd(cmd, force_in_dry_run=True, trace=False, stream_output=False, with_hooks=False)
316+
out, ec = run_cmd(cmd, force_in_dry_run=True, trace=False, stream_output=False, with_hooks=False,
317+
with_sysroot=False)
316318
if ec == 0:
317319
memtotal = int(out.strip()) // (1024**2)
318320

@@ -394,15 +396,16 @@ def get_cpu_vendor():
394396

395397
elif os_type == DARWIN:
396398
cmd = "sysctl -n machdep.cpu.vendor"
397-
out, ec = run_cmd(cmd, force_in_dry_run=True, trace=False, stream_output=False, log_ok=False, with_hooks=False)
399+
out, ec = run_cmd(cmd, force_in_dry_run=True, trace=False, stream_output=False, log_ok=False,
400+
with_hooks=False, with_sysroot=False)
398401
out = out.strip()
399402
if ec == 0 and out in VENDOR_IDS:
400403
vendor = VENDOR_IDS[out]
401404
_log.debug("Determined CPU vendor on DARWIN as being '%s' via cmd '%s" % (vendor, cmd))
402405
else:
403406
cmd = "sysctl -n machdep.cpu.brand_string"
404407
out, ec = run_cmd(cmd, force_in_dry_run=True, trace=False, stream_output=False, log_ok=False,
405-
with_hooks=False)
408+
with_hooks=False, with_sysroot=False)
406409
out = out.strip().split(' ')[0]
407410
if ec == 0 and out in CPU_VENDORS:
408411
vendor = out
@@ -505,7 +508,8 @@ def get_cpu_model():
505508

506509
elif os_type == DARWIN:
507510
cmd = "sysctl -n machdep.cpu.brand_string"
508-
out, ec = run_cmd(cmd, force_in_dry_run=True, trace=False, stream_output=False, with_hooks=False)
511+
out, ec = run_cmd(cmd, force_in_dry_run=True, trace=False, stream_output=False, with_hooks=False,
512+
with_sysroot=False)
509513
if ec == 0:
510514
model = out.strip()
511515
_log.debug("Determined CPU model on Darwin using cmd '%s': %s" % (cmd, model))
@@ -550,7 +554,8 @@ def get_cpu_speed():
550554
elif os_type == DARWIN:
551555
cmd = "sysctl -n hw.cpufrequency_max"
552556
_log.debug("Trying to determine CPU frequency on Darwin via cmd '%s'" % cmd)
553-
out, ec = run_cmd(cmd, force_in_dry_run=True, trace=False, stream_output=False, with_hooks=False)
557+
out, ec = run_cmd(cmd, force_in_dry_run=True, trace=False, stream_output=False, with_hooks=False,
558+
with_sysroot=False)
554559
out = out.strip()
555560
cpu_freq = None
556561
if ec == 0 and out:
@@ -599,7 +604,7 @@ def get_cpu_features():
599604
cmd = "sysctl -n machdep.cpu.%s" % feature_set
600605
_log.debug("Trying to determine CPU features on Darwin via cmd '%s'", cmd)
601606
out, ec = run_cmd(cmd, force_in_dry_run=True, trace=False, stream_output=False, log_ok=False,
602-
with_hooks=False)
607+
with_hooks=False, with_sysroot=False)
603608
if ec == 0:
604609
cpu_feat.extend(out.strip().lower().split())
605610

@@ -626,8 +631,8 @@ def get_gpu_info():
626631
try:
627632
cmd = "nvidia-smi --query-gpu=gpu_name,driver_version --format=csv,noheader"
628633
_log.debug("Trying to determine NVIDIA GPU info on Linux via cmd '%s'", cmd)
629-
out, ec = run_cmd(cmd, simple=False, log_ok=False, log_all=False,
630-
force_in_dry_run=True, trace=False, stream_output=False, with_hooks=False)
634+
out, ec = run_cmd(cmd, simple=False, log_ok=False, log_all=False, force_in_dry_run=True,
635+
trace=False, stream_output=False, with_hooks=False, with_sysroot=False)
631636
if ec == 0:
632637
for line in out.strip().split('\n'):
633638
nvidia_gpu_info = gpu_info.setdefault('NVIDIA', {})
@@ -645,15 +650,15 @@ def get_gpu_info():
645650
try:
646651
cmd = "rocm-smi --showdriverversion --csv"
647652
_log.debug("Trying to determine AMD GPU driver on Linux via cmd '%s'", cmd)
648-
out, ec = run_cmd(cmd, simple=False, log_ok=False, log_all=False,
649-
force_in_dry_run=True, trace=False, stream_output=False, with_hooks=False)
653+
out, ec = run_cmd(cmd, simple=False, log_ok=False, log_all=False, force_in_dry_run=True,
654+
trace=False, stream_output=False, with_hooks=False, with_sysroot=False)
650655
if ec == 0:
651656
amd_driver = out.strip().split('\n')[1].split(',')[1]
652657

653658
cmd = "rocm-smi --showproductname --csv"
654659
_log.debug("Trying to determine AMD GPU info on Linux via cmd '%s'", cmd)
655-
out, ec = run_cmd(cmd, simple=False, log_ok=False, log_all=False,
656-
force_in_dry_run=True, trace=False, stream_output=False, with_hooks=False)
660+
out, ec = run_cmd(cmd, simple=False, log_ok=False, log_all=False, force_in_dry_run=True,
661+
trace=False, stream_output=False, with_hooks=False, with_sysroot=False)
657662
if ec == 0:
658663
for line in out.strip().split('\n')[1:]:
659664
amd_card_series = line.split(',')[1]
@@ -900,7 +905,7 @@ def get_tool_version(tool, version_option='--version', ignore_ec=False):
900905
Output is returned as a single-line string (newlines are replaced by '; ').
901906
"""
902907
out, ec = run_cmd(' '.join([tool, version_option]), simple=False, log_ok=False, force_in_dry_run=True,
903-
trace=False, stream_output=False, with_hooks=False)
908+
trace=False, stream_output=False, with_hooks=False, with_sysroot=False)
904909
if not ignore_ec and ec:
905910
_log.warning("Failed to determine version of %s using '%s %s': %s" % (tool, tool, version_option, out))
906911
return UNKNOWN

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)