mirror of
https://gitlab.freedesktop.org/mesa/mesa.git
synced 2025-12-22 22:10:10 +01:00
pytracediff: implement pager ('less') invocation internally
In order to get rid of the ntracediff.sh wrapper script, implement invocation of 'less' internally, if the stdout is determined to be a tty. Otherwise just print out normally. Signed-off-by: Matti Hamalainen <ccr@tnsp.org> Acked-by: Mike Blumenkrantz <michael.blumenkrantz@gmail.com> Reviewed-by: Dylan Baker <dylan@pnwbakers.com> Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/17135>
This commit is contained in:
parent
95fc0e1b7c
commit
aab5d176b8
1 changed files with 127 additions and 93 deletions
|
|
@ -26,12 +26,14 @@
|
||||||
##########################################################################
|
##########################################################################
|
||||||
|
|
||||||
from parse import *
|
from parse import *
|
||||||
|
import os
|
||||||
import sys
|
import sys
|
||||||
import re
|
import re
|
||||||
import signal
|
import signal
|
||||||
import functools
|
import functools
|
||||||
import argparse
|
import argparse
|
||||||
import difflib
|
import difflib
|
||||||
|
import subprocess
|
||||||
|
|
||||||
assert sys.version_info >= (3, 6), 'Python >= 3.6 required'
|
assert sys.version_info >= (3, 6), 'Python >= 3.6 required'
|
||||||
|
|
||||||
|
|
@ -54,6 +56,8 @@ PKK_ANSI_ITALIC = '3m'
|
||||||
###
|
###
|
||||||
def pkk_fatal(msg):
|
def pkk_fatal(msg):
|
||||||
print(f"ERROR: {msg}", file=sys.stderr)
|
print(f"ERROR: {msg}", file=sys.stderr)
|
||||||
|
if outpipe is not None:
|
||||||
|
outpipe.terminate()
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -61,8 +65,17 @@ def pkk_info(msg):
|
||||||
print(msg, file=sys.stderr)
|
print(msg, file=sys.stderr)
|
||||||
|
|
||||||
|
|
||||||
|
def pkk_output(outpipe, msg):
|
||||||
|
if outpipe is not None:
|
||||||
|
print(msg, file=outpipe.stdin)
|
||||||
|
else:
|
||||||
|
print(msg)
|
||||||
|
|
||||||
|
|
||||||
def pkk_signal_handler(signal, frame):
|
def pkk_signal_handler(signal, frame):
|
||||||
print("\nQuitting due to SIGINT / Ctrl+C!")
|
print("\nQuitting due to SIGINT / Ctrl+C!")
|
||||||
|
if outpipe is not None:
|
||||||
|
outpipe.terminate()
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -259,6 +272,16 @@ def pkk_format_line(line, indent, width):
|
||||||
### Main program starts
|
### Main program starts
|
||||||
###
|
###
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
### Check if output is a terminal
|
||||||
|
outpipe = None
|
||||||
|
redirect = False
|
||||||
|
|
||||||
|
try:
|
||||||
|
defwidth = os.get_terminal_size().columns
|
||||||
|
redirect = True
|
||||||
|
except OSError:
|
||||||
|
defwidth = 80
|
||||||
|
|
||||||
signal.signal(signal.SIGINT, pkk_signal_handler)
|
signal.signal(signal.SIGINT, pkk_signal_handler)
|
||||||
|
|
||||||
### Parse arguments
|
### Parse arguments
|
||||||
|
|
@ -307,7 +330,7 @@ if __name__ == "__main__":
|
||||||
|
|
||||||
optparser.add_argument("-w", "--width",
|
optparser.add_argument("-w", "--width",
|
||||||
dest="output_width",
|
dest="output_width",
|
||||||
type=functools.partial(pkk_arg_range, vmin=16, vmax=512), default=80,
|
type=functools.partial(pkk_arg_range, vmin=16, vmax=512), default=defwidth,
|
||||||
metavar="N",
|
metavar="N",
|
||||||
help="output width (default: %(default)s)")
|
help="output width (default: %(default)s)")
|
||||||
|
|
||||||
|
|
@ -327,6 +350,11 @@ if __name__ == "__main__":
|
||||||
print("The files are identical.")
|
print("The files are identical.")
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
|
|
||||||
|
### Redirect output to 'less' if stdout is a tty
|
||||||
|
try:
|
||||||
|
if redirect:
|
||||||
|
outpipe = subprocess.Popen(["less", "-S", "-R"], stdin=subprocess.PIPE, encoding="utf8")
|
||||||
|
|
||||||
### Output results
|
### Output results
|
||||||
pkk_info("Outputting diff ...")
|
pkk_info("Outputting diff ...")
|
||||||
colwidth = int((options.output_width - 3) / 2)
|
colwidth = int((options.output_width - 3) / 2)
|
||||||
|
|
@ -340,7 +368,7 @@ if __name__ == "__main__":
|
||||||
show_args = False
|
show_args = False
|
||||||
if options.suppress_common:
|
if options.suppress_common:
|
||||||
if tag != prevtag:
|
if tag != prevtag:
|
||||||
print("[...]")
|
pkk_output(outpipe, "[...]")
|
||||||
continue
|
continue
|
||||||
|
|
||||||
sep = "|"
|
sep = "|"
|
||||||
|
|
@ -427,7 +455,7 @@ if __name__ == "__main__":
|
||||||
ansi1 = ansi2 = ansi1 + PKK_ANSI_ESC + PKK_ANSI_YELLOW
|
ansi1 = ansi2 = ansi1 + PKK_ANSI_ESC + PKK_ANSI_YELLOW
|
||||||
|
|
||||||
# Output line
|
# Output line
|
||||||
print(colfmt.format(
|
pkk_output(outpipe, colfmt.format(
|
||||||
ansi1, pkk_format_line(line1, indent, colwidth), ansiend,
|
ansi1, pkk_format_line(line1, indent, colwidth), ansiend,
|
||||||
ansisep, sep, ansiend,
|
ansisep, sep, ansiend,
|
||||||
ansi2, pkk_format_line(line2, indent, colwidth), ansiend).
|
ansi2, pkk_format_line(line2, indent, colwidth), ansiend).
|
||||||
|
|
@ -436,6 +464,12 @@ if __name__ == "__main__":
|
||||||
nline += 1
|
nline += 1
|
||||||
|
|
||||||
if tag == "equal" and options.suppress_common:
|
if tag == "equal" and options.suppress_common:
|
||||||
print("[...]")
|
pkk_output(outpipe, "[...]")
|
||||||
|
|
||||||
prevtag = tag
|
prevtag = tag
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
pkk_fatal(str(e))
|
||||||
|
|
||||||
|
if outpipe is not None:
|
||||||
|
outpipe.communicate()
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue