Skip to content

Commit

Permalink
Merge pull request #35 from laixintao/reopen-stdin
Browse files Browse the repository at this point in the history
support passing profile data though PIPE, by reopen fd=2 if stdin is not a tty.
  • Loading branch information
laixintao committed Oct 4, 2023
2 parents db14a48 + ef75c0b commit d222ba1
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 12 deletions.
12 changes: 12 additions & 0 deletions flameshow/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import os
import time

import sys

import click

from flameshow import __version__
Expand Down Expand Up @@ -36,6 +38,15 @@ def setup_log(enabled, level, loglocation):
}


def ensure_tty():
if os.isatty(0):
return

logger.info("stdin is not a tty, replace it to fd=2")
sys.stdin.close()
sys.stdin = os.fdopen(2)


def run_app(verbose, log_to, profile_f, _debug_exit_after_rednder):
log_level = LOG_LEVEL[verbose]
setup_log(log_to is not None, log_level, log_to)
Expand All @@ -48,6 +59,7 @@ def run_app(verbose, log_to, profile_f, _debug_exit_after_rednder):
t01 = time.time()
logger.info("Parse profile, took %.3fs", t01 - t0)

ensure_tty()
app = FlameshowApp(
profile,
_debug_exit_after_rednder,
Expand Down
8 changes: 0 additions & 8 deletions flameshow/render/flamegraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ def generate_frame_maps(self, width, focused_stack_id):
logger.info("frame maps: %s", frame_maps)

def _generate_for_children(frame):
logger.debug("generate frame_maps for %s", frame)
# generate for children
my_maps = frame_maps[frame._id]
for sample_i, my_map in enumerate(my_maps):
Expand Down Expand Up @@ -248,13 +247,6 @@ def render_line(self, y: int) -> Strip:
)
cursor += my_width

logger.debug(
"%s in line %d, frame_map=%s",
frame,
y,
frame_map,
)

strip = Strip(segments)
return strip

Expand Down
27 changes: 23 additions & 4 deletions tests/test_cli_click.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from click.testing import CliRunner
from flameshow.main import main
from unittest.mock import patch
from flameshow.main import main, ensure_tty
from unittest.mock import patch, MagicMock


def test_print_version():
Expand All @@ -11,10 +11,29 @@ def test_print_version():
assert result.output == "1.2.3\n"


def test_run_app():
@patch("flameshow.main.os")
def test_run_app(mock_os, data_dir):
mock_os.isatty.return_value = True
runner = CliRunner()
result = runner.invoke(
main, ["tests/pprof_data/profile-10seconds.out"], input="q"
main, [str(data_dir / "profile-10seconds.out")], input="q"
)

assert result.exit_code == 0


@patch("flameshow.main.sys")
@patch("flameshow.main.os")
def test_ensure_tty_when_its_not(mock_os, mock_sys):
mock_os.isatty.return_value = False
opened_fd = object()
mock_os.fdopen.return_value = opened_fd

fake_stdin = MagicMock()
mock_sys.stdin = fake_stdin

ensure_tty()

fake_stdin.close.assert_called()
assert hasattr(mock_sys, "stdin")
assert mock_sys.stdin is opened_fd

0 comments on commit d222ba1

Please sign in to comment.