Check pager's executable before subprocess.Popen

Get the value of `PAGER` environment variable in case it's defined, check the executable, if it exists - use a pager, otherwise - print function.

Fixes: https://pagure.io/freeipa/issue/7746
Reviewed-By: Christian Heimes <cheimes@redhat.com>
Reviewed-By: Rob Crittenden <rcritten@redhat.com>
This commit is contained in:
Oleg Kozlov
2018-12-07 14:06:29 +01:00
parent 03edd82d1a
commit a0e09526b3
3 changed files with 43 additions and 5 deletions
+13 -2
View File
@@ -37,6 +37,7 @@ import sys
import ssl
import termios
import fcntl
import shutil
import struct
import subprocess
@@ -1203,17 +1204,27 @@ def get_terminal_height(fd=1):
return os.environ.get("LINES", 25)
def open_in_pager(data):
def get_pager():
""" Get path to a pager
:return: path to the file if it exists otherwise None
:rtype: str or None
"""
pager = os.environ.get('PAGER', 'less')
return shutil.which(pager)
def open_in_pager(data, pager):
"""
Open text data in pager
Args:
data (bytes): data to view in pager
pager (str): path to the pager
Returns:
None
"""
pager = os.environ.get("PAGER", "less")
pager_process = subprocess.Popen([pager], stdin=subprocess.PIPE)
try: