Skip to content

Commit ab05d81

Browse files
committed
Make shell escape when escaping users optional
- move function out of class and add dedicated direct test Signed-off-by: Stefan Marr <[email protected]>
1 parent 8e60dd7 commit ab05d81

File tree

2 files changed

+58
-19
lines changed

2 files changed

+58
-19
lines changed

rebench/model/run_id.py

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,28 @@
3535
from ..statistics import WithSamples
3636

3737

38+
def expand_user(possible_path, shell_escape):
39+
something_changed = False
40+
41+
# split will change the type of quotes, which may cause issues with shell variables
42+
parts = shlex.split(possible_path)
43+
for i, part in enumerate(parts):
44+
expanded = os.path.expanduser(part)
45+
if "~" in expanded and ":" in expanded:
46+
path_list = expanded.split(":")
47+
expanded = ":".join([os.path.expanduser(p) for p in path_list])
48+
if parts[i] != expanded:
49+
something_changed = True
50+
parts[i] = expanded
51+
52+
if something_changed:
53+
if shell_escape:
54+
return shlex.join(parts)
55+
return ' '.join(parts)
56+
57+
return possible_path
58+
59+
3860
class RunId(object):
3961
"""
4062
A RunId is a concrete instantiation of the possible combinations of
@@ -111,7 +133,7 @@ def env(self):
111133

112134
self._expandend_env = self.benchmark.run_details.env
113135
for key, value in self._expandend_env.items():
114-
self._expandend_env[key] = self._expand_user(value)
136+
self._expandend_env[key] = expand_user(value, False)
115137
return self._expandend_env
116138

117139
@property
@@ -312,27 +334,10 @@ def cmdline(self):
312334
return self._cmdline
313335
return self._construct_cmdline()
314336

315-
def _expand_user(self, possible_path):
316-
something_changed = False
317-
318-
# split will change the type of quotes, which may cause issues with shell variables
319-
parts = shlex.split(possible_path)
320-
for i, part in enumerate(parts):
321-
expanded = os.path.expanduser(part)
322-
if "~" in expanded and ":" in expanded:
323-
path_list = expanded.split(":")
324-
expanded = ":".join([os.path.expanduser(p) for p in path_list])
325-
if parts[i] != expanded:
326-
something_changed = True
327-
parts[i] = expanded
328-
if something_changed:
329-
return shlex.join(parts)
330-
return possible_path
331-
332337
def cmdline_for_next_invocation(self):
333338
"""Replace the invocation number in the command line"""
334339
cmdline = self.cmdline() % {"invocation": self.completed_invocations + 1}
335-
cmdline = self._expand_user(cmdline)
340+
cmdline = expand_user(cmdline, True)
336341
return cmdline
337342

338343
def _construct_cmdline(self):

rebench/tests/model/run_id_test.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,42 @@
1+
from os.path import expanduser
2+
import pytest
3+
14
from ...configurator import Configurator, load_config
5+
from ...model.run_id import expand_user
26
from ...persistence import DataStore
37
from ..rebench_test_case import ReBenchTestCase
48

59

10+
def _simple_expand(path):
11+
return path.replace("~", expanduser("~"))
12+
13+
14+
def _expand(paths):
15+
return [(p, _simple_expand(p)) for p in paths]
16+
17+
18+
@pytest.mark.parametrize(
19+
"possible_path, after_expansion",
20+
_expand(
21+
["~/foo/bar", "~/foo ~/bar -cp ~/here:~/there", "?.lua;../../awfy/Lua/?.lua"]
22+
),
23+
)
24+
def test_expand_user_no_escape(possible_path, after_expansion):
25+
expanded = expand_user(possible_path, False)
26+
assert expanded == after_expansion
27+
28+
29+
@pytest.mark.parametrize(
30+
"possible_path, after_expansion",
31+
_expand(
32+
["~/foo/bar", "~/foo ~/bar -cp ~/here:~/there", "'?.lua;../../awfy/Lua/?.lua'"]
33+
),
34+
)
35+
def test_expand_user_with_escape(possible_path, after_expansion):
36+
expanded = expand_user(possible_path, True)
37+
assert expanded == after_expansion
38+
39+
640
class RunIdTest(ReBenchTestCase):
741
def setUp(self):
842
super(RunIdTest, self).setUp()

0 commit comments

Comments
 (0)