Skip to content

Commit

Permalink
Fix 'broken pipe' errors when piping output of 'show' to other comman…
Browse files Browse the repository at this point in the history
…ds (sonic-net#60)
  • Loading branch information
jleveque authored Jun 8, 2017
1 parent c1b48a6 commit e604cea
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions show/main.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#! /usr/bin/python -u

import click
import errno
import os
import subprocess
import sys
from click_default_group import DefaultGroup

try:
Expand Down Expand Up @@ -81,14 +83,23 @@ def get_command(self, ctx, cmd_name):


def run_command(command, pager=False):
if pager is True:
click.echo(click.style("Command: ", fg='cyan') + click.style(command, fg='green'))
p = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE)
click.echo_via_pager(p.stdout.read())
else:
click.echo(click.style("Command: ", fg='cyan') + click.style(command, fg='green'))
p = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE)
click.echo(p.stdout.read())
click.echo(click.style("Command: ", fg='cyan') + click.style(command, fg='green'))
p = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE)

try:
if pager is True and sys.stdout.isatty():
click.echo_via_pager(p.stdout.read())
else:
click.echo(p.stdout.read())
except IOError as e:
# In our version of Click, click.echo() and click.echo_via_pager() do not properly handle
# SIGPIPE, and if a pipe is broken before all output is processed (e.g., pipe output to 'head'),
# it will result in a stack trace. This is apparently fixed upstream, but for now, we silently
# ignore SIGPIPE here.
if e.errno == errno.EPIPE:
sys.exit(0)
else:
raise


CONTEXT_SETTINGS = dict(help_option_names=['-h', '--help', '?'])
Expand Down

0 comments on commit e604cea

Please sign in to comment.