2022-04-14 16:43:10 +02:00
|
|
|
"""Logic for checking and displaying man pages."""
|
|
|
|
|
|
|
|
import subprocess
|
|
|
|
import os
|
|
|
|
from httpie.context import Environment
|
|
|
|
|
2023-05-22 20:56:30 +02:00
|
|
|
|
2022-04-14 16:43:10 +02:00
|
|
|
MAN_COMMAND = 'man'
|
|
|
|
NO_MAN_PAGES = os.getenv('HTTPIE_NO_MAN_PAGES', False)
|
|
|
|
|
2023-05-22 20:56:30 +02:00
|
|
|
# On some systems, HTTP(n) might exist, but we are only interested in HTTP(1).
|
|
|
|
# For more information on man page sections: <https://unix.stackexchange.com/a/138643>
|
2022-05-05 20:17:37 +02:00
|
|
|
MAN_PAGE_SECTION = '1'
|
|
|
|
|
2022-04-14 16:43:10 +02:00
|
|
|
|
|
|
|
def is_available(program: str) -> bool:
|
2023-05-22 20:56:30 +02:00
|
|
|
"""
|
|
|
|
Check whether `program`'s man pages are available on this system.
|
2022-04-14 16:43:10 +02:00
|
|
|
|
2023-05-22 20:56:30 +02:00
|
|
|
"""
|
2022-04-14 16:43:10 +02:00
|
|
|
if NO_MAN_PAGES or os.system == 'nt':
|
|
|
|
return False
|
2022-05-05 20:17:37 +02:00
|
|
|
try:
|
|
|
|
process = subprocess.run(
|
|
|
|
[MAN_COMMAND, MAN_PAGE_SECTION, program],
|
|
|
|
shell=False,
|
|
|
|
stdout=subprocess.DEVNULL,
|
|
|
|
stderr=subprocess.DEVNULL
|
|
|
|
)
|
|
|
|
except Exception:
|
2023-05-22 20:56:30 +02:00
|
|
|
# There might be some errors outside the process, e.g
|
2022-05-05 20:17:37 +02:00
|
|
|
# a permission error to execute something that is not an
|
|
|
|
# executable.
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
return process.returncode == 0
|
2022-04-14 16:43:10 +02:00
|
|
|
|
|
|
|
|
|
|
|
def display_for(env: Environment, program: str) -> None:
|
2023-05-22 20:56:30 +02:00
|
|
|
"""
|
|
|
|
Open the system man page for the given command (http/https/httpie).
|
2022-04-14 16:43:10 +02:00
|
|
|
|
2023-05-22 20:56:30 +02:00
|
|
|
"""
|
2022-04-14 16:43:10 +02:00
|
|
|
subprocess.run(
|
2022-05-05 20:17:37 +02:00
|
|
|
[MAN_COMMAND, MAN_PAGE_SECTION, program],
|
|
|
|
stdout=env.stdout,
|
|
|
|
stderr=env.stderr
|
2022-04-14 16:43:10 +02:00
|
|
|
)
|