Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add -c option: program passed as string #61

Merged
merged 2 commits into from
May 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 27 additions & 6 deletions src/memray/commands/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ def _run_tracker(
try:
if args.run_as_module:
runpy.run_module(args.script, run_name="__main__", alter_sys=True)
elif args.run_as_cmd:
exec(args.script, {"__name__": "__main__"})
godlygeek marked this conversation as resolved.
Show resolved Hide resolved
else:
runpy.run_path(args.script, run_name="__main__")
finally:
Expand All @@ -62,13 +64,15 @@ def _child_process(
port: int,
native: bool,
run_as_module: bool,
run_as_cmd: bool,
quiet: bool,
script: str,
script_args: List[str],
) -> None:
args = argparse.Namespace(
native=native,
run_as_module=run_as_module,
run_as_cmd=run_as_cmd,
quiet=quiet,
script=script,
script_args=script_args,
Expand All @@ -84,7 +88,7 @@ def _run_child_process_and_attach(args: argparse.Namespace) -> None:
raise MemrayCommandError(f"Invalid port: {port}", exit_code=1)

arguments = (
f"{port},{args.native},{args.run_as_module},{args.quiet},"
f"{port},{args.native},{args.run_as_module},{args.run_as_cmd},{args.quiet},"
f'"{args.script}",{args.script_args}'
)
tracked_app_cmd = [
Expand Down Expand Up @@ -128,8 +132,12 @@ def _run_with_socket_output(args: argparse.Namespace) -> None:

def _run_with_file_output(args: argparse.Namespace) -> None:
if args.output is None:
output = f"memray-{os.path.basename(args.script)}.{os.getpid()}.bin"
filename = os.path.join(os.path.dirname(args.script), output)
script_name = args.script
if args.run_as_cmd:
script_name = "string"

output = f"memray-{os.path.basename(script_name)}.{os.getpid()}.bin"
filename = os.path.join(os.path.dirname(script_name), output)
else:
filename = args.output

Expand Down Expand Up @@ -163,7 +171,7 @@ class RunCommand:
"""Run the specified application and track memory usage"""

def prepare_parser(self, parser: argparse.ArgumentParser) -> None:
parser.usage = "%(prog)s [-m module | file] [args]"
parser.usage = "%(prog)s [-m module | -c cmd | file] [args]"
output_group = parser.add_mutually_exclusive_group()
output_group.add_argument(
"-o",
Expand Down Expand Up @@ -218,6 +226,13 @@ def prepare_parser(self, parser: argparse.ArgumentParser) -> None:
action="store_true",
default=False,
)
parser.add_argument(
"-c",
help="Program passed in as string",
action="store_true",
dest="run_as_cmd",
default=False,
)
parser.add_argument(
"-m",
help="Run library module as a script (terminates option list)",
Expand All @@ -237,18 +252,24 @@ def validate_target_file(self, args: argparse.Namespace) -> None:
if args.run_as_module:
return
try:
source = pathlib.Path(args.script).read_bytes()
if args.run_as_cmd:
source = bytes(args.script, "UTF-8")
else:
source = pathlib.Path(args.script).read_bytes()
ast.parse(source)
except (SyntaxError, ValueError):
raise MemrayCommandError(
"Only Python files can be executed under memray", exit_code=1
"Only valid Python files or commands can be executed under memray",
exit_code=1,
)

def run(self, args: argparse.Namespace, parser: argparse.ArgumentParser) -> None:
if args.live_port is not None and not args.live_remote_mode:
parser.error("The --live-port argument requires --live-remote")
if args.follow_fork is True and (args.live_mode or args.live_remote_mode):
parser.error("--follow-fork cannot be used with the live TUI")
if args.run_as_cmd and pathlib.Path(args.script).exists():
parser.error("remove the option -c to run a file")

self.validate_target_file(args)

Expand Down
5 changes: 4 additions & 1 deletion tests/integration/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,10 @@ def test_run_file_that_is_not_python(self, capsys, option):

# THEN
captured = capsys.readouterr()
assert captured.err.strip() == "Only Python files can be executed under memray"
assert (
captured.err.strip()
== "Only valid Python files or commands can be executed under memray"
)

@patch("memray.commands.run.os.getpid")
def test_run_file_exists(self, getpid, tmp_path, monkeypatch, capsys):
Expand Down
20 changes: 18 additions & 2 deletions tests/unit/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def test_run_without_arguments(
main(["run"])

captured = capsys.readouterr()
assert "usage: memray run [-m module | file] [args]" in captured.err
assert "usage: memray run [-m module | -c cmd | file] [args]" in captured.err

def test_run_default_output(
self, getpid_mock, runpy_mock, tracker_mock, validate_mock
Expand Down Expand Up @@ -94,6 +94,22 @@ def test_run_module(self, getpid_mock, runpy_mock, tracker_mock, validate_mock):
"foobar", run_name="__main__", alter_sys=True
)

def test_run_cmd_is_validated(
self, getpid_mock, runpy_mock, tracker_mock, validate_mock
):
with patch.object(RunCommand, "validate_target_file"):
assert 0 == main(["run", "-c", "x = [i for i in range(10)]"])
with pytest.raises(SyntaxError):
main(["run", "-c", "[i for i in range(10)"])

def test_run_cmd(self, getpid_mock, runpy_mock, tracker_mock, validate_mock):
with patch("memray.commands.run.exec") as mock_exec:
assert 0 == main(["run", "-c", "x = 10; y = abs(-10)"])
assert not runpy_mock.called
mock_exec.assert_called_with(
"x = 10; y = abs(-10)", {"__name__": "__main__"}
)

def test_run_file(self, getpid_mock, runpy_mock, tracker_mock, validate_mock):
with patch.object(RunCommand, "validate_target_file"):
assert 0 == main(["run", "foobar.py", "arg1", "arg2"])
Expand Down Expand Up @@ -136,7 +152,7 @@ def test_run_with_live(
sys.executable,
"-c",
"from memray.commands.run import _child_process;"
'_child_process(1234,False,False,False,"./directory/foobar.py",'
'_child_process(1234,False,False,False,False,"./directory/foobar.py",'
"['arg1', 'arg2'])",
],
stderr=-1,
Expand Down