From fa839565f2eb2f94d2a74c83993f3cfcacb4689a Mon Sep 17 00:00:00 2001 From: Reid <61492567+reidliu41@users.noreply.github.com> Date: Wed, 16 Jul 2025 10:43:19 +0800 Subject: [PATCH] [Misc] Refactor: Improve argument handling for `conda` command (#20481) Signed-off-by: reidliu41 --- vllm/collect_env.py | 45 +++++++++++++++++++++++++-------------------- 1 file changed, 25 insertions(+), 20 deletions(-) diff --git a/vllm/collect_env.py b/vllm/collect_env.py index 64172a9bf9..ee43ad12e8 100644 --- a/vllm/collect_env.py +++ b/vllm/collect_env.py @@ -96,25 +96,30 @@ DEFAULT_PIP_PATTERNS = { def run(command): """Return (return-code, stdout, stderr).""" shell = True if type(command) is str else False - p = subprocess.Popen(command, - stdout=subprocess.PIPE, - stderr=subprocess.PIPE, - shell=shell) - raw_output, raw_err = p.communicate() - rc = p.returncode - if get_platform() == 'win32': - enc = 'oem' - else: - enc = locale.getpreferredencoding() - output = raw_output.decode(enc) - if command == 'nvidia-smi topo -m': - # don't remove the leading whitespace of `nvidia-smi topo -m` - # because they are meaningful - output = output.rstrip() - else: - output = output.strip() - err = raw_err.decode(enc) - return rc, output, err.strip() + try: + p = subprocess.Popen(command, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + shell=shell) + raw_output, raw_err = p.communicate() + rc = p.returncode + if get_platform() == 'win32': + enc = 'oem' + else: + enc = locale.getpreferredencoding() + output = raw_output.decode(enc) + if command == 'nvidia-smi topo -m': + # don't remove the leading whitespace of `nvidia-smi topo -m` + # because they are meaningful + output = output.rstrip() + else: + output = output.strip() + err = raw_err.decode(enc) + return rc, output, err.strip() + + except FileNotFoundError: + cmd_str = command if isinstance(command, str) else command[0] + return 127, '', f"Command not found: {cmd_str}" def run_and_read_all(run_lambda, command): @@ -148,7 +153,7 @@ def get_conda_packages(run_lambda, patterns=None): if patterns is None: patterns = DEFAULT_CONDA_PATTERNS conda = os.environ.get('CONDA_EXE', 'conda') - out = run_and_read_all(run_lambda, "{} list".format(conda)) + out = run_and_read_all(run_lambda, [conda, 'list']) if out is None: return out