| 39 | print("VM Ready.", file=sys.stderr) |
| 40 | |
| 41 | def exec(self, command): |
| 42 | if not self.process: |
| 43 | raise RuntimeError("VM not started") |
| 44 | |
| 45 | req = json.dumps({"cmd": "exec", "command": command}) |
| 46 | self.process.stdin.write(req + "\n") |
| 47 | self.process.stdin.flush() |
| 48 | |
| 49 | resp_line = self.process.stdout.readline() |
| 50 | if not resp_line: |
| 51 | raise RuntimeError("VM closed connection") |
| 52 | |
| 53 | resp = json.loads(resp_line) |
| 54 | if resp.get('status') == 'ok': |
| 55 | return resp['result'] |
| 56 | else: |
| 57 | raise RuntimeError(f"VM Error: {resp.get('error')}") |
| 58 | |
| 59 | def stop(self): |
| 60 | if self.process: |