Run a command and return its stdout/stderr as a string. Parameters ---------- cmd : str A command to be executed in the system shell. Returns ------- output : str A string containing the combination of stdout and stderr from t
(self, cmd: str)
| 57 | self.logfile = sys.stdout |
| 58 | |
| 59 | def getoutput(self, cmd: str) -> str | None: |
| 60 | """Run a command and return its stdout/stderr as a string. |
| 61 | |
| 62 | Parameters |
| 63 | ---------- |
| 64 | cmd : str |
| 65 | A command to be executed in the system shell. |
| 66 | |
| 67 | Returns |
| 68 | ------- |
| 69 | output : str |
| 70 | A string containing the combination of stdout and stderr from the |
| 71 | subprocess, in whatever order the subprocess originally wrote to its |
| 72 | file descriptors (so the order of the information in this string is the |
| 73 | correct order as would be seen if running the command in a terminal). |
| 74 | """ |
| 75 | import pexpect |
| 76 | |
| 77 | assert self.sh is not None |
| 78 | try: |
| 79 | res = pexpect.run(self.sh, args=["-c", cmd]) |
| 80 | assert isinstance(res, str) |
| 81 | return res.replace("\r\n", "\n") |
| 82 | except KeyboardInterrupt: |
| 83 | print('^C', file=sys.stderr, end='') |
| 84 | return None |
| 85 | |
| 86 | def system(self, cmd: str) -> int: |
| 87 | """Execute a command in a subshell. |