Skip to content

Commit

Permalink
Make shell escape when escaping users optional
Browse files Browse the repository at this point in the history
- move function out of class and add dedicated direct test

Signed-off-by: Stefan Marr <[email protected]>
  • Loading branch information
smarr committed Jan 27, 2025
1 parent 8e60dd7 commit ab05d81
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 19 deletions.
43 changes: 24 additions & 19 deletions rebench/model/run_id.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,28 @@
from ..statistics import WithSamples


def expand_user(possible_path, shell_escape):
something_changed = False

# split will change the type of quotes, which may cause issues with shell variables
parts = shlex.split(possible_path)
for i, part in enumerate(parts):
expanded = os.path.expanduser(part)
if "~" in expanded and ":" in expanded:
path_list = expanded.split(":")
expanded = ":".join([os.path.expanduser(p) for p in path_list])
if parts[i] != expanded:
something_changed = True
parts[i] = expanded

if something_changed:
if shell_escape:
return shlex.join(parts)
return ' '.join(parts)

return possible_path


class RunId(object):
"""
A RunId is a concrete instantiation of the possible combinations of
Expand Down Expand Up @@ -111,7 +133,7 @@ def env(self):

self._expandend_env = self.benchmark.run_details.env
for key, value in self._expandend_env.items():
self._expandend_env[key] = self._expand_user(value)
self._expandend_env[key] = expand_user(value, False)
return self._expandend_env

@property
Expand Down Expand Up @@ -312,27 +334,10 @@ def cmdline(self):
return self._cmdline
return self._construct_cmdline()

def _expand_user(self, possible_path):
something_changed = False

# split will change the type of quotes, which may cause issues with shell variables
parts = shlex.split(possible_path)
for i, part in enumerate(parts):
expanded = os.path.expanduser(part)
if "~" in expanded and ":" in expanded:
path_list = expanded.split(":")
expanded = ":".join([os.path.expanduser(p) for p in path_list])
if parts[i] != expanded:
something_changed = True
parts[i] = expanded
if something_changed:
return shlex.join(parts)
return possible_path

def cmdline_for_next_invocation(self):
"""Replace the invocation number in the command line"""
cmdline = self.cmdline() % {"invocation": self.completed_invocations + 1}
cmdline = self._expand_user(cmdline)
cmdline = expand_user(cmdline, True)
return cmdline

def _construct_cmdline(self):
Expand Down
34 changes: 34 additions & 0 deletions rebench/tests/model/run_id_test.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,42 @@
from os.path import expanduser
import pytest

from ...configurator import Configurator, load_config
from ...model.run_id import expand_user
from ...persistence import DataStore
from ..rebench_test_case import ReBenchTestCase


def _simple_expand(path):
return path.replace("~", expanduser("~"))


def _expand(paths):
return [(p, _simple_expand(p)) for p in paths]


@pytest.mark.parametrize(
"possible_path, after_expansion",
_expand(
["~/foo/bar", "~/foo ~/bar -cp ~/here:~/there", "?.lua;../../awfy/Lua/?.lua"]
),
)
def test_expand_user_no_escape(possible_path, after_expansion):
expanded = expand_user(possible_path, False)
assert expanded == after_expansion


@pytest.mark.parametrize(
"possible_path, after_expansion",
_expand(
["~/foo/bar", "~/foo ~/bar -cp ~/here:~/there", "'?.lua;../../awfy/Lua/?.lua'"]
),
)
def test_expand_user_with_escape(possible_path, after_expansion):
expanded = expand_user(possible_path, True)
assert expanded == after_expansion


class RunIdTest(ReBenchTestCase):
def setUp(self):
super(RunIdTest, self).setUp()
Expand Down

0 comments on commit ab05d81

Please sign in to comment.