|
| 1 | +""" |
| 2 | +Windows-specific functionality for stdio client operations. |
| 3 | +""" |
| 4 | + |
| 5 | +import shutil |
| 6 | +import subprocess |
| 7 | +import sys |
| 8 | +from pathlib import Path |
| 9 | +from typing import TextIO |
| 10 | + |
| 11 | +import anyio |
| 12 | +from anyio.abc import Process |
| 13 | + |
| 14 | + |
| 15 | +def get_windows_executable_command(command: str) -> str: |
| 16 | + """ |
| 17 | + Get the correct executable command normalized for Windows. |
| 18 | +
|
| 19 | + On Windows, commands might exist with specific extensions (.exe, .cmd, etc.) |
| 20 | + that need to be located for proper execution. |
| 21 | +
|
| 22 | + Args: |
| 23 | + command: Base command (e.g., 'uvx', 'npx') |
| 24 | +
|
| 25 | + Returns: |
| 26 | + str: Windows-appropriate command path |
| 27 | + """ |
| 28 | + try: |
| 29 | + # First check if command exists in PATH as-is |
| 30 | + if command_path := shutil.which(command): |
| 31 | + return command_path |
| 32 | + |
| 33 | + # Check for Windows-specific extensions |
| 34 | + for ext in [".cmd", ".bat", ".exe", ".ps1"]: |
| 35 | + ext_version = f"{command}{ext}" |
| 36 | + if ext_path := shutil.which(ext_version): |
| 37 | + return ext_path |
| 38 | + |
| 39 | + # For regular commands or if we couldn't find special versions |
| 40 | + return command |
| 41 | + except OSError: |
| 42 | + # Handle file system errors during path resolution |
| 43 | + # (permissions, broken symlinks, etc.) |
| 44 | + return command |
| 45 | + |
| 46 | + |
| 47 | +async def create_windows_process( |
| 48 | + command: str, |
| 49 | + args: list[str], |
| 50 | + env: dict[str, str] | None = None, |
| 51 | + errlog: TextIO = sys.stderr, |
| 52 | + cwd: Path | str | None = None, |
| 53 | +): |
| 54 | + """ |
| 55 | + Creates a subprocess in a Windows-compatible way. |
| 56 | +
|
| 57 | + Windows processes need special handling for console windows and |
| 58 | + process creation flags. |
| 59 | +
|
| 60 | + Args: |
| 61 | + command: The command to execute |
| 62 | + args: Command line arguments |
| 63 | + env: Environment variables |
| 64 | + errlog: Where to send stderr output |
| 65 | + cwd: Working directory for the process |
| 66 | +
|
| 67 | + Returns: |
| 68 | + A process handle |
| 69 | + """ |
| 70 | + try: |
| 71 | + # Try with Windows-specific flags to hide console window |
| 72 | + process = await anyio.open_process( |
| 73 | + [command, *args], |
| 74 | + env=env, |
| 75 | + # Ensure we don't create console windows for each process |
| 76 | + creationflags=subprocess.CREATE_NO_WINDOW # type: ignore |
| 77 | + if hasattr(subprocess, "CREATE_NO_WINDOW") |
| 78 | + else 0, |
| 79 | + stderr=errlog, |
| 80 | + cwd=cwd, |
| 81 | + ) |
| 82 | + return process |
| 83 | + except Exception: |
| 84 | + # Don't raise, let's try to create the process without creation flags |
| 85 | + process = await anyio.open_process( |
| 86 | + [command, *args], env=env, stderr=errlog, cwd=cwd |
| 87 | + ) |
| 88 | + return process |
| 89 | + |
| 90 | + |
| 91 | +async def terminate_windows_process(process: Process): |
| 92 | + """ |
| 93 | + Terminate a Windows process. |
| 94 | +
|
| 95 | + Note: On Windows, terminating a process with process.terminate() doesn't |
| 96 | + always guarantee immediate process termination. |
| 97 | + So we give it 2s to exit, or we call process.kill() |
| 98 | + which sends a SIGKILL equivalent signal. |
| 99 | +
|
| 100 | + Args: |
| 101 | + process: The process to terminate |
| 102 | + """ |
| 103 | + try: |
| 104 | + process.terminate() |
| 105 | + with anyio.fail_after(2.0): |
| 106 | + await process.wait() |
| 107 | + except TimeoutError: |
| 108 | + # Force kill if it doesn't terminate |
| 109 | + process.kill() |
0 commit comments