Skip to content

Commit ca9186e

Browse files
committed
only log shell command output when output_file option is enabled in run_shell_cmd
1 parent 316a89b commit ca9186e

File tree

2 files changed

+31
-25
lines changed

2 files changed

+31
-25
lines changed

easybuild/tools/run.py

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -107,13 +107,13 @@ def pad_4_spaces(msg):
107107
pad_4_spaces(f"working directory -> {self.cmd_result.work_dir}"),
108108
]
109109

110-
if self.cmd_result.stderr is None:
110+
if self.cmd_result.stderr is None and self.cmd_result.out_file is not None:
111111
error_info.append(pad_4_spaces(f"output (stdout + stderr) -> {self.cmd_result.out_file}"))
112112
else:
113-
error_info.extend([
114-
pad_4_spaces(f"output (stdout) -> {self.cmd_result.out_file}"),
115-
pad_4_spaces(f"error/warnings (stderr) -> {self.cmd_result.err_file}"),
116-
])
113+
if self.cmd_result.out_file is not None:
114+
error_info.append(pad_4_spaces(f"output (stdout) -> {self.cmd_result.out_file}"))
115+
if self.cmd_result.err_file is not None:
116+
error_info.append(pad_4_spaces(f"error/warnings (stderr) -> {self.cmd_result.err_file}"))
117117

118118
caller_file_name, caller_line_nr, caller_function_name = self.caller_info
119119
called_from_info = f"'{caller_function_name}' function in {caller_file_name} (line {caller_line_nr})"
@@ -174,7 +174,7 @@ def cache_aware_func(cmd, *args, **kwargs):
174174
@run_shell_cmd_cache
175175
def run_shell_cmd(cmd, fail_on_error=True, split_stderr=False, stdin=None, env=None,
176176
hidden=False, in_dry_run=False, verbose_dry_run=False, work_dir=None, shell=True,
177-
output_file=False, stream_output=False, asynchronous=False, with_hooks=True,
177+
output_file=True, stream_output=False, asynchronous=False, with_hooks=True,
178178
qa_patterns=None, qa_wait_patterns=None):
179179
"""
180180
Run specified (interactive) shell command, and capture output + exit code.
@@ -224,17 +224,22 @@ def to_cmd_str(cmd):
224224
work_dir = os.getcwd()
225225

226226
cmd_str = to_cmd_str(cmd)
227-
cmd_name = cmd_str.split(' ')[0]
227+
cmd_name = os.path.basename(cmd_str.split(' ')[0])
228228

229229
# temporary output file(s) for command output
230-
tmpdir = tempfile.mkdtemp(prefix='run-shell-cmd-')
231-
cmd_out_fp = os.path.join(tmpdir, f'{cmd_name}.out')
232-
_log.info(f'run_cmd: Output of "{cmd_str}" will be logged to {cmd_out_fp}')
233-
if split_stderr:
234-
cmd_err_fp = os.path.join(tmpdir, f'{cmd_name}.err')
235-
_log.info(f'run_cmd: Errors and warnings of "{cmd_str}" will be logged to {cmd_err_fp}')
230+
if output_file:
231+
toptmpdir = os.path.join(tempfile.gettempdir(), 'run-shell-cmd-output')
232+
os.makedirs(toptmpdir, exist_ok=True)
233+
tmpdir = tempfile.mkdtemp(dir=toptmpdir, prefix=f'{cmd_name}-')
234+
cmd_out_fp = os.path.join(tmpdir, f'out.txt')
235+
_log.info(f'run_cmd: Output of "{cmd_str}" will be logged to {cmd_out_fp}')
236+
if split_stderr:
237+
cmd_err_fp = os.path.join(tmpdir, f'err.txt')
238+
_log.info(f'run_cmd: Errors and warnings of "{cmd_str}" will be logged to {cmd_err_fp}')
239+
else:
240+
cmd_err_fp = None
236241
else:
237-
cmd_err_fp = None
242+
cmd_out_fp, cmd_err_fp = None, None
238243

239244
# early exit in 'dry run' mode, after printing the command that would be run (unless 'hidden' is enabled)
240245
if not in_dry_run and build_option('extended_dry_run'):
@@ -279,14 +284,15 @@ def to_cmd_str(cmd):
279284
stderr = proc.stderr.decode('utf-8', 'ignore') if split_stderr else None
280285

281286
# store command output to temporary file(s)
282-
try:
283-
with open(cmd_out_fp, 'w') as fp:
284-
fp.write(output)
285-
if split_stderr:
286-
with open(cmd_err_fp, 'w') as fp:
287-
fp.write(stderr)
288-
except IOError as err:
289-
raise EasyBuildError(f"Failed to dump command output to temporary file: {err}")
287+
if output_file:
288+
try:
289+
with open(cmd_out_fp, 'w') as fp:
290+
fp.write(output)
291+
if split_stderr:
292+
with open(cmd_err_fp, 'w') as fp:
293+
fp.write(stderr)
294+
except IOError as err:
295+
raise EasyBuildError(f"Failed to dump command output to temporary file: {err}")
290296

291297
res = RunShellCmdResult(cmd=cmd_str, exit_code=proc.returncode, output=output, stderr=stderr, work_dir=work_dir,
292298
out_file=cmd_out_fp, err_file=cmd_err_fp)

test/framework/run.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ def handler(signum, _):
356356
r"^\s+exit code\s* -> -9",
357357
r"^\s+working directory\s* -> " + work_dir,
358358
r"^\s+called from\s* -> 'test_run_shell_cmd_fail' function in .*/test/.*/run.py \(line [0-9]+\)",
359-
r"^\s+output \(stdout \+ stderr\)\s* -> .*/run-shell-cmd-.*/kill.out",
359+
r"^\s+output \(stdout \+ stderr\)\s* -> .*/run-shell-cmd-output/kill-.*/out.txt",
360360
]
361361
for pattern in patterns:
362362
regex = re.compile(pattern, re.M)
@@ -391,8 +391,8 @@ def handler(signum, _):
391391
r"^\s+exit code\s+ -> -9",
392392
r"^\s+working directory\s+ -> " + work_dir,
393393
r"^\s+called from\s+ -> 'test_run_shell_cmd_fail' function in .*/test/.*/run.py \(line [0-9]+\)",
394-
r"^\s+output \(stdout\)\s+ -> .*/run-shell-cmd-.*/kill.out",
395-
r"^\s+error/warnings \(stderr\)\s+ -> .*/run-shell-cmd-.*/kill.err",
394+
r"^\s+output \(stdout\)\s+ -> .*/run-shell-cmd-output/kill-.*/out.txt",
395+
r"^\s+error/warnings \(stderr\)\s+ -> .*/run-shell-cmd-output/kill-.*/err.txt",
396396
]
397397
for pattern in patterns:
398398
regex = re.compile(pattern, re.M)

0 commit comments

Comments
 (0)