Skip to content

Commit faf8f14

Browse files
committed
functionalise the simplification and add tests
1 parent 22e12b4 commit faf8f14

File tree

2 files changed

+34
-4
lines changed

2 files changed

+34
-4
lines changed

easybuild/tools/run.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,19 @@ def cache_aware_func(cmd, *args, **kwargs):
181181
run_shell_cmd_cache = run_cmd_cache
182182

183183

184+
def fileprefix_from_cmd(cmd, allowed_chars=False):
185+
"""
186+
Simplify the cmd to only the allowed_chars we want in a filename
187+
188+
:param cmd: the cmd (string)
189+
:param allowed_chars: characters allowed in filename (defaults to string.ascii_letters + string.digits)
190+
"""
191+
if not allowed_chars:
192+
allowed_chars = string.ascii_letters + string.digits
193+
194+
return ''.join([c for c in cmd if c in allowed_chars])
195+
196+
184197
@run_shell_cmd_cache
185198
def run_shell_cmd(cmd, fail_on_error=True, split_stderr=False, stdin=None, env=None,
186199
hidden=False, in_dry_run=False, verbose_dry_run=False, work_dir=None, use_bash=True,
@@ -244,9 +257,7 @@ def to_cmd_str(cmd):
244257
if output_file:
245258
toptmpdir = os.path.join(tempfile.gettempdir(), 'run-shell-cmd-output')
246259
os.makedirs(toptmpdir, exist_ok=True)
247-
# restrict the allowed characters in the name of the output_file
248-
allowed_chars = string.ascii_letters + string.digits
249-
cmd_name = ''.join([c for c in os.path.basename(cmd_str.split(' ')[0]) if c in allowed_chars])
260+
cmd_name = fileprefix_from_cmd(os.path.basename(cmd_str.split(' ')[0]))
250261
tmpdir = tempfile.mkdtemp(dir=toptmpdir, prefix=f'{cmd_name}-')
251262
cmd_out_fp = os.path.join(tmpdir, 'out.txt')
252263
_log.info(f'run_cmd: Output of "{cmd_str}" will be logged to {cmd_out_fp}')

test/framework/run.py

Lines changed: 20 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
@@ -51,7 +52,7 @@
5152
from easybuild.tools.config import update_build_option
5253
from easybuild.tools.filetools import adjust_permissions, change_dir, mkdir, read_file, write_file
5354
from easybuild.tools.run import RunShellCmdResult, RunShellCmdError, check_async_cmd, check_log_for_errors
54-
from easybuild.tools.run import complete_cmd, get_output_from_process, parse_log_for_error
55+
from easybuild.tools.run import complete_cmd, fileprefix_from_cmd, get_output_from_process, parse_log_for_error
5556
from easybuild.tools.run import run_cmd, run_cmd_qa, run_shell_cmd, subprocess_terminate
5657
from easybuild.tools.config import ERROR, IGNORE, WARN
5758

@@ -193,6 +194,24 @@ def test_run_shell_cmd_basic(self):
193194
self.assertTrue(isinstance(res.output, str))
194195
self.assertTrue(res.work_dir and isinstance(res.work_dir, str))
195196

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

0 commit comments

Comments
 (0)