Run a shell command and return its output.
(cmd: list[str], cwd: Path)
| 2862 | |
| 2863 | |
| 2864 | async def run_command(cmd: list[str], cwd: Path) -> str: |
| 2865 | """Run a shell command and return its output.""" |
| 2866 | try: |
| 2867 | process = await asyncio.create_subprocess_exec( |
| 2868 | *cmd, |
| 2869 | cwd=cwd, |
| 2870 | stdout=asyncio.subprocess.PIPE, |
| 2871 | stderr=asyncio.subprocess.STDOUT, |
| 2872 | ) |
| 2873 | |
| 2874 | stdout_bytes, _ = await process.communicate() |
| 2875 | stdout = stdout_bytes.decode("utf-8", errors="replace") if stdout_bytes else "" |
| 2876 | |
| 2877 | if process.returncode != 0: |
| 2878 | return f"Command failed with exit code {process.returncode}:\n{stdout}" |
| 2879 | |
| 2880 | return stdout |
| 2881 | except Exception as e: |
| 2882 | return f"Error running command: {str(e)}" |
| 2883 | |
| 2884 | |
| 2885 | async def main(): |
no test coverage detected