|
3 | 3 | import contextlib |
4 | 4 | import os |
5 | 5 | import re |
| 6 | +import subprocess |
| 7 | +import sys |
6 | 8 | from pathlib import Path |
7 | 9 | from typing import TYPE_CHECKING, Optional |
8 | 10 |
|
|
11 | 13 | from codeflash.either import Failure, Success |
12 | 14 |
|
13 | 15 | if TYPE_CHECKING: |
| 16 | + from collections.abc import Mapping |
| 17 | + |
14 | 18 | from codeflash.either import Result |
15 | 19 |
|
| 20 | + |
16 | 21 | # PowerShell patterns and prefixes |
17 | 22 | POWERSHELL_RC_EXPORT_PATTERN = re.compile( |
18 | 23 | r'^\$env:CODEFLASH_API_KEY\s*=\s*(?:"|\')?(cf-[^\s"\']+)(?:"|\')?\s*$', re.MULTILINE |
@@ -231,3 +236,24 @@ def save_api_key_to_rc(api_key: str) -> Result[str, str]: |
231 | 236 | f"To ensure your Codeflash API key is automatically loaded into your environment at startup, you can create {shell_rc_path} and add the following line:{LF}" |
232 | 237 | f"{LF}{api_key_line}{LF}" |
233 | 238 | ) |
| 239 | + |
| 240 | + |
| 241 | +def get_cross_platform_subprocess_run_args( |
| 242 | + cwd: Path | str | None = None, |
| 243 | + env: Mapping[str, str] | None = None, |
| 244 | + timeout: Optional[float] = None, |
| 245 | + check: bool = False, # noqa: FBT001, FBT002 |
| 246 | + text: bool = True, # noqa: FBT001, FBT002 |
| 247 | + capture_output: bool = True, # noqa: FBT001, FBT002 (only for non-Windows) |
| 248 | +) -> dict[str, str]: |
| 249 | + run_args = {"cwd": cwd, "env": env, "text": text, "timeout": timeout, "check": check} |
| 250 | + if sys.platform == "win32": |
| 251 | + creationflags = subprocess.CREATE_NEW_PROCESS_GROUP |
| 252 | + run_args["creationflags"] = creationflags |
| 253 | + run_args["stdout"] = subprocess.PIPE |
| 254 | + run_args["stderr"] = subprocess.PIPE |
| 255 | + run_args["stdin"] = subprocess.DEVNULL |
| 256 | + else: |
| 257 | + run_args["capture_output"] = capture_output |
| 258 | + |
| 259 | + return run_args |
0 commit comments