| 1 | # Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | # All rights reserved. |
| 3 | # |
| 4 | # This source code is licensed under the terms described in the LICENSE file in |
| 5 | # the root directory of this source tree. |
| 6 | |
| 7 | import errno |
| 8 | import os |
| 9 | import pty |
| 10 | import select |
| 11 | import signal |
| 12 | import subprocess |
| 13 | import sys |
| 14 | import termios |
| 15 | |
| 16 | from termcolor import cprint |
| 17 | |
| 18 | |
| 19 | # run a command in a pseudo-terminal, with interrupt handling, |
| 20 | # useful when you want to run interactive things |
| 21 | def run_with_pty(command): |
| 22 | master, slave = pty.openpty() |
| 23 | |
| 24 | old_settings = termios.tcgetattr(sys.stdin) |
| 25 | original_sigint = signal.getsignal(signal.SIGINT) |
| 26 | |
| 27 | ctrl_c_pressed = False |
| 28 | |
| 29 | def sigint_handler(signum, frame): |
| 30 | nonlocal ctrl_c_pressed |
| 31 | ctrl_c_pressed = True |
| 32 | cprint("\nCtrl-C detected. Aborting...", "white", attrs=["bold"]) |
| 33 | |
| 34 | try: |
| 35 | # Set up the signal handler |
| 36 | signal.signal(signal.SIGINT, sigint_handler) |
| 37 | |
| 38 | new_settings = termios.tcgetattr(sys.stdin) |
| 39 | new_settings[3] = new_settings[3] & ~termios.ECHO # Disable echo |
| 40 | new_settings[3] = new_settings[3] & ~termios.ICANON # Disable canonical mode |
| 41 | termios.tcsetattr(sys.stdin, termios.TCSADRAIN, new_settings) |
| 42 | |
| 43 | process = subprocess.Popen( |
| 44 | command, |
| 45 | stdin=slave, |
| 46 | stdout=slave, |
| 47 | stderr=slave, |
| 48 | universal_newlines=True, |
| 49 | preexec_fn=os.setsid, |
| 50 | ) |
| 51 | |
| 52 | # Close the slave file descriptor as it's now owned by the subprocess |
| 53 | os.close(slave) |
| 54 | |
| 55 | def handle_io(): |
| 56 | while not ctrl_c_pressed: |
| 57 | try: |
| 58 | rlist, _, _ = select.select([sys.stdin, master], [], [], 0.1) |
| 59 | |
| 60 | if sys.stdin in rlist: |
| 61 | data = os.read(sys.stdin.fileno(), 1024) |
| 62 | if not data: |
| 63 | break |
| 64 | os.write(master, data) |
| 65 | |
| 66 | if master in rlist: |
| 67 | data = os.read(master, 1024) |
| 68 | if not data: |
| 69 | break |
| 70 | sys.stdout.buffer.write(data) |
| 71 | sys.stdout.flush() |
| 72 | |
| 73 | except KeyboardInterrupt: |
| 74 | # This will be raised when Ctrl+C is pressed |
| 75 | break |
| 76 | |
| 77 | if process.poll() is not None: |
| 78 | break |
| 79 | |
| 80 | handle_io() |
| 81 | except (EOFError, KeyboardInterrupt): |
| 82 | pass |
| 83 | except OSError as e: |
| 84 | if e.errno != errno.EIO: |
| 85 | raise |
| 86 | finally: |
| 87 | # Clean up |
| 88 | termios.tcsetattr(sys.stdin, termios.TCSADRAIN, old_settings) |
| 89 | signal.signal(signal.SIGINT, original_sigint) |
| 90 | |
| 91 | os.close(master) |
| 92 | if process.poll() is None: |
| 93 | process.terminate() |
| 94 | process.wait() |
| 95 | |
| 96 | return process.returncode |
| 97 | |
| 98 | |
| 99 | def run_command(command): |
| 100 | process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 101 | output, error = process.communicate() |
| 102 | if process.returncode != 0: |
| 103 | print(f"Error: {error.decode('utf-8')}") |
| 104 | sys.exit(1) |
| 105 | return output.decode("utf-8") |