Skip to content

Commit eadfdc7

Browse files
authored
Merge pull request #4454 from branfosj/cmd_filename
allow only alphanumeric characters in the output filename
2 parents c14a130 + a309aae commit eadfdc7

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

easybuild/tools/run.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import re
4343
import signal
4444
import shutil
45+
import string
4546
import subprocess
4647
import sys
4748
import tempfile
@@ -187,6 +188,19 @@ def cache_aware_func(cmd, *args, **kwargs):
187188
run_shell_cmd_cache = run_cmd_cache
188189

189190

191+
def fileprefix_from_cmd(cmd, allowed_chars=False):
192+
"""
193+
Simplify the cmd to only the allowed_chars we want in a filename
194+
195+
:param cmd: the cmd (string)
196+
:param allowed_chars: characters allowed in filename (defaults to string.ascii_letters + string.digits + "_-")
197+
"""
198+
if not allowed_chars:
199+
allowed_chars = f"{string.ascii_letters}{string.digits}_-"
200+
201+
return ''.join([c for c in cmd if c in allowed_chars])
202+
203+
190204
@run_shell_cmd_cache
191205
def run_shell_cmd(cmd, fail_on_error=True, split_stderr=False, stdin=None, env=None,
192206
hidden=False, in_dry_run=False, verbose_dry_run=False, work_dir=None, use_bash=True,
@@ -238,7 +252,6 @@ def to_cmd_str(cmd):
238252
work_dir = os.getcwd()
239253

240254
cmd_str = to_cmd_str(cmd)
241-
cmd_name = os.path.basename(cmd_str.split(' ')[0])
242255

243256
thread_id = None
244257
if asynchronous:
@@ -254,6 +267,7 @@ def to_cmd_str(cmd):
254267
if output_file:
255268
toptmpdir = os.path.join(tempfile.gettempdir(), 'run-shell-cmd-output')
256269
os.makedirs(toptmpdir, exist_ok=True)
270+
cmd_name = fileprefix_from_cmd(os.path.basename(cmd_str.split(' ')[0]))
257271
tmpdir = tempfile.mkdtemp(dir=toptmpdir, prefix=f'{cmd_name}-')
258272
cmd_out_fp = os.path.join(tmpdir, 'out.txt')
259273
_log.info(f'run_cmd: Output of "{cmd_str}" will be logged to {cmd_out_fp}')

test/framework/run.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import os
3636
import re
3737
import signal
38+
import string
3839
import stat
3940
import subprocess
4041
import sys
@@ -52,7 +53,7 @@
5253
from easybuild.tools.config import update_build_option
5354
from easybuild.tools.filetools import adjust_permissions, change_dir, mkdir, read_file, write_file
5455
from easybuild.tools.run import RunShellCmdResult, RunShellCmdError, check_async_cmd, check_log_for_errors
55-
from easybuild.tools.run import complete_cmd, get_output_from_process, parse_log_for_error
56+
from easybuild.tools.run import complete_cmd, fileprefix_from_cmd, get_output_from_process, parse_log_for_error
5657
from easybuild.tools.run import run_cmd, run_cmd_qa, run_shell_cmd, subprocess_terminate
5758
from easybuild.tools.config import ERROR, IGNORE, WARN
5859

@@ -194,6 +195,26 @@ def test_run_shell_cmd_basic(self):
194195
self.assertTrue(isinstance(res.output, str))
195196
self.assertTrue(res.work_dir and isinstance(res.work_dir, str))
196197

198+
def test_fileprefix_from_cmd(self):
199+
"""test simplifications from fileprefix_from_cmd."""
200+
cmds = {
201+
'abd123': 'abd123',
202+
'ab"a': 'aba',
203+
'a{:$:S@"a': 'aSa',
204+
'cmd-with-dash': 'cmd-with-dash',
205+
'cmd_with_underscore': 'cmd_with_underscore',
206+
}
207+
for cmd, expected_simplification in cmds.items():
208+
self.assertEqual(fileprefix_from_cmd(cmd), expected_simplification)
209+
210+
cmds = {
211+
'abd123': 'abd',
212+
'ab"a': 'aba',
213+
'0a{:$:2@"a': 'aa',
214+
}
215+
for cmd, expected_simplification in cmds.items():
216+
self.assertEqual(fileprefix_from_cmd(cmd, allowed_chars=string.ascii_letters), expected_simplification)
217+
197218
def test_run_cmd_log(self):
198219
"""Test logging of executed commands."""
199220
fd, logfile = tempfile.mkstemp(suffix='.log', prefix='eb-test-')

0 commit comments

Comments
 (0)