Small refactor for run_with_pty

This commit is contained in:
Ashwin Bharambe 2025-01-28 09:32:33 -08:00
parent 8332ea23ad
commit aee6237685
3 changed files with 13 additions and 10 deletions

View file

@ -6,7 +6,6 @@
import argparse import argparse
import os import os
import sys
from pathlib import Path from pathlib import Path
from llama_stack.cli.subcommand import Subcommand from llama_stack.cli.subcommand import Subcommand
@ -71,7 +70,7 @@ class StackRun(Subcommand):
BUILDS_BASE_DIR, BUILDS_BASE_DIR,
DISTRIBS_BASE_DIR, DISTRIBS_BASE_DIR,
) )
from llama_stack.distribution.utils.exec import run_with_pty, run_with_win from llama_stack.distribution.utils.exec import run_with_pty
if not args.config: if not args.config:
self.parser.error("Must specify a config file to run") self.parser.error("Must specify a config file to run")
@ -199,4 +198,4 @@ class StackRun(Subcommand):
return return
run_args.extend(["--env", f"{key}={value}"]) run_args.extend(["--env", f"{key}={value}"])
run_with_win(args) if sys.platform.startswith("win") else run_with_pty(args) run_with_pty(run_args)

View file

@ -21,7 +21,7 @@ from llama_stack.distribution.distribution import get_provider_registry
from llama_stack.distribution.utils.config_dirs import BUILDS_BASE_DIR from llama_stack.distribution.utils.config_dirs import BUILDS_BASE_DIR
from llama_stack.distribution.utils.exec import run_command, run_with_pty, run_with_win from llama_stack.distribution.utils.exec import run_command, run_with_pty
from llama_stack.providers.datatypes import Api from llama_stack.providers.datatypes import Api
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
@ -157,9 +157,7 @@ def build_image(
is_terminal = sys.stdin.isatty() is_terminal = sys.stdin.isatty()
if is_terminal: if is_terminal:
return_code = ( return_code = run_with_pty(args)
run_with_win(args) if sys.platform.startswith("win") else run_with_pty(args)
)
else: else:
return_code = run_command(args) return_code = run_command(args)

View file

@ -15,9 +15,16 @@ import sys
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
def run_with_pty(command):
if sys.platform.startswith("win"):
return _run_with_pty_win(command)
else:
return _run_with_pty_unix(command)
# run a command in a pseudo-terminal, with interrupt handling, # run a command in a pseudo-terminal, with interrupt handling,
# useful when you want to run interactive things # useful when you want to run interactive things
def run_with_pty(command): def _run_with_pty_unix(command):
import pty import pty
import termios import termios
@ -99,9 +106,8 @@ def run_with_pty(command):
# run a command in a pseudo-terminal in windows, with interrupt handling, # run a command in a pseudo-terminal in windows, with interrupt handling,
def run_with_win(command): def _run_with_pty_win(command):
""" """
Alternative to run_with_pty for Windows platforms.
Runs a command with interactive support using subprocess directly. Runs a command with interactive support using subprocess directly.
""" """
try: try: